From d248099e3ab9f85eadfc25291f1ee14a90993a4e Mon Sep 17 00:00:00 2001 From: Terrance Date: Sun, 10 Sep 2023 15:21:49 +0100 Subject: [PATCH] Allow filtering of individual accounts (#259) * Allow filtering of individual accounts The existing behaviour toggles all accounts from a single provider when one of them is selected in the filter dialog. This change means selections are managed by account name rather than type, in order to allow filtering on individual accounts. * Use combined type|name key for account filter * Add identifier helper to contact data * Add persistence for hidden accounts --- .../main/java/com/bnyro/contacts/obj/ContactData.kt | 1 + .../main/java/com/bnyro/contacts/obj/FilterOptions.kt | 5 +++-- .../com/bnyro/contacts/ui/components/ContactsList.kt | 2 +- .../com/bnyro/contacts/ui/components/ContactsPage.kt | 5 ++++- .../contacts/ui/components/dialogs/FilterDialog.kt | 11 ++++++----- .../main/java/com/bnyro/contacts/util/Preferences.kt | 2 ++ 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/bnyro/contacts/obj/ContactData.kt b/app/src/main/java/com/bnyro/contacts/obj/ContactData.kt index 0a4b67bc..b701f7a6 100644 --- a/app/src/main/java/com/bnyro/contacts/obj/ContactData.kt +++ b/app/src/main/java/com/bnyro/contacts/obj/ContactData.kt @@ -25,6 +25,7 @@ data class ContactData( var groups: List = listOf(), var websites: List = listOf() ) { + val accountIdentifier get() = "$accountType|$accountName" fun getNameBySortOrder(sortOrder: SortOrder): String? { return when (sortOrder) { SortOrder.FIRSTNAME -> displayName diff --git a/app/src/main/java/com/bnyro/contacts/obj/FilterOptions.kt b/app/src/main/java/com/bnyro/contacts/obj/FilterOptions.kt index 65e8b025..0091d9fe 100644 --- a/app/src/main/java/com/bnyro/contacts/obj/FilterOptions.kt +++ b/app/src/main/java/com/bnyro/contacts/obj/FilterOptions.kt @@ -5,13 +5,14 @@ import com.bnyro.contacts.util.Preferences data class FilterOptions( var sortOder: SortOrder, - var hiddenAccountNames: List, + var hiddenAccountIdentifiers: List, var visibleGroups: List ) { companion object { fun default(): FilterOptions { val sortOrder = SortOrder.fromInt(Preferences.getInt(Preferences.sortOrderKey, 0)) - return FilterOptions(sortOrder, listOf(), listOf()) + val hiddenAccounts = Preferences.getStringSet(Preferences.hiddenAccountsKey, emptySet())!!.toList() + return FilterOptions(sortOrder, hiddenAccounts, listOf()) } } } diff --git a/app/src/main/java/com/bnyro/contacts/ui/components/ContactsList.kt b/app/src/main/java/com/bnyro/contacts/ui/components/ContactsList.kt index efe18ce1..47279fe5 100644 --- a/app/src/main/java/com/bnyro/contacts/ui/components/ContactsList.kt +++ b/app/src/main/java/com/bnyro/contacts/ui/components/ContactsList.kt @@ -28,7 +28,7 @@ fun ContactsList( val state = rememberLazyListState() val contactGroups = remember(contacts) { contacts.asSequence().filter { - !filterOptions.hiddenAccountNames.contains(it.accountName) + !filterOptions.hiddenAccountIdentifiers.contains(it.accountIdentifier) }.filter { if (filterOptions.visibleGroups.isEmpty()) { true diff --git a/app/src/main/java/com/bnyro/contacts/ui/components/ContactsPage.kt b/app/src/main/java/com/bnyro/contacts/ui/components/ContactsPage.kt index 49d07f7c..6096f99c 100644 --- a/app/src/main/java/com/bnyro/contacts/ui/components/ContactsPage.kt +++ b/app/src/main/java/com/bnyro/contacts/ui/components/ContactsPage.kt @@ -379,7 +379,10 @@ fun ContactsPage( showFilterDialog = false }, onFilterChanged = { - Preferences.edit { putInt(Preferences.sortOrderKey, it.sortOder.ordinal) } + Preferences.edit { + putInt(Preferences.sortOrderKey, it.sortOder.ordinal) + putStringSet(Preferences.hiddenAccountsKey, it.hiddenAccountIdentifiers.toSet()) + } filterOptions = it }, initialFilters = filterOptions, diff --git a/app/src/main/java/com/bnyro/contacts/ui/components/dialogs/FilterDialog.kt b/app/src/main/java/com/bnyro/contacts/ui/components/dialogs/FilterDialog.kt index 5d299228..b913c33c 100644 --- a/app/src/main/java/com/bnyro/contacts/ui/components/dialogs/FilterDialog.kt +++ b/app/src/main/java/com/bnyro/contacts/ui/components/dialogs/FilterDialog.kt @@ -31,7 +31,7 @@ fun FilterDialog( } var hiddenAccountNames by remember { - mutableStateOf(initialFilters.hiddenAccountNames) + mutableStateOf(initialFilters.hiddenAccountIdentifiers) } var visibleGroups by remember { @@ -71,14 +71,15 @@ fun FilterDialog( title = stringResource(R.string.account_type), entries = availableAccountTypes.map { it.second }, selections = availableAccountTypes.filter { - !hiddenAccountNames.contains(it.first) + !hiddenAccountNames.contains(it.first + "|" + it.second) }.map { it.second }, onSelectionChanged = { index, newValue -> - val selectedAccountType = availableAccountTypes[index].first + val selection = availableAccountTypes[index] + val selectedAccountName = selection.first + "|" + selection.second hiddenAccountNames = if (newValue) { - hiddenAccountNames - selectedAccountType + hiddenAccountNames - selectedAccountName } else { - hiddenAccountNames + selectedAccountType + hiddenAccountNames + selectedAccountName } } ) diff --git a/app/src/main/java/com/bnyro/contacts/util/Preferences.kt b/app/src/main/java/com/bnyro/contacts/util/Preferences.kt index 840a44c8..6da45cb2 100644 --- a/app/src/main/java/com/bnyro/contacts/util/Preferences.kt +++ b/app/src/main/java/com/bnyro/contacts/util/Preferences.kt @@ -19,6 +19,7 @@ object Preferences { const val backupDirKey = "backupDir" const val backupTypeKey = "backupType" const val sortOrderKey = "sorting" + const val hiddenAccountsKey = "hiddenAccounts" const val backupIntervalKey = "backupInterval" const val maxBackupAmountKey = "maxBackupAmount" const val collapseBottomBarKey = "collapseBottomBar" @@ -33,6 +34,7 @@ object Preferences { fun getBoolean(key: String, defValue: Boolean) = preferences.getBoolean(key, defValue) fun getString(key: String, defValue: String) = preferences.getString(key, defValue) fun getInt(key: String, defValue: Int) = preferences.getInt(key, defValue) + fun getStringSet(key: String, defValue: Set) = preferences.getStringSet(key, defValue) fun edit(action: SharedPreferences.Editor.() -> Unit) { preferences.edit().apply(action).apply()