Show Home's name in the view editor, size icon picker to five per row (#59)

This commit is contained in:
makiolaj committed 2026-11-30 19:00:14 +01:00
1 parent 662e167c6f
commit 3b418076f1
2 files changed
+45 -23

No files matched your search

@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.ui.views
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Spacer
@@ -25,6 +26,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
@@ -90,10 +92,11 @@ fun EditViewScreen(
) {
if (view == null) return@CollapsingScaffold
// Home keeps its translated name until it is given one of its own.
val homeLabel = stringResource(R.string.view_home)
LaunchedEffect(view.id) { viewModel.fillHomeName(homeLabel) }
NameField(
name = view.name,
placeholder = if (view.id == View.HOME_ID) stringResource(R.string.view_home) else stringResource(R.string.view_name_hint),
placeholder = if (view.id == View.HOME_ID) homeLabel else stringResource(R.string.view_name_hint),
autoFocus = isNew,
onNameChange = viewModel::rename,
onImeAction = save,
@@ -192,6 +195,8 @@ fun EditViewScreen(
/** How many icons lead the picker as suggestions. */
private const val SUGGESTED_ICONS = 10
private const val ICONS_PER_ROW = 5
private val IconGap = 12.dp
/**
* The curated view icons in a full-screen grid; picking one closes it. With
@@ -221,25 +226,28 @@ internal fun ViewIconPicker(
val (suggested, more) = ViewIcon.entries.withIndex().partition { it.index < SUGGESTED_ICONS }
listOf(R.string.view_icon_suggested to suggested, R.string.view_icon_more to more).forEach { (header, icons) ->
GroupedSectionHeader(stringResource(header))
FlowRow(
modifier = Modifier.fillMaxWidth().padding(horizontal = GroupedListInset),
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
icons.forEach { (_, icon) ->
val chosen = icon == current
Surface(
onClick = {
onSelect(icon)
onDismiss()
},
shape = if (chosen) CircleShape else RoundedCornerShape(16.dp),
color = if (chosen) MaterialTheme.colorScheme.secondaryContainer else MaterialTheme.colorScheme.surfaceContainerHigh,
contentColor = if (chosen) MaterialTheme.colorScheme.onSecondaryContainer else MaterialTheme.colorScheme.onSurface,
modifier = Modifier.size(56.dp).semantics { selected = chosen },
) {
Box(contentAlignment = Alignment.Center) {
Icon(icon.vector, contentDescription = stringResource(icon.label))
BoxWithConstraints(Modifier.fillMaxWidth().padding(horizontal = GroupedListInset)) {
val tile = (maxWidth - IconGap * (ICONS_PER_ROW - 1)) / ICONS_PER_ROW
FlowRow(
maxItemsInEachRow = ICONS_PER_ROW,
horizontalArrangement = Arrangement.spacedBy(IconGap),
verticalArrangement = Arrangement.spacedBy(IconGap),
) {
icons.forEach { (_, icon) ->
val chosen = icon == current
Surface(
onClick = {
onSelect(icon)
onDismiss()
},
shape = if (chosen) CircleShape else RoundedCornerShape(20.dp),
color = if (chosen) MaterialTheme.colorScheme.secondaryContainer else MaterialTheme.colorScheme.surfaceContainerHigh,
contentColor = if (chosen) MaterialTheme.colorScheme.onSecondaryContainer else MaterialTheme.colorScheme.onSurface,
modifier = Modifier.size(tile).semantics { selected = chosen },
) {
Box(contentAlignment = Alignment.Center) {
Icon(icon.vector, contentDescription = stringResource(icon.label), modifier = Modifier.size(28.dp))
}
}
}
}
@@ -110,6 +110,7 @@ class EditViewViewModel @Inject constructor(
private var bound = false
private var saving = false
private var homeLabel: String? = null
private val _saveFailed = MutableStateFlow(false)
@@ -130,6 +131,18 @@ class EditViewViewModel @Inject constructor(
fun rename(name: String) = _draft.update { it?.copy(name = name) }
/**
* Shows Home's translated [label] as its name while it has none of its own;
* saved unchanged, it stays unnamed so it keeps following the app's language.
*/
fun fillHomeName(label: String) {
if (homeLabel != null) return
val draft = _draft.value ?: return
if (draft.id != View.HOME_ID) return
homeLabel = label
if (draft.name.isBlank()) rename(label)
}
fun setIcon(icon: ViewIcon) = _draft.update { it?.copy(icon = icon) }
fun setQuickAdd(on: Boolean) = _draft.update { it?.copy(quickAdd = on) }
@@ -152,8 +165,9 @@ class EditViewViewModel @Inject constructor(
onSaved()
return@launch
}
val view = stored?.copy(name = draft.name.trim(), icon = draft.icon, scope = draft.scope, quickAdd = draft.quickAdd)
?: draft.copy(name = draft.name.trim())
val name = draft.name.trim().takeUnless { draft.id == View.HOME_ID && it == homeLabel }.orEmpty()
val view = stored?.copy(name = name, icon = draft.icon, scope = draft.scope, quickAdd = draft.quickAdd)
?: draft.copy(name = name)
val id = settingsPrefs.saveView(view)
if (id == null) {
saving = false