Edit layout's ✕ puts the blocks back as they were, asking first when something changed (#59)

This commit is contained in:
makiolaj committed 2026-11-27 17:18:19 +01:00
1 parent 51561a9a11
commit dc9c08ce2b
4 files changed
+67 -5

No files matched your search

@@ -51,6 +51,9 @@ object ActionShapes {
/** Switch a block between half and full width — a pill, wide like what it makes. */
val ResizeBlock: RoundedPolygon get() = MaterialShapes.Pill
/** Throw away a layout's edits — a clover, apart from the softer ✓ beside it. */
val DiscardLayout: RoundedPolygon get() = MaterialShapes.Clover8Leaf
/** Take a block out of a layout — a soft burst, apart from [Delete], since it can be undone. */
val RemoveBlock: RoundedPolygon get() = MaterialShapes.SoftBurst
}
@@ -98,18 +98,26 @@ import de.jeanlucmakiola.floret.components.SelectedCheck
import de.jeanlucmakiola.floret.components.positionOf
import de.jeanlucmakiola.floret.identity.FloretDensity
/** The top bar while a layout is edited: its title, *Add block* and ✓ Done. */
/** The top bar while a layout is edited: ✕ to discard, its title, *Add block* and ✓ Done. */
@Composable
internal fun EditLayoutTopBar(onAdd: () -> Unit, onDone: () -> Unit) {
internal fun EditLayoutTopBar(onDiscard: () -> Unit, onAdd: () -> Unit, onDone: () -> Unit) {
Surface(color = MaterialTheme.colorScheme.surface) {
Row(
modifier = Modifier
.fillMaxWidth()
.windowInsetsPadding(TopAppBarDefaults.windowInsets)
.height(72.dp)
.padding(start = 24.dp, end = 12.dp),
.padding(horizontal = 12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
ActionButton(
shape = ActionShapes.DiscardLayout,
icon = Icons.Rounded.Close,
contentDescription = stringResource(R.string.edit_layout_discard),
onClick = onDiscard,
size = 48.dp,
)
Spacer(Modifier.width(8.dp))
Text(
stringResource(R.string.view_edit_layout),
style = MaterialTheme.typography.titleLarge,
@@ -31,6 +31,7 @@ import androidx.compose.material.icons.rounded.MoreVert
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material.icons.rounded.Tune
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
@@ -38,6 +39,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
@@ -156,7 +158,12 @@ fun ListsScreen(
viewModel.clearWriteFailure()
}
}
// System back closes search, or leaves the layout editor, before leaving the screen.
// What a discard of the layout editing would put back, and whether that changes anything.
val layoutAtStart by viewModel.layoutAtStart.collectAsStateWithLifecycle()
LaunchedEffect(editing, content?.view?.id) { if (editing) content?.view?.let(viewModel::beginEditing) }
val layoutChanged = layoutAtStart?.let { start -> start.viewId == content?.view?.id && start.blocks != content.view.blocks } == true
var confirmDiscard by remember { mutableStateOf(false) }
// System back closes search, or leaves the layout editor keeping its edits as ✓ does, before leaving the screen.
BackHandler(enabled = searchActive, onBack = closeSearch)
BackHandler(enabled = editing, onBack = viewModel::stopEditingLayout)
LaunchedEffect(editing) { if (editing) closeSearch() }
@@ -197,7 +204,11 @@ fun ListsScreen(
modifier = modifier,
topBar = {
if (editing) {
EditLayoutTopBar(onAdd = { addingBlock = true }, onDone = viewModel::stopEditingLayout)
EditLayoutTopBar(
onDiscard = { if (layoutChanged) confirmDiscard = true else viewModel.discardLayoutEdits() },
onAdd = { addingBlock = true },
onDone = viewModel::stopEditingLayout,
)
} else {
HomeTopBar(
viewTitle = content?.view?.let { viewName(it.name, it.id) },
@@ -362,6 +373,22 @@ fun ListsScreen(
)
}
if (confirmDiscard) {
AlertDialog(
onDismissRequest = { confirmDiscard = false },
title = { Text(stringResource(R.string.edit_layout_discard_title)) },
text = { Text(stringResource(R.string.edit_layout_discard_body)) },
confirmButton = {
TextButton(onClick = { confirmDiscard = false; viewModel.discardLayoutEdits() }) {
Text(stringResource(R.string.edit_layout_discard_confirm))
}
},
dismissButton = {
TextButton(onClick = { confirmDiscard = false }) { Text(stringResource(R.string.dialog_cancel)) }
},
)
}
if (content == null || !editing) return
if (addingBlock) {
AddBlockPicker(
@@ -122,6 +122,9 @@ data class WeekStripDays(val days: List<LocalDate>, val today: LocalDate, val du
/** A block just removed from view [viewId], from [index], until its undo lapses. */
data class RemovedBlock(val viewId: Long, val block: Block, val index: Int)
/** The blocks of view [viewId] when its layout editing began, which a discard restores. */
data class LayoutAtStart(val viewId: Long, val blocks: List<Block>)
/** Block [block] of view [viewId] as its settings screen changes it, until saved. */
data class BlockDraft(val viewId: Long, val block: Block)
@@ -450,9 +453,30 @@ class ListsViewModel @Inject constructor(
(state.value as? ListsUiState.Content)?.view?.id?.let(session::editLayout)
}
/** Leaves the layout editor keeping what was changed in it. */
fun stopEditingLayout() {
session.stopEditingLayout()
_blockDraft.value = null
_layoutAtStart.value = null
}
private val _layoutAtStart = MutableStateFlow<LayoutAtStart?>(null)
/** The blocks of the view being edited as they were when its editing began. */
val layoutAtStart: StateFlow<LayoutAtStart?> = _layoutAtStart.asStateFlow()
/** Notes [view]'s blocks as the ones a discard goes back to, once per editing session. */
fun beginEditing(view: View) {
if (_layoutAtStart.value?.viewId != view.id) _layoutAtStart.value = LayoutAtStart(view.id, view.blocks)
}
/** Leaves the layout editor and puts the blocks back as they were when it opened. */
fun discardLayoutEdits() {
val start = _layoutAtStart.value
removedTimer?.cancel()
_removed.value = null
stopEditingLayout()
if (start != null) editBlocks(start.viewId) { start.blocks }
}
/**