From 8d94643ff78bdac683423afb6479b26bf7e673cd Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 22 Sep 2022 15:58:49 +0200 Subject: [PATCH] RoomList: revert changes on LiveData and fix previous issues (not updated list) --- .../list/home/HomeFilteredRoomsController.kt | 8 ---- .../room/list/home/HomeRoomListFragment.kt | 10 ++--- .../room/list/home/HomeRoomListViewModel.kt | 38 +++++++++++-------- .../room/list/home/HomeRoomListViewState.kt | 3 -- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeFilteredRoomsController.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeFilteredRoomsController.kt index 2b4a514750..ebf322dc23 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeFilteredRoomsController.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeFilteredRoomsController.kt @@ -16,7 +16,6 @@ package im.vector.app.features.home.room.list.home -import androidx.paging.PagedList import com.airbnb.epoxy.EpoxyModel import com.airbnb.epoxy.paging.PagedListEpoxyController import im.vector.app.core.platform.StateView @@ -76,13 +75,6 @@ class HomeFilteredRoomsController @Inject constructor( this.emptyStateData = state } - fun submitPagedList(newList: PagedList) { - submitList(newList) - if (newList.isEmpty()) { - requestForcedModelBuild() - } - } - override fun buildItemModel(currentPosition: Int, item: RoomSummary?): EpoxyModel<*> { return if (item == null) { val host = this diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListFragment.kt index 9b8a686f37..767da24388 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListFragment.kt @@ -152,12 +152,10 @@ class HomeRoomListFragment : headersController.submitData(it) } - roomListViewModel.onEach(HomeRoomListViewState::roomsPagedList) { roomsList -> - roomsList?.let { - roomsController.submitPagedList(it) - if (it.isEmpty()) { - roomsController.requestForcedModelBuild() - } + roomListViewModel.roomsLivePagedList.observe(viewLifecycleOwner) { roomsList -> + roomsController.submitList(roomsList) + if (roomsList.isEmpty()) { + roomsController.requestForcedModelBuild() } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt index ad2656cec1..41cf2189c0 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewModel.kt @@ -17,8 +17,11 @@ package im.vector.app.features.home.room.list.home import android.widget.ImageView -import androidx.lifecycle.asFlow +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Observer import androidx.paging.PagedList +import arrow.core.Option import arrow.core.toOption import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted @@ -35,7 +38,6 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.features.displayname.getBestName import im.vector.app.features.home.room.list.home.header.HomeRoomFilter import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow @@ -69,7 +71,6 @@ import org.matrix.android.sdk.api.session.room.state.isPublic import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.flow.flow -import java.util.concurrent.CancellationException class HomeRoomListViewModel @AssistedInject constructor( @Assisted initialState: HomeRoomListViewState, @@ -87,19 +88,24 @@ class HomeRoomListViewModel @AssistedInject constructor( companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() + private var roomsFlow: Flow>? = null private val pagedListConfig = PagedList.Config.Builder() .setPageSize(10) .setInitialLoadSizeHint(20) .setEnablePlaceholders(true) - .setPrefetchDistance(10) .build() + private val _roomsLivePagedList = MutableLiveData>() + val roomsLivePagedList: LiveData> = _roomsLivePagedList + + private val internalPagedListObserver = Observer> { + _roomsLivePagedList.postValue(it) + } + private var currentFilter: HomeRoomFilter = HomeRoomFilter.ALL private val _emptyStateFlow = MutableSharedFlow>(replay = 1) val emptyStateFlow = _emptyStateFlow.asSharedFlow() - private var roomsFlowJob: Job? = null - private var filteredPagedRoomSummariesLive: UpdatableLivePageResult? = null init { @@ -223,6 +229,8 @@ class HomeRoomListViewModel @AssistedInject constructor( } private fun observeRooms() = viewModelScope.launch { + filteredPagedRoomSummariesLive?.livePagedList?.removeObserver(internalPagedListObserver) + val builder = RoomSummaryQueryParams.Builder().also { it.memberships = listOf(Membership.JOIN) } @@ -233,7 +241,6 @@ class HomeRoomListViewModel @AssistedInject constructor( } else { RoomSortOrder.ACTIVITY } - val liveResults = session.roomService().getFilteredPagedRoomSummariesLive( params, pagedListConfig, @@ -249,21 +256,15 @@ class HomeRoomListViewModel @AssistedInject constructor( } .onEach { selectedSpaceOption -> val selectedSpace = selectedSpaceOption.orNull() - liveResults.queryParams = liveResults.queryParams.copy( + filteredPagedRoomSummariesLive?.queryParams = liveResults.queryParams.copy( spaceFilter = selectedSpace?.roomId.toActiveSpaceOrNoFilter() ) emitEmptyState() } + .also { roomsFlow = it } .launchIn(viewModelScope) - roomsFlowJob?.cancel(CancellationException()) - - roomsFlowJob = liveResults.livePagedList - .asFlow() - .onEach { - setState { copy(roomsPagedList = it) } - } - .launchIn(viewModelScope) + liveResults.livePagedList.observeForever(internalPagedListObserver) } private fun observeOrderPreferences() { @@ -344,6 +345,11 @@ class HomeRoomListViewModel @AssistedInject constructor( } } + override fun onCleared() { + super.onCleared() + filteredPagedRoomSummariesLive?.livePagedList?.removeObserver(internalPagedListObserver) + } + private fun handleChangeRoomFilter(newFilter: HomeRoomFilter) { if (currentFilter == newFilter) { return diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewState.kt b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewState.kt index db3a57e63e..c05e285ddb 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewState.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/home/HomeRoomListViewState.kt @@ -16,14 +16,11 @@ package im.vector.app.features.home.room.list.home -import androidx.paging.PagedList import com.airbnb.mvrx.MavericksState import im.vector.app.core.platform.StateView import im.vector.app.features.home.room.list.home.header.RoomsHeadersData -import org.matrix.android.sdk.api.session.room.model.RoomSummary data class HomeRoomListViewState( val state: StateView.State = StateView.State.Content, val headersData: RoomsHeadersData = RoomsHeadersData(), - val roomsPagedList: PagedList? = null, ) : MavericksState