Modern Apps git

← Modern-Apps.git

contacts: multi-number action buttons, reliable edit refresh (#254, #658)

Commit
85e35fb7f87bb7dd4119ff15813cc8e0fff2ac56
Author
vayun-mathur <[email protected]>
When
2026-10-14 00:50 UTC (1 month ago)
Parents
8e44c24

Diff

contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsActionButtons.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsActionButtons.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsActionButtons.ktnew file mode 100644index 0000000000..f182277aa1--- /dev/null+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsActionButtons.kt@@ -0,0 +1,100 @@+package com.vayunmathur.contacts.ui++import android.content.Context+import androidx.compose.foundation.background+import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.unit.dp+import com.vayunmathur.contacts.data.PhoneNumber+import com.vayunmathur.contacts.util.ContactPlatforms+import com.vayunmathur.contacts.util.PackageUtils+import com.vayunmathur.library.ui.BadgedBox+import com.vayunmathur.library.ui.DropdownMenu+import com.vayunmathur.library.ui.DropdownMenuItem+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Text++/** Number-first picker shown by the Call/Message/Video buttons for multi-number contacts. */+@Composable+internal fun NumberPickerDropdown(+    expanded: Boolean,+    onDismiss: () -> Unit,+    phoneNumbers: List<PhoneNumber>,+    onPick: (String) -> Unit,+) {+    val context = LocalContext.current+    DropdownMenu(expanded = expanded, onDismissRequest = onDismiss) {+        phoneNumbers.forEach { phone ->+            DropdownMenuItem(+                text = {+                    Column {+                        Text(formatPhoneNumber(phone.number))+                        Text(+                            phone.typeString(context),+                            style = MaterialTheme.typography.bodySmall,+                            color = MaterialTheme.colorScheme.onSurfaceVariant,+                        )+                    }+                },+                onClick = { onPick(phone.number) }+            )+        }+    }+}++internal fun launchSingleVideoOption(+    context: Context,+    number: String,+    platforms: ContactPlatforms,+    isGoogleMeetInstalled: Boolean,+) {+    when {+        isGoogleMeetInstalled -> launchGoogleMeet(context, number)+        platforms.whatsAppVideoId != null -> placePlatformCall(context, number, PackageUtils.WHATSAPP_PACKAGE, isVideo = true, fallbackDataRowId = platforms.whatsAppVideoId)+        platforms.signalVideoId != null -> placePlatformCall(context, number, PackageUtils.SIGNAL_PACKAGE, isVideo = true, fallbackDataRowId = platforms.signalVideoId)+        platforms.telegramVideoId != null -> placePlatformCall(context, number, PackageUtils.TELEGRAM_PACKAGE, isVideo = true, fallbackDataRowId = platforms.telegramVideoId)+    }+}++@Composable+fun ActionButton(+    icon: @Composable () -> Unit,+    label: String,+    dropdownContent: (@Composable () -> Unit)? = null,+    action: () -> Unit+) {+    Column(+        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,+        verticalArrangement = Arrangement.spacedBy(8.dp)+    ) {+        BadgedBox(+            badge = {}+        ) {+            Box(+                modifier = Modifier+                    .size(56.dp)+                    .clip(CircleShape)+                    .background(MaterialTheme.colorScheme.surfaceVariant)+                    .clickable { action() },+                contentAlignment = androidx.compose.ui.Alignment.Center+            ) {+                Box(+                    modifier = Modifier.size(24.dp),+                    contentAlignment = androidx.compose.ui.Alignment.Center+                ) {+                    icon()+                }+                dropdownContent?.invoke()+            }+        }+        Text(text = label, style = MaterialTheme.typography.labelMedium)+    }+}
contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsHeader.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsHeader.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsHeader.ktindex 620c6d5b10..e5bc8bc005 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsHeader.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsHeader.kt@@ -1,10 +1,7 @@ package com.vayunmathur.contacts.ui  import android.content.Intent-import androidx.compose.foundation.background-import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement-import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row@@ -12,15 +9,12 @@ import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size-import androidx.compose.foundation.shape.CircleShape 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.Alignment import androidx.compose.ui.Modifier-import androidx.compose.ui.draw.clip import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight@@ -29,9 +23,9 @@ import androidx.compose.ui.unit.dp import androidx.core.net.toUri import com.vayunmathur.contacts.R import com.vayunmathur.contacts.data.Contact+import com.vayunmathur.contacts.data.PhoneNumber import com.vayunmathur.contacts.util.ContactPlatforms import com.vayunmathur.contacts.util.PackageUtils-import com.vayunmathur.library.ui.BadgedBox import com.vayunmathur.library.ui.DropdownMenu import com.vayunmathur.library.ui.DropdownMenuItem import com.vayunmathur.library.ui.ExternalIntents@@ -120,7 +114,7 @@ fun ProfileHeader(contact: Contact, decodePhoto: ((String) -> android.graphics.B  @Composable fun ActionButtonsRow(-    number: String?,+    phoneNumbers: List<PhoneNumber>,     email: String?,     platforms: ContactPlatforms,     isGoogleMeetInstalled: Boolean@@ -132,7 +126,13 @@ fun ActionButtonsRow(             .padding(vertical = 8.dp),         horizontalArrangement = Arrangement.SpaceEvenly     ) {-        if (number != null) {+        if (phoneNumbers.isNotEmpty()) {+            var callNumber by remember(phoneNumbers) { mutableStateOf(phoneNumbers.first().number) }+            var smsNumber by remember(phoneNumbers) { mutableStateOf(phoneNumbers.first().number) }+            var videoNumber by remember(phoneNumbers) { mutableStateOf(phoneNumbers.first().number) }+            var showCallNumberPicker by remember { mutableStateOf(false) }+            var showSmsNumberPicker by remember { mutableStateOf(false) }+            var showVideoNumberPicker by remember { mutableStateOf(false) }             var showCallDropdown by remember { mutableStateOf(false) }             var showSmsDropdown by remember { mutableStateOf(false) }             var showVideoDropdown by remember { mutableStateOf(false) }@@ -141,37 +141,54 @@ fun ActionButtonsRow(                 icon = { IconCall() },                 label = stringResource(UiR.string.call),                 action = {-                    if (platforms.hasAnyPlatform) {+                    if (phoneNumbers.size == 1 && !platforms.hasAnyPlatform) {+                        placeCall(context, phoneNumbers.first().number)+                    } else if (phoneNumbers.size == 1) {+                        callNumber = phoneNumbers.first().number                         showCallDropdown = true                     } else {-                        placeCall(context, number)+                        showCallNumberPicker = true                     }                 },                 dropdownContent = {+                    NumberPickerDropdown(+                        expanded = showCallNumberPicker,+                        onDismiss = { showCallNumberPicker = false },+                        phoneNumbers = phoneNumbers,+                        onPick = { picked ->+                            showCallNumberPicker = false+                            if (platforms.hasAnyPlatform) {+                                callNumber = picked+                                showCallDropdown = true+                            } else {+                                placeCall(context, picked)+                            }+                        }+                    )                     DropdownMenu(expanded = showCallDropdown, onDismissRequest = { showCallDropdown = false }) {                         DropdownMenuItem(                             text = { Text(stringResource(R.string.system_default)) },                             onClick = {-                                placeCall(context, number)+                                placeCall(context, callNumber)                                 showCallDropdown = false                             }                         )                         platforms.signalCallId?.let { id ->                             DropdownMenuItem(                                 text = { Text(stringResource(R.string.signal)) },-                                onClick = { placePlatformCall(context, number, PackageUtils.SIGNAL_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }+                                onClick = { placePlatformCall(context, callNumber, PackageUtils.SIGNAL_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }                             )                         }                         platforms.whatsAppCallId?.let { id ->                             DropdownMenuItem(                                 text = { Text(stringResource(R.string.whatsapp)) },-                                onClick = { placePlatformCall(context, number, PackageUtils.WHATSAPP_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }+                                onClick = { placePlatformCall(context, callNumber, PackageUtils.WHATSAPP_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }                             )                         }                         platforms.telegramCallId?.let { id ->                             DropdownMenuItem(                                 text = { Text(stringResource(R.string.telegram)) },-                                onClick = { placePlatformCall(context, number, PackageUtils.TELEGRAM_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }+                                onClick = { placePlatformCall(context, callNumber, PackageUtils.TELEGRAM_PACKAGE, fallbackDataRowId = id); showCallDropdown = false }                             )                         }                     }@@ -181,18 +198,35 @@ fun ActionButtonsRow(                 icon = { IconSms() },                 label = stringResource(R.string.action_message),                 action = {-                    if (platforms.hasAnyPlatform) {+                    if (phoneNumbers.size == 1 && !platforms.hasAnyPlatform) {+                        ExternalIntents.sendSms(context, phoneNumbers.first().number)+                    } else if (phoneNumbers.size == 1) {+                        smsNumber = phoneNumbers.first().number                         showSmsDropdown = true                     } else {-                        ExternalIntents.sendSms(context, number)+                        showSmsNumberPicker = true                     }                 },                 dropdownContent = {+                    NumberPickerDropdown(+                        expanded = showSmsNumberPicker,+                        onDismiss = { showSmsNumberPicker = false },+                        phoneNumbers = phoneNumbers,+                        onPick = { picked ->+                            showSmsNumberPicker = false+                            if (platforms.hasAnyPlatform) {+                                smsNumber = picked+                                showSmsDropdown = true+                            } else {+                                ExternalIntents.sendSms(context, picked)+                            }+                        }+                    )                     DropdownMenu(expanded = showSmsDropdown, onDismissRequest = { showSmsDropdown = false }) {                         DropdownMenuItem(                             text = { Text(stringResource(R.string.system_default)) },                             onClick = {-                                ExternalIntents.sendSms(context, number)+                                ExternalIntents.sendSms(context, smsNumber)                                 showSmsDropdown = false                             }                         )@@ -234,41 +268,55 @@ fun ActionButtonsRow(                     icon = { IconVideoCamera() },                     label = stringResource(R.string.action_video),                     action = {-                        if (videoOptionCount == 1) {-                            when {-                                isGoogleMeetInstalled -> launchGoogleMeet(context, number)-                                platforms.whatsAppVideoId != null -> placePlatformCall(context, number, PackageUtils.WHATSAPP_PACKAGE, isVideo = true, fallbackDataRowId = platforms.whatsAppVideoId)-                                platforms.signalVideoId != null -> placePlatformCall(context, number, PackageUtils.SIGNAL_PACKAGE, isVideo = true, fallbackDataRowId = platforms.signalVideoId)-                                platforms.telegramVideoId != null -> placePlatformCall(context, number, PackageUtils.TELEGRAM_PACKAGE, isVideo = true, fallbackDataRowId = platforms.telegramVideoId)-                            }+                        if (phoneNumbers.size > 1) {+                            showVideoNumberPicker = true                         } else {-                            showVideoDropdown = true+                            videoNumber = phoneNumbers.first().number+                            if (videoOptionCount == 1) {+                                launchSingleVideoOption(context, videoNumber, platforms, isGoogleMeetInstalled)+                            } else {+                                showVideoDropdown = true+                            }                         }                     },                     dropdownContent = {+                        NumberPickerDropdown(+                            expanded = showVideoNumberPicker,+                            onDismiss = { showVideoNumberPicker = false },+                            phoneNumbers = phoneNumbers,+                            onPick = { picked ->+                                showVideoNumberPicker = false+                                videoNumber = picked+                                if (videoOptionCount == 1) {+                                    launchSingleVideoOption(context, picked, platforms, isGoogleMeetInstalled)+                                } else {+                                    showVideoDropdown = true+                                }+                            }+                        )                         DropdownMenu(expanded = showVideoDropdown, onDismissRequest = { showVideoDropdown = false }) {                             if (isGoogleMeetInstalled) {                                 DropdownMenuItem(                                     text = { Text(stringResource(R.string.google_meet)) },-                                    onClick = { launchGoogleMeet(context, number); showVideoDropdown = false }+                                    onClick = { launchGoogleMeet(context, videoNumber); showVideoDropdown = false }                                 )                             }                             platforms.whatsAppVideoId?.let { id ->                                 DropdownMenuItem(                                     text = { Text(stringResource(R.string.whatsapp)) },-                                    onClick = { placePlatformCall(context, number, PackageUtils.WHATSAPP_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }+                                    onClick = { placePlatformCall(context, videoNumber, PackageUtils.WHATSAPP_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }                                 )                             }                             platforms.signalVideoId?.let { id ->                                 DropdownMenuItem(                                     text = { Text(stringResource(R.string.signal)) },-                                    onClick = { placePlatformCall(context, number, PackageUtils.SIGNAL_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }+                                    onClick = { placePlatformCall(context, videoNumber, PackageUtils.SIGNAL_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }                                 )                             }                             platforms.telegramVideoId?.let { id ->                                 DropdownMenuItem(                                     text = { Text(stringResource(R.string.telegram)) },-                                    onClick = { placePlatformCall(context, number, PackageUtils.TELEGRAM_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }+                                    onClick = { placePlatformCall(context, videoNumber, PackageUtils.TELEGRAM_PACKAGE, isVideo = true, fallbackDataRowId = id); showVideoDropdown = false }                                 )                             }                         }@@ -285,38 +333,3 @@ fun ActionButtonsRow(         }     } }--@Composable-fun ActionButton(-    icon: @Composable () -> Unit,-    label: String,-    dropdownContent: (@Composable () -> Unit)? = null,-    action: () -> Unit-) {-    Column(-        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,-        verticalArrangement = Arrangement.spacedBy(8.dp)-    ) {-        BadgedBox(-            badge = {}-        ) {-            Box(-                modifier = Modifier-                    .size(56.dp)-                    .clip(CircleShape)-                    .background(MaterialTheme.colorScheme.surfaceVariant)-                    .clickable { action() },-                contentAlignment = androidx.compose.ui.Alignment.Center-            ) {-                Box(-                    modifier = Modifier.size(24.dp),-                    contentAlignment = androidx.compose.ui.Alignment.Center-                ) {-                    icon()-                }-                dropdownContent?.invoke()-            }-        }-        Text(text = label, style = MaterialTheme.typography.labelMedium)-    }-}
contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.ktindex 0e577b3e8f..5c221d5183 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsPage.kt@@ -189,7 +189,7 @@ fun ContactDetailsScreen(             item {                 Box(Modifier.staggeredEntrance(index = 0, arriving = arriving)) {                     ActionButtonsRow(-                        details.phoneNumbers.firstOrNull()?.number,+                        details.phoneNumbers,                         details.emails.firstOrNull()?.address,                         platforms,                         state.isGoogleMeetInstalled
contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsSections.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsSections.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsSections.ktindex 7e64542880..2acec24588 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsSections.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactDetailsSections.kt@@ -59,9 +59,6 @@ internal fun PhonesSection(         verticalArrangement = Arrangement.spacedBy(2.dp),     ) {         details.phoneNumbers.forEachIndexed { index, phone ->-            var showCallDropdown by androidx.compose.runtime.remember(phone.id) {-                androidx.compose.runtime.mutableStateOf(false)-            }             var showSmsDropdown by androidx.compose.runtime.remember(phone.id) {                 androidx.compose.runtime.mutableStateOf(false)             }@@ -79,20 +76,10 @@ internal fun PhonesSection(                     }                 },                 onClick = {-                    if (platforms.hasAnyPlatform) {-                        showCallDropdown = true-                    } else {-                        placeCall(context, phone.number)-                    }-                },-                dropdownContent = {-                    CommunicationDropdown(-                        expanded = showCallDropdown,-                        onDismiss = { showCallDropdown = false },-                        number = phone.number,-                        type = CommunicationType.CALL,-                        platforms = platforms-                    )+                    // Tapping the number itself places a plain system call, so the row+                    // stays distinct from the Call/Message buttons above (which route+                    // through Signal/WhatsApp/Telegram when those rows exist).+                    placeCall(context, phone.number)                 },                 trailingDropdownContent = {                     CommunicationDropdown(
contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModelSim.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModelSim.kt b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModelSim.ktindex 85c4a091c6..3687a6c99d 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModelSim.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModelSim.kt@@ -80,12 +80,27 @@ internal suspend fun ContactViewModel.updateExistingSimContact( internal suspend fun ContactViewModel.persistProviderContact(     contact: com.vayunmathur.contacts.data.Contact ): Boolean {+    val app = getApplication<Application>()     val contactId = contact.id     val details = contact.details-    val oldDetails = contacts.value.find { it.id == contactId }?.details+    // Read the old rows straight from the provider rather than trusting the cached+    // list: the cache lags the provider across account syncs and external edits, and+    // updates built against stale data-row ids match zero rows without error, so the+    // batch reports success while nothing changes.+    val oldDetails = contactId.takeIf { it != 0L }?.let {+        runCatching { com.vayunmathur.contacts.data.getDetails(app, it) }.getOrNull()+            ?: com.vayunmathur.contacts.data.ContactDetails.empty()+    } ?: contacts.value.find { it.id == contactId }?.details         ?: allContactsState.value.find { it.id == contactId }?.details         ?: com.vayunmathur.contacts.data.ContactDetails.empty()-    return contact.save(getApplication(), details, oldDetails)+    val ok = contact.save(app, details, oldDetails)+    if (ok) {+        // The provider observer re-syncs on its own debounce, but an edit must be+        // visible immediately - refresh now so the details page and list update+        // without waiting for the observer round-trip.+        syncFromSystem()+    }+    return ok }  fun ContactViewModel.simDisplayLabel(account: ContactAccount): String? =