mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-04-24 15:08:52 +02:00
Use derived state for contrastColor in BrightDisplayActivity
This commit is contained in:
parent
10fa019813
commit
2e1821320f
@ -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)
|
||||||
|
@ -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 = {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user