refactor home room list

This commit is contained in:
valere 2023-01-30 09:45:03 +01:00
parent 992ec7f3f4
commit c4e03c59a0
4 changed files with 35 additions and 51 deletions

View file

@ -24,6 +24,7 @@ interface UpdatableLivePageResult {
val livePagedList: LiveData<PagedList<RoomSummary>> val livePagedList: LiveData<PagedList<RoomSummary>>
val liveBoundaries: LiveData<ResultBoundaries> val liveBoundaries: LiveData<ResultBoundaries>
var queryParams: RoomSummaryQueryParams var queryParams: RoomSummaryQueryParams
var sortOrder: RoomSortOrder
} }
data class ResultBoundaries( data class ResultBoundaries(

View file

@ -265,6 +265,7 @@ internal class RoomSummaryDataSource @Inject constructor(
) )
return object : UpdatableLivePageResult { return object : UpdatableLivePageResult {
override val livePagedList: LiveData<PagedList<RoomSummary>> = mapped override val livePagedList: LiveData<PagedList<RoomSummary>> = mapped
override val liveBoundaries: LiveData<ResultBoundaries> override val liveBoundaries: LiveData<ResultBoundaries>
@ -274,7 +275,14 @@ internal class RoomSummaryDataSource @Inject constructor(
set(value) { set(value) {
field = value field = value
realmDataSourceFactory.updateQuery { realmDataSourceFactory.updateQuery {
roomSummariesQuery(it, value).process(sortOrder) roomSummariesQuery(it, value).process(this.sortOrder)
}
}
override var sortOrder: RoomSortOrder = sortOrder
set(value) {
field = value
realmDataSourceFactory.updateQuery {
roomSummariesQuery(it, this.queryParams).process(value)
} }
} }
} }

View file

@ -151,7 +151,7 @@ class HomeRoomListFragment :
roomListViewModel.onEach(HomeRoomListViewState::headersData) { roomListViewModel.onEach(HomeRoomListViewState::headersData) {
headersController.submitData(it) headersController.submitData(it)
} }
roomListViewModel.roomsLivePagedList.observe(viewLifecycleOwner) { roomsList -> roomListViewModel.filteredPagedRoomSummariesLive.livePagedList.observe(viewLifecycleOwner) { roomsList ->
roomsController.submitRoomsList(roomsList) roomsController.submitRoomsList(roomsList)
} }
roomListViewModel.onEach(HomeRoomListViewState::emptyState) { emptyState -> roomListViewModel.onEach(HomeRoomListViewState::emptyState) { emptyState ->

View file

@ -17,9 +17,6 @@
package im.vector.app.features.home.room.list.home package im.vector.app.features.home.room.list.home
import android.widget.ImageView import android.widget.ImageView
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.paging.PagedList import androidx.paging.PagedList
import com.airbnb.mvrx.MavericksViewModelFactory import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted import dagger.assisted.Assisted
@ -88,20 +85,26 @@ class HomeRoomListViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<HomeRoomListViewModel, HomeRoomListViewState> by hiltMavericksViewModelFactory() companion object : MavericksViewModelFactory<HomeRoomListViewModel, HomeRoomListViewState> by hiltMavericksViewModelFactory()
private val pagedListConfig = PagedList.Config.Builder() private val pagedListConfig = PagedList.Config.Builder()
.setPageSize(10) .setPageSize(30)
.setInitialLoadSizeHint(20) .setInitialLoadSizeHint(50)
.setEnablePlaceholders(true) .setEnablePlaceholders(true)
.setPrefetchDistance(10)
.build() .build()
private val _roomsLivePagedList = MutableLiveData<PagedList<RoomSummary>>() val filteredPagedRoomSummariesLive: UpdatableLivePageResult by lazy {
val roomsLivePagedList: LiveData<PagedList<RoomSummary>> = _roomsLivePagedList val builder = RoomSummaryQueryParams.Builder().also {
it.memberships = listOf(Membership.JOIN)
private val internalPagedListObserver = Observer<PagedList<RoomSummary>> { it.spaceFilter = spaceStateHandler.getCurrentSpace()?.roomId.toActiveSpaceOrNoFilter()
_roomsLivePagedList.postValue(it) }
val params = getFilteredQueryParams(initialState.headersData.currentFilter, builder.build())
val sortOrder = RoomSortOrder.ACTIVITY
session.roomService().getFilteredPagedRoomSummariesLive(
params,
pagedListConfig,
sortOrder
)
} }
private var filteredPagedRoomSummariesLive: UpdatableLivePageResult? = null
init { init {
observeOrderPreferences() observeOrderPreferences()
observeInvites() observeInvites()
@ -119,11 +122,9 @@ class HomeRoomListViewModel @AssistedInject constructor(
.onEach { selectedSpaceOption -> .onEach { selectedSpaceOption ->
val selectedSpace = selectedSpaceOption.orNull() val selectedSpace = selectedSpaceOption.orNull()
updateEmptyState() updateEmptyState()
filteredPagedRoomSummariesLive?.let { liveResults -> filteredPagedRoomSummariesLive.queryParams = filteredPagedRoomSummariesLive.queryParams.copy(
liveResults.queryParams = liveResults.queryParams.copy( spaceFilter = selectedSpace?.roomId.toActiveSpaceOrNoFilter()
spaceFilter = selectedSpace?.roomId.toActiveSpaceOrNoFilter() )
)
}
} }
.launchIn(viewModelScope) .launchIn(viewModelScope)
} }
@ -239,33 +240,14 @@ class HomeRoomListViewModel @AssistedInject constructor(
} }
} }
private fun observeRooms(currentFilter: HomeRoomFilter, isAZOrdering: Boolean) {
filteredPagedRoomSummariesLive?.livePagedList?.removeObserver(internalPagedListObserver)
val builder = RoomSummaryQueryParams.Builder().also {
it.memberships = listOf(Membership.JOIN)
it.spaceFilter = spaceStateHandler.getCurrentSpace()?.roomId.toActiveSpaceOrNoFilter()
}
val params = getFilteredQueryParams(currentFilter, builder.build())
val sortOrder = if (isAZOrdering) {
RoomSortOrder.NAME
} else {
RoomSortOrder.ACTIVITY
}
val liveResults = session.roomService().getFilteredPagedRoomSummariesLive(
params,
pagedListConfig,
sortOrder
).also {
filteredPagedRoomSummariesLive = it
}
liveResults.livePagedList.observeForever(internalPagedListObserver)
}
private fun observeOrderPreferences() { private fun observeOrderPreferences() {
preferencesStore.isAZOrderingEnabledFlow preferencesStore.isAZOrderingEnabledFlow
.onEach { isAZOrdering -> .onEach { isAZOrdering ->
val currentFilter = awaitState().headersData.currentFilter filteredPagedRoomSummariesLive.sortOrder = if (isAZOrdering) {
observeRooms(currentFilter, isAZOrdering) RoomSortOrder.NAME
} else {
RoomSortOrder.ACTIVITY
}
}.launchIn(viewModelScope) }.launchIn(viewModelScope)
} }
@ -340,11 +322,6 @@ class HomeRoomListViewModel @AssistedInject constructor(
} }
} }
override fun onCleared() {
filteredPagedRoomSummariesLive?.livePagedList?.removeObserver(internalPagedListObserver)
super.onCleared()
}
private fun handleChangeRoomFilter(newFilter: HomeRoomFilter) { private fun handleChangeRoomFilter(newFilter: HomeRoomFilter) {
viewModelScope.launch { viewModelScope.launch {
changeRoomFilter(newFilter) changeRoomFilter(newFilter)
@ -359,9 +336,7 @@ class HomeRoomListViewModel @AssistedInject constructor(
setState { copy(headersData = headersData.copy(currentFilter = newFilter)) } setState { copy(headersData = headersData.copy(currentFilter = newFilter)) }
updateEmptyState() updateEmptyState()
analyticsTracker.updateUserProperties(UserProperties(allChatsActiveFilter = newFilter.toTrackingValue())) analyticsTracker.updateUserProperties(UserProperties(allChatsActiveFilter = newFilter.toTrackingValue()))
filteredPagedRoomSummariesLive?.let { liveResults -> filteredPagedRoomSummariesLive.queryParams = getFilteredQueryParams(newFilter, filteredPagedRoomSummariesLive.queryParams)
liveResults.queryParams = getFilteredQueryParams(newFilter, liveResults.queryParams)
}
} }
fun isPublicRoom(roomId: String): Boolean { fun isPublicRoom(roomId: String): Boolean {