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

Commit

Permalink
PR adjust
Browse files Browse the repository at this point in the history
Addressed by #235 (comment)
  • Loading branch information
XinyueZ committed Nov 16, 2018
1 parent f1c8405 commit 917edcc
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package com.google.samples.apps.sunflower.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.google.samples.apps.sunflower.PlantDetailFragment
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.Plant
import com.google.samples.apps.sunflower.data.PlantRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

/**
Expand All @@ -32,11 +35,34 @@ class PlantDetailViewModel(
plantRepository: PlantRepository,
private val gardenPlantingRepository: GardenPlantingRepository,
private val plantId: String
) : ScopedViewModel() {
) : ViewModel() {

val isPlanted: LiveData<Boolean>
val plant: LiveData<Plant>

/**
* This is the job for all coroutines started by this ViewModel.
*
* Cancelling this job will cancel all coroutines started by this ViewModel.
*/
private val viewModelJob = Job()

/**
* This is the scope for all coroutines launched by [PlantDetailViewModel].
*
* Since we pass [viewModelJob], you can cancel all coroutines launched by [viewModelScope] by calling
* viewModelJob.cancel(). This is called in [onCleared].
*/
private val viewModelScope = CoroutineScope(IO + viewModelJob)

/**
* Cancel all coroutines when the ViewModel is cleared.
*/
override fun onCleared() {
super.onCleared()
viewModelJob.cancel()
}

init {

/* The getGardenPlantingForPlant method returns a LiveData from querying the database. The
Expand All @@ -49,7 +75,7 @@ class PlantDetailViewModel(
plant = plantRepository.getPlant(plantId)
}

fun addPlantToGarden() = launch(IO) {
fun addPlantToGarden() = viewModelScope.launch(IO) {
gardenPlantingRepository.createGardenPlanting(plantId)
}
}

0 comments on commit 917edcc

Please sign in to comment.