Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -22,6 +22,7 @@ import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -34,6 +35,7 @@ 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
Expand All @@ -44,6 +46,7 @@ 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 com.google.samples.apps.nowinandroid.util.isSystemInDarkTheme
import com.google.samples.apps.nowinandroid.util.toNightMode
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down Expand Up @@ -79,6 +82,23 @@ class MainActivity : ComponentActivity() {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)

// Apply the saved dark theme config at startup to ensure the splash screen
// uses the correct theme. This addresses issue #633.
lifecycleScope.launch {
viewModel.uiState
.map { state ->
if (state is Success) {
state.userData.darkThemeConfig.toNightMode()
} else {
null
}
}
.first { it != null } // Only take the first non-null mode
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This flow processing logic can be made more concise and idiomatic by using filterIsInstance to filter for the Success state, followed by map. This approach is more declarative and improves readability by removing the manual type check and null handling.

                .filterIsInstance<Success>()
                .map { it.userData.darkThemeConfig.toNightMode() }
                .first()

.let { mode ->
AppCompatDelegate.setDefaultNightMode(mode)
}
}

// We keep this as a mutable state, so that we can track changes inside the composition.
// This allows us to react to dark/light mode changes.
var themeSettings by mutableStateOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.nowinandroid.util

import androidx.appcompat.app.AppCompatDelegate
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig

/**
* Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant.
* This is used to set the application-level night mode for the splash screen and system UI.
*/
fun DarkThemeConfig.toNightMode(): Int = when (this) {
DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
DarkThemeConfig.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
DarkThemeConfig.DARK -> AppCompatDelegate.MODE_NIGHT_YES
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.nowinandroid.feature.settings.impl

import androidx.appcompat.app.AppCompatDelegate
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig

/**
* Converts [DarkThemeConfig] to the corresponding [AppCompatDelegate] night mode constant.
* This is used to set the application-level night mode for the splash screen and system UI.
*/
internal fun DarkThemeConfig.toNightMode(): Int = when (this) {
DarkThemeConfig.FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
DarkThemeConfig.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
DarkThemeConfig.DARK -> AppCompatDelegate.MODE_NIGHT_YES
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

This toNightMode() extension function is identical to the one added in app/src/main/kotlin/com/google/samples/apps/nowinandroid/util/DarkThemeConfigExt.kt. This code duplication should be avoided to improve maintainability. Consider moving this function to a shared module that both the app and feature:settings modules can depend on, for example a core:ui or core:common module. This would make the function internal to that shared module but available to both the app and feature:settings modules.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.samples.apps.nowinandroid.feature.settings.impl

import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
Expand Down Expand Up @@ -62,6 +63,11 @@ class SettingsViewModel @Inject constructor(
fun updateDarkThemeConfig(darkThemeConfig: DarkThemeConfig) {
viewModelScope.launch {
userDataRepository.setDarkThemeConfig(darkThemeConfig)

// Apply the night mode at the application level to ensure the splash screen
// uses the correct theme on cold start. This addresses issue #633.
// Reference: https://developer.android.com/develop/ui/views/theming/darktheme#change-themes
AppCompatDelegate.setDefaultNightMode(darkThemeConfig.toNightMode())
}
}

Expand Down