Browse Source

Reduce delay when selecting room list filters (#3160)

Previously, this had to wait for a recomposition until it was launched, which took ~100ms in a debug build. With these changes, the side effect is emitted in 10-20ms instead.
test/ffi-kotlin-room-list-tests
Jorge Martin Espinosa 2 months ago committed by GitHub
parent
commit
d9a498c86a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 32
      features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/filters/RoomListFiltersPresenter.kt

32
features/roomlist/impl/src/main/kotlin/io/element/android/features/roomlist/impl/filters/RoomListFiltersPresenter.kt

@ -17,13 +17,13 @@
package io.element.android.features.roomlist.impl.filters package io.element.android.features.roomlist.impl.filters
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import io.element.android.features.roomlist.impl.filters.selection.FilterSelectionStrategy import io.element.android.features.roomlist.impl.filters.selection.FilterSelectionStrategy
import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.matrix.api.roomlist.RoomListService import io.element.android.libraries.matrix.api.roomlist.RoomListService
import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
import io.element.android.libraries.matrix.api.roomlist.RoomListFilter as MatrixRoomListFilter import io.element.android.libraries.matrix.api.roomlist.RoomListFilter as MatrixRoomListFilter
@ -31,10 +31,10 @@ class RoomListFiltersPresenter @Inject constructor(
private val roomListService: RoomListService, private val roomListService: RoomListService,
private val filterSelectionStrategy: FilterSelectionStrategy, private val filterSelectionStrategy: FilterSelectionStrategy,
) : Presenter<RoomListFiltersState> { ) : Presenter<RoomListFiltersState> {
private val initialFilters = filterSelectionStrategy.filterSelectionStates.value.toPersistentList()
@Composable @Composable
override fun present(): RoomListFiltersState { override fun present(): RoomListFiltersState {
val filters by filterSelectionStrategy.filterSelectionStates.collectAsState()
fun handleEvents(event: RoomListFiltersEvents) { fun handleEvents(event: RoomListFiltersEvents) {
when (event) { when (event) {
RoomListFiltersEvents.ClearSelectedFilters -> { RoomListFiltersEvents.ClearSelectedFilters -> {
@ -46,12 +46,15 @@ class RoomListFiltersPresenter @Inject constructor(
} }
} }
LaunchedEffect(filters) { val filters by produceState(initialValue = initialFilters) {
val allRoomsFilter = MatrixRoomListFilter.All( filterSelectionStrategy.filterSelectionStates
filters .map { filters ->
.filter { it.isSelected } value = filters.toPersistentList()
.map { roomListFilter -> filters.mapNotNull { filterState ->
when (roomListFilter.filter) { if (!filterState.isSelected) {
return@mapNotNull null
}
when (filterState.filter) {
RoomListFilter.Rooms -> MatrixRoomListFilter.Category.Group RoomListFilter.Rooms -> MatrixRoomListFilter.Category.Group
RoomListFilter.People -> MatrixRoomListFilter.Category.People RoomListFilter.People -> MatrixRoomListFilter.Category.People
RoomListFilter.Unread -> MatrixRoomListFilter.Unread RoomListFilter.Unread -> MatrixRoomListFilter.Unread
@ -59,12 +62,15 @@ class RoomListFiltersPresenter @Inject constructor(
RoomListFilter.Invites -> MatrixRoomListFilter.Invite RoomListFilter.Invites -> MatrixRoomListFilter.Invite
} }
} }
) }
roomListService.allRooms.updateFilter(allRoomsFilter) .collect { filters ->
val result = MatrixRoomListFilter.All(filters)
roomListService.allRooms.updateFilter(result)
}
} }
return RoomListFiltersState( return RoomListFiltersState(
filterSelectionStates = filters.toPersistentList(), filterSelectionStates = filters,
eventSink = ::handleEvents eventSink = ::handleEvents
) )
} }

Loading…
Cancel
Save