Use derived state for contrastColor in BrightDisplayActivity

This commit is contained in:
Ensar Sarajčić 2023-10-02 16:53:39 +02:00
parent 10fa019813
commit 2e1821320f
2 changed files with 9 additions and 2 deletions

View File

@ -7,12 +7,15 @@ import android.view.WindowManager
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple
import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.compose.theme.AppThemeSurface
import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.getFormattedDuration import com.simplemobiletools.commons.extensions.getFormattedDuration
import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown
@ -41,11 +44,13 @@ class BrightDisplayActivity : ComponentActivity() {
setContent { setContent {
AppThemeSurface { AppThemeSurface {
val backgroundColor by viewModel.backgroundColor.collectAsStateWithLifecycle() val backgroundColor by viewModel.backgroundColor.collectAsStateWithLifecycle()
val contrastColor by remember { derivedStateOf { backgroundColor.getContrastColor() } }
val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle() val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle()
val timerText by viewModel.timerText.collectAsStateWithLifecycle() val timerText by viewModel.timerText.collectAsStateWithLifecycle()
BrightDisplayScreen( BrightDisplayScreen(
backgroundColor = backgroundColor, backgroundColor = backgroundColor,
contrastColor = contrastColor,
onChangeColorPress = { onChangeColorPress = {
ColorPickerDialog(this, config.brightDisplayColor, true, currentColorCallback = { ColorPickerDialog(this, config.brightDisplayColor, true, currentColorCallback = {
viewModel.updateBackgroundColor(it) viewModel.updateBackgroundColor(it)

View File

@ -28,6 +28,7 @@ import com.simplemobiletools.flashlight.views.SleepTimer
@Composable @Composable
internal fun BrightDisplayScreen( internal fun BrightDisplayScreen(
backgroundColor: Int, backgroundColor: Int,
contrastColor: Int,
timerText: String, timerText: String,
timerVisible: Boolean, timerVisible: Boolean,
onChangeColorPress: () -> Unit, onChangeColorPress: () -> Unit,
@ -44,14 +45,14 @@ internal fun BrightDisplayScreen(
.align(Alignment.Center) .align(Alignment.Center)
.border( .border(
width = 1.dp, width = 1.dp,
color = Color(backgroundColor.getContrastColor()), color = Color(contrastColor),
shape = MaterialTheme.shapes.extraLarge shape = MaterialTheme.shapes.extraLarge
), ),
onClick = onChangeColorPress onClick = onChangeColorPress
) { ) {
Text( Text(
text = stringResource(id = R.string.change_color), text = stringResource(id = R.string.change_color),
color = Color(backgroundColor.getContrastColor()) color = Color(contrastColor)
) )
} }
@ -77,6 +78,7 @@ private fun BrightDisplayScreenPreview() {
AppThemeSurface { AppThemeSurface {
BrightDisplayScreen( BrightDisplayScreen(
backgroundColor = MaterialTheme.colorScheme.background.toArgb(), backgroundColor = MaterialTheme.colorScheme.background.toArgb(),
contrastColor = MaterialTheme.colorScheme.background.toArgb().getContrastColor(),
timerText = "00:00", timerText = "00:00",
timerVisible = true, timerVisible = true,
onChangeColorPress = {}, onChangeColorPress = {},