From fc58680c06ab9f0cd889257915e7e52e9bdbad56 Mon Sep 17 00:00:00 2001 From: Don Turner Date: Fri, 19 Jul 2024 09:00:14 +0100 Subject: [PATCH] Remove string-based argument name from navigation code Change-Id: I7c880cc73374e053ea0d41df00a4bda95ed4b551 --- .../nowinandroid/feature/interests/InterestsViewModel.kt | 8 +++++--- .../feature/interests/navigation/InterestsNavigation.kt | 2 -- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt index 0ffa3a473..67cc8884f 100644 --- a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt +++ b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt @@ -25,7 +25,6 @@ import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCa import com.google.samples.apps.nowinandroid.core.domain.TopicSortField import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute -import com.google.samples.apps.nowinandroid.feature.interests.navigation.TOPIC_ID_KEY import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow @@ -41,9 +40,12 @@ class InterestsViewModel @Inject constructor( getFollowableTopics: GetFollowableTopicsUseCase, ) : ViewModel() { + // Key used to save and retrieve the currently selected topic id from saved state. + private val selectedTopicIdKey = "selectedTopicIdKey" + private val interestsRoute: InterestsRoute = savedStateHandle.toRoute() private val selectedTopicId = savedStateHandle.getStateFlow( - key = TOPIC_ID_KEY, + key = selectedTopicIdKey, initialValue = interestsRoute.initialTopicId, ) @@ -64,7 +66,7 @@ class InterestsViewModel @Inject constructor( } fun onTopicClick(topicId: String?) { - savedStateHandle[TOPIC_ID_KEY] = topicId + savedStateHandle[selectedTopicIdKey] = topicId } } diff --git a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt index b0a85a5be..be06333b5 100644 --- a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt +++ b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt @@ -20,8 +20,6 @@ import androidx.navigation.NavController import androidx.navigation.NavOptions import kotlinx.serialization.Serializable -const val TOPIC_ID_KEY = "topicId" - @Serializable data class InterestsRoute( // The ID of the topic which will be initially selected at this destination val initialTopicId: String?,