Migrate color picker to compose
This commit is contained in:
parent
c354d115ee
commit
59537d28bd
|
@ -2,20 +2,24 @@ package com.simplemobiletools.flashlight.activities
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.pm.ActivityInfo
|
import android.content.pm.ActivityInfo
|
||||||
|
import android.graphics.Color
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.WindowManager
|
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.Composable
|
||||||
import androidx.compose.runtime.derivedStateOf
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.lifecycle.viewModelScope
|
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.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.ColorPickerAlertDialog
|
||||||
import com.simplemobiletools.commons.extensions.getContrastColor
|
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
|
||||||
|
@ -31,6 +35,7 @@ import kotlin.system.exitProcess
|
||||||
|
|
||||||
class BrightDisplayActivity : ComponentActivity() {
|
class BrightDisplayActivity : ComponentActivity() {
|
||||||
private val viewModel by viewModels<BrightDisplayViewModel>()
|
private val viewModel by viewModels<BrightDisplayViewModel>()
|
||||||
|
private val preferences by lazy { config }
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
window.addFlags(
|
window.addFlags(
|
||||||
|
@ -44,34 +49,56 @@ class BrightDisplayActivity : ComponentActivity() {
|
||||||
enableEdgeToEdgeSimple()
|
enableEdgeToEdgeSimple()
|
||||||
setContent {
|
setContent {
|
||||||
AppThemeSurface {
|
AppThemeSurface {
|
||||||
val backgroundColor by viewModel.backgroundColor.collectAsStateWithLifecycle()
|
val colorPickerDialogState = rememberAlertDialogState().apply {
|
||||||
val contrastColor by remember { derivedStateOf { backgroundColor.getContrastColor() } }
|
ColorPicker()
|
||||||
val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle()
|
}
|
||||||
val timerText by viewModel.timerText.collectAsStateWithLifecycle()
|
|
||||||
|
|
||||||
BrightDisplayScreen(
|
ScreenContent(colorPickerDialogState)
|
||||||
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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
|
|
@ -45,6 +45,8 @@ class Config(context: Context) : BaseConfig(context) {
|
||||||
get() = prefs.getInt(BRIGHT_DISPLAY_COLOR, Color.WHITE)
|
get() = prefs.getInt(BRIGHT_DISPLAY_COLOR, Color.WHITE)
|
||||||
set(brightDisplayColor) = prefs.edit().putInt(BRIGHT_DISPLAY_COLOR, brightDisplayColor).apply()
|
set(brightDisplayColor) = prefs.edit().putInt(BRIGHT_DISPLAY_COLOR, brightDisplayColor).apply()
|
||||||
|
|
||||||
|
val brightDisplayColorFlow = ::brightDisplayColor.asFlowNonNull()
|
||||||
|
|
||||||
var forcePortraitMode: Boolean
|
var forcePortraitMode: Boolean
|
||||||
get() = prefs.getBoolean(FORCE_PORTRAIT_MODE, true)
|
get() = prefs.getBoolean(FORCE_PORTRAIT_MODE, true)
|
||||||
set(forcePortraitMode) = prefs.edit().putBoolean(FORCE_PORTRAIT_MODE, forcePortraitMode).apply()
|
set(forcePortraitMode) = prefs.edit().putBoolean(FORCE_PORTRAIT_MODE, forcePortraitMode).apply()
|
||||||
|
|
Loading…
Reference in New Issue