From bc784d8824c53a2d468c2c5a76fd4578bfb5fdef Mon Sep 17 00:00:00 2001 From: lihenggui Date: Mon, 14 Oct 2024 14:46:04 -0700 Subject: [PATCH] Migrate topic module to cmp --- .../nowinandroid/feature/topic/TopicScreen.kt | 3 ++- .../feature/topic/TopicViewModel.kt | 5 ++-- .../feature/topic/di/TopicModule.kt | 26 +++++++++++++++++++ .../topic/navigation/TopicNavigation.kt | 11 ++++---- 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/di/TopicModule.kt diff --git a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt index bf3b6d01b..da2fddee0 100644 --- a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt +++ b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt @@ -71,6 +71,7 @@ import nowinandroid.feature.topic.generated.resources.feature_topic_loading import org.jetbrains.compose.resources.stringResource import org.jetbrains.compose.ui.tooling.preview.Preview import org.jetbrains.compose.ui.tooling.preview.PreviewParameter +import org.koin.compose.viewmodel.koinViewModel @Composable internal fun TopicScreen( @@ -78,7 +79,7 @@ internal fun TopicScreen( onBackClick: () -> Unit, onTopicClick: (String) -> Unit, modifier: Modifier = Modifier, - viewModel: TopicViewModel, + viewModel: TopicViewModel = koinViewModel(), ) { val topicUiState: TopicUiState by viewModel.topicUiState.collectAsStateWithLifecycle() val newsUiState: NewsUiState by viewModel.newsUiState.collectAsStateWithLifecycle() diff --git a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index 2a1f98431..04684db15 100644 --- a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -37,9 +37,10 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch -import me.tatarka.inject.annotations.Inject +import org.koin.android.annotation.KoinViewModel -class TopicViewModel @Inject constructor( +@KoinViewModel +class TopicViewModel( savedStateHandle: SavedStateHandle, private val userDataRepository: UserDataRepository, topicsRepository: TopicsRepository, diff --git a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/di/TopicModule.kt b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/di/TopicModule.kt new file mode 100644 index 000000000..d0cd4f563 --- /dev/null +++ b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/di/TopicModule.kt @@ -0,0 +1,26 @@ +/* + * 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.topic.di + +import com.google.samples.apps.nowinandroid.feature.topic.TopicViewModel +import org.koin.core.module.dsl.viewModel +import org.koin.core.module.dsl.viewModelOf +import org.koin.dsl.module + +val topicModule = module { + viewModelOf(::TopicViewModel) +} \ No newline at end of file diff --git a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt index 99d073cd0..fabb82b10 100644 --- a/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt +++ b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt @@ -20,6 +20,7 @@ import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavOptionsBuilder import androidx.navigation.compose.composable +import com.google.samples.apps.nowinandroid.feature.topic.TopicScreen import kotlinx.serialization.Serializable @Serializable data class TopicRoute(val id: String) @@ -36,10 +37,10 @@ fun NavGraphBuilder.topicScreen( onTopicClick: (String) -> Unit, ) { composable { -// TopicScreen( -// showBackButton = showBackButton, -// onBackClick = onBackClick, -// onTopicClick = onTopicClick, -// ) + TopicScreen( + showBackButton = showBackButton, + onBackClick = onBackClick, + onTopicClick = onTopicClick, + ) } }