refactor(common): move ColorScheme logic into LobstersTheme

This commit is contained in:
Harsh Shandilya 2022-09-30 11:04:03 +05:30
parent 388e9ac0bd
commit 9ee4d3b344
No known key found for this signature in database
3 changed files with 22 additions and 30 deletions

View file

@ -74,12 +74,12 @@ fun LobstersApp(
val savedPosts by viewModel.savedPosts.collectAsState(emptyMap()) val savedPosts by viewModel.savedPosts.collectAsState(emptyMap())
LobstersTheme( LobstersTheme(
dynamicColor = true,
providedValues = providedValues =
arrayOf( arrayOf(
LocalUriHandler provides urlLauncher, LocalUriHandler provides urlLauncher,
LocalHTMLConverter provides htmlConverter, LocalHTMLConverter provides htmlConverter,
), ),
colorScheme = decideColorScheme(LocalContext.current),
) { ) {
val currentUiMode = LocalConfiguration.current.uiMode val currentUiMode = LocalConfiguration.current.uiMode
val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation() val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation()

View file

@ -2,20 +2,13 @@ package dev.msfjarvis.claw.android.ui
import android.content.Context import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.os.Build
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
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 androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.navigation.NavController import androidx.navigation.NavController
import dev.msfjarvis.claw.android.ui.navigation.Destinations import dev.msfjarvis.claw.android.ui.navigation.Destinations
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
import dev.msfjarvis.claw.common.posts.PostActions import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.theme.DarkThemeColors
import dev.msfjarvis.claw.common.theme.LightThemeColors
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.database.local.SavedPost import dev.msfjarvis.claw.database.local.SavedPost
import java.time.LocalDateTime import java.time.LocalDateTime
@ -37,24 +30,6 @@ fun String.toLocalDateTime(): LocalDateTime {
return LocalDateTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(this)) return LocalDateTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(this))
} }
@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
}
}
}
@Composable @Composable
fun rememberPostActions( fun rememberPostActions(
urlLauncher: UrlLauncher, urlLauncher: UrlLauncher,

View file

@ -1,15 +1,19 @@
package dev.msfjarvis.claw.common.theme package dev.msfjarvis.claw.common.theme
import androidx.compose.material3.ColorScheme import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidedValue import androidx.compose.runtime.ProvidedValue
import androidx.compose.ui.platform.LocalContext
import com.halilibo.richtext.ui.material3.SetupMaterial3RichText import com.halilibo.richtext.ui.material3.SetupMaterial3RichText
val LightThemeColors = internal val LightThemeColors =
lightColorScheme( lightColorScheme(
primary = md_theme_light_primary, primary = md_theme_light_primary,
onPrimary = md_theme_light_onPrimary, onPrimary = md_theme_light_onPrimary,
@ -38,7 +42,7 @@ val LightThemeColors =
inverseSurface = md_theme_light_inverseSurface, inverseSurface = md_theme_light_inverseSurface,
) )
val DarkThemeColors = internal val DarkThemeColors =
darkColorScheme( darkColorScheme(
primary = md_theme_dark_primary, primary = md_theme_dark_primary,
onPrimary = md_theme_dark_onPrimary, onPrimary = md_theme_dark_onPrimary,
@ -69,10 +73,23 @@ val DarkThemeColors =
@Composable @Composable
fun LobstersTheme( fun LobstersTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
dynamicColor: Boolean = false,
providedValues: Array<ProvidedValue<*>> = emptyArray(), providedValues: Array<ProvidedValue<*>> = emptyArray(),
colorScheme: ColorScheme,
content: @Composable () -> Unit, content: @Composable () -> Unit,
) { ) {
val colorScheme =
when {
dynamicColor && Build.VERSION.SDK_INT >= 31 -> {
val context = LocalContext.current
if (darkTheme) {
dynamicDarkColorScheme(context)
} else {
dynamicLightColorScheme(context)
}
}
else -> if (darkTheme) DarkThemeColors else LightThemeColors
}
CompositionLocalProvider(*providedValues) { CompositionLocalProvider(*providedValues) {
MaterialTheme(colorScheme = colorScheme, typography = AppTypography) { MaterialTheme(colorScheme = colorScheme, typography = AppTypography) {
SetupMaterial3RichText { content() } SetupMaterial3RichText { content() }