mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
Migrate BrightDisplayActivity to compose
This commit is contained in:
@ -1,23 +1,31 @@
|
||||
package com.simplemobiletools.flashlight.activities
|
||||
|
||||
import android.app.Application
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
import android.widget.RelativeLayout
|
||||
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.extensions.*
|
||||
import com.simplemobiletools.flashlight.databinding.ActivityBrightDisplayBinding
|
||||
import com.simplemobiletools.commons.extensions.getFormattedDuration
|
||||
import com.simplemobiletools.flashlight.extensions.config
|
||||
import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown
|
||||
import com.simplemobiletools.flashlight.models.Events
|
||||
import com.simplemobiletools.flashlight.screens.BrightDisplayScreen
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import org.greenrobot.eventbus.ThreadMode
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class BrightDisplayActivity : SimpleActivity() {
|
||||
private val binding by viewBinding(ActivityBrightDisplayBinding::inflate)
|
||||
private val viewModel by viewModels<BrightDisplayViewModel>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
window.addFlags(
|
||||
@ -29,29 +37,35 @@ class BrightDisplayActivity : SimpleActivity() {
|
||||
|
||||
useDynamicTheme = false
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(binding.root)
|
||||
supportActionBar?.hide()
|
||||
setBackgroundColor(config.brightDisplayColor)
|
||||
enableEdgeToEdgeSimple()
|
||||
setContent {
|
||||
AppThemeSurface {
|
||||
val backgroundColor by viewModel.backgroundColor.collectAsStateWithLifecycle()
|
||||
val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle()
|
||||
val timerText by viewModel.timerText.collectAsStateWithLifecycle()
|
||||
|
||||
binding.brightDisplayChangeColor.setOnClickListener {
|
||||
ColorPickerDialog(this, config.brightDisplayColor, true, currentColorCallback = {
|
||||
setBackgroundColor(it)
|
||||
}) { wasPositivePressed, color ->
|
||||
if (wasPositivePressed) {
|
||||
config.brightDisplayColor = color
|
||||
|
||||
val contrastColor = color.getContrastColor()
|
||||
binding.brightDisplayChangeColor.apply {
|
||||
setTextColor(contrastColor)
|
||||
background.applyColorFilter(contrastColor)
|
||||
BrightDisplayScreen(
|
||||
backgroundColor = backgroundColor,
|
||||
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)
|
||||
}
|
||||
}
|
||||
},
|
||||
timerVisible = timerVisible,
|
||||
timerText = timerText,
|
||||
onTimerCloseClick = {
|
||||
stopSleepTimer()
|
||||
}
|
||||
} else {
|
||||
setBackgroundColor(config.brightDisplayColor)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
binding.sleepTimerStop.setOnClickListener { stopSleepTimer() }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
@ -59,8 +73,6 @@ class BrightDisplayActivity : SimpleActivity() {
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
toggleBrightness(true)
|
||||
requestedOrientation = if (config.forcePortraitMode) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_SENSOR
|
||||
|
||||
(binding.sleepTimerHolder.layoutParams as RelativeLayout.LayoutParams).bottomMargin = navigationBarHeight
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@ -69,28 +81,6 @@ class BrightDisplayActivity : SimpleActivity() {
|
||||
toggleBrightness(false)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
private fun setBackgroundColor(color: Int) {
|
||||
binding.apply {
|
||||
brightDisplay.background = ColorDrawable(color)
|
||||
|
||||
val contrastColor = config.brightDisplayColor.getContrastColor()
|
||||
brightDisplayChangeColor.apply {
|
||||
setTextColor(contrastColor)
|
||||
background.applyColorFilter(contrastColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun toggleBrightness(increase: Boolean) {
|
||||
val layout = window.attributes
|
||||
layout.screenBrightness = (if (increase) 1 else 0).toFloat()
|
||||
@ -98,17 +88,50 @@ class BrightDisplayActivity : SimpleActivity() {
|
||||
}
|
||||
|
||||
private fun stopSleepTimer() {
|
||||
binding.sleepTimerHolder.fadeOut()
|
||||
viewModel.hideTimer()
|
||||
stopSleepTimerCountDown()
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
|
||||
binding.sleepTimerValue.text = event.seconds.getFormattedDuration()
|
||||
binding.sleepTimerHolder.beVisible()
|
||||
|
||||
if (event.seconds == 0) {
|
||||
exitProcess(0)
|
||||
internal class BrightDisplayViewModel(
|
||||
application: Application
|
||||
) : AndroidViewModel(application) {
|
||||
|
||||
|
||||
private val _timerText: MutableStateFlow<String> = MutableStateFlow("00:00")
|
||||
val timerText = _timerText.asStateFlow()
|
||||
|
||||
private val _timerVisible: MutableStateFlow<Boolean> = MutableStateFlow(false)
|
||||
val timerVisible = _timerVisible.asStateFlow()
|
||||
|
||||
private val _backgroundColor: MutableStateFlow<Int> = MutableStateFlow(application.config.backgroundColor)
|
||||
val backgroundColor = _backgroundColor.asStateFlow()
|
||||
|
||||
init {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
|
||||
_timerText.value = event.seconds.getFormattedDuration()
|
||||
_timerVisible.value = true
|
||||
|
||||
if (event.seconds == 0) {
|
||||
exitProcess(0)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateBackgroundColor(color: Int) {
|
||||
_backgroundColor.value = color
|
||||
}
|
||||
|
||||
fun hideTimer() {
|
||||
_timerVisible.value = false
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user