From ec060056ddaab56ef8c839fb8e0be35c4e0206ef Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 16 Nov 2026 20:01:01 +0100 Subject: [PATCH] fix(#325): copy events as a full-screen two-step flow with a route card --- .../calendula/ui/calendars/BackupScreen.kt | 3 +- .../ui/calendars/CopyEventsScreen.kt | 376 ++++++++++++++++++ .../calendula/ui/calendars/CopyEventsSheet.kt | 247 ------------ .../ui/calendars/CopyEventsViewModel.kt | 4 +- .../calendula/ui/common/ResultSummary.kt | 135 +++++++ .../calendula/ui/imports/ImportScreen.kt | 192 +-------- app/src/main/res/values/strings.xml | 3 +- floret-kit | 2 +- 8 files changed, 536 insertions(+), 426 deletions(-) create mode 100644 app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsScreen.kt delete mode 100644 app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt create mode 100644 app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ResultSummary.kt diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt index 4dd5401a..67f31445 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt @@ -188,7 +188,7 @@ fun BackupScreen( if (showCopy) { val calendars by copyViewModel.calendars.collectAsStateWithLifecycle() val selection by copyViewModel.selection.collectAsStateWithLifecycle() - CopyEventsSheet( + CopyEventsScreen( calendars = calendars, selection = selection, run = copyRun, @@ -196,6 +196,7 @@ fun BackupScreen( onSelectTarget = copyViewModel::setTarget, onStart = copyViewModel::start, onCancel = copyViewModel::cancel, + onClearFinished = copyViewModel::clearFinished, onDismiss = { showCopy = false copyViewModel.clearFinished() diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsScreen.kt new file mode 100644 index 00000000..e5e7ee21 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsScreen.kt @@ -0,0 +1,376 @@ +package de.jeanlucmakiola.calendula.ui.calendars + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ColumnScope +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Close +import androidx.compose.material.icons.outlined.Info +import androidx.compose.material.icons.rounded.ArrowDownward +import androidx.compose.material3.Button +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi +import androidx.compose.material3.Icon +import androidx.compose.material3.LinearWavyProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedButton +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.pluralStringResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.domain.CalendarSource +import de.jeanlucmakiola.calendula.domain.FailureReason +import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip +import de.jeanlucmakiola.calendula.ui.common.CalendarFailure +import de.jeanlucmakiola.calendula.ui.common.CalendarPickerGroups +import de.jeanlucmakiola.calendula.ui.common.ImportSummaryStats +import de.jeanlucmakiola.calendula.ui.common.ResultBadge +import de.jeanlucmakiola.calendula.ui.common.ResultMessage +import de.jeanlucmakiola.calendula.ui.common.importSummaryNotes +import de.jeanlucmakiola.floret.components.ResultBadge as FloretResultBadge +import de.jeanlucmakiola.calendula.ui.common.orUnnamed +import de.jeanlucmakiola.floret.components.FullScreenPicker +import de.jeanlucmakiola.floret.components.GroupedHint +import de.jeanlucmakiola.floret.components.GroupedListInset +import de.jeanlucmakiola.floret.components.IconInfoCard + +/** + * Copy every event of one calendar into another (#325), in the import flow's + * steps: pick the source, pick the target and copy, watch it run, see the result. + * The run lives in [CopyEventsViewModel], so the screen can be closed and + * reopened mid-copy. + */ +@Composable +fun CopyEventsScreen( + calendars: CopyCalendars, + selection: CopySelection, + run: CopyRunState, + onSelectSource: (Long) -> Unit, + onSelectTarget: (Long) -> Unit, + onStart: () -> Unit, + onCancel: () -> Unit, + onClearFinished: () -> Unit, + onDismiss: () -> Unit, +) { + val list = (calendars as? CopyCalendars.Ready)?.calendars.orEmpty() + val sources = remember(list) { copySources(list) } + val source = sources.firstOrNull { it.id == selection.sourceId } + val targets = remember(list, source?.id) { copyTargets(list, source?.id) } + val target = targets.firstOrNull { it.id == selection.targetId } + var step by rememberSaveable { mutableStateOf(CopyStep.Source) } + val idle = run == CopyRunState.Idle + // The run's ends, held once seen so a calendar gone or a list reloading mid-run doesn't blank them. + val runSource = rememberLastSeen(selection.sourceId, list) + val runTarget = rememberLastSeen(selection.targetId, list) + // A source that stopped qualifying sends the target step back to the start. + // Keyed on the chosen id too: right after a pick the selection may not have arrived yet. + LaunchedEffect(step, selection.sourceId, source, calendars) { + val gone = selection.sourceId != null && source == null && calendars is CopyCalendars.Ready + if (step == CopyStep.Target && gone) step = CopyStep.Source + } + + FullScreenPicker( + title = stringResource( + when { + !idle -> R.string.calendars_copy_action + step == CopyStep.Source -> R.string.calendars_copy_from_title + else -> R.string.calendars_copy_to_title + }, + ), + onDismiss = { if (idle && step == CopyStep.Target) step = CopyStep.Source else onDismiss() }, + // The target step's back only steps back, so it mustn't preview closing the screen. + predictiveBack = !(idle && step == CopyStep.Target), + scrollable = idle, + actions = { + if (idle && step == CopyStep.Target) { + Button( + onClick = onStart, + enabled = source != null && target != null, + modifier = Modifier.padding(end = 12.dp), + ) { + Text(stringResource(R.string.calendars_copy_start)) + } + } + }, + ) { + if (!idle) { + val copyMore = { + onClearFinished() + step = CopyStep.Source + } + CopyRun( + run = run, + source = runSource, + target = runTarget, + onCancel = onCancel, + // A retry only where the picker would still offer both ends; otherwise pick again. + onRetry = if (source != null && target != null) onStart else copyMore, + onCopyMore = copyMore, + onClose = onDismiss, + ) + return@FullScreenPicker + } + when (calendars) { + CopyCalendars.Loading -> { + Box(Modifier.fillMaxWidth().padding(32.dp), contentAlignment = Alignment.Center) { + CircularProgressIndicator() + } + return@FullScreenPicker + } + CopyCalendars.Failure -> { + CalendarFailure(reason = FailureReason.ProviderUnavailable, onRetry = onDismiss) + return@FullScreenPicker + } + is CopyCalendars.Ready -> Unit + } + if (sources.isEmpty()) { + GroupedHint(stringResource(R.string.calendars_copy_none)) + return@FullScreenPicker + } + when (step) { + CopyStep.Source -> { + IconInfoCard( + icon = Icons.Outlined.Info, + iconContentDescription = null, + modifier = Modifier.padding(horizontal = GroupedListInset), + ) { + Text(stringResource(R.string.calendars_copy_hint), style = MaterialTheme.typography.bodyMedium) + } + Spacer(Modifier.height(16.dp)) + CalendarPickerGroups( + calendars = sources, + selectedId = source?.id, + onSelect = { + onSelectSource(it) + step = CopyStep.Target + }, + ) + } + CopyStep.Target -> { + CopyRouteCard(source, target, Modifier.padding(horizontal = GroupedListInset)) + Spacer(Modifier.height(16.dp)) + CalendarPickerGroups( + calendars = targets, + selectedId = target?.id, + onSelect = onSelectTarget, + ) + } + } + } +} + +private enum class CopyStep { Source, Target } + +/** Progress while the copy runs, then its outcome; both under the route it took. */ +@Composable +private fun ColumnScope.CopyRun( + run: CopyRunState, + source: CalendarSource?, + target: CalendarSource?, + onCancel: () -> Unit, + onRetry: () -> Unit, + onCopyMore: () -> Unit, + onClose: () -> Unit, +) { + // Centred where it fits, scrolling where it doesn't (landscape, large fonts). + BoxWithConstraints(Modifier.fillMaxWidth().weight(1f)) { + Column( + Modifier + .fillMaxWidth() + .verticalScroll(rememberScrollState()) + .heightIn(min = maxHeight) + .padding(horizontal = GroupedListInset) + .padding(top = 8.dp, bottom = 24.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.SpaceBetween, + ) { + CopyRouteCard(source, target, pickable = false) + Column(Modifier.fillMaxWidth().padding(vertical = 24.dp), horizontalAlignment = Alignment.CenterHorizontally) { + when (run) { + is CopyRunState.Running -> CopyProgress(run) + is CopyRunState.Done -> CopyDone(run) + CopyRunState.Cancelled -> { + FloretResultBadge( + Icons.Default.Close, + MaterialTheme.colorScheme.secondaryContainer, + MaterialTheme.colorScheme.onSecondaryContainer, + ) + ResultMessage(stringResource(R.string.calendars_copy_cancelled)) + } + CopyRunState.Failed -> { + ResultBadge(error = true) + ResultMessage(stringResource(R.string.calendars_copy_failed)) + } + CopyRunState.Idle -> Unit + } + } + when (run) { + is CopyRunState.Running -> OutlinedButton(onClick = onCancel, modifier = Modifier.fillMaxWidth()) { + Text(stringResource(R.string.dialog_cancel)) + } + is CopyRunState.Done -> CopyEndButtons(stringResource(R.string.calendars_copy_more), onCopyMore, onClose) + CopyRunState.Cancelled, CopyRunState.Failed -> + CopyEndButtons(stringResource(R.string.state_retry), onRetry, onClose) + CopyRunState.Idle -> Unit + } + } + } +} + +/** The calendar with [id] in [list], or the last one seen under that id while it is missing. */ +@Composable +private fun rememberLastSeen(id: Long?, list: List): CalendarSource? { + val held = remember(id) { mutableStateOf(null) } + list.firstOrNull { it.id == id }?.let { held.value = it } + return held.value +} + +@OptIn(ExperimentalMaterial3ExpressiveApi::class) +@Composable +private fun CopyProgress(run: CopyRunState.Running) { + val total = run.total + if (total == null || total == 0) { + LinearWavyProgressIndicator(modifier = Modifier.fillMaxWidth()) + } else { + LinearWavyProgressIndicator(progress = { run.done.toFloat() / total }, modifier = Modifier.fillMaxWidth()) + } + Spacer(Modifier.height(16.dp)) + Text( + text = copyProgressText(run), + style = MaterialTheme.typography.titleMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) +} + +/** "Reading events…" until the source is read, then "120 of 2,400 events". */ +@Composable +fun copyProgressText(run: CopyRunState.Running): String { + val total = run.total ?: return stringResource(R.string.calendars_copy_reading) + return pluralStringResource(R.plurals.calendars_copy_progress, total, run.done, total) +} + +@Composable +private fun CopyDone(run: CopyRunState.Done) { + val summary = run.summary + // As on the import: only a run the target cut short reads as an error. + ResultBadge(error = run.stopped) + ResultMessage( + title = stringResource(run.titleRes), + notes = importSummaryNotes( + summary, + stoppedNote = if (run.stopped) { + pluralStringResource(R.plurals.calendars_copy_stopped, summary.notAttempted, summary.notAttempted) + } else { + null + }, + ), + ) + Spacer(Modifier.height(24.dp)) + ImportSummaryStats(summary) +} + +/** The way on ([secondary], e.g. Copy more) above the way out (Close). */ +@Composable +private fun CopyEndButtons(secondary: String, onSecondary: () -> Unit, onClose: () -> Unit) { + Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { + OutlinedButton(onClick = onSecondary, modifier = Modifier.fillMaxWidth()) { Text(secondary) } + Button(onClick = onClose, modifier = Modifier.fillMaxWidth()) { Text(stringResource(R.string.import_close)) } + } +} + +/** + * Where the events come from and go to, with an arrow between. A missing end + * reads "Choose a calendar" while [pickable], or as unnamed on the run screens. + */ +@Composable +private fun CopyRouteCard( + source: CalendarSource?, + target: CalendarSource?, + modifier: Modifier = Modifier, + pickable: Boolean = true, +) { + Surface( + modifier = modifier.fillMaxWidth(), + shape = RoundedCornerShape(24.dp), + color = MaterialTheme.colorScheme.surfaceContainerHigh, + ) { + Column(Modifier.padding(16.dp)) { + CopyRouteEnd(stringResource(R.string.calendars_copy_from), source, pickable) + Box(Modifier.width(40.dp).padding(vertical = 4.dp), contentAlignment = Alignment.Center) { + Surface(shape = CircleShape, color = MaterialTheme.colorScheme.primaryContainer) { + Icon( + Icons.Rounded.ArrowDownward, + contentDescription = null, + tint = MaterialTheme.colorScheme.onPrimaryContainer, + modifier = Modifier.padding(6.dp).size(20.dp), + ) + } + } + CopyRouteEnd(stringResource(R.string.calendars_copy_to), target, pickable) + } + } +} + +@Composable +private fun CopyRouteEnd(label: String, calendar: CalendarSource?, pickable: Boolean) { + Row(verticalAlignment = Alignment.CenterVertically) { + if (calendar != null) { + CalendarColorChip(calendar.color) + } else { + Surface( + modifier = Modifier.size(40.dp), + shape = CircleShape, + color = MaterialTheme.colorScheme.surfaceContainerHigh, + border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant), + ) {} + } + Spacer(Modifier.width(16.dp)) + Column { + Text(label, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text( + calendar?.displayName?.orUnnamed() + ?: if (pickable) stringResource(R.string.calendars_copy_choose) else "".orUnnamed(), + style = MaterialTheme.typography.titleMedium, + color = if (calendar != null) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + if (calendar != null) { + Text( + if (calendar.isLocal) stringResource(R.string.calendars_local_header) else calendar.accountName, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } + } + } +} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt deleted file mode 100644 index 818bd4db..00000000 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsSheet.kt +++ /dev/null @@ -1,247 +0,0 @@ -package de.jeanlucmakiola.calendula.ui.calendars - -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding -import androidx.compose.material3.CircularProgressIndicator -import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi -import androidx.compose.material3.LinearWavyProgressIndicator -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedButton -import androidx.compose.material3.Text -import androidx.compose.material3.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.saveable.rememberSaveable -import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.res.pluralStringResource -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp -import de.jeanlucmakiola.calendula.R -import de.jeanlucmakiola.calendula.domain.CalendarSource -import de.jeanlucmakiola.calendula.domain.FailureReason -import de.jeanlucmakiola.calendula.ui.common.CalendarFailure -import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip -import de.jeanlucmakiola.calendula.ui.common.CalendarPickerGroups -import de.jeanlucmakiola.calendula.ui.common.orUnnamed -import de.jeanlucmakiola.floret.components.FullScreenPicker -import de.jeanlucmakiola.floret.components.GroupedHint -import de.jeanlucmakiola.floret.components.GroupedRow -import de.jeanlucmakiola.floret.components.Position - -/** - * Copy every event of one calendar into another (#325): pick the source and the - * target, copy, watch it run. The run lives in [CopyEventsViewModel], so this - * sheet can be closed and reopened mid-copy. - */ -@Composable -fun CopyEventsSheet( - calendars: CopyCalendars, - selection: CopySelection, - run: CopyRunState, - onSelectSource: (Long) -> Unit, - onSelectTarget: (Long) -> Unit, - onStart: () -> Unit, - onCancel: () -> Unit, - onDismiss: () -> Unit, -) { - val list = (calendars as? CopyCalendars.Ready)?.calendars.orEmpty() - val running = run is CopyRunState.Running - val sources = remember(list) { copySources(list) } - // A running copy keeps showing its source even if it stopped qualifying meanwhile. - val source = (if (running) list else sources).firstOrNull { it.id == selection.sourceId } - val targets = remember(list, source?.id) { copyTargets(list, source?.id) } - val target = targets.firstOrNull { it.id == selection.targetId } - var picking by rememberSaveable { mutableStateOf(null) } - - FullScreenPicker( - title = stringResource(R.string.calendars_copy_action), - onDismiss = onDismiss, - predictiveBack = true, - actions = { - if (!running) { - TextButton(onClick = onStart, enabled = source != null && target != null) { - Text(stringResource(R.string.calendars_copy_start)) - } - } - }, - ) { - GroupedHint(stringResource(R.string.calendars_copy_hint), style = MaterialTheme.typography.bodySmall) - // A copy under way keeps its progress and Cancel whatever the list does. - if (run == CopyRunState.Idle) when (calendars) { - CopyCalendars.Loading -> { - Box(Modifier.fillMaxWidth().padding(32.dp), contentAlignment = Alignment.Center) { - CircularProgressIndicator() - } - return@FullScreenPicker - } - CopyCalendars.Failure -> { - CalendarFailure(reason = FailureReason.ProviderUnavailable, onRetry = onDismiss) - return@FullScreenPicker - } - is CopyCalendars.Ready -> Unit - } - if (sources.isEmpty() && run == CopyRunState.Idle) { - GroupedHint(stringResource(R.string.calendars_copy_none)) - return@FullScreenPicker - } - CopyEndRow( - label = stringResource(R.string.calendars_copy_from), - calendar = source, - position = Position.Top, - onClick = if (running) null else ({ picking = CopyEnd.Source }), - ) - CopyEndRow( - label = stringResource(R.string.calendars_copy_to), - calendar = target, - position = Position.Bottom, - onClick = if (running || source == null) null else ({ picking = CopyEnd.Target }), - ) - Column( - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp, vertical = 16.dp), - verticalArrangement = Arrangement.spacedBy(12.dp), - ) { - when (run) { - is CopyRunState.Running -> CopyProgress(run, onCancel) - is CopyRunState.Done -> CopySummary(run) - CopyRunState.Cancelled -> CopyNote(stringResource(R.string.calendars_copy_cancelled)) - CopyRunState.Failed -> CopyNote( - stringResource(R.string.calendars_copy_failed), - error = true, - ) - CopyRunState.Idle -> Unit - } - } - } - - when (picking) { - CopyEnd.Source -> FullScreenPicker( - title = stringResource(R.string.calendars_copy_from_title), - onDismiss = { picking = null }, - predictiveBack = true, - ) { - CalendarPickerGroups( - calendars = sources, - selectedId = source?.id, - onSelect = { - onSelectSource(it) - picking = null - }, - ) - } - CopyEnd.Target -> FullScreenPicker( - title = stringResource(R.string.calendars_copy_to_title), - onDismiss = { picking = null }, - predictiveBack = true, - ) { - CalendarPickerGroups( - calendars = targets, - selectedId = target?.id, - onSelect = { - onSelectTarget(it) - picking = null - }, - ) - } - null -> Unit - } -} - -private enum class CopyEnd { Source, Target } - -@Composable -private fun CopyEndRow( - label: String, - calendar: CalendarSource?, - position: Position, - onClick: (() -> Unit)?, -) { - GroupedRow( - title = label, - summary = calendar?.displayName?.orUnnamed() ?: stringResource(R.string.calendars_copy_choose), - position = position, - dimmed = onClick == null, - leading = calendar?.let { { CalendarColorChip(it.color) } }, - onClick = onClick, - ) -} - -@OptIn(ExperimentalMaterial3ExpressiveApi::class) -@Composable -private fun CopyProgress(run: CopyRunState.Running, onCancel: () -> Unit) { - val total = run.total - if (total == null || total == 0) { - LinearWavyProgressIndicator(modifier = Modifier.fillMaxWidth()) - } else { - LinearWavyProgressIndicator( - progress = { run.done.toFloat() / total }, - modifier = Modifier.fillMaxWidth(), - ) - } - Text( - text = copyProgressText(run), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) - OutlinedButton(onClick = onCancel, modifier = Modifier.fillMaxWidth()) { - Text(stringResource(R.string.dialog_cancel)) - } -} - -/** "Reading events…" until the source is read, then "120 of 2,400 events". */ -@Composable -fun copyProgressText(run: CopyRunState.Running): String { - val total = run.total ?: return stringResource(R.string.calendars_copy_reading) - return pluralStringResource(R.plurals.calendars_copy_progress, total, run.done, total) -} - -@Composable -private fun CopySummary(run: CopyRunState.Done) { - val summary = run.summary - Text( - stringResource(run.titleRes), - style = MaterialTheme.typography.titleMedium, - color = MaterialTheme.colorScheme.onSurface, - ) - Row(horizontalArrangement = Arrangement.spacedBy(24.dp)) { - CopyCount(stringResource(R.string.import_done_added_label), summary.imported) - if (summary.skippedDuplicate > 0) { - CopyCount(stringResource(R.string.import_done_skipped_label), summary.skippedDuplicate) - } - if (summary.failed > 0) CopyCount(stringResource(R.string.import_done_failed_label), summary.failed) - } - if (summary.skippedDuplicate > 0) CopyNote(stringResource(R.string.import_done_dedup_note)) - if (summary.failed > 0) CopyNote(stringResource(R.string.import_done_failed_note)) - if (summary.notAttempted > 0) { - CopyNote( - pluralStringResource(R.plurals.calendars_copy_stopped, summary.notAttempted, summary.notAttempted), - error = true, - ) - } -} - -@Composable -private fun CopyCount(label: String, count: Int) { - Column { - Text(count.toString(), style = MaterialTheme.typography.headlineSmall, color = MaterialTheme.colorScheme.onSurface) - Text(label, style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.onSurfaceVariant) - } -} - -@Composable -private fun CopyNote(text: String, error: Boolean = false) { - Text( - text, - style = MaterialTheme.typography.bodyMedium, - color = if (error) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant, - ) -} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt index f07df8b7..d94fe9c4 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CopyEventsViewModel.kt @@ -120,7 +120,7 @@ data class CopySelection(val sourceId: Long? = null, val targetId: Long? = null) CopySelection(sourceId = id, targetId = targetId?.takeIf { it != id }) } -/** The calendar list the copy sheet picks from. */ +/** The calendar list the copy screen picks from. */ sealed interface CopyCalendars { data object Loading : CopyCalendars data object Failure : CopyCalendars @@ -137,7 +137,7 @@ sealed interface CopyRunState { /** The target stopped taking events part way, so some were never tried. */ val stopped: Boolean get() = summary.notAttempted > 0 - /** How the run ended, the same words on the row and in the sheet. */ + /** How the run ended, the same words on the row and on the result screen. */ @get:StringRes val titleRes: Int get() = when { stopped -> R.string.calendars_copy_stopped_title diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ResultSummary.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ResultSummary.kt new file mode 100644 index 00000000..0c9d5375 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/common/ResultSummary.kt @@ -0,0 +1,135 @@ +package de.jeanlucmakiola.calendula.ui.common + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.RowScope +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material.icons.filled.PriorityHigh +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.pluralStringResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.clearAndSetSemantics +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.domain.ics.IcsImportSummary +import de.jeanlucmakiola.floret.components.ResultBadge as FloretResultBadge + +/** floret's [FloretResultBadge] as a check on success, or an exclamation mark in the error colours. */ +@Composable +fun ResultBadge(error: Boolean) { + val scheme = MaterialTheme.colorScheme + if (error) { + FloretResultBadge(Icons.Default.PriorityHigh, scheme.errorContainer, scheme.onErrorContainer) + } else { + FloretResultBadge(Icons.Default.Check, scheme.primaryContainer, scheme.onPrimaryContainer) + } +} + +/** A result screen's headline under its badge, then its notes; an error note takes the error colour. */ +@Composable +fun ResultMessage(title: String, notes: List = emptyList()) { + Spacer(Modifier.height(24.dp)) + Text( + title, + style = MaterialTheme.typography.headlineSmall, + color = MaterialTheme.colorScheme.onSurface, + textAlign = TextAlign.Center, + ) + notes.forEach { note -> + Spacer(Modifier.height(8.dp)) + Text( + note.text, + style = MaterialTheme.typography.bodyMedium, + color = if (note.error) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant, + textAlign = TextAlign.Center, + ) + } +} + +data class ResultNote(val text: String, val error: Boolean = false) + +/** The notes an import summary carries: failures, skipped duplicates, then [stoppedNote] when part was never tried. */ +@Composable +fun importSummaryNotes(summary: IcsImportSummary, stoppedNote: String?): List = buildList { + if (summary.failed > 0) add(ResultNote(stringResource(R.string.import_done_failed_note))) + if (summary.skippedDuplicate > 0) add(ResultNote(stringResource(R.string.import_done_dedup_note))) + if (stoppedNote != null) add(ResultNote(stoppedNote, error = true)) +} + +/** Added / skipped / failed as big-number tiles, the latter two only when non-zero. */ +@Composable +fun ImportSummaryStats(summary: IcsImportSummary, modifier: Modifier = Modifier) { + Row(modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) { + ResultStatCard( + count = summary.imported, + label = stringResource(R.string.import_done_added_label), + contentDescription = pluralStringResource(R.plurals.import_done_imported, summary.imported, summary.imported), + container = MaterialTheme.colorScheme.secondaryContainer, + onContainer = MaterialTheme.colorScheme.onSecondaryContainer, + ) + if (summary.skippedDuplicate > 0) { + ResultStatCard( + count = summary.skippedDuplicate, + label = stringResource(R.string.import_done_skipped_label), + contentDescription = pluralStringResource( + R.plurals.import_done_skipped, + summary.skippedDuplicate, + summary.skippedDuplicate, + ), + container = MaterialTheme.colorScheme.surfaceContainerHighest, + onContainer = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + if (summary.failed > 0) { + ResultStatCard( + count = summary.failed, + label = stringResource(R.string.import_done_failed_label), + contentDescription = pluralStringResource(R.plurals.import_done_failed, summary.failed, summary.failed), + container = MaterialTheme.colorScheme.errorContainer, + onContainer = MaterialTheme.colorScheme.onErrorContainer, + ) + } + } +} + +/** A big-number tonal tile summarising one outcome. */ +@Composable +private fun RowScope.ResultStatCard( + count: Int, + label: String, + contentDescription: String, + container: Color, + onContainer: Color, +) { + Surface( + modifier = Modifier + .weight(1f) + .clearAndSetSemantics { this.contentDescription = contentDescription }, + shape = RoundedCornerShape(24.dp), + color = container, + ) { + Column( + Modifier.padding(vertical = 20.dp, horizontal = 12.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(2.dp), + ) { + Text(count.toString(), style = MaterialTheme.typography.displaySmall, color = onContainer) + Text(label, style = MaterialTheme.typography.labelLarge, color = onContainer.copy(alpha = 0.85f)) + } + } +} diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt index 9fd95c6c..242e24b2 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/imports/ImportScreen.kt @@ -1,15 +1,9 @@ package de.jeanlucmakiola.calendula.ui.imports import android.net.Uri -import androidx.compose.animation.core.Animatable -import androidx.compose.animation.core.Spring -import androidx.compose.animation.core.spring -import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth @@ -17,13 +11,9 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.shape.CircleShape -import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Check import androidx.compose.material.icons.filled.Close -import androidx.compose.material.icons.filled.PriorityHigh import androidx.compose.material3.Button import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.ExperimentalMaterial3Api @@ -31,25 +21,17 @@ import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold -import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clip -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.graphicsLayer -import androidx.compose.ui.semantics.clearAndSetSemantics -import androidx.compose.ui.semantics.contentDescription -import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp @@ -59,6 +41,10 @@ import de.jeanlucmakiola.calendula.R import de.jeanlucmakiola.calendula.domain.EventForm import de.jeanlucmakiola.calendula.domain.ics.IcsParseWarning import de.jeanlucmakiola.calendula.ui.common.CalendarPickerGroups +import de.jeanlucmakiola.calendula.ui.common.ImportSummaryStats +import de.jeanlucmakiola.calendula.ui.common.ResultBadge +import de.jeanlucmakiola.calendula.ui.common.ResultMessage +import de.jeanlucmakiola.calendula.ui.common.importSummaryNotes import de.jeanlucmakiola.floret.identity.predictiveBack /** @@ -211,18 +197,6 @@ private fun ManyContent( @Composable private fun DoneContent(state: ImportUiState.Done, onClose: () -> Unit) { - // A little expressive pop on the success badge — springs in on first show. - val badgeScale = remember { Animatable(0.7f) } - LaunchedEffect(Unit) { - badgeScale.animateTo( - targetValue = 1f, - animationSpec = spring( - dampingRatio = Spring.DampingRatioMediumBouncy, - stiffness = Spring.StiffnessLow, - ), - ) - } - // Part of the file never reached the calendar, so the screen doesn't get to // read as a plain success. val stopped = state.summary.notAttempted > 0 @@ -232,118 +206,23 @@ private fun DoneContent(state: ImportUiState.Done, onClose: () -> Unit) { horizontalAlignment = Alignment.CenterHorizontally, ) { Spacer(Modifier.weight(1f)) - Box( - Modifier - .size(112.dp) - .graphicsLayer { - scaleX = badgeScale.value - scaleY = badgeScale.value - } - .clip(CircleShape) - .background( - if (stopped) { - MaterialTheme.colorScheme.errorContainer - } else { - MaterialTheme.colorScheme.primaryContainer - }, - ), - contentAlignment = Alignment.Center, - ) { - Icon( - if (stopped) Icons.Default.PriorityHigh else Icons.Default.Check, - contentDescription = null, - tint = if (stopped) { - MaterialTheme.colorScheme.onErrorContainer - } else { - MaterialTheme.colorScheme.onPrimaryContainer - }, - modifier = Modifier.size(56.dp), - ) - } - Spacer(Modifier.height(24.dp)) - Text( - stringResource( + ResultBadge(error = stopped) + ResultMessage( + title = stringResource( if (stopped) R.string.import_done_stopped_title else R.string.import_done_title, ), - style = MaterialTheme.typography.headlineSmall, - color = MaterialTheme.colorScheme.onSurface, + // The counts cover the part of the file that was tried, so say how much never was. + notes = importSummaryNotes( + state.summary, + stoppedNote = if (stopped) { + pluralStringResource(R.plurals.import_done_stopped, state.summary.notAttempted, state.summary.notAttempted) + } else { + null + }, + ), ) - if (state.summary.failed > 0) { - Spacer(Modifier.height(8.dp)) - Text( - stringResource(R.string.import_done_failed_note), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - textAlign = TextAlign.Center, - ) - } - if (state.summary.skippedDuplicate > 0) { - Spacer(Modifier.height(8.dp)) - Text( - stringResource(R.string.import_done_dedup_note), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - textAlign = TextAlign.Center, - ) - } - // The one note that isn't about individual events: the counts below cover - // the part of the file that was tried, so say how much never was. - if (stopped) { - Spacer(Modifier.height(8.dp)) - Text( - pluralStringResource( - R.plurals.import_done_stopped, - state.summary.notAttempted, - state.summary.notAttempted, - ), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.error, - textAlign = TextAlign.Center, - ) - } Spacer(Modifier.height(24.dp)) - Row( - Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy(12.dp), - ) { - ImportStatCard( - count = state.summary.imported, - label = stringResource(R.string.import_done_added_label), - contentDescription = pluralStringResource( - R.plurals.import_done_imported, - state.summary.imported, - state.summary.imported, - ), - container = MaterialTheme.colorScheme.secondaryContainer, - onContainer = MaterialTheme.colorScheme.onSecondaryContainer, - ) - if (state.summary.skippedDuplicate > 0) { - ImportStatCard( - count = state.summary.skippedDuplicate, - label = stringResource(R.string.import_done_skipped_label), - contentDescription = pluralStringResource( - R.plurals.import_done_skipped, - state.summary.skippedDuplicate, - state.summary.skippedDuplicate, - ), - container = MaterialTheme.colorScheme.surfaceContainerHighest, - onContainer = MaterialTheme.colorScheme.onSurfaceVariant, - ) - } - if (state.summary.failed > 0) { - ImportStatCard( - count = state.summary.failed, - label = stringResource(R.string.import_done_failed_label), - contentDescription = pluralStringResource( - R.plurals.import_done_failed, - state.summary.failed, - state.summary.failed, - ), - container = MaterialTheme.colorScheme.errorContainer, - onContainer = MaterialTheme.colorScheme.onErrorContainer, - ) - } - } + ImportSummaryStats(state.summary) Spacer(Modifier.weight(1f)) Button( onClick = onClose, @@ -354,41 +233,6 @@ private fun DoneContent(state: ImportUiState.Done, onClose: () -> Unit) { } } -/** A big-number tonal tile summarising one import outcome (added / skipped / failed). */ -@Composable -private fun RowScope.ImportStatCard( - count: Int, - label: String, - contentDescription: String, - container: Color, - onContainer: Color, -) { - Surface( - modifier = Modifier - .weight(1f) - .clearAndSetSemantics { this.contentDescription = contentDescription }, - shape = RoundedCornerShape(24.dp), - color = container, - ) { - Column( - Modifier.padding(vertical = 20.dp, horizontal = 12.dp), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(2.dp), - ) { - Text( - count.toString(), - style = MaterialTheme.typography.displaySmall, - color = onContainer, - ) - Text( - label, - style = MaterialTheme.typography.labelLarge, - color = onContainer.copy(alpha = 0.85f), - ) - } - } -} - @Composable private fun WarningText(warning: IcsParseWarning) { val text = when (warning) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 791612c5..a699fe13 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -891,13 +891,14 @@ No calendar can take the imported events yet. Copy events Copy one calendar\'s events into another, for example to move off Google. - Copies your events, including repeating ones with the occurrences you changed or deleted. Cancelled events and invitations you declined are left out, guests stay behind, and the original calendar is left as it is. + The original calendar stays as it is. Guests and invitations you declined are left out. From To Choose a calendar Copy from Copy into Copy + Copy more Reading events… %1$d of %2$d event diff --git a/floret-kit b/floret-kit index 389cc029..f5b51922 160000 --- a/floret-kit +++ b/floret-kit @@ -1 +1 @@ -Subproject commit 389cc0298f341e293dee15f27dada3d72123b5f9 +Subproject commit f5b51922cf5ec2dc6eff2c70e08fbfec870eb138