all: add support for dynamic colors

This commit is contained in:
Harsh Shandilya 2021-12-31 01:16:31 +05:30
parent 3f3da0ab18
commit bac7611bee
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
4 changed files with 46 additions and 14 deletions

View file

@ -0,0 +1,29 @@
package dev.msfjarvis.claw.android.ui
import android.content.Context
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import dev.msfjarvis.claw.common.theme.DarkThemeColors
import dev.msfjarvis.claw.common.theme.LightThemeColors
@Composable
fun decideColorScheme(context: Context): ColorScheme {
val isDarkTheme = isSystemInDarkTheme()
return if (Build.VERSION.SDK_INT >= 31) {
if (isDarkTheme) {
dynamicDarkColorScheme(context)
} else {
dynamicLightColorScheme(context)
}
} else {
if (isDarkTheme) {
DarkThemeColors
} else {
LightThemeColors
}
}
}

View file

@ -1,6 +1,5 @@
package dev.msfjarvis.claw.android.ui
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
@ -18,6 +17,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
@ -92,12 +92,12 @@ fun LobstersApp(
currentDestination = destination.route ?: Destinations.Hottest
}
LobstersTheme(
darkTheme = isSystemInDarkTheme(),
providedValues =
arrayOf(
LocalUriHandler provides urlLauncher,
LocalHTMLConverter provides htmlConverter,
),
colorScheme = decideColorScheme(LocalContext.current),
) {
ProvideWindowInsets {
val statusBarColor = MaterialTheme.colorScheme.background

View file

@ -1,6 +1,6 @@
package dev.msfjarvis.claw.common.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
@ -11,7 +11,7 @@ import androidx.compose.ui.graphics.Color
val titleColor = Color(0xFF7395D9)
private val LightThemeColors =
val LightThemeColors =
lightColorScheme(
primary = md_theme_light_primary,
onPrimary = md_theme_light_onPrimary,
@ -40,7 +40,7 @@ private val LightThemeColors =
inverseSurface = md_theme_light_inverseSurface,
)
private val DarkThemeColors =
val DarkThemeColors =
darkColorScheme(
primary = md_theme_dark_primary,
onPrimary = md_theme_dark_onPrimary,
@ -71,17 +71,11 @@ private val DarkThemeColors =
@Composable
fun LobstersTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
providedValues: Array<ProvidedValue<*>> = emptyArray(),
colorScheme: ColorScheme,
content: @Composable () -> Unit,
) {
val colors =
if (!darkTheme) {
LightThemeColors
} else {
DarkThemeColors
}
CompositionLocalProvider(*providedValues) {
MaterialTheme(colorScheme = colors, typography = AppTypography, content = content)
MaterialTheme(colorScheme = colorScheme, typography = AppTypography, content = content)
}
}

View file

@ -1,3 +1,4 @@
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
@ -18,6 +19,8 @@ import dev.msfjarvis.claw.api.LobstersApi
import dev.msfjarvis.claw.common.posts.LobstersCard
import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.posts.toDbModel
import dev.msfjarvis.claw.common.theme.DarkThemeColors
import dev.msfjarvis.claw.common.theme.LightThemeColors
import dev.msfjarvis.claw.common.theme.LobstersTheme
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.database.local.SavedPost
@ -59,8 +62,14 @@ fun main() = auroraApplication {
undecorated = true,
onCloseRequest = ::exitApplication,
) {
val colorScheme =
if (isSystemInDarkTheme()) {
DarkThemeColors
} else {
LightThemeColors
}
LobstersTheme(
darkTheme = false,
colorScheme = colorScheme,
providedValues = arrayOf(LocalUriHandler provides urlLauncher),
) {
Box(