mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 18:27:00 +05:30
refactor(common): move ColorScheme
logic into LobstersTheme
This commit is contained in:
parent
388e9ac0bd
commit
9ee4d3b344
3 changed files with 22 additions and 30 deletions
|
@ -74,12 +74,12 @@ fun LobstersApp(
|
|||
val savedPosts by viewModel.savedPosts.collectAsState(emptyMap())
|
||||
|
||||
LobstersTheme(
|
||||
dynamicColor = true,
|
||||
providedValues =
|
||||
arrayOf(
|
||||
LocalUriHandler provides urlLauncher,
|
||||
LocalHTMLConverter provides htmlConverter,
|
||||
),
|
||||
colorScheme = decideColorScheme(LocalContext.current),
|
||||
) {
|
||||
val currentUiMode = LocalConfiguration.current.uiMode
|
||||
val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation()
|
||||
|
|
|
@ -2,20 +2,13 @@ package dev.msfjarvis.claw.android.ui
|
|||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.os.Build
|
||||
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.remember
|
||||
import androidx.navigation.NavController
|
||||
import dev.msfjarvis.claw.android.ui.navigation.Destinations
|
||||
import dev.msfjarvis.claw.android.viewmodel.ClawViewModel
|
||||
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.database.local.SavedPost
|
||||
import java.time.LocalDateTime
|
||||
|
@ -37,24 +30,6 @@ fun String.toLocalDateTime(): LocalDateTime {
|
|||
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
|
||||
fun rememberPostActions(
|
||||
urlLauncher: UrlLauncher,
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
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.darkColorScheme
|
||||
import androidx.compose.material3.dynamicDarkColorScheme
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.ProvidedValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import com.halilibo.richtext.ui.material3.SetupMaterial3RichText
|
||||
|
||||
val LightThemeColors =
|
||||
internal val LightThemeColors =
|
||||
lightColorScheme(
|
||||
primary = md_theme_light_primary,
|
||||
onPrimary = md_theme_light_onPrimary,
|
||||
|
@ -38,7 +42,7 @@ val LightThemeColors =
|
|||
inverseSurface = md_theme_light_inverseSurface,
|
||||
)
|
||||
|
||||
val DarkThemeColors =
|
||||
internal val DarkThemeColors =
|
||||
darkColorScheme(
|
||||
primary = md_theme_dark_primary,
|
||||
onPrimary = md_theme_dark_onPrimary,
|
||||
|
@ -69,10 +73,23 @@ val DarkThemeColors =
|
|||
|
||||
@Composable
|
||||
fun LobstersTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
dynamicColor: Boolean = false,
|
||||
providedValues: Array<ProvidedValue<*>> = emptyArray(),
|
||||
colorScheme: ColorScheme,
|
||||
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) {
|
||||
MaterialTheme(colorScheme = colorScheme, typography = AppTypography) {
|
||||
SetupMaterial3RichText { content() }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue