Skip to content

Commit

Permalink
SyncIntervalDialog: use MaterialAlertDialogBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Aug 19, 2023
1 parent 8e61c8b commit 9ea2df2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 106 deletions.
15 changes: 8 additions & 7 deletions app/src/main/java/at/bitfire/icsdroid/ui/CalendarListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = {}
)
}
}
41 changes: 20 additions & 21 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,27 @@
<string name="calendar_list_battery_whitelist">Battery: Whitelist ICSx⁵ for short sync intervals</string>
<string name="calendar_list_battery_whitelist_settings">Settings</string>
<!-- settings, currently in CalendarListActivity -->
<string name="settings_force_dark_theme">Force dark theme</string>
<string name="calendar_list_privacy_policy">Privacy policy</string>
<string name="set_sync_interval_title">Set sync. interval:</string>
<string-array name="set_sync_interval_seconds" translatable="false">
<item>-1</item>
<item>900</item>
<item>3600</item>
<item>7200</item>
<item>14400</item>
<item>86400</item>
<item>604800</item>
</string-array>
<string-array name="set_sync_interval_names">
<item>Only manually</item>
<item>Every 15 minutes</item>
<item>Every hour</item>
<item>Every 2 hours</item>
<item>Every 4 hours</item>
<item>Once a day</item>
<item>Once a week</item>
</string-array>
<string name="settings_force_dark_theme">Force dark theme</string>

<!-- AddCalendarActivity -->
<string name="activity_add_calendar">Subscribe to calendar</string>
Expand Down Expand Up @@ -78,26 +97,6 @@

<!-- synchronization -->
<string name="sync_error_title">Synchronization error</string>
<string name="set_sync_interval_save">Save</string>
<string name="set_sync_interval_title">Set sync. interval:</string>
<string-array name="set_sync_interval_seconds" translatable="false">
<item>-1</item>
<item>900</item>
<item>3600</item>
<item>7200</item>
<item>14400</item>
<item>86400</item>
<item>604800</item>
</string-array>
<string-array name="set_sync_interval_names">
<item>Only manually</item>
<item>Every 15 minutes</item>
<item>Every hour</item>
<item>Every 2 hours</item>
<item>Every 4 hours</item>
<item>Once a day</item>
<item>Once a week</item>
</string-array>
<string name="sync_permission_required">Permission required</string>
<string name="sync_permission_required_sync_calendar">Needing permission to sync your calendar</string>
<string name="could_not_open_storage_file">Couldn\'t open file from storage</string>
Expand Down

0 comments on commit 9ea2df2

Please sign in to comment.