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

Commit

Permalink
Update Navigation to 2.2.0 and add SavedStateHandle (#574)
Browse files Browse the repository at this point in the history
* Update Navigation to 2.2.0 and add SavedStateHandle

Persist values in PlantListViewModel using SavedStateHandle

* Fix lint error
  • Loading branch information
tiembo authored Feb 3, 2020
1 parent 2da1ffa commit c6c1379
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.google.samples.apps.sunflower.viewmodels.PlantListViewModel
class PlantListFragment : Fragment() {

private val viewModel: PlantListViewModel by viewModels {
InjectorUtils.providePlantListViewModelFactory(requireContext())
InjectorUtils.providePlantListViewModelFactory(this)
}

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.samples.apps.sunflower.utilities

import android.content.Context
import androidx.fragment.app.Fragment
import com.google.samples.apps.sunflower.data.AppDatabase
import com.google.samples.apps.sunflower.data.GardenPlantingRepository
import com.google.samples.apps.sunflower.data.PlantRepository
Expand Down Expand Up @@ -46,9 +47,9 @@ object InjectorUtils {
return GardenPlantingListViewModelFactory(repository)
}

fun providePlantListViewModelFactory(context: Context): PlantListViewModelFactory {
val repository = getPlantRepository(context)
return PlantListViewModelFactory(repository)
fun providePlantListViewModelFactory(fragment: Fragment): PlantListViewModelFactory {
val repository = getPlantRepository(fragment.requireContext())
return PlantListViewModelFactory(repository, fragment)
}

fun providePlantDetailViewModelFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.samples.apps.sunflower.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import com.google.samples.apps.sunflower.PlantListFragment
Expand All @@ -27,11 +28,12 @@ import com.google.samples.apps.sunflower.data.PlantRepository
/**
* The ViewModel for [PlantListFragment].
*/
class PlantListViewModel internal constructor(plantRepository: PlantRepository) : ViewModel() {
class PlantListViewModel internal constructor(
plantRepository: PlantRepository,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {

private val growZoneNumber = MutableLiveData<Int>(NO_GROW_ZONE)

val plants: LiveData<List<Plant>> = growZoneNumber.switchMap {
val plants: LiveData<List<Plant>> = getSavedGrowZoneNumber().switchMap {
if (it == NO_GROW_ZONE) {
plantRepository.getPlants()
} else {
Expand All @@ -40,16 +42,21 @@ class PlantListViewModel internal constructor(plantRepository: PlantRepository)
}

fun setGrowZoneNumber(num: Int) {
growZoneNumber.value = num
savedStateHandle.set(GROW_ZONE_SAVED_STATE_KEY, num)
}

fun clearGrowZoneNumber() {
growZoneNumber.value = NO_GROW_ZONE
savedStateHandle.set(GROW_ZONE_SAVED_STATE_KEY, NO_GROW_ZONE)
}

fun isFiltered() = growZoneNumber.value != NO_GROW_ZONE
fun isFiltered() = getSavedGrowZoneNumber().value != NO_GROW_ZONE

private fun getSavedGrowZoneNumber(): MutableLiveData<Int> {
return savedStateHandle.getLiveData<Int>(GROW_ZONE_SAVED_STATE_KEY, NO_GROW_ZONE)
}

companion object {
private const val NO_GROW_ZONE = -1
private const val GROW_ZONE_SAVED_STATE_KEY = "GROW_ZONE_SAVED_STATE_KEY"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@

package com.google.samples.apps.sunflower.viewmodels

import android.os.Bundle
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

import androidx.savedstate.SavedStateRegistryOwner
import com.google.samples.apps.sunflower.data.PlantRepository

/**
* Factory for creating a [PlantListViewModel] with a constructor that takes a [PlantRepository].
*/
class PlantListViewModelFactory(
private val repository: PlantRepository
) : ViewModelProvider.NewInstanceFactory() {
private val repository: PlantRepository,
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>) = PlantListViewModel(repository) as T
override fun <T : ViewModel?> create(
key: String,
modelClass: Class<T>,
handle: SavedStateHandle
): T {
return PlantListViewModel(repository, handle) as T
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ buildscript {
ktxVersion = '1.0.2'
lifecycleVersion = '2.2.0-alpha01'
materialVersion = '1.1.0-alpha09'
navigationVersion = '2.0.0'
navigationVersion = '2.2.0'
recyclerViewVersion = '1.1.0-alpha05'
roomVersion = '2.1.0'
runnerVersion = '1.0.1'
Expand Down

0 comments on commit c6c1379

Please sign in to comment.