From 59537d28bdb502d4ef704b3d074496fe6b1686e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 5 Oct 2023 10:34:06 +0200 Subject: [PATCH] Migrate color picker to compose --- .../activities/BrightDisplayActivity.kt | 75 +++++++++++++------ .../flashlight/helpers/Config.kt | 2 + 2 files changed, 53 insertions(+), 24 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 36dd1dc..75fb2f2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/BrightDisplayActivity.kt @@ -2,20 +2,24 @@ package com.simplemobiletools.flashlight.activities import android.app.Application import android.content.pm.ActivityInfo +import android.graphics.Color import android.os.Bundle import android.view.WindowManager import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.viewModels +import androidx.compose.runtime.Composable 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 androidx.lifecycle.viewModelScope +import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState +import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.theme.AppThemeSurface -import com.simplemobiletools.commons.dialogs.ColorPickerDialog +import com.simplemobiletools.commons.dialogs.ColorPickerAlertDialog import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.extensions.getFormattedDuration import com.simplemobiletools.flashlight.extensions.config @@ -31,6 +35,7 @@ import kotlin.system.exitProcess class BrightDisplayActivity : ComponentActivity() { private val viewModel by viewModels() + private val preferences by lazy { config } override fun onCreate(savedInstanceState: Bundle?) { window.addFlags( @@ -44,34 +49,56 @@ class BrightDisplayActivity : ComponentActivity() { enableEdgeToEdgeSimple() 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() + val colorPickerDialogState = rememberAlertDialogState().apply { + ColorPicker() + } - BrightDisplayScreen( - backgroundColor = backgroundColor, - contrastColor = contrastColor, - onChangeColorPress = { - ColorPickerDialog(this, config.brightDisplayColor, true, currentColorCallback = { - viewModel.updateBackgroundColor(it) - }) { wasPositivePressed, color -> - if (wasPositivePressed) { - config.brightDisplayColor = color - viewModel.updateBackgroundColor(color) - } else { - viewModel.updateBackgroundColor(config.brightDisplayColor) - } - } - }, - sleepTimer = { - AnimatedSleepTimer(timerText = timerText, timerVisible = timerVisible, onTimerClosePress = ::stopSleepTimer) - } - ) + ScreenContent(colorPickerDialogState) } } } + @Composable + private fun AlertDialogState.ColorPicker() { + val brightDisplayColor by preferences.brightDisplayColorFlow.collectAsStateWithLifecycle(Color.WHITE) + DialogMember { + ColorPickerAlertDialog( + alertDialogState = this, + color = brightDisplayColor, + removeDimmedBackground = true, + onActiveColorChange = { + viewModel.updateBackgroundColor(it) + }, + onButtonPressed = { wasPositivePressed, color -> + if (wasPositivePressed) { + config.brightDisplayColor = color + viewModel.updateBackgroundColor(color) + } else { + viewModel.updateBackgroundColor(config.brightDisplayColor) + } + } + ) + } + } + + @Composable + private fun ScreenContent(colorPickerDialogState: AlertDialogState) { + 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 = { + colorPickerDialogState.show() + }, + sleepTimer = { + AnimatedSleepTimer(timerText = timerText, timerVisible = timerVisible, onTimerClosePress = ::stopSleepTimer) + } + ) + } + override fun onResume() { super.onResume() window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt index c106ab5..81bf2ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt @@ -45,6 +45,8 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getInt(BRIGHT_DISPLAY_COLOR, Color.WHITE) set(brightDisplayColor) = prefs.edit().putInt(BRIGHT_DISPLAY_COLOR, brightDisplayColor).apply() + val brightDisplayColorFlow = ::brightDisplayColor.asFlowNonNull() + var forcePortraitMode: Boolean get() = prefs.getBoolean(FORCE_PORTRAIT_MODE, true) set(forcePortraitMode) = prefs.edit().putBoolean(FORCE_PORTRAIT_MODE, forcePortraitMode).apply()