feat: map all color schemes correctly

upgrade kotlin to 1.8.22 and latest compose versioning
This commit is contained in:
FunkyMuse 2023-06-30 14:16:35 +02:00
parent 9edf16cf19
commit 3b72df5844
5 changed files with 39 additions and 79 deletions

View File

@ -30,8 +30,7 @@ class SettingsActivity : AppCompatActivity() {
setContent { setContent {
AppThemeSurface { AppThemeSurface {
SettingsScreen( SettingsScreen(
customizeColors = ::handleCustomizeColorsClick, goBack = ::finish, customizeColors = ::handleCustomizeColorsClick, goBack = ::finish
backgroundColor = getProperBackgroundColor(),
) )
} }
} }

View File

@ -27,14 +27,12 @@ import com.simplemobiletools.commons.R
fun SettingsScreen( fun SettingsScreen(
goBack: () -> Unit, goBack: () -> Unit,
customizeColors: () -> Unit, customizeColors: () -> Unit,
backgroundColor: Int,
topBarsScrolledContainerColor: Color = MaterialTheme.colorScheme.primary topBarsScrolledContainerColor: Color = MaterialTheme.colorScheme.primary
) { ) {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState()) val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
Scaffold( Scaffold(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color(backgroundColor))
.nestedScroll(scrollBehavior.nestedScrollConnection), .nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = { topBar = {
TopAppBar( TopAppBar(
@ -91,5 +89,5 @@ fun SettingsScreen(
@MyDevices @MyDevices
@Composable @Composable
private fun SettingsScreenPreview() { private fun SettingsScreenPreview() {
AppThemeSurface { SettingsScreen(goBack = {}, customizeColors = {}, backgroundColor = MaterialTheme.colorScheme.background.toArgb()) } AppThemeSurface { SettingsScreen(goBack = {}, customizeColors = {}) }
} }

View File

@ -4,15 +4,14 @@ import android.content.Context
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.toArgb
import com.simplemobiletools.calculator.extensions.config import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme
import com.simplemobiletools.commons.extensions.isWhiteTheme import com.simplemobiletools.commons.extensions.isWhiteTheme
import com.simplemobiletools.commons.helpers.DARK_GREY
fun getTheme(showTransparentTop: Boolean = false, context: Context, materialYouTheme: Theme.SystemDefaultMaterialYou): Theme { fun getTheme(context: Context, materialYouTheme: Theme.SystemDefaultMaterialYou): Theme {
val baseConfig = context.config val baseConfig = context.config
val primaryColorInt = baseConfig.primaryColor val primaryColorInt = baseConfig.primaryColor
val isSystemInDarkTheme = context.isDarkMode() val isSystemInDarkTheme = context.isDarkMode()
val accentColor = baseConfig.accentColor
val backgroundColorTheme = if (baseConfig.isUsingSystemTheme || baseConfig.isUsingAutoTheme) { val backgroundColorTheme = if (baseConfig.isUsingSystemTheme || baseConfig.isUsingAutoTheme) {
@ -27,57 +26,21 @@ fun getTheme(showTransparentTop: Boolean = false, context: Context, materialYouT
val theme = when { val theme = when {
baseConfig.isUsingSystemTheme -> materialYouTheme baseConfig.isUsingSystemTheme -> materialYouTheme
context.isBlackAndWhiteTheme() -> when {//todo fix context.isBlackAndWhiteTheme() -> Theme.BlackAndWhite(
showTransparentTop -> Theme.BlackAndWhite( accentColor = accentColor,
accentColor = md_grey_black.toArgb(), primaryColorInt = primaryColorInt,
primaryColorInt = primaryColorInt, backgroundColorInt = backgroundColor,
backgroundColorInt = backgroundColor, appIconColorInt = appIconColor,
appIconColorInt = appIconColor, textColorInt = textColor
textColorInt = textColor )
)
baseConfig.primaryColor.getContrastColor() == DARK_GREY -> Theme.BlackAndWhite( context.isWhiteTheme() -> Theme.White(
accentColor = md_grey_black.toArgb(), accentColor = accentColor,
primaryColorInt = primaryColorInt, primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor, backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor, appIconColorInt = appIconColor,
textColorInt = theme_dark_background_color.toArgb() textColorInt = textColor
) )
else -> Theme.BlackAndWhite(
accentColor = md_grey_black.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = textColor
)
}
context.isWhiteTheme() -> when {//todo fix
showTransparentTop -> Theme.White(
accentColor = md_grey_white.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = theme_dark_background_color.toArgb()
)
baseConfig.primaryColor.getContrastColor() == android.graphics.Color.WHITE -> Theme.White(
accentColor = md_grey_white.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = theme_light_background_color.toArgb()
)
else -> Theme.White(
accentColor = md_grey_white.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = theme_dark_background_color.toArgb()
)
}
else -> { else -> {
val customPrimaryColor = when (primaryColorInt) { val customPrimaryColor = when (primaryColorInt) {

View File

@ -17,7 +17,6 @@ import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.graphics.toColor
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
@ -30,7 +29,6 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.APP_ICON_IDS import com.simplemobiletools.commons.helpers.APP_ICON_IDS
import com.simplemobiletools.commons.helpers.APP_LAUNCHER_NAME import com.simplemobiletools.commons.helpers.APP_LAUNCHER_NAME
import com.simplemobiletools.commons.helpers.DARK_GREY
import com.simplemobiletools.commons.helpers.HIGHER_ALPHA import com.simplemobiletools.commons.helpers.HIGHER_ALPHA
private val DarkColorScheme = darkColorScheme( private val DarkColorScheme = darkColorScheme(
@ -40,11 +38,11 @@ private val DarkColorScheme = darkColorScheme(
) )
@get:ReadOnlyComposable @get:ReadOnlyComposable
val disabledTextColor @Composable get() = if (isSystemInDarkTheme() || MaterialTheme.colorScheme.background.luminance() < 0.5) Color.DarkGray else Color.LightGray val disabledTextColor @Composable get() = if (isInDarkThemeAndSurfaceIsNotLitWell()) Color.DarkGray else Color.LightGray
@get:ReadOnlyComposable @get:ReadOnlyComposable
val textSubTitleColor val textSubTitleColor
@Composable get() = if (isSystemInDarkTheme() || MaterialTheme.colorScheme.background.luminance() < 0.5) { @Composable get() = if (isInDarkThemeAndSurfaceIsNotLitWell()) {
Color.White.copy(0.5f) Color.White.copy(0.5f)
} else { } else {
Color.Black.copy( Color.Black.copy(
@ -60,7 +58,7 @@ fun preferenceSummaryColor(isEnabled: Boolean) =
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
fun preferenceTitleColor(isEnabled: Boolean) = if (isEnabled) LocalContentColor.current else disabledTextColor fun preferenceTitleColor(isEnabled: Boolean) = if (isEnabled) MaterialTheme.colorScheme.onSurface else disabledTextColor
interface CommonTheme { interface CommonTheme {
val primaryColorInt: Int val primaryColorInt: Int
@ -120,11 +118,15 @@ sealed class Theme : CommonTheme {
appIconColorInt = LocalContext.current.config.appIconColor, appIconColorInt = LocalContext.current.config.appIconColor,
primaryColorInt = LocalContext.current.config.primaryColor, primaryColorInt = LocalContext.current.config.primaryColor,
backgroundColorInt = LocalContext.current.config.backgroundColor, backgroundColorInt = LocalContext.current.config.backgroundColor,
textColorInt = (if (isSystemInDarkTheme() || MaterialTheme.colorScheme.background.luminance() < 0.5) Color.White else Color.Black).toArgb() textColorInt = (if (isInDarkThemeAndSurfaceIsNotLitWell()) Color.White else Color.Black).toArgb()
) )
} }
} }
@Composable
@ReadOnlyComposable
private fun isInDarkThemeAndSurfaceIsNotLitWell() = isSystemInDarkTheme() || MaterialTheme.colorScheme.surface.luminance() < 0.5
@Composable @Composable
fun Theme( fun Theme(
@ -147,15 +149,17 @@ fun Theme(
onSurface = theme.textColor onSurface = theme.textColor
) )
theme is Theme.White -> lightColorScheme( theme is Theme.White -> darkColorScheme(
primary = theme.primaryColor, primary = Color(theme.accentColor),
surface = theme.backgroundColor, surface = theme.backgroundColor,
tertiary = Color(theme.accentColor), tertiary = theme.primaryColor,
onSurface = theme.textColor onSurface = theme.textColor
) )
theme is Theme.BlackAndWhite -> darkColorScheme( theme is Theme.BlackAndWhite -> darkColorScheme(
primary = theme.primaryColor, surface = theme.backgroundColor, tertiary = Color(theme.accentColor), primary = Color(theme.accentColor),
surface = theme.backgroundColor,
tertiary = theme.primaryColor,
onSurface = theme.textColor onSurface = theme.textColor
) )
@ -240,23 +244,19 @@ fun AppThemeSurface(
val context = LocalContext.current val context = LocalContext.current
val materialYouTheme = systemDefaultMaterialYou() val materialYouTheme = systemDefaultMaterialYou()
var currentTheme by remember { mutableStateOf(getTheme(context = context, materialYouTheme = materialYouTheme)) } var currentTheme by remember { mutableStateOf(getTheme(context = context, materialYouTheme = materialYouTheme)) }
Log.d("Current theme", currentTheme.toString()) OnLifecycleEvent { event ->
OnLifecycleEvent { if (event == Lifecycle.Event.ON_RESUME) {
if (it == Lifecycle.Event.ON_RESUME) {
currentTheme = getTheme(context = context, materialYouTheme = materialYouTheme) currentTheme = getTheme(context = context, materialYouTheme = materialYouTheme)
} }
} }
Theme(theme = currentTheme) { Theme(theme = currentTheme) {
Surface( Surface(modifier = modifier.fillMaxSize()) {
modifier = modifier
.fillMaxSize()
) {
content() content()
} }
} }
} }
fun Context.isDarkMode(): Boolean { internal fun Context.isDarkMode(): Boolean {
val darkModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK val darkModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return darkModeFlag == Configuration.UI_MODE_NIGHT_YES return darkModeFlag == Configuration.UI_MODE_NIGHT_YES
} }

View File

@ -1,15 +1,15 @@
[versions] [versions]
#jetbrains #jetbrains
kotlin = "1.8.21" kotlin = "1.8.22"
#KSP #KSP
ksp = "1.8.21-1.0.11" ksp = "1.8.22-1.0.11"
#Androidx #Androidx
androidx-customView = "1.2.0-alpha02" androidx-customView = "1.2.0-alpha02"
androidx-customViewPooling = "1.0.0" androidx-customViewPooling = "1.0.0"
#Compose #Compose
composeActivity = "1.8.0-alpha06" composeActivity = "1.8.0-alpha06"
compose = "1.6.0-alpha01" compose = "1.6.0-alpha01"
composeCompiler = "1.4.7" composeCompiler = "1.4.8"
composeMaterial3 = "1.2.0-alpha03" composeMaterial3 = "1.2.0-alpha03"
accompanist = "0.31.4-beta" accompanist = "0.31.4-beta"
#AutoFitTextView #AutoFitTextView