add ViewModel

master
Bastien OLLIER 2 years ago
parent 307f51c601
commit 2aa3d6c49c

@ -46,8 +46,8 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.retrofit2:converter-moshi:2.9.0" implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0" implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.fragment:fragment-ktx:1.5.6' implementation 'androidx.fragment:fragment-ktx:1.5.6'
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
@ -58,6 +58,9 @@ dependencies {
kapt 'androidx.room:room-compiler:2.5.1' kapt 'androidx.room:room-compiler:2.5.1'
implementation 'androidx.room:room-ktx:2.5.1' implementation 'androidx.room:room-ktx:2.5.1'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.1"
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'

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

@ -2,10 +2,8 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
tools:context=".LikesPage">
<!-- TODO: Update blank fragment layout -->
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

Loading…
Cancel
Save