-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SyncIntervalDialog: use MaterialAlertDialogBuilder
- Loading branch information
Showing
3 changed files
with
45 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 17 additions & 78 deletions
95
app/src/main/java/at/bitfire/icsdroid/ui/dialog/SyncIntervalDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters