From caa482bc71faa2f20ddb1e1f0fdba92e9a1aef2a Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 16 Nov 2023 23:45:31 +0100 Subject: [PATCH 1/6] Kotlinify codebase - Remove unnecessary nullable types - Replace no-op method bodies with Unit - Convert to expression body - Replace if with when - Remove braces from 'when' entries - Remove braces from if statement - Convert to single line lambda - oneline if/returns - Replace 'contains' call with 'in' operator Following this refactor, it could be great to envision a more "strict" code formatter like ktlint 1.0 (we are currently stuck at 0.48.1) --- .../apps/nowinandroid/ui/NavigationTest.kt | 2 +- .../samples/apps/nowinandroid/MainActivity.kt | 4 +- .../apps/nowinandroid/di/JankStatsModule.kt | 23 +++--- .../samples/apps/nowinandroid/ui/NiaApp.kt | 4 +- .../apps/nowinandroid/ui/NiaAppState.kt | 4 +- .../test/uiautomator/UiAutomatorHelpers.kt | 14 ++-- .../baselineprofile/StartupBaselineProfile.kt | 13 ++-- .../apps/nowinandroid/PrintTestApks.kt | 6 +- .../apps/nowinandroid/core/result/Result.kt | 13 ++-- .../DefaultRecentSearchRepository.kt | 4 +- .../fake/FakeRecentSearchRepository.kt | 4 +- .../fake/FakeSearchContentsRepository.kt | 2 +- .../repository/fake/FakeTopicsRepository.kt | 5 +- ...CompositeUserNewsResourceRepositoryTest.kt | 4 +- .../core/data/UserNewsResourceTest.kt | 4 +- .../data/testdoubles/TestNewsResourceDao.kt | 6 +- .../testdoubles/TestNiaNetworkDataSource.kt | 11 ++- .../core/data/testdoubles/TestTopicDao.kt | 21 ++---- .../core/datastore/ListToMapMigration.kt | 5 +- .../datastore/NiaPreferencesDataSource.kt | 8 +-- .../core/designsystem/ThemeTest.kt | 70 +++++++------------ .../component/DynamicAsyncImage.kt | 3 +- .../core/designsystem/component/TopAppBar.kt | 4 +- .../component/scrollbar/ScrollbarExt.kt | 9 +-- .../core/designsystem/theme/Tint.kt | 2 +- .../core/domain/GetFollowableTopicsUseCase.kt | 28 ++++---- .../core/model/data/UserNewsResource.kt | 11 ++- .../core/notifications/SystemTrayNotifier.kt | 40 +++++------ .../core/testing/NiaTestRunner.kt | 5 +- .../testing/repository/TestNewsRepository.kt | 4 +- .../repository/TestRecentSearchRepository.kt | 4 +- .../TestSearchContentsRepository.kt | 9 +-- .../repository/TestTopicsRepository.kt | 5 +- .../core/testing/util/MainDispatcherRule.kt | 8 +-- .../core/testing/util/TestAnalyticsHelper.kt | 2 +- .../core/testing/util/TestSyncManager.kt | 4 +- .../core/ui/JankStatsExtensions.kt | 4 +- .../apps/nowinandroid/core/ui/NewsFeed.kt | 4 +- .../feature/bookmarks/BookmarksScreen.kt | 3 +- .../navigation/BookmarksNavigation.kt | 2 +- .../foryou/navigation/ForYouNavigation.kt | 2 +- .../navigation/InterestsNavigation.kt | 2 +- .../feature/search/SearchScreen.kt | 8 +-- .../feature/topic/TopicViewModel.kt | 33 ++------- .../topic/navigation/TopicNavigation.kt | 2 +- .../lint/designsystem/DesignSystemDetector.kt | 15 ++-- 46 files changed, 161 insertions(+), 279 deletions(-) diff --git a/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt b/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt index 5861cda58..d57556ac2 100644 --- a/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt +++ b/app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt @@ -90,7 +90,7 @@ class NavigationTest { lateinit var topicsRepository: TopicsRepository private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) = - ReadOnlyProperty { _, _ -> activity.getString(resId) } + ReadOnlyProperty { _, _ -> activity.getString(resId) } // The strings used for matching in these tests private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.navigate_up) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt index 7fe1bc674..0d03aa300 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -90,9 +90,7 @@ class MainActivity : ComponentActivity() { lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.uiState - .onEach { - uiState = it - } + .onEach { uiState = it } .collect() } } diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/di/JankStatsModule.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/di/JankStatsModule.kt index be64d057f..56d1b6e24 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/di/JankStatsModule.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/di/JankStatsModule.kt @@ -20,6 +20,7 @@ import android.app.Activity import android.util.Log import android.view.Window import androidx.metrics.performance.JankStats +import androidx.metrics.performance.JankStats.OnFrameListener import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -29,26 +30,20 @@ import dagger.hilt.android.components.ActivityComponent @InstallIn(ActivityComponent::class) object JankStatsModule { @Provides - fun providesOnFrameListener(): JankStats.OnFrameListener { - return JankStats.OnFrameListener { frameData -> - // Make sure to only log janky frames. - if (frameData.isJank) { - // We're currently logging this but would better report it to a backend. - Log.v("NiA Jank", frameData.toString()) - } + fun providesOnFrameListener(): OnFrameListener = OnFrameListener { frameData -> + // Make sure to only log janky frames. + if (frameData.isJank) { + // We're currently logging this but would better report it to a backend. + Log.v("NiA Jank", frameData.toString()) } } @Provides - fun providesWindow(activity: Activity): Window { - return activity.window - } + fun providesWindow(activity: Activity): Window = activity.window @Provides fun providesJankStats( window: Window, - frameListener: JankStats.OnFrameListener, - ): JankStats { - return JankStats.createAndTrack(window, frameListener) - } + frameListener: OnFrameListener, + ): JankStats = JankStats.createAndTrack(window, frameListener) } diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index aa85afebd..caefeab4d 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -97,9 +97,7 @@ fun NiaApp( ) { val shouldShowGradientBackground = appState.currentTopLevelDestination == TopLevelDestination.FOR_YOU - var showSettingsDialog by rememberSaveable { - mutableStateOf(false) - } + var showSettingsDialog by rememberSaveable { mutableStateOf(false) } NiaBackground { NiaGradientBackground( diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt index 09e70069e..c652d08f1 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt @@ -164,9 +164,7 @@ class NiaAppState( } } - fun navigateToSearch() { - navController.navigateToSearch() - } + fun navigateToSearch() = navController.navigateToSearch() } /** diff --git a/benchmarks/src/main/kotlin/androidx/test/uiautomator/UiAutomatorHelpers.kt b/benchmarks/src/main/kotlin/androidx/test/uiautomator/UiAutomatorHelpers.kt index 85867b982..b0eb754c7 100644 --- a/benchmarks/src/main/kotlin/androidx/test/uiautomator/UiAutomatorHelpers.kt +++ b/benchmarks/src/main/kotlin/androidx/test/uiautomator/UiAutomatorHelpers.kt @@ -29,15 +29,11 @@ import androidx.test.uiautomator.HasChildrenOp.EXACTLY fun untilHasChildren( childCount: Int = 1, op: HasChildrenOp = AT_LEAST, -): UiObject2Condition { - return object : UiObject2Condition() { - override fun apply(element: UiObject2): Boolean { - return when (op) { - AT_LEAST -> element.childCount >= childCount - EXACTLY -> element.childCount == childCount - AT_MOST -> element.childCount <= childCount - } - } +): UiObject2Condition = object : UiObject2Condition() { + override fun apply(element: UiObject2): Boolean = when (op) { + AT_LEAST -> element.childCount >= childCount + EXACTLY -> element.childCount == childCount + AT_MOST -> element.childCount <= childCount } } diff --git a/benchmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/baselineprofile/StartupBaselineProfile.kt b/benchmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/baselineprofile/StartupBaselineProfile.kt index c5a88e1bd..d8128a670 100644 --- a/benchmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/baselineprofile/StartupBaselineProfile.kt +++ b/benchmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/baselineprofile/StartupBaselineProfile.kt @@ -16,6 +16,7 @@ package com.google.samples.apps.nowinandroid.baselineprofile +import androidx.benchmark.macro.MacrobenchmarkScope import androidx.benchmark.macro.junit4.BaselineProfileRule import com.google.samples.apps.nowinandroid.PACKAGE_NAME import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications @@ -30,11 +31,9 @@ class StartupBaselineProfile { @get:Rule val baselineProfileRule = BaselineProfileRule() @Test - fun generate() = - baselineProfileRule.collect( - PACKAGE_NAME, - includeInStartupProfile = true, - ) { - startActivityAndAllowNotifications() - } + fun generate() = baselineProfileRule.collect( + PACKAGE_NAME, + includeInStartupProfile = true, + profileBlock = MacrobenchmarkScope::startActivityAndAllowNotifications, + ) } diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/PrintTestApks.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/PrintTestApks.kt index 6c08216cc..4928072fc 100644 --- a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/PrintTestApks.kt +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/PrintTestApks.kt @@ -79,14 +79,12 @@ internal abstract class PrintApkLocationTask : DefaultTask() { fun taskAction() { val hasFiles = sources.orNull?.any { directory -> directory.asFileTree.files.any { - it.isFile && it.parentFile.path.contains("build${File.separator}generated").not() + it.isFile && "build${File.separator}generated" !in it.parentFile.path } } ?: throw RuntimeException("Cannot check androidTest sources") // Don't print APK location if there are no androidTest source files - if (!hasFiles) { - return - } + if (!hasFiles) return val builtArtifacts = builtArtifactsLoader.get().load(apkFolder.get()) ?: throw RuntimeException("Cannot load APKs") diff --git a/core/common/src/main/kotlin/com/google/samples/apps/nowinandroid/core/result/Result.kt b/core/common/src/main/kotlin/com/google/samples/apps/nowinandroid/core/result/Result.kt index 6ae12d634..22376d082 100644 --- a/core/common/src/main/kotlin/com/google/samples/apps/nowinandroid/core/result/Result.kt +++ b/core/common/src/main/kotlin/com/google/samples/apps/nowinandroid/core/result/Result.kt @@ -23,15 +23,10 @@ import kotlinx.coroutines.flow.onStart sealed interface Result { data class Success(val data: T) : Result - data class Error(val exception: Throwable? = null) : Result + data class Error(val exception: Throwable) : Result data object Loading : Result } -fun Flow.asResult(): Flow> { - return this - .map> { - Result.Success(it) - } - .onStart { emit(Result.Loading) } - .catch { emit(Result.Error(it)) } -} +fun Flow.asResult(): Flow> = map> { Result.Success(it) } + .onStart { emit(Result.Loading) } + .catch { emit(Result.Error(it)) } diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultRecentSearchRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultRecentSearchRepository.kt index 702c2dcd2..ca7e62853 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultRecentSearchRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultRecentSearchRepository.kt @@ -39,9 +39,7 @@ class DefaultRecentSearchRepository @Inject constructor( override fun getRecentSearchQueries(limit: Int): Flow> = recentSearchQueryDao.getRecentSearchQueryEntities(limit).map { searchQueries -> - searchQueries.map { - it.asExternalModel() - } + searchQueries.map { it.asExternalModel() } } override suspend fun clearRecentSearches() = recentSearchQueryDao.clearRecentSearchQueries() diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeRecentSearchRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeRecentSearchRepository.kt index fc649f3ec..025b51f68 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeRecentSearchRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeRecentSearchRepository.kt @@ -26,10 +26,10 @@ import javax.inject.Inject * Fake implementation of the [RecentSearchRepository] */ class FakeRecentSearchRepository @Inject constructor() : RecentSearchRepository { - override suspend fun insertOrReplaceRecentSearch(searchQuery: String) { /* no-op */ } + override suspend fun insertOrReplaceRecentSearch(searchQuery: String) = Unit override fun getRecentSearchQueries(limit: Int): Flow> = flowOf(emptyList()) - override suspend fun clearRecentSearches() { /* no-op */ } + override suspend fun clearRecentSearches() = Unit } diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeSearchContentsRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeSearchContentsRepository.kt index d15890a10..65cced452 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeSearchContentsRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeSearchContentsRepository.kt @@ -27,7 +27,7 @@ import javax.inject.Inject */ class FakeSearchContentsRepository @Inject constructor() : SearchContentsRepository { - override suspend fun populateFtsData() { /* no-op */ } + override suspend fun populateFtsData() = Unit override fun searchContents(searchQuery: String): Flow = flowOf() override fun getSearchContentsCount(): Flow = flowOf(1) } diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt index 1ab9c9353..0eefc8451 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/fake/FakeTopicsRepository.kt @@ -55,9 +55,8 @@ class FakeTopicsRepository @Inject constructor( ) }.flowOn(ioDispatcher) - override fun getTopic(id: String): Flow { - return getTopics().map { it.first { topic -> topic.id == id } } - } + override fun getTopic(id: String): Flow = getTopics() + .map { it.first { topic -> topic.id == id } } override suspend fun syncWith(synchronizer: Synchronizer) = true } diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt index 743fb7e5c..05811f4be 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt @@ -82,7 +82,7 @@ class CompositeUserNewsResourceRepositoryTest { // Check that only news resources with the given topic id are returned. assertEquals( sampleNewsResources - .filter { it.topics.contains(sampleTopic1) } + .filter { sampleTopic1 in it.topics } .mapToUserNewsResources(emptyUserData), userNewsResources.first(), ) @@ -104,7 +104,7 @@ class CompositeUserNewsResourceRepositoryTest { // Check that only news resources with the given topic id are returned. assertEquals( sampleNewsResources - .filter { it.topics.contains(sampleTopic1) } + .filter { sampleTopic1 in it.topics } .mapToUserNewsResources(userData), userNewsResources.first(), ) diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/UserNewsResourceTest.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/UserNewsResourceTest.kt index a21dee863..c7dfd99d0 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/UserNewsResourceTest.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/UserNewsResourceTest.kt @@ -91,14 +91,14 @@ class UserNewsResourceTest { // Construct the expected FollowableTopic. val followableTopic = FollowableTopic( topic = topic, - isFollowed = userData.followedTopics.contains(topic.id), + isFollowed = topic.id in userData.followedTopics, ) assertTrue(userNewsResource.followableTopics.contains(followableTopic)) } // Check that the saved flag is set correctly. assertEquals( - userData.bookmarkedNewsResources.contains(newsResource1.id), + newsResource1.id in userData.bookmarkedNewsResources, userNewsResource.isSaved, ) } diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt index 6e5c45305..dc4b78e01 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt @@ -34,9 +34,7 @@ val nonPresentInterestsIds = setOf("2") */ class TestNewsResourceDao : NewsResourceDao { - private var entitiesStateFlow = MutableStateFlow( - emptyList(), - ) + private val entitiesStateFlow = MutableStateFlow(emptyList()) internal var topicCrossReferences: List = listOf() @@ -131,7 +129,7 @@ class TestNewsResourceDao : NewsResourceDao { override suspend fun deleteNewsResources(ids: List) { val idSet = ids.toSet() entitiesStateFlow.update { entities -> - entities.filterNot { idSet.contains(it.id) } + entities.filterNot { it.id in idSet } } } } diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNiaNetworkDataSource.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNiaNetworkDataSource.kt index 8e248a3aa..7f9a69959 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNiaNetworkDataSource.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNiaNetworkDataSource.kt @@ -91,11 +91,10 @@ class TestNiaNetworkDataSource : NiaNetworkDataSource { } } -fun List.after(version: Int?): List = - when (version) { - null -> this - else -> this.filter { it.changeListVersion > version } - } +fun List.after(version: Int?): List = when (version) { + null -> this + else -> filter { it.changeListVersion > version } +} /** * Return items from [this] whose id defined by [idGetter] is in [ids] if [ids] is not null @@ -105,7 +104,7 @@ private fun List.matchIds( idGetter: (T) -> String, ) = when (ids) { null -> this - else -> ids.toSet().let { idSet -> this.filter { idSet.contains(idGetter(it)) } } + else -> ids.toSet().let { idSet -> filter { idGetter(it) in idSet } } } /** diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestTopicDao.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestTopicDao.kt index a52cc86f6..d217f55d7 100644 --- a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestTopicDao.kt +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestTopicDao.kt @@ -28,20 +28,15 @@ import kotlinx.coroutines.flow.update */ class TestTopicDao : TopicDao { - private var entitiesStateFlow = MutableStateFlow( - emptyList(), - ) + private val entitiesStateFlow = MutableStateFlow(emptyList()) - override fun getTopicEntity(topicId: String): Flow { + override fun getTopicEntity(topicId: String): Flow = throw NotImplementedError("Unused in tests") - } - override fun getTopicEntities(): Flow> = - entitiesStateFlow + override fun getTopicEntities(): Flow> = entitiesStateFlow override fun getTopicEntities(ids: Set): Flow> = - getTopicEntities() - .map { topics -> topics.filter { it.id in ids } } + getTopicEntities().map { topics -> topics.filter { it.id in ids } } override suspend fun getOneOffTopicEntities(): List = emptyList() @@ -55,15 +50,11 @@ class TestTopicDao : TopicDao { override suspend fun upsertTopics(entities: List) { // Overwrite old values with new values - entitiesStateFlow.update { oldValues -> - (entities + oldValues).distinctBy(TopicEntity::id) - } + entitiesStateFlow.update { oldValues -> (entities + oldValues).distinctBy(TopicEntity::id) } } override suspend fun deleteTopics(ids: List) { val idSet = ids.toSet() - entitiesStateFlow.update { entities -> - entities.filterNot { idSet.contains(it.id) } - } + entitiesStateFlow.update { entities -> entities.filterNot { it.id in idSet } } } } diff --git a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ListToMapMigration.kt b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ListToMapMigration.kt index 5bff23340..c1e49a44b 100644 --- a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ListToMapMigration.kt +++ b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/ListToMapMigration.kt @@ -52,7 +52,6 @@ object ListToMapMigration : DataMigration { hasDoneListToMapMigration = true } - override suspend fun shouldMigrate(currentData: UserPreferences): Boolean { - return !currentData.hasDoneListToMapMigration - } + override suspend fun shouldMigrate(currentData: UserPreferences): Boolean = + !currentData.hasDoneListToMapMigration } diff --git a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt index 6dc7725c1..9a76a75a1 100644 --- a/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt +++ b/core/datastore/src/main/kotlin/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt @@ -103,9 +103,7 @@ class NiaPreferencesDataSource @Inject constructor( suspend fun setDynamicColorPreference(useDynamicColor: Boolean) { userPreferences.updateData { - it.copy { - this.useDynamicColor = useDynamicColor - } + it.copy { this.useDynamicColor = useDynamicColor } } } @@ -190,9 +188,7 @@ class NiaPreferencesDataSource @Inject constructor( suspend fun setShouldHideOnboarding(shouldHideOnboarding: Boolean) { userPreferences.updateData { - it.copy { - this.shouldHideOnboarding = shouldHideOnboarding - } + it.copy { this.shouldHideOnboarding = shouldHideOnboarding } } } } diff --git a/core/designsystem/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/ThemeTest.kt b/core/designsystem/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/ThemeTest.kt index b10df053c..92e94f4e5 100644 --- a/core/designsystem/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/ThemeTest.kt +++ b/core/designsystem/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/ThemeTest.kt @@ -16,7 +16,8 @@ package com.google.samples.apps.nowinandroid.core.designsystem -import android.os.Build +import android.os.Build.VERSION.SDK_INT +import android.os.Build.VERSION_CODES import androidx.compose.material3.ColorScheme import androidx.compose.material3.MaterialTheme import androidx.compose.material3.dynamicDarkColorScheme @@ -219,60 +220,41 @@ class ThemeTest { } @Composable - private fun dynamicLightColorSchemeWithFallback(): ColorScheme { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - dynamicLightColorScheme(LocalContext.current) - } else { - LightDefaultColorScheme - } + private fun dynamicLightColorSchemeWithFallback(): ColorScheme = when { + SDK_INT >= VERSION_CODES.S -> dynamicLightColorScheme(LocalContext.current) + else -> LightDefaultColorScheme } @Composable - private fun dynamicDarkColorSchemeWithFallback(): ColorScheme { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - dynamicDarkColorScheme(LocalContext.current) - } else { - DarkDefaultColorScheme - } + private fun dynamicDarkColorSchemeWithFallback(): ColorScheme = when { + SDK_INT >= VERSION_CODES.S -> dynamicDarkColorScheme(LocalContext.current) + else -> DarkDefaultColorScheme } - private fun emptyGradientColors(colorScheme: ColorScheme): GradientColors { - return GradientColors(container = colorScheme.surfaceColorAtElevation(2.dp)) - } + private fun emptyGradientColors(colorScheme: ColorScheme): GradientColors = + GradientColors(container = colorScheme.surfaceColorAtElevation(2.dp)) - private fun defaultGradientColors(colorScheme: ColorScheme): GradientColors { - return GradientColors( - top = colorScheme.inverseOnSurface, - bottom = colorScheme.primaryContainer, - container = colorScheme.surface, - ) - } + private fun defaultGradientColors(colorScheme: ColorScheme): GradientColors = GradientColors( + top = colorScheme.inverseOnSurface, + bottom = colorScheme.primaryContainer, + container = colorScheme.surface, + ) - private fun dynamicGradientColorsWithFallback(colorScheme: ColorScheme): GradientColors { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - emptyGradientColors(colorScheme) - } else { - defaultGradientColors(colorScheme) - } + private fun dynamicGradientColorsWithFallback(colorScheme: ColorScheme): GradientColors = when { + SDK_INT >= VERSION_CODES.S -> emptyGradientColors(colorScheme) + else -> defaultGradientColors(colorScheme) } - private fun defaultBackgroundTheme(colorScheme: ColorScheme): BackgroundTheme { - return BackgroundTheme( - color = colorScheme.surface, - tonalElevation = 2.dp, - ) - } + private fun defaultBackgroundTheme(colorScheme: ColorScheme): BackgroundTheme = BackgroundTheme( + color = colorScheme.surface, + tonalElevation = 2.dp, + ) - private fun defaultTintTheme(): TintTheme { - return TintTheme() - } + private fun defaultTintTheme(): TintTheme = TintTheme() - private fun dynamicTintThemeWithFallback(colorScheme: ColorScheme): TintTheme { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - TintTheme(colorScheme.primary) - } else { - TintTheme() - } + private fun dynamicTintThemeWithFallback(colorScheme: ColorScheme): TintTheme = when { + SDK_INT >= VERSION_CODES.S -> TintTheme(colorScheme.primary) + else -> TintTheme() } /** diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/DynamicAsyncImage.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/DynamicAsyncImage.kt index cc352107b..1092ba4c9 100644 --- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/DynamicAsyncImage.kt +++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/DynamicAsyncImage.kt @@ -28,6 +28,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color.Companion.Unspecified import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.layout.ContentScale @@ -79,7 +80,7 @@ fun DynamicAsyncImage( contentScale = ContentScale.Crop, painter = if (isError.not() && !isLocalInspection) imageLoader else placeholder, contentDescription = contentDescription, - colorFilter = if (iconTint != null) ColorFilter.tint(iconTint) else null, + colorFilter = if (iconTint != Unspecified) ColorFilter.tint(iconTint) else null, ) } } diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/TopAppBar.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/TopAppBar.kt index 99f935d2a..9c716918a 100644 --- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/TopAppBar.kt +++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/TopAppBar.kt @@ -40,9 +40,9 @@ import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons fun NiaTopAppBar( @StringRes titleRes: Int, navigationIcon: ImageVector, - navigationIconContentDescription: String?, + navigationIconContentDescription: String, actionIcon: ImageVector, - actionIconContentDescription: String?, + actionIconContentDescription: String, modifier: Modifier = Modifier, colors: TopAppBarColors = TopAppBarDefaults.centerAlignedTopAppBarColors(), onNavigationClick: () -> Unit = {}, diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt index 7a0282bf7..0984317b9 100644 --- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt +++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt @@ -229,10 +229,5 @@ fun LazyStaggeredGridState.scrollbarState( .collect { value = it } }.value -private inline fun List.floatSumOf(selector: (T) -> Float): Float { - var sum = 0f - for (element in this) { - sum += selector(element) - } - return sum -} +private inline fun List.floatSumOf(selector: (T) -> Float): Float = + fold(0f) { acc, it -> acc + selector(it) } diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/theme/Tint.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/theme/Tint.kt index 848c8d8f5..75ab3a8f6 100644 --- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/theme/Tint.kt +++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/theme/Tint.kt @@ -25,7 +25,7 @@ import androidx.compose.ui.graphics.Color */ @Immutable data class TintTheme( - val iconTint: Color? = null, + val iconTint: Color = Color.Unspecified, ) /** diff --git a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt index c3c045d44..0167a3192 100644 --- a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt +++ b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt @@ -37,22 +37,20 @@ class GetFollowableTopicsUseCase @Inject constructor( * * @param sortBy - the field used to sort the topics. Default NONE = no sorting. */ - operator fun invoke(sortBy: TopicSortField = NONE): Flow> { - return combine( - userDataRepository.userData, - topicsRepository.getTopics(), - ) { userData, topics -> - val followedTopics = topics - .map { topic -> - FollowableTopic( - topic = topic, - isFollowed = topic.id in userData.followedTopics, - ) - } - when (sortBy) { - NAME -> followedTopics.sortedBy { it.topic.name } - else -> followedTopics + operator fun invoke(sortBy: TopicSortField = NONE): Flow> = combine( + userDataRepository.userData, + topicsRepository.getTopics(), + ) { userData, topics -> + val followedTopics = topics + .map { topic -> + FollowableTopic( + topic = topic, + isFollowed = topic.id in userData.followedTopics, + ) } + when (sortBy) { + NAME -> followedTopics.sortedBy { it.topic.name } + else -> followedTopics } } } diff --git a/core/model/src/main/kotlin/com/google/samples/apps/nowinandroid/core/model/data/UserNewsResource.kt b/core/model/src/main/kotlin/com/google/samples/apps/nowinandroid/core/model/data/UserNewsResource.kt index 4ea830022..a56bbcb8d 100644 --- a/core/model/src/main/kotlin/com/google/samples/apps/nowinandroid/core/model/data/UserNewsResource.kt +++ b/core/model/src/main/kotlin/com/google/samples/apps/nowinandroid/core/model/data/UserNewsResource.kt @@ -45,14 +45,13 @@ data class UserNewsResource internal constructor( followableTopics = newsResource.topics.map { topic -> FollowableTopic( topic = topic, - isFollowed = userData.followedTopics.contains(topic.id), + isFollowed = topic.id in userData.followedTopics, ) }, - isSaved = userData.bookmarkedNewsResources.contains(newsResource.id), - hasBeenViewed = userData.viewedNewsResources.contains(newsResource.id), + isSaved = newsResource.id in userData.bookmarkedNewsResources, + hasBeenViewed = newsResource.id in userData.viewedNewsResources, ) } -fun List.mapToUserNewsResources(userData: UserData): List { - return map { UserNewsResource(it, userData) } -} +fun List.mapToUserNewsResources(userData: UserData): List = + map { UserNewsResource(it, userData) } diff --git a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt b/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt index 7e74f819c..fbc60baf1 100644 --- a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt +++ b/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt @@ -24,10 +24,10 @@ import android.app.PendingIntent import android.content.ComponentName import android.content.Context import android.content.Intent -import android.content.pm.PackageManager +import android.content.pm.PackageManager.PERMISSION_GRANTED import android.os.Build.VERSION import android.os.Build.VERSION_CODES -import androidx.core.app.ActivityCompat +import androidx.core.app.ActivityCompat.checkSelfPermission import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.InboxStyle import androidx.core.app.NotificationManagerCompat @@ -57,30 +57,24 @@ class SystemTrayNotifier @Inject constructor( override fun postNewsNotifications( newsResources: List, ) = with(context) { - if (ActivityCompat.checkSelfPermission( - this, - permission.POST_NOTIFICATIONS, - ) != PackageManager.PERMISSION_GRANTED - ) { + if (checkSelfPermission(this, permission.POST_NOTIFICATIONS) != PERMISSION_GRANTED) { return } - val truncatedNewsResources = newsResources - .take(MAX_NUM_NOTIFICATIONS) + val truncatedNewsResources = newsResources.take(MAX_NUM_NOTIFICATIONS) - val newsNotifications = truncatedNewsResources - .map { newsResource -> - createNewsNotification { - setSmallIcon( - com.google.samples.apps.nowinandroid.core.common.R.drawable.ic_nia_notification, - ) - .setContentTitle(newsResource.title) - .setContentText(newsResource.content) - .setContentIntent(newsPendingIntent(newsResource)) - .setGroup(NEWS_NOTIFICATION_GROUP) - .setAutoCancel(true) - } + val newsNotifications = truncatedNewsResources.map { newsResource -> + createNewsNotification { + setSmallIcon( + com.google.samples.apps.nowinandroid.core.common.R.drawable.ic_nia_notification, + ) + .setContentTitle(newsResource.title) + .setContentText(newsResource.content) + .setContentIntent(newsPendingIntent(newsResource)) + .setGroup(NEWS_NOTIFICATION_GROUP) + .setAutoCancel(true) } + } val summaryNotification = createNewsNotification { val title = getString( R.string.news_notification_group_summary, @@ -117,9 +111,7 @@ class SystemTrayNotifier @Inject constructor( newsResources: List, title: String, ): InboxStyle = newsResources - .fold(InboxStyle()) { inboxStyle, newsResource -> - inboxStyle.addLine(newsResource.title) - } + .fold(InboxStyle()) { inboxStyle, newsResource -> inboxStyle.addLine(newsResource.title) } .setBigContentTitle(title) .setSummaryText(title) } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/NiaTestRunner.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/NiaTestRunner.kt index 30254a617..9b3b185df 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/NiaTestRunner.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/NiaTestRunner.kt @@ -25,7 +25,6 @@ import dagger.hilt.android.testing.HiltTestApplication * A custom runner to set up the instrumented application class for tests. */ class NiaTestRunner : AndroidJUnitRunner() { - override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application { - return super.newApplication(cl, HiltTestApplication::class.java.name, context) - } + override fun newApplication(cl: ClassLoader, name: String, context: Context): Application = + super.newApplication(cl, HiltTestApplication::class.java.name, context) } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestNewsRepository.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestNewsRepository.kt index d0bfd21a1..ef065a9f8 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestNewsRepository.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestNewsRepository.kt @@ -43,9 +43,7 @@ class TestNewsRepository : NewsRepository { } } query.filterNewsIds?.let { filterNewsIds -> - result = newsResources.filter { - filterNewsIds.contains(it.id) - } + result = newsResources.filter { it.id in filterNewsIds } } result } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt index 961473401..f700bdc31 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestRecentSearchRepository.kt @@ -32,7 +32,5 @@ class TestRecentSearchRepository : RecentSearchRepository { cachedRecentSearches.add(RecentSearchQuery(searchQuery)) } - override suspend fun clearRecentSearches() { - cachedRecentSearches.clear() - } + override suspend fun clearRecentSearches() = cachedRecentSearches.clear() } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestSearchContentsRepository.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestSearchContentsRepository.kt index 2aa54e463..9b6151449 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestSearchContentsRepository.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestSearchContentsRepository.kt @@ -29,18 +29,15 @@ class TestSearchContentsRepository : SearchContentsRepository { private val cachedTopics: MutableList = mutableListOf() private val cachedNewsResources: MutableList = mutableListOf() - override suspend fun populateFtsData() { /* no-op */ } + override suspend fun populateFtsData() = Unit override fun searchContents(searchQuery: String): Flow = flowOf( SearchResult( topics = cachedTopics.filter { - it.name.contains(searchQuery) || - it.shortDescription.contains(searchQuery) || - it.longDescription.contains(searchQuery) + searchQuery in it.name || searchQuery in it.shortDescription || searchQuery in it.longDescription }, newsResources = cachedNewsResources.filter { - it.content.contains(searchQuery) || - it.title.contains(searchQuery) + searchQuery in it.content || searchQuery in it.title }, ), ) diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestTopicsRepository.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestTopicsRepository.kt index a95469d83..ddccbbe35 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestTopicsRepository.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/repository/TestTopicsRepository.kt @@ -33,9 +33,8 @@ class TestTopicsRepository : TopicsRepository { override fun getTopics(): Flow> = topicsFlow - override fun getTopic(id: String): Flow { - return topicsFlow.map { topics -> topics.find { it.id == id }!! } - } + override fun getTopic(id: String): Flow = + topicsFlow.map { topics -> topics.find { it.id == id }!! } /** * A test-only API to allow controlling the list of topics from tests. diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.kt index 28155f5ad..666c4edd4 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.kt @@ -32,11 +32,7 @@ import org.junit.runner.Description class MainDispatcherRule( private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(), ) : TestWatcher() { - override fun starting(description: Description) { - Dispatchers.setMain(testDispatcher) - } + override fun starting(description: Description) = Dispatchers.setMain(testDispatcher) - override fun finished(description: Description) { - Dispatchers.resetMain() - } + override fun finished(description: Description) = Dispatchers.resetMain() } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt index 005784c21..5f72d30e6 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt @@ -26,5 +26,5 @@ class TestAnalyticsHelper : AnalyticsHelper { events.add(event) } - fun hasLogged(event: AnalyticsEvent) = events.contains(event) + fun hasLogged(event: AnalyticsEvent) = event in events } diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestSyncManager.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestSyncManager.kt index 999b67195..ff1e2fdd9 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestSyncManager.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/util/TestSyncManager.kt @@ -26,9 +26,7 @@ class TestSyncManager : SyncManager { override val isSyncing: Flow = syncStatusFlow - override fun requestSync() { - TODO("Not yet implemented") - } + override fun requestSync(): Unit = TODO("Not yet implemented") /** * A test-only API to set the sync status from tests. diff --git a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt index c9fee1ac8..ef3de1059 100644 --- a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt +++ b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/JankStatsExtensions.kt @@ -50,7 +50,7 @@ fun rememberMetricsStateHolder(): Holder { */ @Composable fun TrackJank( - vararg keys: Any?, + vararg keys: Any, reportMetric: suspend CoroutineScope.(state: Holder) -> Unit, ) { val metrics = rememberMetricsStateHolder() @@ -65,7 +65,7 @@ fun TrackJank( */ @Composable fun TrackDisposableJank( - vararg keys: Any?, + vararg keys: Any, reportMetric: DisposableEffectScope.(state: Holder) -> DisposableEffectResult, ) { val metrics = rememberMetricsStateHolder() diff --git a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt index e2904afc3..7264c7b14 100644 --- a/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt +++ b/core/ui/src/main/kotlin/com/google/samples/apps/nowinandroid/core/ui/NewsFeed.kt @@ -64,9 +64,7 @@ fun LazyStaggeredGridScope.newsFeed( key = { it.id }, contentType = { "newsFeedItem" }, ) { userNewsResource -> - val resourceUrl by remember { - mutableStateOf(Uri.parse(userNewsResource.url)) - } + val resourceUrl by remember { mutableStateOf(Uri.parse(userNewsResource.url)) } val context = LocalContext.current val analyticsHelper = LocalAnalyticsHelper.current val backgroundColor = MaterialTheme.colorScheme.background.toArgb() diff --git a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index 7d51c6e84..1fa429d21 100644 --- a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -47,6 +47,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.platform.testTag @@ -237,7 +238,7 @@ private fun EmptyState(modifier: Modifier = Modifier) { Image( modifier = Modifier.fillMaxWidth(), painter = painterResource(id = R.drawable.img_empty_bookmarks), - colorFilter = if (iconTint != null) ColorFilter.tint(iconTint) else null, + colorFilter = if (iconTint != Color.Unspecified) ColorFilter.tint(iconTint) else null, contentDescription = null, ) diff --git a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt index ebcde4ab1..aa8237da4 100644 --- a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt +++ b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt @@ -24,7 +24,7 @@ import com.google.samples.apps.nowinandroid.feature.bookmarks.BookmarksRoute const val bookmarksRoute = "bookmarks_route" -fun NavController.navigateToBookmarks(navOptions: NavOptions? = null) { +fun NavController.navigateToBookmarks(navOptions: NavOptions) { this.navigate(bookmarksRoute, navOptions) } diff --git a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt index 705495cc2..ca606161d 100644 --- a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt +++ b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt @@ -30,7 +30,7 @@ const val forYouNavigationRoute = "for_you_route/{$LINKED_NEWS_RESOURCE_ID}" private const val DEEP_LINK_URI_PATTERN = "https://www.nowinandroid.apps.samples.google.com/foryou/{$LINKED_NEWS_RESOURCE_ID}" -fun NavController.navigateToForYou(navOptions: NavOptions? = null) { +fun NavController.navigateToForYou(navOptions: NavOptions) { this.navigate(forYouNavigationRoute, navOptions) } diff --git a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt index 7558ec48d..3bc0ad770 100644 --- a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt +++ b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt @@ -26,7 +26,7 @@ import com.google.samples.apps.nowinandroid.feature.interests.InterestsRoute private const val INTERESTS_GRAPH_ROUTE_PATTERN = "interests_graph" const val interestsRoute = "interests_route" -fun NavController.navigateToInterestsGraph(navOptions: NavOptions? = null) { +fun NavController.navigateToInterestsGraph(navOptions: NavOptions) { this.navigate(INTERESTS_GRAPH_ROUTE_PATTERN, navOptions) } diff --git a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt index 65b65f61d..b6e977571 100644 --- a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt +++ b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt @@ -264,9 +264,7 @@ fun EmptySearchResultBody( ) { offset -> tryAnotherSearchString.getStringAnnotations(start = offset, end = offset) .firstOrNull() - ?.let { - onInterestsClick() - } + ?.let { onInterestsClick() } } } } @@ -519,9 +517,7 @@ private fun SearchTextField( } }, onValueChange = { - if (!it.contains("\n")) { - onSearchQueryChanged(it) - } + if ("\n" !in it) onSearchQueryChanged(it) }, modifier = Modifier .fillMaxWidth() diff --git a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index 6adfe0a67..ff1eee319 100644 --- a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -117,22 +117,16 @@ private fun topicUiState( when (followedTopicToTopicResult) { is Result.Success -> { val (followedTopics, topic) = followedTopicToTopicResult.data - val followed = followedTopics.contains(topicId) TopicUiState.Success( followableTopic = FollowableTopic( topic = topic, - isFollowed = followed, + isFollowed = topicId in followedTopics, ), ) } - is Result.Loading -> { - TopicUiState.Loading - } - - is Result.Error -> { - TopicUiState.Error - } + is Result.Loading -> TopicUiState.Loading + is Result.Error -> TopicUiState.Error } } } @@ -151,26 +145,13 @@ private fun newsUiState( val bookmark: Flow> = userDataRepository.userData .map { it.bookmarkedNewsResources } - return combine( - newsStream, - bookmark, - ::Pair, - ) + return combine(newsStream, bookmark, ::Pair) .asResult() .map { newsToBookmarksResult -> when (newsToBookmarksResult) { - is Result.Success -> { - val news = newsToBookmarksResult.data.first - NewsUiState.Success(news) - } - - is Result.Loading -> { - NewsUiState.Loading - } - - is Result.Error -> { - NewsUiState.Error - } + is Result.Success -> NewsUiState.Success(newsToBookmarksResult.data.first) + is Result.Loading -> NewsUiState.Loading + is Result.Error -> NewsUiState.Error } } } diff --git a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt b/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt index c29b57d47..2d75a8d8d 100644 --- a/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt +++ b/feature/topic/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/topic/navigation/TopicNavigation.kt @@ -40,7 +40,7 @@ internal class TopicArgs(val topicId: String) { fun NavController.navigateToTopic(topicId: String) { val encodedId = URLEncoder.encode(topicId, URL_CHARACTER_ENCODING) - this.navigate("topic_route/$encodedId") { + navigate("topic_route/$encodedId") { launchSingleTop = true } } diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemDetector.kt index 4c9d55764..09af17db9 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemDetector.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/designsystem/DesignSystemDetector.kt @@ -34,15 +34,13 @@ import org.jetbrains.uast.UQualifiedReferenceExpression */ class DesignSystemDetector : Detector(), Detector.UastScanner { - override fun getApplicableUastTypes(): List> { - return listOf( - UCallExpression::class.java, - UQualifiedReferenceExpression::class.java, - ) - } + override fun getApplicableUastTypes(): List> = listOf( + UCallExpression::class.java, + UQualifiedReferenceExpression::class.java, + ) - override fun createUastHandler(context: JavaContext): UElementHandler { - return object : UElementHandler() { + override fun createUastHandler(context: JavaContext): UElementHandler = + object : UElementHandler() { override fun visitCallExpression(node: UCallExpression) { val name = node.methodName ?: return val preferredName = METHOD_NAMES[name] ?: return @@ -55,7 +53,6 @@ class DesignSystemDetector : Detector(), Detector.UastScanner { reportIssue(context, node, name, preferredName) } } - } companion object { @JvmField From bdb10a687079ca0fd684fcbd31f729c84a9cfc51 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 21 Dec 2023 18:45:05 +0100 Subject: [PATCH 2/6] Update core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt Co-authored-by: Don Turner --- .../core/designsystem/component/scrollbar/ScrollbarExt.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt index 76fab08d0..3fcc8f2c0 100644 --- a/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt +++ b/core/designsystem/src/main/kotlin/com/google/samples/apps/nowinandroid/core/designsystem/component/scrollbar/ScrollbarExt.kt @@ -231,4 +231,4 @@ fun LazyStaggeredGridState.scrollbarState( } private inline fun List.floatSumOf(selector: (T) -> Float): Float = - fold(0f) { acc, it -> acc + selector(it) } + fold(initial = 0f) { accumulator, listItem -> accumulator + selector(listItem) } From 65f0d5b2d9cb1a743e91dbd7eb06423baf9c03a7 Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 21 Dec 2023 18:48:05 +0100 Subject: [PATCH 3/6] Update feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt --- .../feature/foryou/navigation/ForYouNavigation.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt index efbe67859..8e94a491a 100644 --- a/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt +++ b/feature/foryou/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/foryou/navigation/ForYouNavigation.kt @@ -30,9 +30,7 @@ const val FOR_YOU_ROUTE = "for_you_route/{$LINKED_NEWS_RESOURCE_ID}" private const val DEEP_LINK_URI_PATTERN = "https://www.nowinandroid.apps.samples.google.com/foryou/{$LINKED_NEWS_RESOURCE_ID}" -fun NavController.navigateToForYou(navOptions: NavOptions) { - this.navigate(FOR_YOU_ROUTE, navOptions) -} +fun NavController.navigateToForYou(navOptions: NavOptions) = navigate(FOR_YOU_ROUTE, navOptions) fun NavGraphBuilder.forYouScreen(onTopicClick: (String) -> Unit) { composable( From 6cd5958665f6c54aeb082a4acac28967b6dc946a Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 21 Dec 2023 18:50:26 +0100 Subject: [PATCH 4/6] Update feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt --- .../feature/bookmarks/navigation/BookmarksNavigation.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt index a62b2c5d1..13d0baef0 100644 --- a/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt +++ b/feature/bookmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/navigation/BookmarksNavigation.kt @@ -24,9 +24,7 @@ import com.google.samples.apps.nowinandroid.feature.bookmarks.BookmarksRoute const val BOOKMARKS_ROUTE = "bookmarks_route" -fun NavController.navigateToBookmarks(navOptions: NavOptions) { - this.navigate(BOOKMARKS_ROUTE, navOptions) -} +fun NavController.navigateToBookmarks(navOptions: NavOptions) = navigate(BOOKMARKS_ROUTE, navOptions) fun NavGraphBuilder.bookmarksScreen( onTopicClick: (String) -> Unit, From b7a628ece5852dadbd4e2c69711ebbe24bcf45fc Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 21 Dec 2023 18:50:50 +0100 Subject: [PATCH 5/6] Update feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt --- .../feature/interests/navigation/InterestsNavigation.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt index d6ebaeeb7..2ad7c560b 100644 --- a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt +++ b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt @@ -26,9 +26,7 @@ import com.google.samples.apps.nowinandroid.feature.interests.InterestsRoute private const val INTERESTS_GRAPH_ROUTE_PATTERN = "interests_graph" const val INTERESTS_ROUTE = "interests_route" -fun NavController.navigateToInterestsGraph(navOptions: NavOptions) { - this.navigate(INTERESTS_GRAPH_ROUTE_PATTERN, navOptions) -} +fun NavController.navigateToInterestsGraph(navOptions: NavOptions) = navigate(INTERESTS_GRAPH_ROUTE_PATTERN, navOptions) fun NavGraphBuilder.interestsGraph( onTopicClick: (String) -> Unit, From cd62fd5e01ddf8902dd5c86bd2f0a3b368f8e34f Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 21 Dec 2023 18:51:17 +0100 Subject: [PATCH 6/6] Update feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/navigation/SearchNavigation.kt --- .../feature/search/navigation/SearchNavigation.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/navigation/SearchNavigation.kt b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/navigation/SearchNavigation.kt index a449600b2..81f3576b4 100644 --- a/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/navigation/SearchNavigation.kt +++ b/feature/search/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/search/navigation/SearchNavigation.kt @@ -24,9 +24,7 @@ import com.google.samples.apps.nowinandroid.feature.search.SearchRoute const val SEARCH_ROUTE = "search_route" -fun NavController.navigateToSearch(navOptions: NavOptions? = null) { - this.navigate(SEARCH_ROUTE, navOptions) -} +fun NavController.navigateToSearch(navOptions: NavOptions? = null) = navigate(SEARCH_ROUTE, navOptions) fun NavGraphBuilder.searchScreen( onBackClick: () -> Unit,