From 2657cb75c79ef0ce5939226b43e3430175aa14a5 Mon Sep 17 00:00:00 2001 From: lihenggui Date: Tue, 15 Oct 2024 11:23:53 -0700 Subject: [PATCH] Migrate interest module to use koin --- feature/interests/build.gradle.kts | 2 ++ .../feature/interests/InterestsScreen.kt | 3 ++- .../feature/interests/InterestsViewModel.kt | 3 +-- .../feature/interests/di/InterestModule.kt | 25 +++++++++++++++++++ .../navigation/InterestsNavigation.kt | 2 +- 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/di/InterestModule.kt diff --git a/feature/interests/build.gradle.kts b/feature/interests/build.gradle.kts index 4bffcdb89..abb195c0d 100644 --- a/feature/interests/build.gradle.kts +++ b/feature/interests/build.gradle.kts @@ -18,6 +18,7 @@ plugins { alias(libs.plugins.nowinandroid.cmp.feature) alias(libs.plugins.nowinandroid.android.library.jacoco) alias(libs.plugins.roborazzi) + alias(libs.plugins.kotlin.serialization) } android { namespace = "com.google.samples.apps.nowinandroid.feature.interests" @@ -34,6 +35,7 @@ kotlin { implementation(compose.ui) implementation(compose.components.resources) implementation(compose.components.uiToolingPreview) + implementation(libs.kotlinx.serialization.core) } commonMain.dependencies { implementation(projects.core.testing) diff --git a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt index f3e570709..8d112f5f1 100644 --- a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt +++ b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsScreen.kt @@ -35,13 +35,14 @@ import nowinandroid.feature.interests.generated.resources.feature_interests_empt import nowinandroid.feature.interests.generated.resources.feature_interests_loading import org.jetbrains.compose.resources.stringResource import org.jetbrains.compose.ui.tooling.preview.PreviewParameter +import org.koin.compose.viewmodel.koinViewModel @Composable fun InterestsRoute( onTopicClick: (String) -> Unit, modifier: Modifier = Modifier, highlightSelectedTopic: Boolean = false, - viewModel: InterestsViewModel, + viewModel: InterestsViewModel = koinViewModel(), ) { val uiState by viewModel.uiState.collectAsStateWithLifecycle() diff --git a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt index b49fa4521..ec33dd44a 100644 --- a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt +++ b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/InterestsViewModel.kt @@ -30,9 +30,8 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch -import me.tatarka.inject.annotations.Inject -class InterestsViewModel @Inject constructor( +class InterestsViewModel( private val savedStateHandle: SavedStateHandle, val userDataRepository: UserDataRepository, getFollowableTopics: GetFollowableTopicsUseCase, diff --git a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/di/InterestModule.kt b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/di/InterestModule.kt new file mode 100644 index 000000000..0bb7ac006 --- /dev/null +++ b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/di/InterestModule.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2024 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.interests.di + +import com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel +import org.koin.core.module.dsl.viewModel +import org.koin.dsl.module + +val interestModule = module { + viewModel { InterestsViewModel(get(), get(), get()) } +} diff --git a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt index 7c52bf12c..d83e4a9b2 100644 --- a/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt +++ b/feature/interests/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt @@ -29,5 +29,5 @@ fun NavController.navigateToInterests( initialTopicId: String? = null, navOptions: NavOptions? = null, ) { -// navigate(route = InterestsRoute(initialTopicId), navOptions) + navigate(route = InterestsRoute(initialTopicId), navOptions) }