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 {
AppThemeSurface {
SettingsScreen(
customizeColors = ::handleCustomizeColorsClick, goBack = ::finish,
backgroundColor = getProperBackgroundColor(),
customizeColors = ::handleCustomizeColorsClick, goBack = ::finish
)
}
}

View File

@ -27,14 +27,12 @@ import com.simplemobiletools.commons.R
fun SettingsScreen(
goBack: () -> Unit,
customizeColors: () -> Unit,
backgroundColor: Int,
topBarsScrolledContainerColor: Color = MaterialTheme.colorScheme.primary
) {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
Scaffold(
modifier = Modifier
.fillMaxSize()
.background(Color(backgroundColor))
.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopAppBar(
@ -91,5 +89,5 @@ fun SettingsScreen(
@MyDevices
@Composable
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.toArgb
import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme
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 primaryColorInt = baseConfig.primaryColor
val isSystemInDarkTheme = context.isDarkMode()
val accentColor = baseConfig.accentColor
val backgroundColorTheme = if (baseConfig.isUsingSystemTheme || baseConfig.isUsingAutoTheme) {
@ -27,57 +26,21 @@ fun getTheme(showTransparentTop: Boolean = false, context: Context, materialYouT
val theme = when {
baseConfig.isUsingSystemTheme -> materialYouTheme
context.isBlackAndWhiteTheme() -> when {//todo fix
showTransparentTop -> Theme.BlackAndWhite(
accentColor = md_grey_black.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = textColor
)
context.isBlackAndWhiteTheme() -> Theme.BlackAndWhite(
accentColor = accentColor,
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = textColor
)
baseConfig.primaryColor.getContrastColor() == DARK_GREY -> Theme.BlackAndWhite(
accentColor = md_grey_black.toArgb(),
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = theme_dark_background_color.toArgb()
)
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()
)
}
context.isWhiteTheme() -> Theme.White(
accentColor = accentColor,
primaryColorInt = primaryColorInt,
backgroundColorInt = backgroundColor,
appIconColorInt = appIconColor,
textColorInt = textColor
)
else -> {
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.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.graphics.toColor
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
@ -30,7 +29,6 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.APP_ICON_IDS
import com.simplemobiletools.commons.helpers.APP_LAUNCHER_NAME
import com.simplemobiletools.commons.helpers.DARK_GREY
import com.simplemobiletools.commons.helpers.HIGHER_ALPHA
private val DarkColorScheme = darkColorScheme(
@ -40,11 +38,11 @@ private val DarkColorScheme = darkColorScheme(
)
@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
val textSubTitleColor
@Composable get() = if (isSystemInDarkTheme() || MaterialTheme.colorScheme.background.luminance() < 0.5) {
@Composable get() = if (isInDarkThemeAndSurfaceIsNotLitWell()) {
Color.White.copy(0.5f)
} else {
Color.Black.copy(
@ -60,7 +58,7 @@ fun preferenceSummaryColor(isEnabled: Boolean) =
@Composable
@ReadOnlyComposable
fun preferenceTitleColor(isEnabled: Boolean) = if (isEnabled) LocalContentColor.current else disabledTextColor
fun preferenceTitleColor(isEnabled: Boolean) = if (isEnabled) MaterialTheme.colorScheme.onSurface else disabledTextColor
interface CommonTheme {
val primaryColorInt: Int
@ -120,11 +118,15 @@ sealed class Theme : CommonTheme {
appIconColorInt = LocalContext.current.config.appIconColor,
primaryColorInt = LocalContext.current.config.primaryColor,
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
fun Theme(
@ -147,15 +149,17 @@ fun Theme(
onSurface = theme.textColor
)
theme is Theme.White -> lightColorScheme(
primary = theme.primaryColor,
theme is Theme.White -> darkColorScheme(
primary = Color(theme.accentColor),
surface = theme.backgroundColor,
tertiary = Color(theme.accentColor),
tertiary = theme.primaryColor,
onSurface = theme.textColor
)
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
)
@ -240,23 +244,19 @@ fun AppThemeSurface(
val context = LocalContext.current
val materialYouTheme = systemDefaultMaterialYou()
var currentTheme by remember { mutableStateOf(getTheme(context = context, materialYouTheme = materialYouTheme)) }
Log.d("Current theme", currentTheme.toString())
OnLifecycleEvent {
if (it == Lifecycle.Event.ON_RESUME) {
OnLifecycleEvent { event ->
if (event == Lifecycle.Event.ON_RESUME) {
currentTheme = getTheme(context = context, materialYouTheme = materialYouTheme)
}
}
Theme(theme = currentTheme) {
Surface(
modifier = modifier
.fillMaxSize()
) {
Surface(modifier = modifier.fillMaxSize()) {
content()
}
}
}
fun Context.isDarkMode(): Boolean {
internal fun Context.isDarkMode(): Boolean {
val darkModeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return darkModeFlag == Configuration.UI_MODE_NIGHT_YES
}

View File

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