mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-04-24 23:18:49 +02:00
Properly handle changing preferences values for main screen
This commit is contained in:
parent
b61288407b
commit
de2ce0ea73
@ -14,6 +14,7 @@ import androidx.activity.compose.setContent
|
|||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import com.google.android.material.math.MathUtils
|
import com.google.android.material.math.MathUtils
|
||||||
import com.simplemobiletools.commons.compose.extensions.onEventValue
|
import com.simplemobiletools.commons.compose.extensions.onEventValue
|
||||||
@ -46,6 +47,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
private const val MIN_STROBO_DELAY = 10L
|
private const val MIN_STROBO_DELAY = 10L
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val preferences by lazy { config }
|
||||||
private val viewModel by viewModels<MainViewModel>()
|
private val viewModel by viewModels<MainViewModel>()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -59,10 +61,16 @@ class MainActivity : SimpleActivity() {
|
|||||||
val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle(false)
|
val timerVisible by viewModel.timerVisible.collectAsStateWithLifecycle(false)
|
||||||
val timerText by viewModel.timerText.collectAsStateWithLifecycle()
|
val timerText by viewModel.timerText.collectAsStateWithLifecycle()
|
||||||
val flashlightActive by viewModel.flashlightOn.collectAsStateWithLifecycle()
|
val flashlightActive by viewModel.flashlightOn.collectAsStateWithLifecycle()
|
||||||
val showBrightDisplayButton by config.brightDisplayFlow.collectAsStateWithLifecycle(config.brightDisplay)
|
val showBrightDisplayButton by preferences.brightDisplayFlow.collectAsStateWithLifecycle(
|
||||||
val showSosButton by config.sosFlow.collectAsStateWithLifecycle(config.sos)
|
config.brightDisplay,
|
||||||
|
minActiveState = Lifecycle.State.CREATED
|
||||||
|
)
|
||||||
|
val showSosButton by preferences.sosFlow.collectAsStateWithLifecycle(config.sos, minActiveState = Lifecycle.State.CREATED)
|
||||||
val sosActive by viewModel.sosActive.collectAsStateWithLifecycle()
|
val sosActive by viewModel.sosActive.collectAsStateWithLifecycle()
|
||||||
val showStroboscopeButton by config.stroboscopeFlow.collectAsStateWithLifecycle(config.stroboscope)
|
val showStroboscopeButton by preferences.stroboscopeFlow.collectAsStateWithLifecycle(
|
||||||
|
config.stroboscope,
|
||||||
|
minActiveState = Lifecycle.State.CREATED
|
||||||
|
)
|
||||||
val stroboscopeActive by viewModel.stroboscopeActive.collectAsStateWithLifecycle()
|
val stroboscopeActive by viewModel.stroboscopeActive.collectAsStateWithLifecycle()
|
||||||
val brightnessBarVisible by viewModel.brightnessBarVisible.collectAsStateWithLifecycle(false)
|
val brightnessBarVisible by viewModel.brightnessBarVisible.collectAsStateWithLifecycle(false)
|
||||||
val brightnessBarValue by viewModel.brightnessBarValue.collectAsStateWithLifecycle()
|
val brightnessBarValue by viewModel.brightnessBarValue.collectAsStateWithLifecycle()
|
||||||
@ -118,16 +126,16 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
// binding.apply {
|
// binding.apply {
|
||||||
// changeIconColor(contrastColor, brightDisplayBtn)
|
// changeIconColor(contrastColor, brightDisplayBtn)
|
||||||
// brightDisplayBtn.beVisibleIf(config.brightDisplay)
|
// brightDisplayBtn.beVisibleIf(preferences.brightDisplay)
|
||||||
// sosBtn.beVisibleIf(config.sos)
|
// sosBtn.beVisibleIf(preferences.sos)
|
||||||
//
|
//
|
||||||
// if (sosBtn.currentTextColor != getProperPrimaryColor()) {
|
// if (sosBtn.currentTextColor != getProperPrimaryColor()) {
|
||||||
// sosBtn.setTextColor(contrastColor)
|
// sosBtn.setTextColor(contrastColor)
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// stroboscopeBtn.beVisibleIf(config.stroboscope)
|
// stroboscopeBtn.beVisibleIf(preferences.stroboscope)
|
||||||
//
|
//
|
||||||
// if (!config.stroboscope) {
|
// if (!preferences.stroboscope) {
|
||||||
// mCameraImpl!!.stopStroboscope()
|
// mCameraImpl!!.stopStroboscope()
|
||||||
// stroboscopeBar.beInvisible()
|
// stroboscopeBar.beInvisible()
|
||||||
// }
|
// }
|
||||||
@ -141,7 +149,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
// binding.sleepTimerHolder.background = ColorDrawable(getProperBackgroundColor())
|
// binding.sleepTimerHolder.background = ColorDrawable(getProperBackgroundColor())
|
||||||
// binding.sleepTimerStop.applyColorFilter(getProperTextColor())
|
// binding.sleepTimerStop.applyColorFilter(getProperTextColor())
|
||||||
|
|
||||||
requestedOrientation = if (config.forcePortraitMode) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_SENSOR
|
requestedOrientation = if (preferences.forcePortraitMode) ActivityInfo.SCREEN_ORIENTATION_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_SENSOR
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
|
|
||||||
checkShortcuts()
|
checkShortcuts()
|
||||||
@ -150,7 +158,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
|
|
||||||
if (config.sleepInTS == 0L) {
|
if (preferences.sleepInTS == 0L) {
|
||||||
viewModel.hideTimer()
|
viewModel.hideTimer()
|
||||||
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).cancel(getShutDownPendingIntent())
|
(getSystemService(Context.ALARM_SERVICE) as AlarmManager).cancel(getShutDownPendingIntent())
|
||||||
}
|
}
|
||||||
@ -216,14 +224,14 @@ class MainActivity : SimpleActivity() {
|
|||||||
RadioItem(it, secondsToString(it))
|
RadioItem(it, secondsToString(it))
|
||||||
})
|
})
|
||||||
|
|
||||||
if (items.none { it.id == config.lastSleepTimerSeconds }) {
|
if (items.none { it.id == preferences.lastSleepTimerSeconds }) {
|
||||||
items.add(RadioItem(config.lastSleepTimerSeconds, secondsToString(config.lastSleepTimerSeconds)))
|
items.add(RadioItem(preferences.lastSleepTimerSeconds, secondsToString(config.lastSleepTimerSeconds)))
|
||||||
}
|
}
|
||||||
|
|
||||||
items.sortBy { it.id }
|
items.sortBy { it.id }
|
||||||
items.add(RadioItem(-1, getString(R.string.custom)))
|
items.add(RadioItem(-1, getString(R.string.custom)))
|
||||||
|
|
||||||
RadioGroupDialog(this, items, config.lastSleepTimerSeconds) {
|
RadioGroupDialog(this, items, preferences.lastSleepTimerSeconds) {
|
||||||
if (it as Int == -1) {
|
if (it as Int == -1) {
|
||||||
SleepTimerCustomDialog(this) {
|
SleepTimerCustomDialog(this) {
|
||||||
if (it > 0) {
|
if (it > 0) {
|
||||||
@ -254,8 +262,8 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun pickedSleepTimer(seconds: Int) {
|
private fun pickedSleepTimer(seconds: Int) {
|
||||||
config.lastSleepTimerSeconds = seconds
|
preferences.lastSleepTimerSeconds = seconds
|
||||||
config.sleepInTS = System.currentTimeMillis() + seconds * 1000
|
preferences.sleepInTS = System.currentTimeMillis() + seconds * 1000
|
||||||
startSleepTimer()
|
startSleepTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,13 +279,13 @@ class MainActivity : SimpleActivity() {
|
|||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
private fun checkShortcuts() {
|
private fun checkShortcuts() {
|
||||||
val appIconColor = config.appIconColor
|
val appIconColor = preferences.appIconColor
|
||||||
if (isNougatMR1Plus() && config.lastHandledShortcutColor != appIconColor) {
|
if (isNougatMR1Plus() && preferences.lastHandledShortcutColor != appIconColor) {
|
||||||
val createNewContact = getBrightDisplayShortcut(appIconColor)
|
val createNewContact = getBrightDisplayShortcut(appIconColor)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
shortcutManager.dynamicShortcuts = Arrays.asList(createNewContact)
|
shortcutManager.dynamicShortcuts = Arrays.asList(createNewContact)
|
||||||
config.lastHandledShortcutColor = appIconColor
|
preferences.lastHandledShortcutColor = appIconColor
|
||||||
} catch (ignored: Exception) {
|
} catch (ignored: Exception) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -304,6 +312,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
application: Application
|
application: Application
|
||||||
) : AndroidViewModel(application) {
|
) : AndroidViewModel(application) {
|
||||||
|
|
||||||
|
private val preferences = application.config
|
||||||
|
|
||||||
private val _timerText: MutableStateFlow<String> = MutableStateFlow("00:00")
|
private val _timerText: MutableStateFlow<String> = MutableStateFlow("00:00")
|
||||||
val timerText = _timerText.asStateFlow()
|
val timerText = _timerText.asStateFlow()
|
||||||
@ -349,11 +358,11 @@ class MainActivity : SimpleActivity() {
|
|||||||
camera.onCameraNotAvailable()
|
camera.onCameraNotAvailable()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (application.config.turnFlashlightOn) {
|
if (preferences.turnFlashlightOn) {
|
||||||
camera.enableFlashlight()
|
camera.enableFlashlight()
|
||||||
}
|
}
|
||||||
|
|
||||||
_stroboscopeBarValue.value = application.config.stroboscopeProgress.toFloat() / 100
|
_stroboscopeBarValue.value = preferences.stroboscopeProgress.toFloat() / 100
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
@ -380,7 +389,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
val min = MIN_BRIGHTNESS_LEVEL
|
val min = MIN_BRIGHTNESS_LEVEL
|
||||||
val newLevel = MathUtils.lerp(min.toFloat(), max.toFloat(), newValue)
|
val newLevel = MathUtils.lerp(min.toFloat(), max.toFloat(), newValue)
|
||||||
camera.updateBrightnessLevel(newLevel.toInt())
|
camera.updateBrightnessLevel(newLevel.toInt())
|
||||||
getApplication<Application>().config.brightnessLevel = newLevel.toInt()
|
preferences.brightnessLevel = newLevel.toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateStroboscopeBarValue(newValue: Float) {
|
fun updateStroboscopeBarValue(newValue: Float) {
|
||||||
@ -389,17 +398,25 @@ class MainActivity : SimpleActivity() {
|
|||||||
val min = MIN_STROBO_DELAY
|
val min = MIN_STROBO_DELAY
|
||||||
val newLevel = MathUtils.lerp(min.toFloat(), max.toFloat(), 1 - newValue)
|
val newLevel = MathUtils.lerp(min.toFloat(), max.toFloat(), 1 - newValue)
|
||||||
camera.stroboFrequency = newLevel.toLong()
|
camera.stroboFrequency = newLevel.toLong()
|
||||||
getApplication<Application>().config.stroboscopeFrequency = newLevel.toLong()
|
preferences.stroboscopeFrequency = newLevel.toLong()
|
||||||
getApplication<Application>().config.stroboscopeProgress = ((1 - newLevel) * 100).toInt()
|
preferences.stroboscopeProgress = ((1 - newLevel) * 100).toInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onResume() {
|
fun onResume() {
|
||||||
checkState(MyCameraImpl.isFlashlightOn)
|
checkState(MyCameraImpl.isFlashlightOn)
|
||||||
camera.handleCameraSetup()
|
camera.handleCameraSetup()
|
||||||
|
|
||||||
if (getApplication<Application>().config.turnFlashlightOn) {
|
if (preferences.turnFlashlightOn) {
|
||||||
camera.enableFlashlight()
|
camera.enableFlashlight()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!preferences.stroboscope && _stroboscopeActive.value) {
|
||||||
|
camera.stopStroboscope()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!preferences.sos && _sosActive.value) {
|
||||||
|
camera.stopSOS()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkState(isEnabled: Boolean) {
|
private fun checkState(isEnabled: Boolean) {
|
||||||
|
@ -2,7 +2,9 @@ package com.simplemobiletools.flashlight.helpers
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
|
import com.simplemobiletools.commons.extensions.sharedPreferencesCallback
|
||||||
import com.simplemobiletools.commons.helpers.BaseConfig
|
import com.simplemobiletools.commons.helpers.BaseConfig
|
||||||
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
|
|
||||||
class Config(context: Context) : BaseConfig(context) {
|
class Config(context: Context) : BaseConfig(context) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -13,7 +15,7 @@ class Config(context: Context) : BaseConfig(context) {
|
|||||||
get() = prefs.getBoolean(BRIGHT_DISPLAY, true)
|
get() = prefs.getBoolean(BRIGHT_DISPLAY, true)
|
||||||
set(brightDisplay) = prefs.edit().putBoolean(BRIGHT_DISPLAY, brightDisplay).apply()
|
set(brightDisplay) = prefs.edit().putBoolean(BRIGHT_DISPLAY, brightDisplay).apply()
|
||||||
|
|
||||||
val brightDisplayFlow = ::brightDisplay.asFlow()
|
val brightDisplayFlow = prefs.run { sharedPreferencesCallback { brightDisplay } }.filterNotNull()
|
||||||
|
|
||||||
var stroboscope: Boolean
|
var stroboscope: Boolean
|
||||||
get() = prefs.getBoolean(STROBOSCOPE, true)
|
get() = prefs.getBoolean(STROBOSCOPE, true)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user