Added the name of the location.
This commit is contained in:
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user