Group: should refresh when user data come. [WIP]

This commit is contained in:
ganfra 2019-07-02 20:50:25 +02:00
parent 77fd7cd33c
commit 1d3faa7d9b
4 changed files with 24 additions and 14 deletions

View File

@ -56,6 +56,6 @@ private class LiveDataObservable<T>(
} }
} }


fun <T> LiveData<T>.asObservable(): Observable<T> { fun <T> LiveData<T>.asObservable(defaultValue: T? = null): Observable<T> {
return LiveDataObservable(this) return LiveDataObservable(this, defaultValue)
} }

View File

@ -21,6 +21,7 @@ import im.vector.matrix.android.api.session.group.model.GroupSummary
import im.vector.matrix.android.api.session.pushers.Pusher import im.vector.matrix.android.api.session.pushers.Pusher
import im.vector.matrix.android.api.session.room.model.RoomSummary import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.sync.SyncState import im.vector.matrix.android.api.session.sync.SyncState
import im.vector.matrix.android.api.session.user.model.User
import io.reactivex.Observable import io.reactivex.Observable


class RxSession(private val session: Session) { class RxSession(private val session: Session) {
@ -41,6 +42,10 @@ class RxSession(private val session: Session) {
return session.livePushers().asObservable() return session.livePushers().asObservable()
} }


fun liveUser(userId: String): Observable<User?> {
return session.observeUser(userId).asObservable(User(userId))
}

} }


fun Session.rx(): RxSession { fun Session.rx(): RxSession {

View File

@ -54,6 +54,7 @@ class GroupListFragment : VectorBaseFragment(), GroupSummaryController.Callback
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
groupController.callback = this groupController.callback = this
stateView.contentView = groupListEpoxyRecyclerView stateView.contentView = groupListEpoxyRecyclerView
groupListEpoxyRecyclerView.setHasFixedSize(true)
groupListEpoxyRecyclerView.setController(groupController) groupListEpoxyRecyclerView.setController(groupController)
viewModel.subscribe { renderState(it) } viewModel.subscribe { renderState(it) }
viewModel.openGroupLiveData.observeEvent(this) { viewModel.openGroupLiveData.observeEvent(this) {

View File

@ -26,11 +26,16 @@ import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.group.model.GroupSummary import im.vector.matrix.android.api.session.group.model.GroupSummary
import im.vector.matrix.android.api.session.user.model.User
import im.vector.matrix.rx.rx import im.vector.matrix.rx.rx
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.LiveEvent import im.vector.riotx.core.utils.LiveEvent
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import timber.log.Timber
import java.util.concurrent.TimeUnit


const val ALL_COMMUNITIES_GROUP_ID = "ALL_COMMUNITIES_GROUP_ID" const val ALL_COMMUNITIES_GROUP_ID = "ALL_COMMUNITIES_GROUP_ID"


@ -89,22 +94,21 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
} }


private fun observeGroupSummaries() { private fun observeGroupSummaries() {
session Observable.combineLatest<User?, List<GroupSummary>, List<GroupSummary>>(
.rx() session.rx().liveUser(session.myUserId),
.liveGroupSummaries() session.rx().liveGroupSummaries(),
.map { BiFunction { user, groupSummary ->
val myUser = session.getUser(session.sessionParams.credentials.userId)
val allCommunityGroup = GroupSummary( val allCommunityGroup = GroupSummary(
groupId = ALL_COMMUNITIES_GROUP_ID, groupId = ALL_COMMUNITIES_GROUP_ID,
displayName = stringProvider.getString(R.string.group_all_communities), displayName = stringProvider.getString(R.string.group_all_communities),
avatarUrl = myUser?.avatarUrl ?: "") avatarUrl = user.avatarUrl ?: "")
listOf(allCommunityGroup) + it listOf(allCommunityGroup) + groupSummary
}
.execute { async ->
// TODO Phase2 Handle the case where the selected group is deleted on another client
val newSelectedGroup = selectedGroup ?: async()?.firstOrNull()
copy(asyncGroups = async, selectedGroup = newSelectedGroup)
} }
).execute { async ->
// TODO Phase2 Handle the case where the selected group is deleted on another client
val newSelectedGroup = selectedGroup ?: async()?.firstOrNull()
copy(asyncGroups = async, selectedGroup = newSelectedGroup)
}
} }