From 2e1821320fab631faa0aea212475ea6a866dbc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 2 Oct 2023 16:53:39 +0200 Subject: [PATCH] Use derived state for contrastColor in BrightDisplayActivity --- .../flashlight/activities/BrightDisplayActivity.kt | 5 +++++ .../flashlight/screens/BrightDisplayScreen.kt | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt index 419bf53..328f5c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt @@ -7,12 +7,15 @@ import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.dialogs.ColorPickerDialog +import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.getFormattedDuration import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown @@ -41,11 +44,13 @@ class BrightDisplayActivity : ComponentActivity() { setContent { AppThemeSurface { val backgroundColor by viewModel.backgroundColor.collectAsStateWithLifecycle() + val contrastColor by remember { derivedStateOf { backgroundColor.getContrastColor() } } val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle() val timerText by viewModel.timerText.collectAsStateWithLifecycle() BrightDisplayScreen( backgroundColor = backgroundColor, + contrastColor = contrastColor, onChangeColorPress = { ColorPickerDialog(this, config.brightDisplayColor, true, currentColorCallback = { viewModel.updateBackgroundColor(it) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/screens/BrightDisplayScreen.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/screens/BrightDisplayScreen.kt index 5dbf258..b7d2992 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/screens/BrightDisplayScreen.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/screens/BrightDisplayScreen.kt @@ -28,6 +28,7 @@ import com.simplemobiletools.flashlight.views.SleepTimer @Composable internal fun BrightDisplayScreen( backgroundColor: Int, + contrastColor: Int, timerText: String, timerVisible: Boolean, onChangeColorPress: () -> Unit, @@ -44,14 +45,14 @@ internal fun BrightDisplayScreen( .align(Alignment.Center) .border( width = 1.dp, - color = Color(backgroundColor.getContrastColor()), + color = Color(contrastColor), shape = MaterialTheme.shapes.extraLarge ), onClick = onChangeColorPress ) { Text( text = stringResource(id = R.string.change_color), - color = Color(backgroundColor.getContrastColor()) + color = Color(contrastColor) ) } @@ -77,6 +78,7 @@ private fun BrightDisplayScreenPreview() { AppThemeSurface { BrightDisplayScreen( backgroundColor = MaterialTheme.colorScheme.background.toArgb(), + contrastColor = MaterialTheme.colorScheme.background.toArgb().getContrastColor(), timerText = "00:00", timerVisible = true, onChangeColorPress = {},