diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt index a23b951..e8c6af9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -19,7 +19,9 @@ import androidx.activity.compose.setContent import androidx.activity.result.contract.ActivityResultContracts 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.compose.ui.res.stringResource import androidx.core.content.ContextCompat import androidx.lifecycle.AndroidViewModel @@ -289,20 +291,26 @@ class MainActivity : ComponentActivity() { alertDialogState: AlertDialogState, onCustomValueSelected: () -> Unit ) { - val items = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map { - RadioItem(it, secondsToString(it)) - }) + val lastSleepTimerSeconds by preferences.lastSleepTimerSecondsFlow.collectAsStateWithLifecycle(preferences.lastSleepTimerSeconds) + val items by remember { + derivedStateOf { + val finalItems = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map { + RadioItem(it, secondsToString(it)) + }) - if (items.none { it.id == preferences.lastSleepTimerSeconds }) { - items.add(RadioItem(preferences.lastSleepTimerSeconds, secondsToString(config.lastSleepTimerSeconds))) + if (finalItems.none { it.id == lastSleepTimerSeconds }) { + finalItems.add(RadioItem(lastSleepTimerSeconds, secondsToString(lastSleepTimerSeconds))) + } + + finalItems.sortBy { it.id } + finalItems.add(RadioItem(-1, getString(R.string.custom))) + finalItems.toImmutableList() + } } - items.sortBy { it.id } - items.add(RadioItem(-1, getString(R.string.custom))) - RadioGroupAlertDialog( alertDialogState = alertDialogState, - items = items.toImmutableList(), + items = items, selectedItemId = preferences.lastSleepTimerSeconds, callback = { if (it as Int == -1) { 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 81bf2ba..dd04bfd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Config.kt @@ -61,6 +61,8 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getInt(LAST_SLEEP_TIMER_SECONDS, 30 * 60) set(lastSleepTimerSeconds) = prefs.edit().putInt(LAST_SLEEP_TIMER_SECONDS, lastSleepTimerSeconds).apply() + val lastSleepTimerSecondsFlow = ::lastSleepTimerSeconds.asFlowNonNull() + var sleepInTS: Long get() = prefs.getLong(SLEEP_IN_TS, 0) set(sleepInTS) = prefs.edit().putLong(SLEEP_IN_TS, sleepInTS).apply()