Skip to content

Commit

Permalink
Allow filtering of individual accounts (#259)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Terrance authored Sep 10, 2023
1 parent 96edf8c commit d248099
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/bnyro/contacts/obj/ContactData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data class ContactData(
var groups: List<ContactsGroup> = listOf(),
var websites: List<ValueWithType> = listOf()
) {
val accountIdentifier get() = "$accountType|$accountName"
fun getNameBySortOrder(sortOrder: SortOrder): String? {
return when (sortOrder) {
SortOrder.FIRSTNAME -> displayName
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/bnyro/contacts/obj/FilterOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import com.bnyro.contacts.util.Preferences

data class FilterOptions(
var sortOder: SortOrder,
var hiddenAccountNames: List<String>,
var hiddenAccountIdentifiers: List<String>,
var visibleGroups: List<ContactsGroup>
) {
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())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun FilterDialog(
}

var hiddenAccountNames by remember {
mutableStateOf(initialFilters.hiddenAccountNames)
mutableStateOf(initialFilters.hiddenAccountIdentifiers)
}

var visibleGroups by remember {
Expand Down Expand Up @@ -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
}
}
)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/bnyro/contacts/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<String>) = preferences.getStringSet(key, defValue)

fun edit(action: SharedPreferences.Editor.() -> Unit) {
preferences.edit().apply(action).apply()
Expand Down

0 comments on commit d248099

Please sign in to comment.