Group avatar: clean and optimize a bit.

This commit is contained in:
ganfra 2019-09-30 19:36:19 +02:00
parent de30e7c1c6
commit 0814f53fed
6 changed files with 35 additions and 49 deletions

View File

@ -47,7 +47,7 @@ class RxSession(private val session: Session) {
}
fun liveUser(userId: String): Observable<Optional<User>> {
return session.liveUser(userId).asObservable()
return session.liveUser(userId).asObservable().distinctUntilChanged()
}
fun liveUsers(): Observable<List<User>> {

View File

@ -20,19 +20,31 @@ import com.jakewharton.rxrelay2.BehaviorRelay
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
open class RxStore<T>(defaultValue: T? = null) {
open class RxStore<T>(private val defaultValue: T? = null) {
private val storeSubject: BehaviorRelay<T> = if (defaultValue == null) {
BehaviorRelay.create<T>()
} else {
BehaviorRelay.createDefault(defaultValue)
var storeRelay = createRelay()
fun clear() {
storeRelay = createRelay()
}
fun get(): T? {
return storeRelay.value
}
fun observe(): Observable<T> {
return storeSubject.hide().observeOn(Schedulers.computation())
return storeRelay.hide().observeOn(Schedulers.computation())
}
fun post(value: T) {
storeSubject.accept(value)
storeRelay.accept(value)
}
private fun createRelay(): BehaviorRelay<T> {
return if (defaultValue == null) {
BehaviorRelay.create<T>()
} else {
BehaviorRelay.createDefault(defaultValue)
}
}
}

View File

@ -98,7 +98,8 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState:
override fun onCleared() {
super.onCleared()
selectedGroupStore.clear()
homeRoomListStore.clear()
session.removeListener(this)
}

View File

@ -38,6 +38,7 @@ import im.vector.riotx.features.home.room.list.RoomListParams
import im.vector.riotx.features.home.room.list.UnreadCounterBadgeView
import im.vector.riotx.features.workers.signout.SignOutViewModel
import kotlinx.android.synthetic.main.fragment_home_detail.*
import timber.log.Timber
import javax.inject.Inject
@ -196,6 +197,7 @@ class HomeDetailFragment : VectorBaseFragment(), KeysBackupBanner.Delegate {
}
override fun invalidate() = withState(viewModel) {
Timber.v(it.toString())
unreadCounterBadgeViews[INDEX_CATCHUP].render(UnreadCounterBadgeView.State(it.notificationCountCatchup, it.notificationHighlightCatchup))
unreadCounterBadgeViews[INDEX_PEOPLE].render(UnreadCounterBadgeView.State(it.notificationCountPeople, it.notificationHighlightPeople))
unreadCounterBadgeViews[INDEX_ROOMS].render(UnreadCounterBadgeView.State(it.notificationCountRooms, it.notificationHighlightRooms))

View File

@ -16,28 +16,19 @@
package im.vector.riotx.features.home
import arrow.core.None
import arrow.core.firstOrNone
import arrow.core.toOption
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
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.room.model.Membership
import im.vector.matrix.rx.rx
import im.vector.riotx.R
import im.vector.riotx.core.di.HasScreenInjector
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.features.home.group.ALL_COMMUNITIES_GROUP_ID
import im.vector.riotx.features.home.group.SelectedGroupStore
import im.vector.riotx.features.home.room.list.RoomListFragment
import im.vector.riotx.features.ui.UiStateRepository
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
/**
@ -105,35 +96,6 @@ class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: Ho
private fun observeSelectedGroupStore() {
selectedGroupStore
.observe()
// TODO I do not like that, but it crashes with
// Thread: RxComputationThreadPool-2, Exception: java.lang.IllegalStateException: Cannot invoke observeForever on a background thread
.observeOn(AndroidSchedulers.mainThread())
.flatMap { optionGroupSummary ->
val group = optionGroupSummary.orNull()
when {
group == null ->
Observable.just(None)
group.groupId == ALL_COMMUNITIES_GROUP_ID ->
session
.rx()
.liveUser(session.myUserId)
.map { optionalUser ->
GroupSummary(
groupId = ALL_COMMUNITIES_GROUP_ID,
membership = Membership.JOIN,
displayName = stringProvider.getString(R.string.group_all_communities),
avatarUrl = optionalUser.getOrNull()?.avatarUrl ?: "")
}
.map { it.toOption() }
else ->
session
.rx()
.liveGroupSummaries()
.map { it.filter { groupSummary -> groupSummary.groupId == group.groupId } }
.map { it.firstOrNone() }
}
}
.observeOn(Schedulers.computation())
.subscribe {
setState {
copy(groupSummary = it)

View File

@ -71,7 +71,11 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
private fun observeSelectionState() {
selectSubscribe(GroupListViewState::selectedGroup) {
if (it != null) {
_openGroupLiveData.postLiveEvent(it)
val selectedGroup = selectedGroupStore.get()?.orNull()
// We only wan to open group if the updated selectedGroup is a different one.
if (selectedGroup?.groupId != it.groupId) {
_openGroupLiveData.postLiveEvent(it)
}
val optionGroup = Option.fromNullable(it)
selectedGroupStore.post(optionGroup)
}
@ -114,7 +118,12 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
}
)
.execute { async ->
val newSelectedGroup = selectedGroup ?: async()?.firstOrNull()
val currentSelectedGroupId = selectedGroup?.groupId
val newSelectedGroup = if (currentSelectedGroupId != null) {
async()?.find { it.groupId == currentSelectedGroupId }
} else {
async()?.firstOrNull()
}
copy(asyncGroups = async, selectedGroup = newSelectedGroup)
}
}