Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.samples.apps.nowinandroid

import android.app.UiModeManager
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
Expand All @@ -34,12 +36,14 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.metrics.performance.JankStats
import androidx.tracing.trace
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper
import com.google.samples.apps.nowinandroid.core.analytics.LocalAnalyticsHelper
import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
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
Expand All @@ -48,6 +52,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -127,6 +132,28 @@ class MainActivity : ComponentActivity() {
}
}

// Sync the user's dark theme preference to the system-level UiModeManager so the
// splash screen uses the correct theme on the next cold start.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val uiModeManager = getSystemService(UiModeManager::class.java)
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.mapNotNull { (it as? Success)?.userData?.darkThemeConfig }
.distinctUntilChanged()
.collect { darkThemeConfig ->
uiModeManager.setApplicationNightMode(
when (darkThemeConfig) {
DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO
DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO
DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES
},
)
}
}
}
}
Comment on lines 137 to 155

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve performance, it's better to get the UiModeManager instance once and reuse it, rather than calling getSystemService on every emission of the darkThemeConfig flow. You can move the getSystemService call outside the collect block.

Suggested change
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.mapNotNull { (it as? Success)?.userData?.darkThemeConfig }
.distinctUntilChanged()
.collect { darkThemeConfig ->
val uiModeManager = getSystemService(UiModeManager::class.java)
uiModeManager.setApplicationNightMode(
when (darkThemeConfig) {
DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO
DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO
DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES
},
)
}
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val uiModeManager = getSystemService(UiModeManager::class.java)
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.mapNotNull { (it as? Success)?.userData?.darkThemeConfig }
.distinctUntilChanged()
.collect { darkThemeConfig ->
uiModeManager.setApplicationNightMode(
when (darkThemeConfig) {
DarkThemeConfig.FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO
DarkThemeConfig.LIGHT -> UiModeManager.MODE_NIGHT_NO
DarkThemeConfig.DARK -> UiModeManager.MODE_NIGHT_YES
},
)
}
}
}
}


// Keep the splash screen on-screen until the UI state is loaded. This condition is
// evaluated each time the app needs to be redrawn so it should be fast to avoid blocking
// the UI.
Expand Down