Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit f02de96

Browse files
XinyueZnic0lette
authored andcommitted
[UPDATE/OPTIMISE] Introduce viewmodel-ktx and simplify PlantDetailViewMode (#348)
* Update lifecycle-lib-2.1.0-alpha02 * Introduce viewmodel-ktx and simplify PlantDetailViewModel
1 parent 7cb2da2 commit f02de96

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
5757
implementation "androidx.core:core-ktx:$rootProject.ktxVersion"
5858
implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"
59+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
5960
implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
6061
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
6162
implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"

app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package com.google.samples.apps.sunflower.viewmodels
1919
import androidx.lifecycle.LiveData
2020
import androidx.lifecycle.Transformations
2121
import androidx.lifecycle.ViewModel
22+
import androidx.lifecycle.viewModelScope
2223
import com.google.samples.apps.sunflower.PlantDetailFragment
2324
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
2425
import com.google.samples.apps.sunflower.data.Plant
2526
import com.google.samples.apps.sunflower.data.PlantRepository
26-
import kotlinx.coroutines.CoroutineScope
27-
import kotlinx.coroutines.Dispatchers.Main
28-
import kotlinx.coroutines.Job
27+
import kotlinx.coroutines.ExperimentalCoroutinesApi
28+
import kotlinx.coroutines.cancel
2929
import kotlinx.coroutines.launch
3030

3131
/**
@@ -40,27 +40,13 @@ class PlantDetailViewModel(
4040
val isPlanted: LiveData<Boolean>
4141
val plant: LiveData<Plant>
4242

43-
/**
44-
* This is the job for all coroutines started by this ViewModel.
45-
*
46-
* Cancelling this job will cancel all coroutines started by this ViewModel.
47-
*/
48-
private val viewModelJob = Job()
49-
50-
/**
51-
* This is the scope for all coroutines launched by [PlantDetailViewModel].
52-
*
53-
* Since we pass [viewModelJob], you can cancel all coroutines launched by [viewModelScope] by calling
54-
* viewModelJob.cancel(). This is called in [onCleared].
55-
*/
56-
private val viewModelScope = CoroutineScope(Main + viewModelJob)
57-
5843
/**
5944
* Cancel all coroutines when the ViewModel is cleared.
6045
*/
46+
@ExperimentalCoroutinesApi
6147
override fun onCleared() {
6248
super.onCleared()
63-
viewModelJob.cancel()
49+
viewModelScope.cancel()
6450
}
6551

6652
init {

0 commit comments

Comments
 (0)