Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Android Studio Code Styles Ktlint #1350

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions .editorconfig
@@ -1,7 +1,9 @@
# https://editorconfig.org/
# This configuration is used by ktlint when spotless invokes it
root = true

[*.{kt,kts}]
ij_kotlin_allow_trailing_comma=true
ij_kotlin_allow_trailing_comma_on_call_site=true
ktlint_code_style = android_studio
ktlint_function_naming_ignore_when_annotated_with=Composable, Test
Expand Up @@ -38,21 +38,21 @@ import com.google.samples.apps.nowinandroid.R
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.rules.GrantPostNotificationsPermissionRule
import com.google.samples.apps.nowinandroid.feature.bookmarks.R as BookmarksR
import com.google.samples.apps.nowinandroid.feature.foryou.R as FeatureForyouR
import com.google.samples.apps.nowinandroid.feature.search.R as FeatureSearchR
import com.google.samples.apps.nowinandroid.feature.settings.R as SettingsR
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlin.properties.ReadOnlyProperty
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import javax.inject.Inject
import kotlin.properties.ReadOnlyProperty
import com.google.samples.apps.nowinandroid.feature.bookmarks.R as BookmarksR
import com.google.samples.apps.nowinandroid.feature.foryou.R as FeatureForyouR
import com.google.samples.apps.nowinandroid.feature.search.R as FeatureSearchR
import com.google.samples.apps.nowinandroid.feature.settings.R as SettingsR

/**
* Tests all the navigation flows that are handled by the navigation library.
Expand Down Expand Up @@ -93,15 +93,25 @@ class NavigationTest {
ReadOnlyProperty<Any, String> { _, _ -> activity.getString(resId) }

// The strings used for matching in these tests
private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up)
private val navigateUp by composeTestRule.stringResource(
FeatureForyouR.string.feature_foryou_navigate_up,
)
private val forYou by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_title)
private val interests by composeTestRule.stringResource(FeatureSearchR.string.feature_search_interests)
private val interests by composeTestRule.stringResource(
FeatureSearchR.string.feature_search_interests,
)
private val sampleTopic = "Headlines"
private val appName by composeTestRule.stringResource(R.string.app_name)
private val saved by composeTestRule.stringResource(BookmarksR.string.feature_bookmarks_title)
private val settings by composeTestRule.stringResource(SettingsR.string.feature_settings_top_app_bar_action_icon_description)
private val brand by composeTestRule.stringResource(SettingsR.string.feature_settings_brand_android)
private val ok by composeTestRule.stringResource(SettingsR.string.feature_settings_dismiss_dialog_button_text)
private val settings by composeTestRule.stringResource(
SettingsR.string.feature_settings_top_app_bar_action_icon_description,
)
private val brand by composeTestRule.stringResource(
SettingsR.string.feature_settings_brand_android,
)
private val ok by composeTestRule.stringResource(
SettingsR.string.feature_settings_dismiss_dialog_button_text,
)

@Before
fun setup() = hiltRule.inject()
Expand Down
Expand Up @@ -37,11 +37,11 @@ import com.google.samples.apps.nowinandroid.uitesthiltmanifest.HiltComponentActi
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import javax.inject.Inject

/**
* Tests that the navigation UI is rendered correctly on different screen sizes.
Expand Down
Expand Up @@ -35,16 +35,16 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepo
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.util.TestNetworkMonitor
import com.google.samples.apps.nowinandroid.core.testing.util.TestTimeZoneMonitor
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.TimeZone
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Tests [NiaAppState].
Expand Down Expand Up @@ -167,7 +167,9 @@ class NiaAppStateTest {
}

@Test
fun niaAppState_whenNetworkMonitorIsOffline_StateIsOffline() = runTest(UnconfinedTestDispatcher()) {
fun niaAppState_whenNetworkMonitorIsOffline_StateIsOffline() = runTest(
UnconfinedTestDispatcher(),
) {
composeTestRule.setContent {
state = NiaAppState(
navController = NavHostController(LocalContext.current),
Expand Down
Expand Up @@ -51,10 +51,10 @@ import com.google.samples.apps.nowinandroid.core.ui.LocalTimeZone
import com.google.samples.apps.nowinandroid.ui.NiaApp
import com.google.samples.apps.nowinandroid.ui.rememberNiaAppState
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

private const val TAG = "MainActivity"

Expand Down Expand Up @@ -172,9 +172,7 @@ class MainActivity : ComponentActivity() {
* Returns `true` if the Android theme should be used, as a function of the [uiState].
*/
@Composable
private fun shouldUseAndroidTheme(
uiState: MainActivityUiState,
): Boolean = when (uiState) {
private fun shouldUseAndroidTheme(uiState: MainActivityUiState): Boolean = when (uiState) {
Loading -> false
is Success -> when (uiState.userData.themeBrand) {
ThemeBrand.DEFAULT -> false
Expand All @@ -186,9 +184,7 @@ private fun shouldUseAndroidTheme(
* Returns `true` if the dynamic color is disabled, as a function of the [uiState].
*/
@Composable
private fun shouldDisableDynamicTheming(
uiState: MainActivityUiState,
): Boolean = when (uiState) {
private fun shouldDisableDynamicTheming(uiState: MainActivityUiState): Boolean = when (uiState) {
Loading -> false
is Success -> !uiState.userData.useDynamicColor
}
Expand All @@ -198,9 +194,7 @@ private fun shouldDisableDynamicTheming(
* current system context.
*/
@Composable
private fun shouldUseDarkTheme(
uiState: MainActivityUiState,
): Boolean = when (uiState) {
private fun shouldUseDarkTheme(uiState: MainActivityUiState): Boolean = when (uiState) {
Loading -> isSystemInDarkTheme()
is Success -> when (uiState.userData.darkThemeConfig) {
DarkThemeConfig.FOLLOW_SYSTEM -> isSystemInDarkTheme()
Expand Down
Expand Up @@ -23,11 +23,11 @@ import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
import com.google.samples.apps.nowinandroid.core.model.data.UserData
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject

@HiltViewModel
class MainActivityViewModel @Inject constructor(
Expand Down
Expand Up @@ -42,8 +42,6 @@ object JankStatsModule {
fun providesWindow(activity: Activity): Window = activity.window

@Provides
fun providesJankStats(
window: Window,
frameListener: OnFrameListener,
): JankStats = JankStats.createAndTrack(window, frameListener)
fun providesJankStats(window: Window, frameListener: OnFrameListener): JankStats =
JankStats.createAndTrack(window, frameListener)
}
Expand Up @@ -70,10 +70,10 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopAp
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.GradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors
import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR
import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog
import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR

@Composable
fun NiaApp(appState: NiaAppState) {
Expand Down Expand Up @@ -181,6 +181,7 @@ internal fun NiaApp(
val destination = appState.currentTopLevelDestination
val shouldShowTopAppBar = destination != null
if (destination != null) {
@Suppress("ktlint:standard:max-line-length")
NiaTopAppBar(
titleRes = destination.titleTextId,
navigationIcon = NiaIcons.Search,
Expand Down Expand Up @@ -294,24 +295,23 @@ private fun NiaBottomBar(
}
}

private fun Modifier.notificationDot(): Modifier =
composed {
val tertiaryColor = MaterialTheme.colorScheme.tertiary
drawWithContent {
drawContent()
drawCircle(
tertiaryColor,
radius = 5.dp.toPx(),
// This is based on the dimensions of the NavigationBar's "indicator pill";
// however, its parameters are private, so we must depend on them implicitly
// (NavigationBarTokens.ActiveIndicatorWidth = 64.dp)
center = center + Offset(
64.dp.toPx() * .45f,
32.dp.toPx() * -.45f - 6.dp.toPx(),
),
)
}
private fun Modifier.notificationDot(): Modifier = composed {
val tertiaryColor = MaterialTheme.colorScheme.tertiary
drawWithContent {
drawContent()
drawCircle(
tertiaryColor,
radius = 5.dp.toPx(),
// This is based on the dimensions of the NavigationBar's "indicator pill";
// however, its parameters are private, so we must depend on them implicitly
// (NavigationBarTokens.ActiveIndicatorWidth = 64.dp)
center = center + Offset(
64.dp.toPx() * .45f,
32.dp.toPx() * -.45f - 6.dp.toPx(),
),
)
}
}

private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) =
this?.hierarchy?.any {
Expand Down
Expand Up @@ -128,7 +128,9 @@ class NiaAppState(
*/
val topLevelDestinationsWithUnreadResources: StateFlow<Set<TopLevelDestination>> =
userNewsResourceRepository.observeAllForFollowedTopics()
.combine(userNewsResourceRepository.observeAllBookmarked()) { forYouNewsResources, bookmarkedNewsResources ->
.combine(
userNewsResourceRepository.observeAllBookmarked(),
) { forYouNewsResources, bookmarkedNewsResources ->
setOfNotNull(
FOR_YOU.takeIf { forYouNewsResources.any { !it.hasBeenViewed } },
BOOKMARKS.takeIf { bookmarkedNewsResources.any { !it.hasBeenViewed } },
Expand Down
Expand Up @@ -20,8 +20,8 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.google.samples.apps.nowinandroid.feature.interests.navigation.TOPIC_ID_ARG
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject
import kotlinx.coroutines.flow.StateFlow

@HiltViewModel
class Interests2PaneViewModel @Inject constructor(
Expand Down
Expand Up @@ -60,9 +60,7 @@ fun NavGraphBuilder.interestsListDetailScreen() {
}

@Composable
internal fun InterestsListDetailScreen(
viewModel: Interests2PaneViewModel = hiltViewModel(),
) {
internal fun InterestsListDetailScreen(viewModel: Interests2PaneViewModel = hiltViewModel()) {
val selectedTopicId by viewModel.selectedTopicId.collectAsStateWithLifecycle()
InterestsListDetailScreen(
selectedTopicId = selectedTopicId,
Expand All @@ -72,10 +70,7 @@ internal fun InterestsListDetailScreen(

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
internal fun InterestsListDetailScreen(
selectedTopicId: String?,
onTopicClick: (String) -> Unit,
) {
internal fun InterestsListDetailScreen(selectedTopicId: String?, onTopicClick: (String) -> Unit) {
val listDetailNavigator = rememberListDetailPaneScaffoldNavigator()
BackHandler(listDetailNavigator.canNavigateBack()) {
listDetailNavigator.navigateBack()
Expand Down
Expand Up @@ -19,10 +19,10 @@ package com.google.samples.apps.nowinandroid.util
import android.util.Log
import androidx.profileinstaller.ProfileVerifier
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.guava.await
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
* Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].
Expand Down
Expand Up @@ -45,6 +45,8 @@ import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import java.util.TimeZone
import javax.inject.Inject
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Before
Expand All @@ -56,8 +58,6 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode
import java.util.TimeZone
import javax.inject.Inject

/**
* Tests that the navigation UI is rendered correctly on different screen sizes.
Expand Down
Expand Up @@ -46,6 +46,7 @@ import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
Expand All @@ -59,7 +60,6 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode
import javax.inject.Inject

/**
* Tests that the Snackbar is correctly displayed on different screen sizes.
Expand Down
Expand Up @@ -30,11 +30,10 @@ class BookmarksBaselineProfile {
@get:Rule val baselineProfileRule = BaselineProfileRule()

@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()
fun generate() = baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()

// Navigate to saved screen
goToBookmarksScreen()
}
// Navigate to saved screen
goToBookmarksScreen()
}
}
Expand Up @@ -32,13 +32,12 @@ class ForYouBaselineProfile {
@get:Rule val baselineProfileRule = BaselineProfileRule()

@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()
fun generate() = baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()

// Scroll the feed critical user journey
forYouWaitForContent()
forYouSelectTopics(true)
forYouScrollFeedDownUp()
}
// Scroll the feed critical user journey
forYouWaitForContent()
forYouSelectTopics(true)
forYouScrollFeedDownUp()
}
}
Expand Up @@ -31,12 +31,11 @@ class InterestsBaselineProfile {
@get:Rule val baselineProfileRule = BaselineProfileRule()

@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()
fun generate() = baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndAllowNotifications()

// Navigate to interests screen
goToInterestsScreen()
interestsScrollTopicsDownUp()
}
// Navigate to interests screen
goToInterestsScreen()
interestsScrollTopicsDownUp()
}
}