Remember items for custom sleep timer dialog

This commit is contained in:
Ensar Sarajčić 2023-10-05 13:16:44 +02:00
parent c33664b9e7
commit af6691c06a
2 changed files with 19 additions and 9 deletions

View File

@ -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) {

View File

@ -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()