mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-02-16 11:50:47 +01:00
Migrate WidgetBrightDisplayConfigureActivity to compose
This commit is contained in:
parent
68f4e32ce2
commit
0ff554a835
@ -1,50 +1,66 @@
|
||||
package com.simplemobiletools.flashlight.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Intent
|
||||
import android.content.res.ColorStateList
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.widget.SeekBar
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.viewModels
|
||||
import androidx.compose.runtime.getValue
|
||||
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.dialogs.FeatureLockedDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.extensions.isOrWasThankYouInstalled
|
||||
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
|
||||
import com.simplemobiletools.flashlight.R
|
||||
import com.simplemobiletools.flashlight.databinding.WidgetBrightDisplayConfigBinding
|
||||
import com.simplemobiletools.flashlight.extensions.config
|
||||
import com.simplemobiletools.flashlight.helpers.MyWidgetBrightDisplayProvider
|
||||
import com.simplemobiletools.flashlight.screens.WidgetBrightDisplayConfigureScreen
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
class WidgetBrightDisplayConfigureActivity : SimpleActivity() {
|
||||
private val binding by viewBinding(WidgetBrightDisplayConfigBinding::inflate)
|
||||
private val viewModel by viewModels<WidgetBrightDisplayConfigureViewModel>()
|
||||
|
||||
private var mWidgetAlpha = 0f
|
||||
private var mWidgetId = 0
|
||||
private var mWidgetColor = 0
|
||||
private var mWidgetColorWithoutTransparency = 0
|
||||
private var mFeatureLockedDialog: FeatureLockedDialog? = null
|
||||
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
useDynamicTheme = false
|
||||
super.onCreate(savedInstanceState)
|
||||
setResult(Activity.RESULT_CANCELED)
|
||||
setContentView(binding.root)
|
||||
initVariables()
|
||||
|
||||
val isCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false
|
||||
mWidgetId = intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
viewModel.setWidgetId(intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID)
|
||||
|
||||
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID && !isCustomizingColors) {
|
||||
if (viewModel.widgetId.value == AppWidgetManager.INVALID_APPWIDGET_ID && !isCustomizingColors) {
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.apply {
|
||||
configSave.setOnClickListener { saveConfig() }
|
||||
configWidgetColor.setOnClickListener { pickBackgroundColor() }
|
||||
enableEdgeToEdgeSimple()
|
||||
setContent {
|
||||
AppThemeSurface {
|
||||
val widgetColor by viewModel.widgetColor.collectAsStateWithLifecycle()
|
||||
val widgetAlpha by viewModel.widgetAlpha.collectAsStateWithLifecycle()
|
||||
|
||||
val primaryColor = getProperPrimaryColor()
|
||||
configWidgetSeekbar.setColors(getProperTextColor(), primaryColor, primaryColor)
|
||||
WidgetBrightDisplayConfigureScreen(
|
||||
widgetColor = widgetColor,
|
||||
widgetAlpha = widgetAlpha,
|
||||
onSliderChanged = {
|
||||
viewModel.changeAlpha(it)
|
||||
},
|
||||
onColorPressed = {
|
||||
pickBackgroundColor()
|
||||
},
|
||||
onSavePressed = {
|
||||
saveConfig()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isCustomizingColors && !isOrWasThankYouInstalled()) {
|
||||
@ -54,11 +70,6 @@ class WidgetBrightDisplayConfigureActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binding.configSave.apply {
|
||||
backgroundTintList = ColorStateList.valueOf(getProperPrimaryColor())
|
||||
setTextColor(getProperPrimaryColor().getContrastColor())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
@ -70,65 +81,65 @@ class WidgetBrightDisplayConfigureActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun initVariables() {
|
||||
mWidgetColor = config.widgetBgColor
|
||||
if (mWidgetColor == resources.getColor(R.color.default_widget_bg_color) && config.isUsingSystemTheme) {
|
||||
mWidgetColor = resources.getColor(R.color.you_primary_color, theme)
|
||||
}
|
||||
|
||||
mWidgetAlpha = Color.alpha(mWidgetColor) / 255.toFloat()
|
||||
|
||||
mWidgetColorWithoutTransparency = Color.rgb(Color.red(mWidgetColor), Color.green(mWidgetColor), Color.blue(mWidgetColor))
|
||||
binding.configWidgetSeekbar.apply {
|
||||
setOnSeekBarChangeListener(seekbarChangeListener)
|
||||
progress = (mWidgetAlpha * 100).toInt()
|
||||
}
|
||||
updateColors()
|
||||
}
|
||||
|
||||
private fun saveConfig() {
|
||||
config.widgetBgColor = mWidgetColor
|
||||
config.widgetBgColor = viewModel.widgetColor.value
|
||||
requestWidgetUpdate()
|
||||
|
||||
Intent().apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, viewModel.widgetId.value)
|
||||
setResult(Activity.RESULT_OK, this)
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
private fun pickBackgroundColor() {
|
||||
ColorPickerDialog(this, mWidgetColorWithoutTransparency) { wasPositivePressed, color ->
|
||||
ColorPickerDialog(this, viewModel.widgetColor.value) { wasPositivePressed, color ->
|
||||
if (wasPositivePressed) {
|
||||
mWidgetColorWithoutTransparency = color
|
||||
updateColors()
|
||||
viewModel.updateColor(color)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestWidgetUpdate() {
|
||||
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetBrightDisplayProvider::class.java).apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(viewModel.widgetId.value))
|
||||
sendBroadcast(this)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateColors() {
|
||||
mWidgetColor = mWidgetColorWithoutTransparency.adjustAlpha(mWidgetAlpha)
|
||||
binding.apply {
|
||||
configWidgetColor.setFillWithStroke(mWidgetColor, mWidgetColor)
|
||||
configImage.background.mutate().applyColorFilter(mWidgetColor)
|
||||
}
|
||||
}
|
||||
internal class WidgetBrightDisplayConfigureViewModel(
|
||||
application: Application
|
||||
) : AndroidViewModel(application) {
|
||||
|
||||
private val seekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
|
||||
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
||||
mWidgetAlpha = progress.toFloat() / 100.toFloat()
|
||||
updateColors()
|
||||
|
||||
private val _widgetAlpha = MutableStateFlow(0f)
|
||||
val widgetAlpha = _widgetAlpha.asStateFlow()
|
||||
|
||||
private val _widgetId = MutableStateFlow(0)
|
||||
val widgetId = _widgetId.asStateFlow()
|
||||
|
||||
private val _widgetColor = MutableStateFlow(0)
|
||||
val widgetColor = _widgetColor.asStateFlow()
|
||||
|
||||
fun changeAlpha(newAlpha: Float) {
|
||||
_widgetAlpha.value = newAlpha
|
||||
}
|
||||
|
||||
override fun onStartTrackingTouch(seekBar: SeekBar) {}
|
||||
fun updateColor(newColor: Int) {
|
||||
_widgetColor.value = newColor
|
||||
}
|
||||
|
||||
override fun onStopTrackingTouch(seekBar: SeekBar) {}
|
||||
fun setWidgetId(widgetId: Int) {
|
||||
_widgetId.value = widgetId
|
||||
}
|
||||
|
||||
init {
|
||||
_widgetColor.value = application.config.widgetBgColor
|
||||
if (_widgetColor.value == application.resources.getColor(R.color.default_widget_bg_color) && application.config.isUsingSystemTheme) {
|
||||
_widgetColor.value = application.resources.getColor(R.color.you_primary_color, application.theme)
|
||||
}
|
||||
|
||||
_widgetAlpha.value = Color.alpha(_widgetColor.value) / 255f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,115 @@
|
||||
package com.simplemobiletools.flashlight.screens
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.SolidColor
|
||||
import androidx.compose.ui.graphics.painter.BrushPainter
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.dimensionResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.constraintlayout.compose.ConstraintLayout
|
||||
import com.simplemobiletools.commons.compose.extensions.MyDevices
|
||||
import com.simplemobiletools.commons.compose.theme.AppThemeSurface
|
||||
import com.simplemobiletools.commons.extensions.adjustAlpha
|
||||
import com.simplemobiletools.flashlight.R
|
||||
|
||||
@Composable
|
||||
internal fun WidgetBrightDisplayConfigureScreen(
|
||||
widgetColor: Int,
|
||||
widgetAlpha: Float,
|
||||
onSliderChanged: (Float) -> Unit,
|
||||
onColorPressed: () -> Unit,
|
||||
onSavePressed: () -> Unit
|
||||
) {
|
||||
ConstraintLayout(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
val (brightDisplay, bottomControls, saveButton) = createRefs()
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.padding(dimensionResource(id = R.dimen.activity_margin))
|
||||
.padding(bottom = dimensionResource(id = R.dimen.activity_margin))
|
||||
.constrainAs(brightDisplay) {
|
||||
top.linkTo(parent.top)
|
||||
end.linkTo(parent.end)
|
||||
start.linkTo(parent.start)
|
||||
bottom.linkTo(bottomControls.top)
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.align(Alignment.Center)
|
||||
.size(dimensionResource(id = R.dimen.main_button_size)),
|
||||
painter = painterResource(id = R.drawable.ic_bright_display_vector),
|
||||
contentDescription = stringResource(id = R.string.bright_display),
|
||||
tint = Color(widgetColor.adjustAlpha(widgetAlpha))
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier.constrainAs(bottomControls) {
|
||||
bottom.linkTo(saveButton.top)
|
||||
start.linkTo(parent.start)
|
||||
end.linkTo(parent.end)
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
modifier = Modifier
|
||||
.size(dimensionResource(id = R.dimen.widget_colorpicker_size))
|
||||
.padding(dimensionResource(id = R.dimen.tiny_margin))
|
||||
.clip(CircleShape)
|
||||
.clickable { onColorPressed() },
|
||||
painter = BrushPainter(SolidColor(Color(widgetColor))),
|
||||
contentDescription = stringResource(id = R.string.bright_display),
|
||||
tint = Color(widgetColor)
|
||||
)
|
||||
|
||||
Slider(
|
||||
value = widgetAlpha,
|
||||
onValueChange = onSliderChanged,
|
||||
modifier = Modifier
|
||||
.padding(start = dimensionResource(id = R.dimen.medium_margin))
|
||||
.background(
|
||||
color = colorResource(id = R.color.md_grey_white),
|
||||
shape = MaterialTheme.shapes.extraLarge
|
||||
)
|
||||
.padding(horizontal = dimensionResource(id = R.dimen.activity_margin))
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
modifier = Modifier.constrainAs(saveButton) {
|
||||
end.linkTo(parent.end)
|
||||
bottom.linkTo(parent.bottom)
|
||||
},
|
||||
onClick = onSavePressed
|
||||
) {
|
||||
Text(text = stringResource(id = R.string.ok))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@MyDevices
|
||||
private fun WidgetBrightDisplayConfigureScreenPreview() {
|
||||
AppThemeSurface {
|
||||
WidgetBrightDisplayConfigureScreen(
|
||||
widgetColor = MaterialTheme.colorScheme.primary.toArgb(),
|
||||
widgetAlpha = 1f,
|
||||
onSliderChanged = {},
|
||||
onColorPressed = {},
|
||||
onSavePressed = {}
|
||||
)
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ composeActivity = "1.8.0-rc01"
|
||||
compose = "1.6.0-alpha06"
|
||||
composeCompiler = "1.5.3"
|
||||
composeMaterial3 = "1.2.0-alpha08"
|
||||
composeConstraintLayout = "1.0.1"
|
||||
#Gradle
|
||||
gradlePlugins-agp = "8.1.1"
|
||||
#build
|
||||
@ -44,6 +45,7 @@ simple-tools-commons = { module = "com.github.SimpleMobileTools:Simple-Commons",
|
||||
#Compose
|
||||
compose-compiler = { module = "androidx.compose.compiler:compiler", version.ref = "composeCompiler" }
|
||||
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
|
||||
compose-constraintlayout = { module = "androidx.constraintlayout:constraintlayout-compose", version.ref = "composeConstraintLayout" }
|
||||
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "composeMaterial3" }
|
||||
compose-material2 = { module = "androidx.compose.material:material", version.ref = "compose" }
|
||||
compose-material-icons = { module = "androidx.compose.material:material-icons-extended", version.ref = "compose" }
|
||||
@ -61,6 +63,7 @@ compose = [
|
||||
"compose-foundation",
|
||||
"compose-material-icons",
|
||||
"compose-material3",
|
||||
"compose-constraintlayout",
|
||||
"compose-runtime",
|
||||
"compose-ui",
|
||||
"compose-uiTooling-preview",
|
||||
|
Loading…
x
Reference in New Issue
Block a user