fix: community selection with search

This commit is contained in:
Diego Beraldin 2023-10-05 23:54:49 +02:00
parent a83e0c3f6a
commit 9dcdf13f80

View File

@ -75,18 +75,21 @@ class MultiCommunityEditorViewModel(
mvi.updateState { it.copy(searchText = value) } mvi.updateState { it.copy(searchText = value) }
debounceJob = mvi.scope?.launch(Dispatchers.IO) { debounceJob = mvi.scope?.launch(Dispatchers.IO) {
delay(1_000) delay(1_000)
filterCommunities() mvi.updateState {
val filtered = filterCommunities()
it.copy(communities = filtered)
}
} }
} }
private fun filterCommunities() { private fun filterCommunities(): List<Pair<CommunityModel, Boolean>> {
val searchText = uiState.value.searchText val searchText = uiState.value.searchText
val filtered = if (searchText.isNotEmpty()) { val res = if (searchText.isNotEmpty()) {
communities.filter { it.first.name.contains(searchText) } communities.filter { it.first.name.contains(searchText) }
} else { } else {
communities communities
} }
mvi.updateState { it.copy(communities = filtered) } return res
} }
private fun selectImage(index: Int?) { private fun selectImage(index: Int?) {
@ -99,18 +102,24 @@ class MultiCommunityEditorViewModel(
} }
private fun toggleCommunity(index: Int) { private fun toggleCommunity(index: Int) {
mvi.updateState { state -> val toggledCommunity = uiState.value.communities[index]
val newCommunities = state.communities.mapIndexed { idx, item -> val newCommunities = communities.map { item ->
if (idx == index) { if (item.first.id == toggledCommunity.first.id) {
item.first to !item.second item.first to !item.second
} else { } else {
item item
} }
} }
val availableIcons = val availableIcons = newCommunities.filter { i ->
newCommunities.filter { i -> i.second }.mapNotNull { i -> i.first.icon } i.second
}.mapNotNull { i ->
i.first.icon
}
communities = newCommunities
val filtered = filterCommunities()
mvi.updateState { state ->
state.copy( state.copy(
communities = newCommunities, communities = filtered,
availableIcons = availableIcons, availableIcons = availableIcons,
) )
} }