diff --git a/app/build.gradle b/app/build.gradle index d4e4a2f..81570c0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -74,13 +74,18 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.1.1' implementation 'com.github.bumptech.glide:okhttp3-integration:4.1.1' + + // Room + lifecycle implementation "androidx.lifecycle:lifecycle-livedata:2.2.0-alpha02" implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0-alpha02" - implementation "androidx.room:room-runtime:2.2.0-alpha01" kapt "androidx.room:room-compiler:2.2.0-alpha01" - implementation "android.arch.work:work-runtime-ktx:1.0.1" + + + // Dialog for adding a new location + implementation 'com.afollestad.material-dialogs:core:3.1.0' + implementation 'com.afollestad.material-dialogs:input:3.1.0' } diff --git a/app/schemas/bou.amine.apps.mteo.persistence.database.AppDatabase/2.json b/app/schemas/bou.amine.apps.mteo.persistence.database.AppDatabase/2.json new file mode 100644 index 0000000..599616b --- /dev/null +++ b/app/schemas/bou.amine.apps.mteo.persistence.database.AppDatabase/2.json @@ -0,0 +1,52 @@ +{ + "formatVersion": 1, + "database": { + "version": 2, + "identityHash": "503cac2cbbdac52d55e38fb7f11e6365", + "entities": [ + { + "tableName": "locations", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `lat` REAL NOT NULL, `lng` REAL NOT NULL, `name` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lat", + "columnName": "lat", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "lng", + "columnName": "lng", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '503cac2cbbdac52d55e38fb7f11e6365')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/bou/amine/apps/mteo/MainActivity.kt b/app/src/main/java/bou/amine/apps/mteo/MainActivity.kt index 8f12938..9f46d82 100644 --- a/app/src/main/java/bou/amine/apps/mteo/MainActivity.kt +++ b/app/src/main/java/bou/amine/apps/mteo/MainActivity.kt @@ -13,8 +13,11 @@ import androidx.core.content.ContextCompat import androidx.room.Room import bou.amine.apps.mteo.api.DarkSkyApi import bou.amine.apps.mteo.api.ForecastResponse +import bou.amine.apps.mteo.persistence.MIGRATION_1_2 import bou.amine.apps.mteo.persistence.database.AppDatabase import bou.amine.apps.mteo.persistence.entities.LocationView +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.input.input import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -38,7 +41,7 @@ class MainActivity : AppCompatActivity() { db = Room.databaseBuilder( applicationContext, AppDatabase::class.java, "mteo-database" - ).build() + ).addMigrations(MIGRATION_1_2).build() thread { val locations = db.locationDao().locations() @@ -109,10 +112,14 @@ class MainActivity : AppCompatActivity() { override fun onLocationChanged(location: Location) { Toast.makeText(this@MainActivity, "location", Toast.LENGTH_SHORT).show() mLocationManager.removeUpdates(this) - currentLocation = LocationView(location.latitude, location.longitude) - fetchForecastData() - thread { - db.locationDao().insertLocation(currentLocation) + MaterialDialog(this@MainActivity).show { + input { _, text -> + currentLocation = LocationView(location.latitude, location.longitude, text.toString()) + fetchForecastData() + thread { + db.locationDao().insertLocation(currentLocation) + } + } } } } diff --git a/app/src/main/java/bou/amine/apps/mteo/persistence/database/AppDatabase.kt b/app/src/main/java/bou/amine/apps/mteo/persistence/database/AppDatabase.kt index 6244ed6..27ae295 100644 --- a/app/src/main/java/bou/amine/apps/mteo/persistence/database/AppDatabase.kt +++ b/app/src/main/java/bou/amine/apps/mteo/persistence/database/AppDatabase.kt @@ -5,7 +5,7 @@ import androidx.room.RoomDatabase import bou.amine.apps.mteo.persistence.dao.LocationsDao import bou.amine.apps.mteo.persistence.entities.LocationView -@Database(entities = [LocationView::class], version = 1) +@Database(entities = [LocationView::class], version = 2) abstract class AppDatabase : RoomDatabase() { abstract fun locationDao(): LocationsDao } \ No newline at end of file diff --git a/app/src/main/java/bou/amine/apps/mteo/persistence/entities/LocationView.kt b/app/src/main/java/bou/amine/apps/mteo/persistence/entities/LocationView.kt index 094895b..a0da107 100644 --- a/app/src/main/java/bou/amine/apps/mteo/persistence/entities/LocationView.kt +++ b/app/src/main/java/bou/amine/apps/mteo/persistence/entities/LocationView.kt @@ -9,7 +9,9 @@ data class LocationView( @ColumnInfo(name = "lat") val lat: Double, @ColumnInfo(name = "lng") - val lng: Double + val lng: Double, + @ColumnInfo(name = "name") + val name: String ) { @PrimaryKey(autoGenerate = true) var id: Int = 0 diff --git a/app/src/main/java/bou/amine/apps/mteo/persistence/migrations.kt b/app/src/main/java/bou/amine/apps/mteo/persistence/migrations.kt new file mode 100644 index 0000000..6b218ed --- /dev/null +++ b/app/src/main/java/bou/amine/apps/mteo/persistence/migrations.kt @@ -0,0 +1,10 @@ +package bou.amine.apps.mteo.persistence + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +val MIGRATION_1_2: Migration = object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE 'locations' ADD COLUMN 'name' TEXT NOT NULL") + } +} \ No newline at end of file