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.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)

View File

@ -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 = {},