Modern Apps git

← Modern-Apps.git

contacts: show empty-state text on the Contacts and Groups tabs (#560)

Commit
64a4b9cc96f5d14f5965054b67392f2ded7b7225
Author
vayun-mathur <[email protected]>
When
2026-08-23 14:42 UTC (3 months ago)
Parents
0f8f5b9

Diff

contacts/src/main/java/com/vayunmathur/contacts/ui/ContactList.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactList.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactList.ktindex b9c2f3bd6b..c8959b469f 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactList.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/ContactList.kt@@ -104,6 +104,7 @@ fun ContactList(     val groups by viewModel.groups.collectAsStateWithLifecycle()     val showAccountLabels by viewModel.showAccountLabels.collectAsStateWithLifecycle()     val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle()+    val hasLoadedContacts by viewModel.hasLoadedContacts.collectAsStateWithLifecycle()      val last = backStack.last() @@ -119,6 +120,7 @@ fun ContactList(                 else -> null             },             showAddButton = last !is Route.EditContact,+            isLoading = !hasLoadedContacts,         ),         actions = object : ContactsActions by viewModel {             override fun openContact(contact: Contact) = onContactClick(contact)@@ -265,6 +267,24 @@ fun ContactListScreen(state: ContactListUiState, actions: ContactsActions) {         verticalArrangement = Arrangement.spacedBy(8.dp),         scrollBehavior = appBarScrollBehavior(),     ) {+        if (contacts.isEmpty()) {+            item(key = "contacts-empty") {+                when {+                    state.searchQuery.isNotEmpty() -> EmptyState(+                        title = stringResource(R.string.no_contacts_found),+                        modifier = Modifier.fillParentMaxSize(),+                    )+                    state.isLoading -> LoadingState(modifier = Modifier.fillParentMaxSize())+                    else -> EmptyState(+                        title = stringResource(R.string.no_contacts_yet),+                        modifier = Modifier.fillParentMaxSize(),+                        message = stringResource(R.string.no_contacts_yet_message),+                        icon = { IconPerson() },+                    )+                }+            }+        }+         if (favorites.isNotEmpty()) {             item(key = "favorites-header") { FavoritesHeader() }             item(key = "favorites-card") {
contacts/src/main/java/com/vayunmathur/contacts/ui/GroupsPage.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/ui/GroupsPage.kt b/contacts/src/main/java/com/vayunmathur/contacts/ui/GroupsPage.ktindex 11db7e396f..a0d6b247bb 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/ui/GroupsPage.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/ui/GroupsPage.kt@@ -37,6 +37,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle fun GroupsPage(viewModel: ContactViewModel, backStack: NavBackStack<Route>, expandGroupId: Long? = null) {     val groups by viewModel.groups.collectAsStateWithLifecycle()     val contacts by viewModel.contacts.collectAsStateWithLifecycle()+    val hasLoadedContacts by viewModel.hasLoadedContacts.collectAsStateWithLifecycle()      // Membership is already part of every contact, so the rows are derived here rather     // than collected per group — a stateless screen cannot subscribe to one flow per row.@@ -47,7 +48,7 @@ fun GroupsPage(viewModel: ContactViewModel, backStack: NavBackStack<Route>, expa     }      GroupsScreen(-        state = GroupsUiState(groups = groupsWithContacts),+        state = GroupsUiState(groups = groupsWithContacts, isLoading = !hasLoadedContacts),         actions = object : ContactsActions by viewModel {             override fun openContact(contact: com.vayunmathur.contacts.data.Contact) {                 backStack.add(Route.ContactDetail(contact.id))@@ -86,6 +87,21 @@ fun GroupsScreen(state: GroupsUiState, actions: ContactsActions, expandGroupId:         verticalArrangement = Arrangement.spacedBy(4.dp),         scrollBehavior = appBarScrollBehavior(),     ) {+            if (state.groups.isEmpty()) {+                item(key = "groups-empty") {+                    if (state.isLoading) {+                        LoadingState(modifier = Modifier.fillParentMaxSize())+                    } else {+                        EmptyState(+                            title = stringResource(R.string.no_groups_yet),+                            modifier = Modifier.fillParentMaxSize(),+                            message = stringResource(R.string.no_groups_yet_message),+                            icon = { IconGroup() },+                        )+                    }+                }+            }+             state.groups.forEach { (group, contactsInGroup) ->                 val isExpanded = group.id in expandedGroups                 // Combine each group's header + (optional) expanded contacts
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 06c488b62a..2a47ba319c 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactViewModel.kt@@ -84,6 +84,11 @@ class ContactViewModel(application: Application) : AndroidViewModel(application)     private val _searchQuery = MutableStateFlow("")     val searchQuery = _searchQuery.asStateFlow() +    // The contact list starts empty and fills in asynchronously, so screens need to tell+    // "nothing loaded yet" apart from "there really are no contacts".+    private val _hasLoadedContacts = MutableStateFlow(false)+    val hasLoadedContacts: StateFlow<Boolean> = _hasLoadedContacts.asStateFlow()+     val hiddenAccounts: StateFlow<Set<String>> = dataStore.stringSetFlow("hidden_accounts")         .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptySet()) @@ -272,6 +277,8 @@ class ContactViewModel(application: Application) : AndroidViewModel(application)             launch { loadAccountsInternal() }         } catch (e: Exception) {             Log.e("ContactViewModel", "Error loading contacts", e)+        } finally {+            _hasLoadedContacts.value = true         }     } 
contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.ktindex 914dd18489..96cc45c943 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/util/ContactsUiContract.kt@@ -19,6 +19,8 @@ data class ContactListUiState(     /** Contact open in the detail pane, highlighted in the list on a wide screen. */     val openContactId: Long? = null,     val showAddButton: Boolean = true,+    /** True until the first sync from the provider finishes, so an empty list isn't mistaken for "no contacts". */+    val isLoading: Boolean = false, )  /** Everything the contact details page draws. */@@ -33,6 +35,8 @@ data class ContactDetailsUiState( /** Everything the groups page draws. */ data class GroupsUiState(     val groups: List<GroupWithContacts> = emptyList(),+    /** True until the first sync from the provider finishes. */+    val isLoading: Boolean = false, )  /**
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 8f7ed7a86f..c1ad443f52 100644--- a/contacts/src/main/res/values/strings.xml+++ b/contacts/src/main/res/values/strings.xml@@ -223,4 +223,14 @@     <string name="sim_limited_fields_hint">SIM contacts only store name, phone, and email.</string>     <string name="save_failed">Save failed</string>     <string name="importing">Importing…</string>++    <!-- Empty states -->+    <!-- Contacts tab, shown on a first run or when every account is hidden -->+    <string name="no_contacts_yet">No contacts yet</string>+    <!-- Supporting text under no_contacts_yet, pointing at the add button -->+    <string name="no_contacts_yet_message">Tap the add button to create your first contact.</string>+    <!-- Groups tab, shown when the user has not created any group -->+    <string name="no_groups_yet">No groups yet</string>+    <!-- Supporting text under no_groups_yet, pointing at the add button -->+    <string name="no_groups_yet_message">Tap the add button to group contacts together.</string> </resources>