Modern Apps git

← Modern-Apps.git

contacts: fix editor fields, icons and defaults (#604 #605 #606 #607 #608 #617)

Commit
39f43a07f3930c476fddcf665d9a511c07ec9197
Author
vayun-mathur <[email protected]>
When
2026-08-26 17:41 UTC (3 months ago)
Parents
7d4c0a1

Diff

contacts/src/main/java/com/vayunmathur/contacts/ui/EditContactPage.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/EditContactPage.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/EditContactPage.ktindex 2ffea3a91a..30d60616da 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/EditContactPage.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/EditContactPage.kt@@ -29,13 +29,13 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardOptions import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.AssistChip import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.DropdownMenu import com.vayunmathur.library.ui.DropdownMenuItem import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.FilledTonalButton import com.vayunmathur.library.ui.FormDetailGroup-import com.vayunmathur.library.ui.FormSection import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.InputChip import com.vayunmathur.library.ui.LabeledTextField@@ -58,6 +58,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringArrayResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.TextFieldValue@@ -89,6 +90,7 @@ import com.vayunmathur.library.ui.IconCall import com.vayunmathur.library.ui.IconEdit import com.vayunmathur.library.ui.IconEvent import com.vayunmathur.library.ui.IconGroup+import com.vayunmathur.library.ui.IconLocationOn import com.vayunmathur.library.ui.IconMail import com.vayunmathur.library.ui.IconRemoveCircle import com.vayunmathur.library.ui.appBarScrollBehavior@@ -203,7 +205,13 @@ fun EditContactPage(backStack: NavBackStack<Route>, viewModel: ContactViewModel,             Spacer(Modifier.height(24.dp))  -            FormSection {+            // Plain Column rather than FormSection: DetailScaffold already insets its content+            // horizontally, and the section's own padding on top of that made the name fields+            // narrower than every field below them.+            Column(+                modifier = Modifier.fillMaxWidth(),+                verticalArrangement = Arrangement.spacedBy(12.dp),+            ) {                 LabeledTextField(                     value = currentDraft.firstName,                     onValueChange = { v -> viewModel.updateEditDraft { it.copy(firstName = v) } },@@ -343,7 +351,7 @@ fun EditContactPage(backStack: NavBackStack<Route>, viewModel: ContactViewModel,                 onLabelChange = { idx, v -> viewModel.updateEditDraft { it.copy(addresses = it.addresses.toMutableList().also { l -> l[idx] = l[idx].withLabel(v) }) } },                 customLabelText = stringResource(R.string.custom_label),                 customPlaceholder = stringResource(R.string.enter_custom_label),-                addIcon = { IconEvent() },+                addIcon = { IconLocationOn() },             )              Spacer(Modifier.height(16.dp))@@ -392,6 +400,7 @@ fun AccountChooser(             onValueChange = {},             readOnly = true,             label = { Text(stringResource(R.string.account)) },+            singleLine = true,             modifier = Modifier.fillMaxWidth(),             trailingIcon = {                 IconButton(onClick = { expanded = true }) {@@ -442,19 +451,34 @@ private fun getCountryFlagEmoji(phoneNumber: String): String {     } } -val namePrefixes = listOf("None", "Dr", "Mr", "Mrs", "Ms")-val nameSuffixes = listOf("None", "Jr", "Sr", "I", "II", "III", "IV", "V")-+/**+ * The prefix/suffix picker that sits inside the first/last name fields.+ *+ * A chip rather than a bare [TextButton]: it shares the field's background otherwise, so+ * there was nothing to show it is a separate value and not part of the name being typed.+ * [placeholder] is shown while nothing is chosen, since an empty chip reads as a stray icon.+ */ @Composable-fun NamePrefixChooser(namePrefix: String, onNamePrefixChange: (String) -> Unit) {+private fun NameAffixChooser(+    value: String,+    placeholder: String,+    options: List<String>,+    onValueChange: (String) -> Unit,+) {     var expanded by remember { mutableStateOf(false) }-    TextButton(onClick = { expanded = true }) {-        Text(namePrefix)-        IconArrowDropDown()+    val none = stringResource(R.string.name_affix_none)+    Box {+        AssistChip(+            onClick = { expanded = true },+            label = { Text(value.ifEmpty { placeholder }) },+            trailingIcon = { IconArrowDropDown() },+        )         DropdownMenu(expanded, { expanded = false }) {-            namePrefixes.forEach { prefix ->-                DropdownMenuItem(text = { Text(prefix) }, onClick = {-                    onNamePrefixChange(if(prefix == "None") "" else prefix)+            // The clear option is listed by its localized name but stores an empty affix, so+            // the contact does not end up with the word "None" as its title.+            (listOf(none) + options).forEach { option ->+                DropdownMenuItem(text = { Text(option) }, onClick = {+                    onValueChange(if (option == none) "" else option)                     expanded = false                 })             }@@ -462,23 +486,29 @@ fun NamePrefixChooser(namePrefix: String, onNamePrefixChange: (String) -> Unit)     } } +@Composable+fun NamePrefixChooser(namePrefix: String, onNamePrefixChange: (String) -> Unit) {+    NameAffixChooser(+        value = namePrefix,+        placeholder = stringResource(R.string.name_prefix),+        options = stringArrayResource(R.array.name_prefixes).toList(),+        onValueChange = onNamePrefixChange,+    )+}+ @Composable fun NameSuffixChooser(nameSuffix: String, onNameSuffixChange: (String) -> Unit) {-    var expanded by remember { mutableStateOf(false) }-    TextButton(onClick = { expanded = true }) {-        Text(nameSuffix)-        IconArrowDropDown()-        DropdownMenu(expanded, { expanded = false }) {-            nameSuffixes.forEach { suffix ->-                DropdownMenuItem(text = { Text(suffix) }, onClick = {-                    onNameSuffixChange(suffix)-                    expanded = false-                })-            }-        }-    }+    NameAffixChooser(+        value = nameSuffix,+        placeholder = stringResource(R.string.name_suffix),+        options = stringArrayResource(R.array.name_suffixes).toList(),+        onValueChange = onNameSuffixChange,+    ) } +/** Width of a field's trailing [IconButton], kept clear of any full-row tap overlay. */+private val RemoveButtonWidth = 48.dp+ @OptIn(ExperimentalMaterial3Api::class) @Composable private fun Birthday(@@ -495,6 +525,7 @@ private fun Birthday(             onValueChange = { },             readOnly = true,             label = {Text(stringResource(R.string.birthday))},+            singleLine = true,             modifier = Modifier.fillMaxWidth(),             trailingIcon = {                 IconButton(onClick = { setBirthday(null) }) {@@ -502,12 +533,16 @@ private fun Birthday(                 }             }         )-        Box(-            modifier = Modifier-                .matchParentSize()-        ) {-            Box(Modifier.fillMaxWidth(0.9f).fillMaxHeight()-                .clickable { backStack.add(Route.EventDatePickerDialog("birthday",birthday)) }){}+        // The field is read-only and opens the picker, so the whole row is the tap target -+        // except the trailing remove button, which needs its own clicks to reach it.+        Row(Modifier.matchParentSize()) {+            Box(+                Modifier+                    .weight(1f)+                    .fillMaxHeight()+                    .clickable { backStack.add(Route.EventDatePickerDialog("birthday", birthday)) }+            )+            Spacer(Modifier.width(RemoveButtonWidth))         }     } @@ -564,6 +599,7 @@ private fun ColumnScope.DateDetailsSection(                         }                     }                 },+                singleLine = true,                 modifier = Modifier.fillMaxWidth()             )             Box(@@ -583,6 +619,7 @@ private fun ColumnScope.DateDetailsSection(                 },                 label = { Text(stringResource(R.string.custom_label)) },                 placeholder = { Text(stringResource(R.string.enter_custom_label)) },+                singleLine = true,                 modifier = Modifier.fillMaxWidth()             )         }
contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.ktindex 2a47ba319c..58f6ecf5f9 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt@@ -19,6 +19,7 @@ import com.vayunmathur.contacts.data.CDKNickname import com.vayunmathur.contacts.data.CDKPhone import com.vayunmathur.contacts.data.CDKStructuredPostal import com.vayunmathur.contacts.data.Contact+import com.vayunmathur.contacts.data.ContactDetail import com.vayunmathur.contacts.data.ContactDetails import com.vayunmathur.contacts.data.ContactGroup import com.vayunmathur.contacts.data.ContactPrefill@@ -831,7 +832,10 @@ class ContactViewModel(application: Application) : AndroidViewModel(application)             birthday = contact?.birthday?.startDate,             accountName = contact?.accountName ?: _lastSelectedAccount.value?.name ?: "",             accountType = contact?.accountType ?: _lastSelectedAccount.value?.type ?: "",-            phoneNumbers = mergePhones(details?.phoneNumbers ?: emptyList(), prefill?.phones ?: emptyList()),+            // Almost every contact has a phone number, so the editor opens with a row ready+            // rather than making the user press "add phone" first. Blank rows are dropped on save.+            phoneNumbers = mergePhones(details?.phoneNumbers ?: emptyList(), prefill?.phones ?: emptyList())+                .ifEmpty { listOf(ContactDetail.default<PhoneNumber>()) },             emails = mergeEmails(details?.emails ?: emptyList(), prefill?.emails ?: emptyList()),             dates = details?.dates ?: emptyList(),             addresses = mergeAddresses(details?.addresses ?: emptyList(), prefill?.postals ?: emptyList()),@@ -984,13 +988,14 @@ class ContactViewModel(application: Application) : AndroidViewModel(application)             }         }         val original = editingOriginal+        val phoneNumbers = draft.phoneNumbers.filter { it.number.isNotBlank() }         val birthdayId = original?.birthday?.id ?: 0L         val datesWithoutBirthday = draft.dates.filter { it.type != CDKEvent.TYPE_BIRTHDAY }.toMutableList()         draft.birthday?.let { bday ->             datesWithoutBirthday += Event(birthdayId, bday, CDKEvent.TYPE_BIRTHDAY)         }         val details = ContactDetails(-            phoneNumbers = draft.phoneNumbers,+            phoneNumbers = phoneNumbers,             emails = draft.emails,             addresses = draft.addresses,             dates = datesWithoutBirthday,
contacts/src/main/res/values/strings.xml
diff --git a/contacts/src/main/res/values/strings.xml b/contacts/src/main/res/values/strings.xmlindex c1ad443f52..b8a52fd882 100644--- a/contacts/src/main/res/values/strings.xml+++ b/contacts/src/main/res/values/strings.xml@@ -37,6 +37,30 @@     <string name="middle_name">Middle name</string>     <!-- Text field label for last/family name -->     <string name="last_name">Last name</string>+    <!-- Dropdown next to the first name field, for honorifics placed before a name -->+    <string name="name_prefix">Prefix</string>+    <!-- Dropdown next to the last name field, for generational titles placed after a name -->+    <string name="name_suffix">Suffix</string>+    <!-- Option in the name prefix/suffix dropdowns that clears the chosen title -->+    <string name="name_affix_none">None</string>+    <!-- Name prefixes offered in the dropdown. Translate to the honorifics used locally. -->+    <string-array name="name_prefixes">+        <item>Dr</item>+        <item>Mr</item>+        <item>Mrs</item>+        <item>Ms</item>+    </string-array>+    <!-- Name suffixes offered in the dropdown. The roman numerals are generational and are+         normally left as-is; translate Jr and Sr. -->+    <string-array name="name_suffixes">+        <item>Jr</item>+        <item>Sr</item>+        <item>I</item>+        <item>II</item>+        <item>III</item>+        <item>IV</item>+        <item>V</item>+    </string-array>     <!-- Text field label for nickname -->     <string name="nickname">Nickname</string>     <!-- Text field label for company/organization name -->
library/src/main/java/com/vayunmathur/library/util/Navigation.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt b/library/src/main/java/com/vayunmathur/library/util/Navigation.ktindex 963e98d84d..25c1cba3f4 100644--- a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt+++ b/library/src/main/java/com/vayunmathur/library/util/Navigation.kt@@ -40,6 +40,9 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.takeOrElse import androidx.compose.ui.unit.dp+import androidx.lifecycle.ViewModel+import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner+import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation3.runtime.NavEntry import androidx.navigation3.scene.DialogSceneStrategy import androidx.navigation3.ui.NavDisplay@@ -237,9 +240,33 @@ fun <T: NavKey> MainNavigation(         }     } }+/**+ * Keeps a [NavBackStack] alive while the Activity around it is destroyed and recreated.+ *+ * A ViewModel rather than `rememberSaveable` because the stack holds app-defined [NavKey]s,+ * which are not all bundleable — and a configuration change is what actually loses them.+ */+internal class RetainedNavBackStack : ViewModel() {+    var stack: NavBackStack<*>? = null+}+ @Composable fun <T: NavKey> rememberNavBackStack(vararg elements: T): NavBackStack<T> {-    return remember { NavBackStack(elements) }+    // Changing the font scale or rotating recreates the Activity, and a plain remember would+    // rebuild the stack from `elements` — dropping the user back on the start screen, losing+    // whatever they were part-way through. Keyed on the start route so an owner hosting more+    // than one stack (previews render several) gives each its own.+    val owner = LocalViewModelStoreOwner.current+        ?: return remember { NavBackStack(elements) }+    val retained = viewModel<RetainedNavBackStack>(+        viewModelStoreOwner = owner,+        key = "nav-back-stack-${elements.firstOrNull()?.let { it::class.qualifiedName }}",+    )+    return remember(retained) {+        @Suppress("UNCHECKED_CAST")+        (retained.stack as? NavBackStack<T>)+            ?: NavBackStack(elements).also { retained.stack = it }+    } }  fun DialogPage() = DialogSceneStrategy.dialog()
library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.ktindex b06302612d..42d783c611 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/FormComponents.kt@@ -176,6 +176,7 @@ fun <T> FormDetailGroup(                     }                 },                 keyboardOptions = KeyboardOptions(keyboardType = keyboardType),+                singleLine = true,                 modifier = Modifier.fillMaxWidth(),             )             if (isCustom(item) && onLabelChange != null) {@@ -185,6 +186,7 @@ fun <T> FormDetailGroup(                     onValueChange = { v -> onLabelChange(index, v) },                     label = { Text(customLabelText ?: label) },                     placeholder = customPlaceholder?.let { { Text(it) } },+                    singleLine = true,                     modifier = Modifier.fillMaxWidth(),                 )             }