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

Provide isDarkMode to easily access it anywhere #81

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
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 @@ -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
Loading