From dfea9ee70b3ae4c617263efc2fd982559dd428f6 Mon Sep 17 00:00:00 2001 From: lihenggui Date: Mon, 7 Oct 2024 14:53:45 -0700 Subject: [PATCH] Move topic module to cmp --- feature/topic/build.gradle.kts | 42 ++++++++++++++----- .../feature/topic/TopicScreenTest.kt | 0 .../feature/topic/TopicViewModelTest.kt | 2 +- .../feature_topic_ic_topic_placeholder.xml | 0 .../composeResources}/values/strings.xml | 0 .../feature/topic/TopicDetailPlaceholder.kt | 15 ++++--- .../nowinandroid/feature/topic/TopicScreen.kt | 23 +++++----- .../feature/topic/TopicViewModel.kt | 4 +- .../topic/navigation/TopicNavigation.kt | 12 +++--- feature/topic/src/main/AndroidManifest.xml | 17 -------- 10 files changed, 63 insertions(+), 52 deletions(-) rename feature/topic/src/{androidTest => androidInstrumentedTest}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt (100%) rename feature/topic/src/{test => androidUnitTest}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt (99%) rename feature/topic/src/{main/res => commonMain/composeResources}/drawable/feature_topic_ic_topic_placeholder.xml (100%) rename feature/topic/src/{main/res => commonMain/composeResources}/values/strings.xml (100%) rename feature/topic/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt (79%) rename feature/topic/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt (94%) rename feature/topic/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt (98%) rename feature/topic/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt (82%) delete mode 100644 feature/topic/src/main/AndroidManifest.xml diff --git a/feature/topic/build.gradle.kts b/feature/topic/build.gradle.kts index bd8b59ec8..83a1d7a5a 100644 --- a/feature/topic/build.gradle.kts +++ b/feature/topic/build.gradle.kts @@ -15,21 +15,43 @@ */ plugins { - alias(libs.plugins.nowinandroid.android.feature) - alias(libs.plugins.nowinandroid.android.library.compose) + alias(libs.plugins.nowinandroid.cmp.feature) alias(libs.plugins.nowinandroid.android.library.jacoco) + alias(libs.plugins.kotlin.serialization) } android { namespace = "com.google.samples.apps.nowinandroid.feature.topic" } -dependencies { - implementation(projects.core.data) - - testImplementation(projects.core.testing) - testImplementation(libs.robolectric) +kotlin { + sourceSets { + commonMain.dependencies { + implementation(projects.core.data) + implementation(projects.core.ui) + implementation(libs.coil) + implementation(libs.coil.compose) + implementation(compose.material3) + implementation(compose.foundation) + implementation(compose.ui) + implementation(compose.components.resources) + implementation(compose.components.uiToolingPreview) + } + commonMain.dependencies { + implementation(projects.core.testing) + } + androidUnitTest.dependencies { + implementation(libs.robolectric) + implementation(libs.roborazzi) + implementation(projects.core.screenshotTesting) + } + androidInstrumentedTest.dependencies { + implementation(projects.core.testing) + implementation(libs.bundles.androidx.compose.ui.test) + } + } +} - androidTestImplementation(libs.bundles.androidx.compose.ui.test) - androidTestImplementation(projects.core.testing) -} \ No newline at end of file +compose.resources { + publicResClass = true +} diff --git a/feature/topic/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt b/feature/topic/src/androidInstrumentedTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt similarity index 100% rename from feature/topic/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt rename to feature/topic/src/androidInstrumentedTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt diff --git a/feature/topic/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt b/feature/topic/src/androidUnitTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt similarity index 99% rename from feature/topic/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt rename to feature/topic/src/androidUnitTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt index 34f21a59a..47d46ee37 100644 --- a/feature/topic/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt +++ b/feature/topic/src/androidUnitTest/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt @@ -71,7 +71,7 @@ class TopicViewModelTest { fun setup() { viewModel = TopicViewModel( savedStateHandle = SavedStateHandle( - route = TopicRoute(id = testInputTopics[0].topic.id), +// route = TopicRoute(id = testInputTopics[0].topic.id), ), userDataRepository = userDataRepository, topicsRepository = topicsRepository, diff --git a/feature/topic/src/main/res/drawable/feature_topic_ic_topic_placeholder.xml b/feature/topic/src/commonMain/composeResources/drawable/feature_topic_ic_topic_placeholder.xml similarity index 100% rename from feature/topic/src/main/res/drawable/feature_topic_ic_topic_placeholder.xml rename to feature/topic/src/commonMain/composeResources/drawable/feature_topic_ic_topic_placeholder.xml diff --git a/feature/topic/src/main/res/values/strings.xml b/feature/topic/src/commonMain/composeResources/values/strings.xml similarity index 100% rename from feature/topic/src/main/res/values/strings.xml rename to feature/topic/src/commonMain/composeResources/values/strings.xml diff --git a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt similarity index 79% rename from feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt rename to feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt index 627fb8fb3..f5e6f68df 100644 --- a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt +++ b/feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicDetailPlaceholder.kt @@ -28,11 +28,14 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.res.painterResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme +import nowinandroid.feature.topic.generated.resources.Res +import nowinandroid.feature.topic.generated.resources.feature_topic_ic_topic_placeholder +import nowinandroid.feature.topic.generated.resources.feature_topic_select_an_interest +import org.jetbrains.compose.resources.painterResource +import org.jetbrains.compose.resources.stringResource +import org.jetbrains.compose.ui.tooling.preview.Preview @Composable fun TopicDetailPlaceholder(modifier: Modifier = Modifier) { @@ -50,19 +53,19 @@ fun TopicDetailPlaceholder(modifier: Modifier = Modifier) { ), ) { Icon( - painter = painterResource(id = R.drawable.feature_topic_ic_topic_placeholder), + painter = painterResource(Res.drawable.feature_topic_ic_topic_placeholder), contentDescription = null, tint = MaterialTheme.colorScheme.primary, ) Text( - text = stringResource(id = R.string.feature_topic_select_an_interest), + text = stringResource(Res.string.feature_topic_select_an_interest), style = MaterialTheme.typography.titleLarge, ) } } } -@Preview(widthDp = 200, heightDp = 300) +@Preview @Composable fun TopicDetailPlaceholderPreview() { NiaTheme { diff --git a/feature/topic/src/main/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 similarity index 94% rename from feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt rename to feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt index 13fbab784..6dbc2742c 100644 --- a/feature/topic/src/main/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 @@ -46,12 +46,10 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import coil3.ImageLoader +import coil3.compose.LocalPlatformContext import com.google.samples.apps.nowinandroid.core.designsystem.component.DynamicAsyncImage import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaBackground import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaFilterChip @@ -65,10 +63,14 @@ import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews import com.google.samples.apps.nowinandroid.core.ui.TrackScreenViewEvent -import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank import com.google.samples.apps.nowinandroid.core.ui.UserNewsResourcePreviewParameterProvider import com.google.samples.apps.nowinandroid.core.ui.userNewsResourceCardItems -import com.google.samples.apps.nowinandroid.feature.topic.R.string +import nowinandroid.core.ui.generated.resources.core_ui_back +import nowinandroid.feature.topic.generated.resources.Res +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 @Composable internal fun TopicScreen( @@ -76,7 +78,7 @@ internal fun TopicScreen( onBackClick: () -> Unit, onTopicClick: (String) -> Unit, modifier: Modifier = Modifier, - viewModel: TopicViewModel = hiltViewModel(), + viewModel: TopicViewModel, ) { val topicUiState: TopicUiState by viewModel.topicUiState.collectAsStateWithLifecycle() val newsUiState: NewsUiState by viewModel.newsUiState.collectAsStateWithLifecycle() @@ -109,7 +111,7 @@ internal fun TopicScreen( modifier: Modifier = Modifier, ) { val state = rememberLazyListState() - TrackScrollJank(scrollableState = state, stateName = "topic:screen") +// TrackScrollJank(scrollableState = state, stateName = "topic:screen") Box( modifier = modifier, ) { @@ -124,7 +126,7 @@ internal fun TopicScreen( TopicUiState.Loading -> item { NiaLoadingWheel( modifier = modifier, - contentDesc = stringResource(id = string.feature_topic_loading), + contentDesc = stringResource(Res.string.feature_topic_loading), ) } @@ -214,6 +216,7 @@ private fun TopicHeader(name: String, description: String, imageUrl: String) { .align(Alignment.CenterHorizontally) .size(216.dp) .padding(bottom = 12.dp), + imageLoader = ImageLoader(LocalPlatformContext.current), ) Text(name, style = MaterialTheme.typography.displayMedium) if (description.isNotEmpty()) { @@ -292,7 +295,7 @@ private fun TopicToolbar( Icon( imageVector = NiaIcons.ArrowBack, contentDescription = stringResource( - id = com.google.samples.apps.nowinandroid.core.ui.R.string.core_ui_back, + nowinandroid.core.ui.generated.resources.Res.string.core_ui_back, ), ) } diff --git a/feature/topic/src/main/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 similarity index 98% rename from feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt rename to feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index ba8baad14..2a1f98431 100644 --- a/feature/topic/src/main/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 @@ -30,7 +30,6 @@ import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource import com.google.samples.apps.nowinandroid.core.result.Result import com.google.samples.apps.nowinandroid.core.result.asResult import com.google.samples.apps.nowinandroid.feature.topic.navigation.TopicRoute -import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow @@ -38,9 +37,8 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch -import javax.inject.Inject +import me.tatarka.inject.annotations.Inject -@HiltViewModel class TopicViewModel @Inject constructor( savedStateHandle: SavedStateHandle, private val userDataRepository: UserDataRepository, diff --git a/feature/topic/src/main/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 similarity index 82% rename from feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt rename to feature/topic/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt index fabb82b10..6527dd524 100644 --- a/feature/topic/src/main/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 @@ -16,11 +16,13 @@ package com.google.samples.apps.nowinandroid.feature.topic.navigation +import androidx.lifecycle.SavedStateHandle 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 com.google.samples.apps.nowinandroid.feature.topic.TopicViewModel import kotlinx.serialization.Serializable @Serializable data class TopicRoute(val id: String) @@ -37,10 +39,10 @@ fun NavGraphBuilder.topicScreen( onTopicClick: (String) -> Unit, ) { composable { - TopicScreen( - showBackButton = showBackButton, - onBackClick = onBackClick, - onTopicClick = onTopicClick, - ) +// TopicScreen( +// showBackButton = showBackButton, +// onBackClick = onBackClick, +// onTopicClick = onTopicClick, +// ) } } diff --git a/feature/topic/src/main/AndroidManifest.xml b/feature/topic/src/main/AndroidManifest.xml deleted file mode 100644 index 27a3ea8b0..000000000 --- a/feature/topic/src/main/AndroidManifest.xml +++ /dev/null @@ -1,17 +0,0 @@ - - -