Skip to content

Commit

Permalink
Provide isDarkMode to easily access it anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Oct 7, 2024
1 parent e902b8e commit efad6f3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.swisstransfer.ui.screen.main.MainScreen
Expand All @@ -51,11 +50,8 @@ class MainActivity : ComponentActivity() {
}

@Composable
fun isDarkTheme(getTheme: () -> Theme?): Boolean {
val settingsViewModel = hiltViewModel<SettingsViewModel>()
val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null)

private fun isDarkTheme(getTheme: () -> Theme?): Boolean {
return getTheme()?.let {
if (it == Theme.SYSTEM) isSystemInDarkTheme() else it== Theme.DARK
if (it == Theme.SYSTEM) isSystemInDarkTheme() else it == Theme.DARK
} ?: isSystemInDarkTheme()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color

val LocalIsDarkMode = staticCompositionLocalOf { false }
val LocalCustomTypography = staticCompositionLocalOf { Typography }
val LocalCustomColorScheme: ProvidableCompositionLocal<CustomColorScheme> = staticCompositionLocalOf { CustomColorScheme() }
val LocalWindowAdaptiveInfo = staticCompositionLocalOf<WindowAdaptiveInfo> { error("No WindowAdaptiveInfo provided") }
Expand All @@ -41,6 +42,7 @@ fun SwissTransferTheme(
LocalTextStyle provides Typography.bodyRegular,
LocalCustomColorScheme provides customColors,
LocalWindowAdaptiveInfo provides currentWindowAdaptiveInfo(),
LocalIsDarkMode provides isDarkTheme,
) {
MaterialTheme(
colorScheme = if (isDarkTheme) DarkColorScheme else LightColorScheme,
Expand Down

0 comments on commit efad6f3

Please sign in to comment.