diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt index ba8882fe6..f40fc31da 100644 --- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt +++ b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt @@ -22,6 +22,8 @@ import androidx.compose.runtime.snapshots.Snapshot.Companion.withMutableSnapshot import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi +import androidx.lifecycle.viewmodel.compose.saveable import com.google.samples.apps.nowinandroid.core.domain.combine import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository @@ -30,7 +32,6 @@ import com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthor import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.SaveableNewsResource -import com.google.samples.apps.nowinandroid.feature.foryou.util.saveable import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject import kotlinx.coroutines.flow.SharingStarted @@ -42,6 +43,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch +@OptIn(SavedStateHandleSaveableApi::class) @HiltViewModel class ForYouViewModel @Inject constructor( private val authorsRepository: AuthorsRepository, diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/util/SavedStateHandleExtensions.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/util/SavedStateHandleExtensions.kt deleted file mode 100644 index 163c98f9b..000000000 --- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/util/SavedStateHandleExtensions.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.samples.apps.nowinandroid.feature.foryou.util - -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.getValue -import androidx.compose.runtime.saveable.Saver -import androidx.compose.runtime.saveable.autoSaver -import androidx.compose.runtime.setValue -import androidx.lifecycle.SavedStateHandle -import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi -import androidx.lifecycle.viewmodel.compose.saveable -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadOnlyProperty -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -// These are placeholder solutions for https://issuetracker.google.com/issues/224565154 and -// https://issuetracker.google.com/issues/225014345. -// With the following, it is possible to use the Compose Saver APIs to back values in the -// SavedStateHandle to persist through process death, in a similar form as rememberSaveable - -/** - * A [PropertyDelegateProvider] allowing the use of [saveable] with the key provided by the name - * of the property. - * - * https://issuetracker.google.com/issues/225014345 - */ -@OptIn(SavedStateHandleSaveableApi::class) -fun SavedStateHandle.saveable( - saver: Saver = autoSaver(), - init: () -> T, -): PropertyDelegateProvider> = - PropertyDelegateProvider { _, property -> - val value = saveable( - key = property.name, - saver = saver, - init = init - ) - - ReadOnlyProperty { _, _ -> value } - } - -/** - * A nested [PropertyDelegateProvider] allowing the use of [saveable] with the key provided by the - * name, as well as direct access to the value contained with [MutableState]. - * - * This allows the main usage to look almost identical to - * `rememberSaveable { mutableStateOf(...) }`: - * - * ``` - * val value by savedStateHandle.saveable { mutableStateOf("initialValue") } - * ``` - * - * https://issuetracker.google.com/issues/225014345 - */ -@OptIn(SavedStateHandleSaveableApi::class) -@JvmName("saveableMutableState") -fun SavedStateHandle.saveable( - stateSaver: Saver = autoSaver(), - init: () -> MutableState, -): PropertyDelegateProvider> = - PropertyDelegateProvider> { _, property -> - val mutableState = saveable( - key = property.name, - stateSaver = stateSaver, - init = init - ) - - object : ReadWriteProperty { - override fun getValue(thisRef: Any, property: KProperty<*>): T = - mutableState.getValue(thisRef, property) - - override fun setValue(thisRef: Any, property: KProperty<*>, value: T) = - mutableState.setValue(thisRef, property, value) - } - } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7ef9f3b38..f6cab0c90 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,7 +11,7 @@ androidxCore = "1.7.0" androidxDataStore = "1.0.0" androidxEspresso = "3.3.0" androidxHiltNavigationCompose = "1.0.0-rc01" -androidxLifecycle = "2.5.0-alpha06" +androidxLifecycle = "2.5.0-beta01" androidxMacroBenchmark = "1.1.0-beta04" androidxNavigation = "2.4.0-rc01" androidxProfileinstaller = "1.2.0-alpha02"