From f1afca1cde5bcfa330a2ec438e92a7718caadf08 Mon Sep 17 00:00:00 2001 From: travismacdonald Date: Sun, 18 Dec 2022 22:30:49 -0400 Subject: [PATCH 1/4] tidy version catalog toml file --- gradle/libs.versions.toml | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3f5cdad60..9de14e83c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,17 +15,17 @@ androidxEspresso = "3.5.0" androidxHiltNavigationCompose = "1.0.0" androidxLifecycle = "2.6.0-alpha03" androidxMacroBenchmark = "1.1.1" -androidxNavigation = "2.5.3" androidxMetrics = "1.0.0-alpha03" +androidxNavigation = "2.5.3" androidxProfileinstaller = "1.2.0" androidxStartup = "1.1.1" -androidxWindowManager = "1.0.0" androidxTestCore = "1.5.0" androidxTestExt = "1.1.4" -androidxTestRunner = "1.5.1" androidxTestRules = "1.5.0" +androidxTestRunner = "1.5.1" androidxTracing = "1.1.0" androidxUiAutomator = "2.2.0" +androidxWindowManager = "1.0.0" androidxWork = "2.7.1" coil = "2.2.2" hilt = "2.44.2" @@ -61,7 +61,7 @@ androidx-compose-foundation = { group = "androidx.compose.foundation", name = "f androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" } androidx-compose-material-iconsExtended = { group = "androidx.compose.material", name = "material-icons-extended" } androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" } -androidx-compose-material3-windowSizeClass = {group = "androidx.compose.material3", name = "material3-window-size-class" } +androidx-compose-material3-windowSizeClass = { group = "androidx.compose.material3", name = "material3-window-size-class" } androidx-compose-runtime = { group = "androidx.compose.runtime", name = "runtime" } androidx-compose-runtime-livedata = { group = "androidx.compose.runtime", name = "runtime-livedata" } androidx-compose-runtime-tracing = { group = "androidx.compose.runtime", name = "runtime-tracing", version.ref = "androidxComposeRuntimeTracing" } @@ -83,24 +83,24 @@ androidx-navigation-compose = { group = "androidx.navigation", name = "navigatio androidx-navigation-testing = { group = "androidx.navigation", name = "navigation-testing", version.ref = "androidxNavigation" } androidx-profileinstaller = { group = "androidx.profileinstaller", name = "profileinstaller", version.ref = "androidxProfileinstaller" } androidx-startup = { group = "androidx.startup", name = "startup-runtime", version.ref = "androidxStartup" } -androidx-window-manager = {module = "androidx.window:window", version.ref = "androidxWindowManager"} androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTestCore" } -androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" } androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" } -androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" } +androidx-test-ext = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "androidxTestExt" } androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidxTestRules" } +androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" } androidx-test-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "androidxUiAutomator" } -androidx-tracing-ktx = {group = "androidx.tracing", name="tracing-ktx", version.ref = "androidxTracing" } +androidx-tracing-ktx = { group = "androidx.tracing", name="tracing-ktx", version.ref = "androidxTracing" } +androidx-window-manager = { module = "androidx.window:window", version.ref = "androidxWindowManager" } androidx-work-ktx = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "androidxWork" } androidx-work-testing = { group = "androidx.work", name = "work-testing", version.ref = "androidxWork" } -coil-kt = { group = "io.coil-kt", name = "coil", version.ref = "coil"} -coil-kt-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil"} -coil-kt-svg = { group = "io.coil-kt", name = "coil-svg", version.ref = "coil"} +coil-kt = { group = "io.coil-kt", name = "coil", version.ref = "coil" } +coil-kt-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil" } +coil-kt-svg = { group = "io.coil-kt", name = "coil-svg", version.ref = "coil" } hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" } -hilt-ext-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hiltExt" } -hilt-ext-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hiltExt" } -hilt-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" } hilt-android-testing = { group = "com.google.dagger", name = "hilt-android-testing", version.ref = "hilt" } +hilt-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" } +hilt-ext-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hiltExt" } +hilt-ext-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hiltExt" } junit4 = { group = "junit", name = "junit", version.ref = "junit4" } kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk8", version.ref = "kotlin" } kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinxCoroutines" } @@ -109,14 +109,14 @@ kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } lint-api = { group = "com.android.tools.lint", name = "lint-api", version.ref = "lint" } okhttp-logging = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" } -protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" } protobuf-kotlin-lite = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" } -turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } +protobuf-protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" } retrofit-core = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" } retrofit-kotlin-serialization = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "retrofitKotlinxSerializationJson" } -room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" } -room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" } room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" } +room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" } +room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" } +turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } # Dependencies of the included build-logic android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" } @@ -126,9 +126,9 @@ kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-pl android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" } android-test = { id = "com.android.test", version.ref = "androidGradlePlugin" } -ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" } secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" } From 6941e2082be9ee4712b8ed612ad80fdc25c1c06b Mon Sep 17 00:00:00 2001 From: magicalmeghan Date: Mon, 19 Dec 2022 09:22:47 -0800 Subject: [PATCH 2/4] Renamed SaveableNewsResource to UserNewsResource --- .../core/domain/GetSaveableNewsResourcesUseCase.kt | 8 ++++---- .../{SaveableNewsResource.kt => UserNewsResource.kt} | 2 +- ...aseTest.kt => GetUserNewsResourcesUseCaseTest.kt} | 12 ++++++------ .../samples/apps/nowinandroid/core/ui/NewsFeed.kt | 6 +++--- .../feature/bookmarks/BookmarksScreenTest.kt | 6 +++--- .../feature/bookmarks/BookmarksScreen.kt | 4 ++-- .../feature/bookmarks/BookmarksViewModel.kt | 6 +++--- .../nowinandroid/feature/foryou/ForYouScreenTest.kt | 4 ++-- .../apps/nowinandroid/feature/foryou/ForYouScreen.kt | 10 +++++----- .../nowinandroid/feature/foryou/ForYouViewModel.kt | 6 +++--- .../feature/foryou/ForYouViewModelTest.kt | 12 ++++++------ .../nowinandroid/feature/topic/TopicScreenTest.kt | 6 +++--- .../apps/nowinandroid/feature/topic/TopicScreen.kt | 4 ++-- .../nowinandroid/feature/topic/TopicViewModel.kt | 6 +++--- 14 files changed, 46 insertions(+), 46 deletions(-) rename core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/{SaveableNewsResource.kt => UserNewsResource.kt} (96%) rename core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/{GetSaveableNewsResourcesUseCaseTest.kt => GetUserNewsResourcesUseCaseTest.kt} (92%) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt index 4bd2fc6c3..df63a4487 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt @@ -18,7 +18,7 @@ package com.google.samples.apps.nowinandroid.core.domain import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import javax.inject.Inject import kotlinx.coroutines.flow.Flow @@ -47,7 +47,7 @@ class GetSaveableNewsResourcesUseCase @Inject constructor( */ operator fun invoke( filterTopicIds: Set = emptySet() - ): Flow> = + ): Flow> = if (filterTopicIds.isEmpty()) { newsRepository.getNewsResources() } else { @@ -57,11 +57,11 @@ class GetSaveableNewsResourcesUseCase @Inject constructor( private fun Flow>.mapToSaveableNewsResources( savedNewsResourceIds: Flow> -): Flow> = +): Flow> = filterNot { it.isEmpty() } .combine(savedNewsResourceIds) { newsResources, savedNewsResourceIds -> newsResources.map { newsResource -> - SaveableNewsResource( + UserNewsResource( newsResource = newsResource, isSaved = savedNewsResourceIds.contains(newsResource.id) ) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/SaveableNewsResource.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt similarity index 96% rename from core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/SaveableNewsResource.kt rename to core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt index 6850d421f..ddd8a27f6 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/SaveableNewsResource.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt @@ -21,7 +21,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResource /** * A [NewsResource] with the additional information for whether it is saved. */ -data class SaveableNewsResource( +data class UserNewsResource( val newsResource: NewsResource, val isSaved: Boolean, ) diff --git a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCaseTest.kt b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt similarity index 92% rename from core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCaseTest.kt rename to core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt index 876811e61..6bdbd9f34 100644 --- a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCaseTest.kt +++ b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt @@ -16,7 +16,7 @@ package com.google.samples.apps.nowinandroid.core.domain -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.Topic @@ -30,7 +30,7 @@ import kotlinx.datetime.Instant import org.junit.Rule import org.junit.Test -class GetSaveableNewsResourcesUseCaseTest { +class GetUserNewsResourcesUseCaseTest { @get:Rule val mainDispatcherRule = MainDispatcherRule() @@ -55,9 +55,9 @@ class GetSaveableNewsResourcesUseCaseTest { // Check that the correct news resources are returned with their bookmarked state. assertEquals( listOf( - SaveableNewsResource(sampleNewsResources[0], true), - SaveableNewsResource(sampleNewsResources[1], false), - SaveableNewsResource(sampleNewsResources[2], true) + UserNewsResource(sampleNewsResources[0], true), + UserNewsResource(sampleNewsResources[1], false), + UserNewsResource(sampleNewsResources[2], true) ), saveableNewsResources.first() ) @@ -77,7 +77,7 @@ class GetSaveableNewsResourcesUseCaseTest { assertEquals( sampleNewsResources .filter { it.topics.contains(sampleTopic1) } - .map { SaveableNewsResource(it, false) }, + .map { UserNewsResource(it, false) }, saveableNewsResources.first() ) } diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index 55d84af2f..cf2714db7 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -37,7 +37,7 @@ import androidx.compose.ui.tooling.preview.Devices import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources /** @@ -100,7 +100,7 @@ sealed interface NewsFeedUiState { /** * The list of news resources contained in this feed. */ - val feed: List + val feed: List ) : NewsFeedUiState } @@ -126,7 +126,7 @@ fun NewsFeedContentPreview() { newsFeed( feedState = NewsFeedUiState.Success( previewNewsResources.map { - SaveableNewsResource( + UserNewsResource( it, false ) diff --git a/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt b/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt index 1dafc5c98..3421f885a 100644 --- a/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt +++ b/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt @@ -31,7 +31,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollToNode -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import kotlin.test.assertEquals @@ -71,7 +71,7 @@ class BookmarksScreenTest { BookmarksScreen( feedState = NewsFeedUiState.Success( previewNewsResources.take(2) - .map { SaveableNewsResource(it, true) } + .map { UserNewsResource(it, true) } ), removeFromBookmarks = { } ) @@ -110,7 +110,7 @@ class BookmarksScreenTest { BookmarksScreen( feedState = NewsFeedUiState.Success( previewNewsResources.take(2) - .map { SaveableNewsResource(it, true) } + .map { UserNewsResource(it, true) } ), removeFromBookmarks = { newsResourceId -> assertEquals(previewNewsResources[0].id, newsResourceId) diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index 06a87bc9e..0bab4e449 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -52,7 +52,7 @@ import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading @@ -185,7 +185,7 @@ private fun BookmarksGridPreview() { BookmarksGrid( feedState = Success( previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), removeFromBookmarks = {} diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt index 8223ecd1b..8ea565bfd 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt @@ -20,7 +20,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading import dagger.hilt.android.lifecycle.HiltViewModel @@ -41,8 +41,8 @@ class BookmarksViewModel @Inject constructor( val feedUiState: StateFlow = getSaveableNewsResources() .filterNot { it.isEmpty() } - .map { newsResources -> newsResources.filter(SaveableNewsResource::isSaved) } // Only show bookmarked news resources. - .map, NewsFeedUiState>(NewsFeedUiState::Success) + .map { newsResources -> newsResources.filter(UserNewsResource::isSaved) } // Only show bookmarked news resources. + .map, NewsFeedUiState>(NewsFeedUiState::Success) .onStart { emit(Loading) } .stateIn( scope = viewModelScope, diff --git a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt index 702a963c1..ee6e2795b 100644 --- a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt +++ b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt @@ -29,7 +29,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollToNode import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState @@ -226,7 +226,7 @@ class ForYouScreenTest { onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( feed = previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), onTopicCheckedChanged = { _, _ -> }, diff --git a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt index 1427aecff..e1a5b5e9e 100644 --- a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt +++ b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt @@ -83,7 +83,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaOverl import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews @@ -397,7 +397,7 @@ fun ForYouScreenPopulatedFeed() { onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( feed = previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), onTopicCheckedChanged = { _, _ -> }, @@ -418,7 +418,7 @@ fun ForYouScreenOfflinePopulatedFeed() { onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( feed = previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), onTopicCheckedChanged = { _, _ -> }, @@ -441,7 +441,7 @@ fun ForYouScreenTopicSelection() { ), feedState = NewsFeedUiState.Success( feed = previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), onTopicCheckedChanged = { _, _ -> }, @@ -479,7 +479,7 @@ fun ForYouScreenPopulatedAndLoading() { onboardingUiState = OnboardingUiState.Loading, feedState = NewsFeedUiState.Success( feed = previewNewsResources.map { - SaveableNewsResource(it, false) + UserNewsResource(it, false) } ), onTopicCheckedChanged = { _, _ -> }, 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 800cba0d3..645566056 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,7 +22,7 @@ import com.google.samples.apps.nowinandroid.core.data.repository.UserDataReposit import com.google.samples.apps.nowinandroid.core.data.util.SyncStatusMonitor import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject @@ -117,6 +117,6 @@ class ForYouViewModel @Inject constructor( } } -private fun Flow>.mapToFeedState(): Flow = - map, NewsFeedUiState>(NewsFeedUiState::Success) +private fun Flow>.mapToFeedState(): Flow = + map, NewsFeedUiState>(NewsFeedUiState::Success) .onStart { emit(NewsFeedUiState.Loading) } diff --git a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt index ad29aba60..06b7ebe7a 100644 --- a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt +++ b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt @@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.feature.foryou import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.Topic @@ -282,7 +282,7 @@ class ForYouViewModelTest { NewsFeedUiState.Success( feed = sampleNewsResources.map { - SaveableNewsResource( + UserNewsResource( newsResource = it, isSaved = false ) @@ -397,11 +397,11 @@ class ForYouViewModelTest { assertEquals( NewsFeedUiState.Success( feed = listOf( - SaveableNewsResource( + UserNewsResource( newsResource = sampleNewsResources[1], isSaved = false ), - SaveableNewsResource( + UserNewsResource( newsResource = sampleNewsResources[2], isSaved = false ) @@ -497,11 +497,11 @@ class ForYouViewModelTest { assertEquals( NewsFeedUiState.Success( feed = listOf( - SaveableNewsResource( + UserNewsResource( newsResource = sampleNewsResources[1], isSaved = true ), - SaveableNewsResource( + UserNewsResource( newsResource = sampleNewsResources[2], isSaved = false ) diff --git a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt index 9bef39a36..0f6d2dcc0 100644 --- a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt +++ b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt @@ -25,7 +25,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollToNode import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.Topic @@ -101,7 +101,7 @@ class TopicScreenTest { topicUiState = TopicUiState.Loading, newsUiState = NewsUiState.Success( sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( + UserNewsResource( newsResource = newsResource, isSaved = index % 2 == 0, ) @@ -127,7 +127,7 @@ class TopicScreenTest { topicUiState = TopicUiState.Success(testTopic), newsUiState = NewsUiState.Success( sampleNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( + UserNewsResource( newsResource = newsResource, isSaved = index % 2 == 0, ) diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt index 61bd13aa0..b57c96954 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt @@ -53,7 +53,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadi import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews @@ -258,7 +258,7 @@ fun TopicScreenPopulated() { topicUiState = TopicUiState.Success(FollowableTopic(previewTopics[0], false)), newsUiState = NewsUiState.Success( previewNewsResources.mapIndexed { index, newsResource -> - SaveableNewsResource( + UserNewsResource( newsResource = newsResource, isSaved = index % 2 == 0, ) diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index 17096ca88..e65bc1dee 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -24,7 +24,7 @@ import com.google.samples.apps.nowinandroid.core.data.repository.UserDataReposit import com.google.samples.apps.nowinandroid.core.decoder.StringDecoder import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.result.Result import com.google.samples.apps.nowinandroid.core.result.asResult @@ -135,7 +135,7 @@ private fun newsUiState( userDataRepository: UserDataRepository, ): Flow { // Observe news - val news: Flow> = getSaveableNewsResources( + val news: Flow> = getSaveableNewsResources( filterTopicIds = setOf(element = topicId), ) @@ -172,7 +172,7 @@ sealed interface TopicUiState { } sealed interface NewsUiState { - data class Success(val news: List) : NewsUiState + data class Success(val news: List) : NewsUiState object Error : NewsUiState object Loading : NewsUiState } From ec4bceead3668b90cc7f896cb9f6130ae44cb01f Mon Sep 17 00:00:00 2001 From: magicalmeghan Date: Mon, 19 Dec 2022 11:39:05 -0800 Subject: [PATCH 3/4] Renamed SaveableNewsResource to UserNewsResource --- ...Case.kt => GetUserNewsResourcesUseCase.kt} | 36 ++++---- .../core/domain/model/FollowableTopic.kt | 18 +++- .../core/domain/model/UserNewsResource.kt | 84 ++++++++++++++++++- .../domain/GetUserNewsResourcesUseCaseTest.kt | 67 +++++++++++++-- .../core/ui/NewsResourceCardTest.kt | 4 +- .../apps/nowinandroid/core/ui/NewsFeed.kt | 21 ++--- .../nowinandroid/core/ui/NewsResourceCard.kt | 30 +++---- .../core/ui/NewsResourceCardList.kt | 5 +- .../feature/bookmarks/BookmarksScreen.kt | 5 +- .../feature/bookmarks/BookmarksViewModel.kt | 4 +- .../bookmarks/BookmarksViewModelTest.kt | 6 +- .../feature/foryou/ForYouScreen.kt | 17 ++-- .../feature/foryou/ForYouViewModel.kt | 4 +- .../feature/foryou/ForYouViewModelTest.kt | 78 +++++++++++++++-- .../nowinandroid/feature/topic/TopicScreen.kt | 12 +-- .../feature/topic/TopicViewModel.kt | 6 +- .../feature/topic/TopicViewModelTest.kt | 6 +- 17 files changed, 304 insertions(+), 99 deletions(-) rename core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/{GetSaveableNewsResourcesUseCase.kt => GetUserNewsResourcesUseCase.kt} (62%) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCase.kt similarity index 62% rename from core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt rename to core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCase.kt index df63a4487..19d65581f 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetSaveableNewsResourcesUseCase.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCase.kt @@ -18,29 +18,25 @@ package com.google.samples.apps.nowinandroid.core.domain import com.google.samples.apps.nowinandroid.core.data.repository.NewsRepository import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository +import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource +import com.google.samples.apps.nowinandroid.core.model.data.UserData import javax.inject.Inject import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filterNot -import kotlinx.coroutines.flow.map /** * A use case responsible for obtaining news resources with their associated bookmarked (also known * as "saved") state. */ -class GetSaveableNewsResourcesUseCase @Inject constructor( +class GetUserNewsResourcesUseCase @Inject constructor( private val newsRepository: NewsRepository, - userDataRepository: UserDataRepository + private val userDataRepository: UserDataRepository ) { - - private val bookmarkedNewsResources = userDataRepository.userData.map { - it.bookmarkedNewsResources - } - /** - * Returns a list of SaveableNewsResources which match the supplied set of topic ids. + * Returns a list of UserNewsResources which match the supplied set of topic ids. * * @param filterTopicIds - A set of topic ids used to filter the list of news resources. If * this is empty the list of news resources will not be filtered. @@ -52,18 +48,30 @@ class GetSaveableNewsResourcesUseCase @Inject constructor( newsRepository.getNewsResources() } else { newsRepository.getNewsResources(filterTopicIds = filterTopicIds) - }.mapToSaveableNewsResources(bookmarkedNewsResources) + }.mapToSaveableNewsResources(userDataRepository.userData) } private fun Flow>.mapToSaveableNewsResources( - savedNewsResourceIds: Flow> + userData: Flow ): Flow> = filterNot { it.isEmpty() } - .combine(savedNewsResourceIds) { newsResources, savedNewsResourceIds -> + .combine(userData) { newsResources, userData -> newsResources.map { newsResource -> UserNewsResource( - newsResource = newsResource, - isSaved = savedNewsResourceIds.contains(newsResource.id) + id = newsResource.id, + title = newsResource.title, + content = newsResource.content, + url = newsResource.url, + headerImageUrl = newsResource.headerImageUrl, + publishDate = newsResource.publishDate, + type = newsResource.type, + topics = newsResource.topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = userData.followedTopics.contains(topic.id) + ) + }, + isSaved = userData.bookmarkedNewsResources.contains(newsResource.id) ) } } diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt index 87a77daa4..c081abf1f 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt @@ -17,11 +17,27 @@ package com.google.samples.apps.nowinandroid.core.domain.model import com.google.samples.apps.nowinandroid.core.model.data.Topic +import com.google.samples.apps.nowinandroid.core.model.data.previewTopics /** * A [topic] with the additional information for whether or not it is followed. */ -data class FollowableTopic( +data class FollowableTopic( //TODO consider changing to UserTopic and flattening val topic: Topic, val isFollowed: Boolean ) + +val previewFollowableTopics = listOf( + FollowableTopic( + previewTopics[0], + isFollowed = false + ), + FollowableTopic( + previewTopics[1], + isFollowed = true + ), + FollowableTopic( + previewTopics[2], + isFollowed = false + ) +) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt index ddd8a27f6..c70f549b2 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt @@ -17,11 +17,89 @@ package com.google.samples.apps.nowinandroid.core.domain.model import com.google.samples.apps.nowinandroid.core.model.data.NewsResource +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown +import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video +import com.google.samples.apps.nowinandroid.core.model.data.previewTopics +import kotlinx.datetime.Instant +import kotlinx.datetime.LocalDateTime +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toInstant /** - * A [NewsResource] with the additional information for whether it is saved. + * A [NewsResource] with the additional user information. */ data class UserNewsResource( - val newsResource: NewsResource, - val isSaved: Boolean, + val id: String, + val title: String, + val content: String, + val url: String, + val headerImageUrl: String?, + val publishDate: Instant, + val type: NewsResourceType, + val topics: List, + val isSaved: Boolean +) + +val previewUserNewsResources = listOf( + UserNewsResource( + id = "1", + title = "Android Basics with Compose", + content = "We released the first two units of Android Basics with Compose, our first free course that teaches Android Development with Jetpack Compose to anyone; you do not need any prior programming experience other than basic computer literacy to get started. You’ll learn the fundamentals of programming in Kotlin while building Android apps using Jetpack Compose, Android’s modern toolkit that simplifies and accelerates native UI development. These two units are just the beginning; more will be coming soon. Check out Android Basics with Compose to get started on your Android development journey", + url = "https://android-developers.googleblog.com/2022/05/new-android-basics-with-compose-course.html", + headerImageUrl = "https://developer.android.com/images/hero-assets/android-basics-compose.svg", + publishDate = LocalDateTime( + year = 2022, + monthNumber = 5, + dayOfMonth = 4, + hour = 23, + minute = 0, + second = 0, + nanosecond = 0 + ).toInstant(TimeZone.UTC), + type = Codelab, + topics = listOf(previewFollowableTopics[1]), + isSaved = true + ), + UserNewsResource( + id = "2", + title = "Thanks for helping us reach 1M YouTube Subscribers", + content = "Thank you everyone for following the Now in Android series and everything the " + + "Android Developers YouTube channel has to offer. During the Android Developer " + + "Summit, our YouTube channel reached 1 million subscribers! Here’s a small video to " + + "thank you all.", + url = "https://youtu.be/-fJ6poHQrjM", + headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", + publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), + type = Video, + topics = listOf(previewFollowableTopics[0], previewFollowableTopics[1]), + isSaved = false + ), + UserNewsResource( + id = "3", + title = "Transformations and customisations in the Paging Library", + content = "A demonstration of different operations that can be performed " + + "with Paging. Transformations like inserting separators, when to " + + "create a new pager, and customisation options for consuming " + + "PagingData.", + url = "https://youtu.be/ZARz0pjm5YM", + headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", + publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), + type = Video, + topics = listOf(previewFollowableTopics[2]), + isSaved = false + ), + UserNewsResource( + id = "4", + title = "New Jetpack Release", + content = "New Jetpack release includes updates to libraries such as CameraX, Benchmark, and" + + "more!", + url = "https://developer.android.com/jetpack/androidx/versions/all-channel", + headerImageUrl = "", + publishDate = Instant.parse("2022-10-01T00:00:00.000Z"), + type = Unknown, + topics = listOf(previewFollowableTopics[2]), + isSaved = true + ) ) diff --git a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt index 6bdbd9f34..ce730d6f2 100644 --- a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt +++ b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt @@ -16,6 +16,7 @@ package com.google.samples.apps.nowinandroid.core.domain +import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video @@ -38,7 +39,7 @@ class GetUserNewsResourcesUseCaseTest { private val newsRepository = TestNewsRepository() private val userDataRepository = TestUserDataRepository() - val useCase = GetSaveableNewsResourcesUseCase(newsRepository, userDataRepository) + val useCase = GetUserNewsResourcesUseCase(newsRepository, userDataRepository) @Test fun whenNoFilters_allNewsResourcesAreReturned() = runTest { @@ -55,9 +56,51 @@ class GetUserNewsResourcesUseCaseTest { // Check that the correct news resources are returned with their bookmarked state. assertEquals( listOf( - UserNewsResource(sampleNewsResources[0], true), - UserNewsResource(sampleNewsResources[1], false), - UserNewsResource(sampleNewsResources[2], true) + UserNewsResource( + sampleNewsResources[0].id, + sampleNewsResources[0].title, + sampleNewsResources[0].content, + sampleNewsResources[0].url, + sampleNewsResources[0].headerImageUrl, + sampleNewsResources[0].publishDate, + sampleNewsResources[0].type, + sampleNewsResources[0].topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, + true), + UserNewsResource( + sampleNewsResources[1].id, + sampleNewsResources[1].title, + sampleNewsResources[1].content, + sampleNewsResources[1].url, + sampleNewsResources[1].headerImageUrl, + sampleNewsResources[1].publishDate, + sampleNewsResources[1].type, + sampleNewsResources[1].topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, + false), + UserNewsResource( + sampleNewsResources[2].id, + sampleNewsResources[2].title, + sampleNewsResources[2].content, + sampleNewsResources[2].url, + sampleNewsResources[2].headerImageUrl, + sampleNewsResources[2].publishDate, + sampleNewsResources[2].type, + sampleNewsResources[2].topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = true + ) + }, + true), ), saveableNewsResources.first() ) @@ -77,7 +120,21 @@ class GetUserNewsResourcesUseCaseTest { assertEquals( sampleNewsResources .filter { it.topics.contains(sampleTopic1) } - .map { UserNewsResource(it, false) }, + .map { UserNewsResource( + id = it.id, + title = it.title, + content = it.content, + url = it.url, + headerImageUrl = it.headerImageUrl, + publishDate = it.publishDate, + type = it.type, + topics = it.topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, + isSaved = false) }, saveableNewsResources.first() ) } diff --git a/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt index 69f919a62..c334f1bab 100644 --- a/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt +++ b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt @@ -35,7 +35,7 @@ class NewsResourceCardTest { composeTestRule.setContent { NewsResourceCardExpanded( - newsResource = newsWithKnownResourceType, + userNewsResource = newsWithKnownResourceType, isBookmarked = false, onToggleBookmark = {}, onClick = {} @@ -62,7 +62,7 @@ class NewsResourceCardTest { composeTestRule.setContent { NewsResourceCardExpanded( - newsResource = newsWithUnknownResourceType, + userNewsResource = newsWithUnknownResourceType, isBookmarked = false, onToggleBookmark = {}, onClick = {} diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index cf2714db7..a4ccdaca5 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -38,7 +38,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources /** * An extension on [LazyListScope] defining a feed with news resources. @@ -51,21 +51,21 @@ fun LazyGridScope.newsFeed( when (feedState) { NewsFeedUiState.Loading -> Unit is NewsFeedUiState.Success -> { - items(feedState.feed, key = { it.newsResource.id }) { saveableNewsResource -> + items(feedState.feed, key = { it.id }) { userNewsResource -> val resourceUrl by remember { - mutableStateOf(Uri.parse(saveableNewsResource.newsResource.url)) + mutableStateOf(Uri.parse(userNewsResource.url)) } val context = LocalContext.current val backgroundColor = MaterialTheme.colorScheme.background.toArgb() NewsResourceCardExpanded( - newsResource = saveableNewsResource.newsResource, - isBookmarked = saveableNewsResource.isSaved, + userNewsResource = userNewsResource, + isBookmarked = userNewsResource.isSaved, onClick = { launchCustomChromeTab(context, resourceUrl, backgroundColor) }, onToggleBookmark = { onNewsResourcesCheckedChanged( - saveableNewsResource.newsResource.id, - !saveableNewsResource.isSaved + userNewsResource.id, + !userNewsResource.isSaved ) } ) @@ -125,12 +125,7 @@ fun NewsFeedContentPreview() { LazyVerticalGrid(columns = GridCells.Adaptive(300.dp)) { newsFeed( feedState = NewsFeedUiState.Success( - previewNewsResources.map { - UserNewsResource( - it, - false - ) - } + previewUserNewsResources ), onNewsResourcesCheckedChanged = { _, _ -> } ) diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt index 21ee1a7a8..6eb6e7859 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt @@ -56,9 +56,11 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaIconT import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopicTag import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme +import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType -import com.google.samples.apps.nowinandroid.core.model.data.Topic import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import java.time.ZoneId import java.time.format.DateTimeFormatter @@ -73,7 +75,7 @@ import kotlinx.datetime.toJavaInstant @OptIn(ExperimentalMaterial3Api::class) @Composable fun NewsResourceCardExpanded( - newsResource: NewsResource, + userNewsResource: UserNewsResource, isBookmarked: Boolean, onToggleBookmark: () -> Unit, onClick: () -> Unit, @@ -91,9 +93,9 @@ fun NewsResourceCardExpanded( } ) { Column { - if (!newsResource.headerImageUrl.isNullOrEmpty()) { + if (!userNewsResource.headerImageUrl.isNullOrEmpty()) { Row { - NewsResourceHeaderImage(newsResource.headerImageUrl) + NewsResourceHeaderImage(userNewsResource.headerImageUrl) } } Box( @@ -103,18 +105,18 @@ fun NewsResourceCardExpanded( Spacer(modifier = Modifier.height(12.dp)) Row { NewsResourceTitle( - newsResource.title, + userNewsResource.title, modifier = Modifier.fillMaxWidth((.8f)) ) Spacer(modifier = Modifier.weight(1f)) BookmarkButton(isBookmarked, onToggleBookmark) } Spacer(modifier = Modifier.height(12.dp)) - NewsResourceMetaData(newsResource.publishDate, newsResource.type) + NewsResourceMetaData(userNewsResource.publishDate, userNewsResource.type) Spacer(modifier = Modifier.height(12.dp)) - NewsResourceShortDescription(newsResource.content) + NewsResourceShortDescription(userNewsResource.content) Spacer(modifier = Modifier.height(12.dp)) - NewsResourceTopics(newsResource.topics) + NewsResourceTopics(userNewsResource.topics) } } } @@ -227,7 +229,7 @@ fun NewsResourceShortDescription( @Composable fun NewsResourceTopics( - topics: List, + topics: List, modifier: Modifier = Modifier ) { // Store the ID of the Topic which has its "following" menu expanded, if any. @@ -238,17 +240,17 @@ fun NewsResourceTopics( modifier = modifier.horizontalScroll(rememberScrollState()), // causes narrow chips horizontalArrangement = Arrangement.spacedBy(4.dp), ) { - for (topic in topics) { + for (followableTopic in topics) { NiaTopicTag( - expanded = expandedTopicId == topic.id, + expanded = expandedTopicId == followableTopic.topic.id, followed = true, // ToDo: Check if topic is followed onDropdownMenuToggle = { show -> - expandedTopicId = if (show) topic.id else null + expandedTopicId = if (show) followableTopic.topic.id else null }, onFollowClick = { }, // ToDo onUnfollowClick = { }, // ToDo onBrowseClick = { }, // ToDo - text = { Text(text = topic.name.uppercase(Locale.getDefault())) } + text = { Text(text = followableTopic.topic.name.uppercase(Locale.getDefault())) } ) } } @@ -280,7 +282,7 @@ fun ExpandedNewsResourcePreview() { NiaTheme { Surface { NewsResourceCardExpanded( - newsResource = previewNewsResources[0], + userNewsResource = previewUserNewsResources[0], isBookmarked = true, onToggleBookmark = {}, onClick = {} diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt index c76a8124d..c2bdb6580 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt @@ -23,6 +23,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext +import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource /** @@ -37,7 +38,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResource */ fun LazyListScope.newsResourceCardItems( items: List, - newsResourceMapper: (item: T) -> NewsResource, + newsResourceMapper: (item: T) -> UserNewsResource, //TODO remove this? isBookmarkedMapper: (item: T) -> Boolean, onToggleBookmark: (item: T) -> Unit, onItemClick: ((item: T) -> Unit)? = null, @@ -52,7 +53,7 @@ fun LazyListScope.newsResourceCardItems( val context = LocalContext.current NewsResourceCardExpanded( - newsResource = newsResource, + userNewsResource = newsResource, isBookmarked = isBookmarkedMapper(item), onToggleBookmark = { onToggleBookmark(item) }, onClick = { diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index 0bab4e449..9dbad373b 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -53,6 +53,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading @@ -184,9 +185,7 @@ private fun BookmarksGridPreview() { NiaTheme { BookmarksGrid( feedState = Success( - previewNewsResources.map { - UserNewsResource(it, false) - } + previewUserNewsResources ), removeFromBookmarks = {} ) diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt index 8ea565bfd..db94839be 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt @@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.feature.bookmarks import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading @@ -36,7 +36,7 @@ import kotlinx.coroutines.launch @HiltViewModel class BookmarksViewModel @Inject constructor( private val userDataRepository: UserDataRepository, - getSaveableNewsResources: GetSaveableNewsResourcesUseCase + getSaveableNewsResources: GetUserNewsResourcesUseCase ) : ViewModel() { val feedUiState: StateFlow = getSaveableNewsResources() diff --git a/feature/bookmarks/src/test/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 56f30eff5..a8da1a4ff 100644 --- a/feature/bookmarks/src/test/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -16,7 +16,7 @@ package com.google.samples.apps.nowinandroid.feature.bookmarks -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepository import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository @@ -43,7 +43,7 @@ class BookmarksViewModelTest { private val userDataRepository = TestUserDataRepository() private val newsRepository = TestNewsRepository() - private val getSaveableNewsResourcesUseCase = GetSaveableNewsResourcesUseCase( + private val getUserNewsResourcesUseCase = GetUserNewsResourcesUseCase( newsRepository = newsRepository, userDataRepository = userDataRepository ) @@ -53,7 +53,7 @@ class BookmarksViewModelTest { fun setup() { viewModel = BookmarksViewModel( userDataRepository = userDataRepository, - getSaveableNewsResources = getSaveableNewsResourcesUseCase + getSaveableNewsResources = getUserNewsResourcesUseCase ) } diff --git a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt index e1a5b5e9e..fb4d83c7e 100644 --- a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt +++ b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt @@ -84,6 +84,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews @@ -396,9 +397,7 @@ fun ForYouScreenPopulatedFeed() { isSyncing = false, onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( - feed = previewNewsResources.map { - UserNewsResource(it, false) - } + feed = previewUserNewsResources ), onTopicCheckedChanged = { _, _ -> }, saveFollowedTopics = {}, @@ -417,9 +416,7 @@ fun ForYouScreenOfflinePopulatedFeed() { isSyncing = false, onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( - feed = previewNewsResources.map { - UserNewsResource(it, false) - } + feed = previewUserNewsResources ), onTopicCheckedChanged = { _, _ -> }, saveFollowedTopics = {}, @@ -440,9 +437,7 @@ fun ForYouScreenTopicSelection() { topics = previewTopics.map { FollowableTopic(it, false) }, ), feedState = NewsFeedUiState.Success( - feed = previewNewsResources.map { - UserNewsResource(it, false) - } + feed = previewUserNewsResources ), onTopicCheckedChanged = { _, _ -> }, saveFollowedTopics = {}, @@ -478,9 +473,7 @@ fun ForYouScreenPopulatedAndLoading() { isSyncing = true, onboardingUiState = OnboardingUiState.Loading, feedState = NewsFeedUiState.Success( - feed = previewNewsResources.map { - UserNewsResource(it, false) - } + feed = previewUserNewsResources ), onTopicCheckedChanged = { _, _ -> }, saveFollowedTopics = {}, 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 645566056..6c9218a75 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 @@ -21,7 +21,7 @@ import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.data.util.SyncStatusMonitor import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import dagger.hilt.android.lifecycle.HiltViewModel @@ -41,7 +41,7 @@ import kotlinx.coroutines.launch class ForYouViewModel @Inject constructor( syncStatusMonitor: SyncStatusMonitor, private val userDataRepository: UserDataRepository, - private val getSaveableNewsResources: GetSaveableNewsResourcesUseCase, + private val getSaveableNewsResources: GetUserNewsResourcesUseCase, getFollowableTopics: GetFollowableTopicsUseCase ) : ViewModel() { diff --git a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt index 06b7ebe7a..35b8a2f5a 100644 --- a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt +++ b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt @@ -17,7 +17,7 @@ package com.google.samples.apps.nowinandroid.feature.foryou import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResource @@ -54,7 +54,7 @@ class ForYouViewModelTest { private val userDataRepository = TestUserDataRepository() private val topicsRepository = TestTopicsRepository() private val newsRepository = TestNewsRepository() - private val getSaveableNewsResourcesUseCase = GetSaveableNewsResourcesUseCase( + private val getUserNewsResourcesUseCase = GetUserNewsResourcesUseCase( newsRepository = newsRepository, userDataRepository = userDataRepository ) @@ -69,7 +69,7 @@ class ForYouViewModelTest { viewModel = ForYouViewModel( syncStatusMonitor = syncStatusMonitor, userDataRepository = userDataRepository, - getSaveableNewsResources = getSaveableNewsResourcesUseCase, + getSaveableNewsResources = getUserNewsResourcesUseCase, getFollowableTopics = getFollowableTopicsUseCase ) } @@ -283,7 +283,19 @@ class ForYouViewModelTest { feed = sampleNewsResources.map { UserNewsResource( - newsResource = it, + id = it.id, + title = it.title, + content = it.content, + url = it.url, + headerImageUrl = it.headerImageUrl, + publishDate = it.publishDate, + type = it.type, + topics = it.topics.map{ topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, isSaved = false ) } @@ -398,11 +410,35 @@ class ForYouViewModelTest { NewsFeedUiState.Success( feed = listOf( UserNewsResource( - newsResource = sampleNewsResources[1], + id = sampleNewsResources[1].id, + title = sampleNewsResources[1].title, + content = sampleNewsResources[1].content, + url = sampleNewsResources[1].url, + headerImageUrl = sampleNewsResources[1].headerImageUrl, + publishDate = sampleNewsResources[1].publishDate, + type = sampleNewsResources[1].type, + topics = sampleNewsResources[1].topics.map{ topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, isSaved = false ), UserNewsResource( - newsResource = sampleNewsResources[2], + id = sampleNewsResources[2].id, + title = sampleNewsResources[2].title, + content = sampleNewsResources[2].content, + url = sampleNewsResources[2].url, + headerImageUrl = sampleNewsResources[2].headerImageUrl, + publishDate = sampleNewsResources[2].publishDate, + type = sampleNewsResources[2].type, + topics = sampleNewsResources[2].topics.map{ topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, isSaved = false ) ) @@ -498,13 +534,37 @@ class ForYouViewModelTest { NewsFeedUiState.Success( feed = listOf( UserNewsResource( - newsResource = sampleNewsResources[1], + id = sampleNewsResources[1].id, + title = sampleNewsResources[1].title, + content = sampleNewsResources[1].content, + url = sampleNewsResources[1].url, + headerImageUrl = sampleNewsResources[1].headerImageUrl, + publishDate = sampleNewsResources[1].publishDate, + type = sampleNewsResources[1].type, + topics = sampleNewsResources[1].topics.map{ topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, isSaved = true ), UserNewsResource( - newsResource = sampleNewsResources[2], + id = sampleNewsResources[2].id, + title = sampleNewsResources[2].title, + content = sampleNewsResources[2].content, + url = sampleNewsResources[2].url, + headerImageUrl = sampleNewsResources[2].headerImageUrl, + publishDate = sampleNewsResources[2].publishDate, + type = sampleNewsResources[2].type, + topics = sampleNewsResources[2].topics.map{ topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, isSaved = false - ) + ), ) ), viewModel.feedState.value diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt index b57c96954..3e79318eb 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt @@ -54,6 +54,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews @@ -182,9 +183,9 @@ private fun LazyListScope.TopicCards( is NewsUiState.Success -> { newsResourceCardItems( items = news.news, - newsResourceMapper = { it.newsResource }, + newsResourceMapper = { it }, isBookmarkedMapper = { it.isSaved }, - onToggleBookmark = { onBookmarkChanged(it.newsResource.id, !it.isSaved) }, + onToggleBookmark = { onBookmarkChanged(it.id, !it.isSaved) }, itemModifier = Modifier.padding(24.dp) ) } @@ -257,12 +258,7 @@ fun TopicScreenPopulated() { TopicScreen( topicUiState = TopicUiState.Success(FollowableTopic(previewTopics[0], false)), newsUiState = NewsUiState.Success( - previewNewsResources.mapIndexed { index, newsResource -> - UserNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } + previewUserNewsResources ), onBackClick = {}, onFollowClick = {}, diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index e65bc1dee..1f08d233a 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -22,7 +22,7 @@ import androidx.lifecycle.viewModelScope import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.decoder.StringDecoder -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.model.data.Topic @@ -46,7 +46,7 @@ class TopicViewModel @Inject constructor( private val userDataRepository: UserDataRepository, topicsRepository: TopicsRepository, // newsRepository: NewsRepository, - getSaveableNewsResources: GetSaveableNewsResourcesUseCase + getSaveableNewsResources: GetUserNewsResourcesUseCase ) : ViewModel() { private val topicArgs: TopicArgs = TopicArgs(savedStateHandle, stringDecoder) @@ -131,7 +131,7 @@ private fun topicUiState( private fun newsUiState( topicId: String, - getSaveableNewsResources: GetSaveableNewsResourcesUseCase, + getSaveableNewsResources: GetUserNewsResourcesUseCase, userDataRepository: UserDataRepository, ): Flow { // Observe news diff --git a/feature/topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt b/feature/topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt index 32768c4d7..6c61e59d5 100644 --- a/feature/topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt +++ b/feature/topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt @@ -17,7 +17,7 @@ package com.google.samples.apps.nowinandroid.feature.topic import androidx.lifecycle.SavedStateHandle -import com.google.samples.apps.nowinandroid.core.domain.GetSaveableNewsResourcesUseCase +import com.google.samples.apps.nowinandroid.core.domain.GetUserNewsResourcesUseCase import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video @@ -53,7 +53,7 @@ class TopicViewModelTest { private val userDataRepository = TestUserDataRepository() private val topicsRepository = TestTopicsRepository() private val newsRepository = TestNewsRepository() - private val getSaveableNewsResourcesUseCase = GetSaveableNewsResourcesUseCase( + private val getUserNewsResourcesUseCase = GetUserNewsResourcesUseCase( newsRepository = newsRepository, userDataRepository = userDataRepository ) @@ -66,7 +66,7 @@ class TopicViewModelTest { stringDecoder = FakeStringDecoder(), userDataRepository = userDataRepository, topicsRepository = topicsRepository, - getSaveableNewsResources = getSaveableNewsResourcesUseCase + getSaveableNewsResources = getUserNewsResourcesUseCase ) } From 5638e253aea9436adad022538d055ce7e81e3e48 Mon Sep 17 00:00:00 2001 From: Don Turner Date: Mon, 19 Dec 2022 20:47:43 +0000 Subject: [PATCH 4/4] Fix unit and UI tests. Apply spotless. Change-Id: Iab75852a27a44309d349f84e65226911ab59fcd5 --- .../core/domain/model/FollowableTopic.kt | 2 +- .../core/domain/model/UserNewsResource.kt | 3 +- .../domain/GetUserNewsResourcesUseCaseTest.kt | 44 +++---- .../core/ui/NewsResourceCardTest.kt | 6 +- .../nowinandroid/core/ui/NewsResourceCard.kt | 1 - .../core/ui/NewsResourceCardList.kt | 2 +- .../feature/bookmarks/BookmarksScreenTest.kt | 19 ++-- .../feature/bookmarks/BookmarksScreen.kt | 2 - .../feature/foryou/ForYouScreenTest.kt | 13 +-- .../feature/foryou/ForYouScreen.kt | 2 - .../feature/foryou/ForYouViewModelTest.kt | 107 ++++-------------- .../feature/topic/TopicScreenTest.kt | 43 +++---- .../nowinandroid/feature/topic/TopicScreen.kt | 2 - 13 files changed, 86 insertions(+), 160 deletions(-) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt index c081abf1f..4071eb1aa 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/FollowableTopic.kt @@ -22,7 +22,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.previewTopics /** * A [topic] with the additional information for whether or not it is followed. */ -data class FollowableTopic( //TODO consider changing to UserTopic and flattening +data class FollowableTopic( // TODO consider changing to UserTopic and flattening val topic: Topic, val isFollowed: Boolean ) diff --git a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt index c70f549b2..b8cef84a7 100644 --- a/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt +++ b/core/domain/src/main/java/com/google/samples/apps/nowinandroid/core/domain/model/UserNewsResource.kt @@ -21,12 +21,13 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video -import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import kotlinx.datetime.Instant import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone import kotlinx.datetime.toInstant +/* ktlint-disable max-line-length */ + /** * A [NewsResource] with the additional user information. */ diff --git a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt index ce730d6f2..d711d851e 100644 --- a/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt +++ b/core/domain/src/test/java/com/google/samples/apps/nowinandroid/core/domain/GetUserNewsResourcesUseCaseTest.kt @@ -70,7 +70,8 @@ class GetUserNewsResourcesUseCaseTest { isFollowed = false ) }, - true), + true + ), UserNewsResource( sampleNewsResources[1].id, sampleNewsResources[1].title, @@ -85,7 +86,8 @@ class GetUserNewsResourcesUseCaseTest { isFollowed = false ) }, - false), + false + ), UserNewsResource( sampleNewsResources[2].id, sampleNewsResources[2].title, @@ -97,10 +99,11 @@ class GetUserNewsResourcesUseCaseTest { sampleNewsResources[2].topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = true + isFollowed = false ) }, - true), + true + ), ), saveableNewsResources.first() ) @@ -120,21 +123,24 @@ class GetUserNewsResourcesUseCaseTest { assertEquals( sampleNewsResources .filter { it.topics.contains(sampleTopic1) } - .map { UserNewsResource( - id = it.id, - title = it.title, - content = it.content, - url = it.url, - headerImageUrl = it.headerImageUrl, - publishDate = it.publishDate, - type = it.type, - topics = it.topics.map { topic -> - FollowableTopic( - topic = topic, - isFollowed = false - ) - }, - isSaved = false) }, + .map { + UserNewsResource( + id = it.id, + title = it.title, + content = it.content, + url = it.url, + headerImageUrl = it.headerImageUrl, + publishDate = it.publishDate, + type = it.type, + topics = it.topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = false + ) + }, + isSaved = false + ) + }, saveableNewsResources.first() ) } diff --git a/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt index c334f1bab..70ce0662f 100644 --- a/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt +++ b/core/ui/src/androidTest/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardTest.kt @@ -20,7 +20,7 @@ import androidx.activity.ComponentActivity import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithText -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import org.junit.Rule import org.junit.Test @@ -30,7 +30,7 @@ class NewsResourceCardTest { @Test fun testMetaDataDisplay_withCodelabResource() { - val newsWithKnownResourceType = previewNewsResources[0] + val newsWithKnownResourceType = previewUserNewsResources[0] var dateFormatted = "" composeTestRule.setContent { @@ -57,7 +57,7 @@ class NewsResourceCardTest { @Test fun testMetaDataDisplay_withUnknownResource() { - val newsWithUnknownResourceType = previewNewsResources[3] + val newsWithUnknownResourceType = previewUserNewsResources[3] var dateFormatted = "" composeTestRule.setContent { diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt index 6eb6e7859..d6d3ff55e 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt @@ -61,7 +61,6 @@ import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import java.time.ZoneId import java.time.format.DateTimeFormatter import java.util.Locale diff --git a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt index c2bdb6580..c7441c182 100644 --- a/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt +++ b/core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCardList.kt @@ -38,7 +38,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResource */ fun LazyListScope.newsResourceCardItems( items: List, - newsResourceMapper: (item: T) -> UserNewsResource, //TODO remove this? + newsResourceMapper: (item: T) -> UserNewsResource, // TODO remove this? isBookmarkedMapper: (item: T) -> Boolean, onToggleBookmark: (item: T) -> Unit, onItemClick: ((item: T) -> Unit)? = null, diff --git a/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt b/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt index 3421f885a..bd9ca2056 100644 --- a/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt +++ b/feature/bookmarks/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreenTest.kt @@ -31,8 +31,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performScrollToNode -import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -70,8 +69,7 @@ class BookmarksScreenTest { composeTestRule.setContent { BookmarksScreen( feedState = NewsFeedUiState.Success( - previewNewsResources.take(2) - .map { UserNewsResource(it, true) } + previewUserNewsResources.take(2) ), removeFromBookmarks = { } ) @@ -79,7 +77,7 @@ class BookmarksScreenTest { composeTestRule .onNodeWithText( - previewNewsResources[0].title, + previewUserNewsResources[0].title, substring = true ) .assertExists() @@ -88,14 +86,14 @@ class BookmarksScreenTest { composeTestRule.onNode(hasScrollToNodeAction()) .performScrollToNode( hasText( - previewNewsResources[1].title, + previewUserNewsResources[1].title, substring = true ) ) composeTestRule .onNodeWithText( - previewNewsResources[1].title, + previewUserNewsResources[1].title, substring = true ) .assertExists() @@ -109,11 +107,10 @@ class BookmarksScreenTest { composeTestRule.setContent { BookmarksScreen( feedState = NewsFeedUiState.Success( - previewNewsResources.take(2) - .map { UserNewsResource(it, true) } + previewUserNewsResources.take(2) ), removeFromBookmarks = { newsResourceId -> - assertEquals(previewNewsResources[0].id, newsResourceId) + assertEquals(previewUserNewsResources[0].id, newsResourceId) removeFromBookmarksCalled = true } ) @@ -127,7 +124,7 @@ class BookmarksScreenTest { ).filter( hasAnyAncestor( hasText( - previewNewsResources[0].title, + previewUserNewsResources[0].title, substring = true ) ) diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index 9dbad373b..789fbec31 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -52,9 +52,7 @@ import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme -import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Success diff --git a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt index ee6e2795b..70f60811b 100644 --- a/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt +++ b/feature/foryou/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreenTest.kt @@ -29,9 +29,8 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollToNode import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource +import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources import com.google.samples.apps.nowinandroid.core.model.data.Topic -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import org.junit.Rule import org.junit.Test @@ -225,9 +224,7 @@ class ForYouScreenTest { isSyncing = false, onboardingUiState = OnboardingUiState.NotShown, feedState = NewsFeedUiState.Success( - feed = previewNewsResources.map { - UserNewsResource(it, false) - } + feed = previewUserNewsResources ), onTopicCheckedChanged = { _, _ -> }, saveFollowedTopics = {}, @@ -237,7 +234,7 @@ class ForYouScreenTest { composeTestRule .onNodeWithText( - previewNewsResources[0].title, + previewUserNewsResources[0].title, substring = true ) .assertExists() @@ -246,14 +243,14 @@ class ForYouScreenTest { composeTestRule.onNode(hasScrollToNodeAction()) .performScrollToNode( hasText( - previewNewsResources[1].title, + previewUserNewsResources[1].title, substring = true ) ) composeTestRule .onNodeWithText( - previewNewsResources[1].title, + previewUserNewsResources[1].title, substring = true ) .assertExists() diff --git a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt index fb4d83c7e..0e62a9b0b 100644 --- a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt +++ b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt @@ -83,9 +83,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaOverl import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState diff --git a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt index 35b8a2f5a..5ab8d92db 100644 --- a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt +++ b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt @@ -263,7 +263,9 @@ class ForYouViewModelTest { val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() } topicsRepository.sendTopics(sampleTopics) - userDataRepository.setFollowedTopicIds(setOf("0", "1")) + + val followedTopicIds = setOf("0", "1") + userDataRepository.setFollowedTopicIds(followedTopicIds) viewModel.dismissOnboarding() assertEquals( @@ -290,10 +292,10 @@ class ForYouViewModelTest { headerImageUrl = it.headerImageUrl, publishDate = it.publishDate, type = it.type, - topics = it.topics.map{ topic -> + topics = it.topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = false + isFollowed = followedTopicIds.contains(topic.id) ) }, isSaved = false @@ -319,41 +321,9 @@ class ForYouViewModelTest { assertEquals( OnboardingUiState.Shown( - topics = listOf( - FollowableTopic( - topic = Topic( - id = "0", - name = "Headlines", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = false - ), - FollowableTopic( - topic = Topic( - id = "1", - name = "UI", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = false - ), - FollowableTopic( - topic = Topic( - id = "2", - name = "Tools", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = false - ) - ), + topics = sampleTopics.map { + FollowableTopic(it, false) + } ), viewModel.onboardingUiState.value ) @@ -364,45 +334,14 @@ class ForYouViewModelTest { viewModel.feedState.value ) - viewModel.updateTopicSelection("1", isChecked = true) + val followedTopicId = sampleTopics[1].id + viewModel.updateTopicSelection(followedTopicId, isChecked = true) assertEquals( OnboardingUiState.Shown( - topics = listOf( - FollowableTopic( - topic = Topic( - id = "0", - name = "Headlines", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = false - ), - FollowableTopic( - topic = Topic( - id = "1", - name = "UI", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = true - ), - FollowableTopic( - topic = Topic( - id = "2", - name = "Tools", - shortDescription = "", - longDescription = "long description", - url = "URL", - imageUrl = "image URL", - ), - isFollowed = false - ) - ), + topics = sampleTopics.map { + FollowableTopic(it, it.id == followedTopicId) + } ), viewModel.onboardingUiState.value ) @@ -417,10 +356,10 @@ class ForYouViewModelTest { headerImageUrl = sampleNewsResources[1].headerImageUrl, publishDate = sampleNewsResources[1].publishDate, type = sampleNewsResources[1].type, - topics = sampleNewsResources[1].topics.map{ topic -> + topics = sampleNewsResources[1].topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = false + isFollowed = topic.id == followedTopicId ) }, isSaved = false @@ -433,10 +372,10 @@ class ForYouViewModelTest { headerImageUrl = sampleNewsResources[2].headerImageUrl, publishDate = sampleNewsResources[2].publishDate, type = sampleNewsResources[2].type, - topics = sampleNewsResources[2].topics.map{ topic -> + topics = sampleNewsResources[2].topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = false + isFollowed = topic.id == followedTopicId ) }, isSaved = false @@ -520,8 +459,10 @@ class ForYouViewModelTest { launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() } + val followedTopicIds = setOf("1") + topicsRepository.sendTopics(sampleTopics) - userDataRepository.setFollowedTopicIds(setOf("1")) + userDataRepository.setFollowedTopicIds(followedTopicIds) userDataRepository.setShouldHideOnboarding(true) newsRepository.sendNewsResources(sampleNewsResources) viewModel.updateNewsResourceSaved("2", true) @@ -541,10 +482,10 @@ class ForYouViewModelTest { headerImageUrl = sampleNewsResources[1].headerImageUrl, publishDate = sampleNewsResources[1].publishDate, type = sampleNewsResources[1].type, - topics = sampleNewsResources[1].topics.map{ topic -> + topics = sampleNewsResources[1].topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = false + isFollowed = followedTopicIds.contains(topic.id) ) }, isSaved = true @@ -557,10 +498,10 @@ class ForYouViewModelTest { headerImageUrl = sampleNewsResources[2].headerImageUrl, publishDate = sampleNewsResources[2].publishDate, type = sampleNewsResources[2].type, - topics = sampleNewsResources[2].topics.map{ topic -> + topics = sampleNewsResources[2].topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = false + isFollowed = followedTopicIds.contains(topic.id) ) }, isSaved = false diff --git a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt index 0f6d2dcc0..c635b7a53 100644 --- a/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt +++ b/feature/topic/src/androidTest/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreenTest.kt @@ -26,7 +26,6 @@ import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollToNode import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource -import com.google.samples.apps.nowinandroid.core.model.data.NewsResource import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video import com.google.samples.apps.nowinandroid.core.model.data.Topic import kotlinx.datetime.Instant @@ -99,14 +98,7 @@ class TopicScreenTest { composeTestRule.setContent { TopicScreen( topicUiState = TopicUiState.Loading, - newsUiState = NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - UserNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } - ), + newsUiState = NewsUiState.Success(sampleUserNewsResources), onBackClick = { }, onFollowClick = { }, onBookmarkChanged = { _, _ -> }, @@ -126,12 +118,7 @@ class TopicScreenTest { TopicScreen( topicUiState = TopicUiState.Success(testTopic), newsUiState = NewsUiState.Success( - sampleNewsResources.mapIndexed { index, newsResource -> - UserNewsResource( - newsResource = newsResource, - isSaved = index % 2 == 0, - ) - } + sampleUserNewsResources ), onBackClick = { }, onFollowClick = { }, @@ -143,7 +130,7 @@ class TopicScreenTest { composeTestRule .onAllNodes(hasScrollToNodeAction()) .onFirst() - .performScrollToNode(hasText(sampleNewsResources.first().title)) + .performScrollToNode(hasText(sampleUserNewsResources.first().title)) } } @@ -188,8 +175,8 @@ private val testTopics = listOf( ) ) -private val sampleNewsResources = listOf( - NewsResource( +private val sampleUserNewsResources = listOf( + UserNewsResource( id = "1", title = "Thanks for helping us reach 1M YouTube Subscribers", content = "Thank you everyone for following the Now in Android series and everything the " + @@ -201,14 +188,18 @@ private val sampleNewsResources = listOf( publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), type = Video, topics = listOf( - Topic( - id = "0", - name = "Headlines", - shortDescription = "", - longDescription = TOPIC_DESC, - url = "", - imageUrl = "" + FollowableTopic( + topic = Topic( + id = "0", + name = "Headlines", + shortDescription = "", + longDescription = TOPIC_DESC, + url = "", + imageUrl = "" + ), + isFollowed = false ) - ) + ), + isSaved = true ) ) diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt index 3e79318eb..263c04d78 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicScreen.kt @@ -53,9 +53,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadi import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic -import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources -import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources import com.google.samples.apps.nowinandroid.core.model.data.previewTopics import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank