parent
307f51c601
commit
2aa3d6c49c
@ -0,0 +1,26 @@
|
|||||||
|
package fr.iut.mapping
|
||||||
|
|
||||||
|
import androidx.lifecycle.*
|
||||||
|
import fr.iut.mapping.database.Entity.RestaurantEntity
|
||||||
|
import fr.iut.mapping.database.RestaurantRepository
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
class RestaurantViewModel(private val repository: RestaurantRepository): ViewModel() {
|
||||||
|
val restaurantLikes: LiveData<List<RestaurantEntity>> = repository.restaurantLikes.asLiveData()
|
||||||
|
|
||||||
|
fun insert(restaurant: RestaurantEntity) = viewModelScope.launch {
|
||||||
|
repository.insert(restaurant)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class RestaurantViewModelFactory(private val repository: RestaurantRepository) : ViewModelProvider.Factory{
|
||||||
|
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||||
|
if (modelClass.isAssignableFrom(RestaurantViewModel::class.java)) {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
return RestaurantViewModel(repository) as T
|
||||||
|
}
|
||||||
|
throw IllegalArgumentException("Unknown ViewModel class")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue