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.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
@ -289,20 +291,26 @@ class MainActivity : ComponentActivity() {
alertDialogState: AlertDialogState, alertDialogState: AlertDialogState,
onCustomValueSelected: () -> Unit onCustomValueSelected: () -> Unit
) { ) {
val items = ArrayList(listOf(10, 30, 60, 5 * 60, 10 * 60, 30 * 60).map { val lastSleepTimerSeconds by preferences.lastSleepTimerSecondsFlow.collectAsStateWithLifecycle(preferences.lastSleepTimerSeconds)
RadioItem(it, secondsToString(it)) 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 }) { if (finalItems.none { it.id == lastSleepTimerSeconds }) {
items.add(RadioItem(preferences.lastSleepTimerSeconds, secondsToString(config.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( RadioGroupAlertDialog(
alertDialogState = alertDialogState, alertDialogState = alertDialogState,
items = items.toImmutableList(), items = items,
selectedItemId = preferences.lastSleepTimerSeconds, selectedItemId = preferences.lastSleepTimerSeconds,
callback = { callback = {
if (it as Int == -1) { 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) get() = prefs.getInt(LAST_SLEEP_TIMER_SECONDS, 30 * 60)
set(lastSleepTimerSeconds) = prefs.edit().putInt(LAST_SLEEP_TIMER_SECONDS, lastSleepTimerSeconds).apply() set(lastSleepTimerSeconds) = prefs.edit().putInt(LAST_SLEEP_TIMER_SECONDS, lastSleepTimerSeconds).apply()
val lastSleepTimerSecondsFlow = ::lastSleepTimerSeconds.asFlowNonNull()
var sleepInTS: Long var sleepInTS: Long
get() = prefs.getLong(SLEEP_IN_TS, 0) get() = prefs.getLong(SLEEP_IN_TS, 0)
set(sleepInTS) = prefs.edit().putLong(SLEEP_IN_TS, sleepInTS).apply() set(sleepInTS) = prefs.edit().putLong(SLEEP_IN_TS, sleepInTS).apply()