Skip to content

Commit

Permalink
Merge pull request #7186 from vector-im/bugfix/eric/fixes-room-list-u…
Browse files Browse the repository at this point in the history
…pdates

Fixes Room List not getting updated when fragment is not in focus
  • Loading branch information
bmarty authored Sep 21, 2022
2 parents bf19ef8 + d8060a7 commit 52d7369
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.d/7186.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes Room List not getting updated when fragment is not in focus
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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
Expand Down Expand Up @@ -75,6 +76,13 @@ class HomeFilteredRoomsController @Inject constructor(
this.emptyStateData = state
}

fun submitPagedList(newList: PagedList<RoomSummary>) {
submitList(newList)
if (newList.isEmpty()) {
requestForcedModelBuild()
}
}

override fun buildItemModel(currentPosition: Int, item: RoomSummary?): EpoxyModel<*> {
return if (item == null) {
val host = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ class HomeRoomListFragment :
headersController.submitData(it)
}

roomListViewModel.onEach(HomeRoomListViewState::roomsLivePagedList) { roomsListLive ->
roomsListLive?.observe(viewLifecycleOwner) { roomsList ->
roomsController.submitList(roomsList)
if (roomsList.isEmpty()) {
roomListViewModel.onEach(HomeRoomListViewState::roomsPagedList) { roomsList ->
roomsList?.let {
roomsController.submitPagedList(it)
if (it.isEmpty()) {
roomsController.requestForcedModelBuild()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package im.vector.app.features.home.room.list.home

import android.widget.ImageView
import androidx.lifecycle.asFlow
import androidx.paging.PagedList
import arrow.core.Option
import arrow.core.toOption
import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
Expand All @@ -35,6 +35,7 @@ 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
Expand Down Expand Up @@ -68,6 +69,7 @@ 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,
Expand All @@ -85,7 +87,6 @@ class HomeRoomListViewModel @AssistedInject constructor(

companion object : MavericksViewModelFactory<HomeRoomListViewModel, HomeRoomListViewState> by hiltMavericksViewModelFactory()

private var roomsFlow: Flow<Option<RoomSummary>>? = null
private val pagedListConfig = PagedList.Config.Builder()
.setPageSize(10)
.setInitialLoadSizeHint(20)
Expand All @@ -97,6 +98,8 @@ class HomeRoomListViewModel @AssistedInject constructor(
private val _emptyStateFlow = MutableSharedFlow<Optional<StateView.State.Empty>>(replay = 1)
val emptyStateFlow = _emptyStateFlow.asSharedFlow()

private var roomsFlowJob: Job? = null

private var filteredPagedRoomSummariesLive: UpdatableLivePageResult? = null

init {
Expand Down Expand Up @@ -251,10 +254,16 @@ class HomeRoomListViewModel @AssistedInject constructor(
)
emitEmptyState()
}
.also { roomsFlow = it }
.launchIn(viewModelScope)

setState { copy(roomsLivePagedList = liveResults.livePagedList) }
roomsFlowJob?.cancel(CancellationException())

roomsFlowJob = liveResults.livePagedList
.asFlow()
.onEach {
setState { copy(roomsPagedList = it) }
}
.launchIn(viewModelScope)
}

private fun observeOrderPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package im.vector.app.features.home.room.list.home

import androidx.lifecycle.LiveData
import androidx.paging.PagedList
import com.airbnb.mvrx.MavericksState
import im.vector.app.core.platform.StateView
Expand All @@ -26,5 +25,5 @@ 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 roomsLivePagedList: LiveData<PagedList<RoomSummary>>? = null,
val roomsPagedList: PagedList<RoomSummary>? = null,
) : MavericksState

0 comments on commit 52d7369

Please sign in to comment.