From 9ea2df2b7949be36247485065edf061194cb8645 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Sat, 19 Aug 2023 10:53:49 +0200 Subject: [PATCH] SyncIntervalDialog: use MaterialAlertDialogBuilder --- .../icsdroid/ui/CalendarListActivity.kt | 15 +-- .../icsdroid/ui/dialog/SyncIntervalDialog.kt | 95 ++++--------------- app/src/main/res/values/strings.xml | 41 ++++---- 3 files changed, 45 insertions(+), 106 deletions(-) diff --git a/app/src/main/java/at/bitfire/icsdroid/ui/CalendarListActivity.kt b/app/src/main/java/at/bitfire/icsdroid/ui/CalendarListActivity.kt index efb71ea5..04051937 100644 --- a/app/src/main/java/at/bitfire/icsdroid/ui/CalendarListActivity.kt +++ b/app/src/main/java/at/bitfire/icsdroid/ui/CalendarListActivity.kt @@ -203,23 +203,24 @@ class CalendarListActivity: AppCompatActivity() { Icon(Icons.Rounded.MoreVert, stringResource(R.string.action_more)) } - var showingSyncIntervalDialog by remember { mutableStateOf(false) } - if (showingSyncIntervalDialog) + var showSyncIntervalDialog by remember { mutableStateOf(false) } + if (showSyncIntervalDialog) SyncIntervalDialog( - onSetSyncInterval = { - AppAccount.syncInterval(this, it) - showingSyncIntervalDialog = false + currentInterval = AppAccount.syncInterval(this), + onSetSyncInterval = { seconds -> + AppAccount.syncInterval(this, seconds) + showSyncIntervalDialog = false CoroutineScope(Dispatchers.IO).launch { checkSyncSettings() } } - ) { showingSyncIntervalDialog = false } + ) { showSyncIntervalDialog = false } DropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { DropdownMenuItem( - onClick = { showingSyncIntervalDialog = true } + onClick = { showSyncIntervalDialog = true } ) { Text(stringResource(R.string.calendar_list_set_sync_interval)) } diff --git a/app/src/main/java/at/bitfire/icsdroid/ui/dialog/SyncIntervalDialog.kt b/app/src/main/java/at/bitfire/icsdroid/ui/dialog/SyncIntervalDialog.kt index bddaae8e..535974ab 100644 --- a/app/src/main/java/at/bitfire/icsdroid/ui/dialog/SyncIntervalDialog.kt +++ b/app/src/main/java/at/bitfire/icsdroid/ui/dialog/SyncIntervalDialog.kt @@ -1,102 +1,41 @@ package at.bitfire.icsdroid.ui.dialog -import androidx.compose.foundation.layout.padding -import androidx.compose.material.AlertDialog -import androidx.compose.material.DropdownMenuItem -import androidx.compose.material.ExperimentalMaterialApi -import androidx.compose.material.ExposedDropdownMenuBox -import androidx.compose.material.ExposedDropdownMenuDefaults -import androidx.compose.material.MaterialTheme -import androidx.compose.material.OutlinedTextField -import androidx.compose.material.Text -import androidx.compose.material.TextButton import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringArrayResource -import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import at.bitfire.icsdroid.AppAccount import at.bitfire.icsdroid.R +import com.google.android.material.dialog.MaterialAlertDialogBuilder -@OptIn(ExperimentalMaterialApi::class) @Composable fun SyncIntervalDialog( + currentInterval: Long, onSetSyncInterval: (Long) -> Unit, - onDismissRequest: () -> Unit + onDismiss: () -> Unit ) { val context = LocalContext.current - val syncIntervalValues = stringArrayResource(R.array.set_sync_interval_seconds) - val syncIntervalOptions = stringArrayResource(R.array.set_sync_interval_names) - .mapIndexed { i, s -> syncIntervalValues[i].toLong() to s } - .toMap() + val syncIntervalValues = stringArrayResource(R.array.set_sync_interval_seconds).map { it.toLong() } + val currentIntervalIdx = syncIntervalValues.indexOf(currentInterval) - var syncInterval by remember { mutableStateOf(AppAccount.syncInterval(context)) } - - AlertDialog( - onDismissRequest = onDismissRequest, - title = { - Text( - text = stringResource(R.string.set_sync_interval_title), - style = MaterialTheme.typography.subtitle1 - ) - }, - text = { - var expanded by remember { mutableStateOf(false) } - - ExposedDropdownMenuBox( - expanded = expanded, - onExpandedChange = { expanded = !expanded } - ) { - OutlinedTextField( - value = syncIntervalOptions.getValue(syncInterval), - onValueChange = {}, - readOnly = true, - trailingIcon = { - ExposedDropdownMenuDefaults.TrailingIcon(expanded) - }, - modifier = Modifier.padding(top = 12.dp) - ) - ExposedDropdownMenu( - expanded = expanded, - onDismissRequest = { expanded = false } - ) { - syncIntervalOptions.forEach { option -> - DropdownMenuItem( - onClick = { - syncInterval = option.key - expanded = false - } - ) { - Text(option.value) - } - } - } - } - }, - confirmButton = { - TextButton( - onClick = { onSetSyncInterval(syncInterval) } - ) { - Text( - text = stringResource(R.string.set_sync_interval_save).uppercase() - ) - } + MaterialAlertDialogBuilder(context) + .setTitle(R.string.set_sync_interval_title) + .setSingleChoiceItems(R.array.set_sync_interval_names, currentIntervalIdx) { dialog, newIdx -> + onSetSyncInterval(syncIntervalValues[newIdx]) + dialog.dismiss() } - ) + .setOnDismissListener { + onDismiss() + } + .show() } @Preview @Composable fun SyncIntervalDialog_Preview() { SyncIntervalDialog( + -1, // only manually onSetSyncInterval = {}, - onDismissRequest = {} + onDismiss = {} ) -} +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2983646a..6c06b20e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,8 +27,27 @@ Battery: Whitelist ICSx⁵ for short sync intervals Settings - Force dark theme Privacy policy + Set sync. interval: + + -1 + 900 + 3600 + 7200 + 14400 + 86400 + 604800 + + + Only manually + Every 15 minutes + Every hour + Every 2 hours + Every 4 hours + Once a day + Once a week + + Force dark theme Subscribe to calendar @@ -78,26 +97,6 @@ Synchronization error - Save - Set sync. interval: - - -1 - 900 - 3600 - 7200 - 14400 - 86400 - 604800 - - - Only manually - Every 15 minutes - Every hour - Every 2 hours - Every 4 hours - Once a day - Once a week - Permission required Needing permission to sync your calendar Couldn\'t open file from storage