Added the name of the location.

This commit is contained in:
aminecmi 2019-08-04 11:50:03 +02:00
parent f39414ddff
commit 5e518ddc0e
6 changed files with 85 additions and 9 deletions

View File

@ -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'
}

View File

@ -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')"
]
}
}

View File

@ -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)
}
}
}
}
}

View File

@ -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
}

View File

@ -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

View File

@ -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")
}
}