parent
d97ad3724c
commit
74837038a4
@ -0,0 +1,17 @@
|
|||||||
|
package uca.baptistearthur.geocaching.application
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import uca.baptistearthur.geocaching.data.Database
|
||||||
|
|
||||||
|
class RTApplication: Application() {
|
||||||
|
|
||||||
|
lateinit var db: Database
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
db = Database.getInstance(applicationContext) as Database
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package uca.baptistearthur.geocaching.converters
|
||||||
|
|
||||||
|
import androidx.room.TypeConverter
|
||||||
|
import java.util.Date
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import uca.baptistearthur.geocaching.model.Place
|
||||||
|
|
||||||
|
class Converters {
|
||||||
|
@TypeConverter
|
||||||
|
fun toDate(timestamp: Long?): Date? {
|
||||||
|
return if (timestamp == null) null else Date(timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toLong(date: Date?): Long? {
|
||||||
|
return date?.time
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toString(places: List<Place>): String {
|
||||||
|
return Gson().toJson(places)
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
fun toPlaces(value: String): List<Place> {
|
||||||
|
val listType = object : TypeToken<List<Place>>() {}.type
|
||||||
|
return Gson().fromJson(value, listType)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package uca.baptistearthur.geocaching.data
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import androidx.room.TypeConverters
|
||||||
|
import uca.baptistearthur.geocaching.converters.Converters
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
@androidx.room.Database(entities = arrayOf(RoadTripEntity::class), version=1)
|
||||||
|
@TypeConverters(Converters::class)
|
||||||
|
abstract class Database : RoomDatabase(){
|
||||||
|
|
||||||
|
abstract fun roadTripDAO(): RoadTripDAO
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
private var INSTANCE: Database ?= null
|
||||||
|
|
||||||
|
fun getInstance(context: Context) =
|
||||||
|
INSTANCE ?: synchronized(this){
|
||||||
|
val db = Room.databaseBuilder(context, Database::class.java, "roadTripDB").build()
|
||||||
|
INSTANCE = db
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package uca.baptistearthur.geocaching.data
|
||||||
|
|
||||||
|
import androidx.annotation.RequiresPermission.Read
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
@androidx.room.Dao
|
||||||
|
interface RoadTripDAO {
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insertRoadTrip(r: RoadTripEntity)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun deleteRoadTrip(r: RoadTripEntity)
|
||||||
|
|
||||||
|
@Query("SELECT * FROM Roadtrip")
|
||||||
|
fun getAllRoadTrips(): Flow<MutableList<RoadTripEntity>>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM Roadtrip WHERE id = :id")
|
||||||
|
fun getRoadTripById(id: Int): Flow<RoadTripEntity>
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
package uca.baptistearthur.geocaching.model
|
|
||||||
|
|
||||||
import java.util.Date
|
|
||||||
|
|
||||||
class RoadTrip(
|
|
||||||
val name: String,
|
|
||||||
val date: Date,
|
|
||||||
val places: MutableList<Place>
|
|
||||||
){
|
|
||||||
|
|
||||||
fun addPlaceToRoadTripList(place: Place) = places.add(place)
|
|
||||||
fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(Place(latitude, longitude))
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package uca.baptistearthur.geocaching.model
|
||||||
|
|
||||||
|
import androidx.room.ColumnInfo
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
@Entity(tableName = "Roadtrip")
|
||||||
|
class RoadTripEntity(
|
||||||
|
@PrimaryKey(autoGenerate = true) val id: Int,
|
||||||
|
@ColumnInfo(name="name") val name: String,
|
||||||
|
@ColumnInfo(name="date") val date: Date,
|
||||||
|
@ColumnInfo(name="places") val places: MutableList<Place>
|
||||||
|
){
|
||||||
|
|
||||||
|
fun addPlaceToRoadTripList(place: Place) = places.add(place)
|
||||||
|
fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(Place(latitude, longitude))
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package uca.baptistearthur.geocaching.viewModels
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import uca.baptistearthur.geocaching.data.RoadTripDAO
|
||||||
|
import uca.baptistearthur.geocaching.model.RoadTripEntity
|
||||||
|
|
||||||
|
class RoadTripViewModel(val dao: RoadTripDAO): ViewModel() {
|
||||||
|
|
||||||
|
fun getRoadTripById(id: Int) = dao.getRoadTripById(id) // .asLiveData() // ne marche pas -> impossible d'importer
|
||||||
|
|
||||||
|
fun getAllRoadTrips() = dao.getAllRoadTrips() // .asLiveData()
|
||||||
|
|
||||||
|
fun insertRoadTrip(r: RoadTripEntity){
|
||||||
|
viewModelScope.launch {
|
||||||
|
dao.insertRoadTrip(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteRoadTrip(r: RoadTripEntity){
|
||||||
|
viewModelScope.launch {
|
||||||
|
dao.deleteRoadTrip(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package uca.baptistearthur.geocaching.viewModels
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import uca.baptistearthur.geocaching.data.RoadTripDAO
|
||||||
|
|
||||||
|
class ViewModelFactory(private val dao: RoadTripDAO): ViewModelProvider.Factory{
|
||||||
|
|
||||||
|
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||||
|
if (modelClass.isAssignableFrom(RoadTripViewModel::class.java)){
|
||||||
|
return RoadTripViewModel(dao) as T
|
||||||
|
}
|
||||||
|
throw IllegalArgumentException("Unknown ViewModel class")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue