mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 02:15:35 +03:00
Rx: observe on computation by default
This commit is contained in:
parent
93ce0cc5e9
commit
480f14902d
7 changed files with 38 additions and 36 deletions
|
@ -21,23 +21,24 @@ import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary
|
|||
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
||||
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
class RxRoom(private val room: Room) {
|
||||
|
||||
fun liveRoomSummary(fetchLastEvent: Boolean): Observable<RoomSummary> {
|
||||
return room.liveRoomSummary(fetchLastEvent).asObservable()
|
||||
return room.liveRoomSummary(fetchLastEvent).asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun liveRoomMemberIds(): Observable<List<String>> {
|
||||
return room.getRoomMemberIdsLive().asObservable()
|
||||
return room.getRoomMemberIdsLive().asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun liveAnnotationSummary(eventId: String): Observable<EventAnnotationsSummary> {
|
||||
return room.getEventSummaryLive(eventId).asObservable()
|
||||
return room.getEventSummaryLive(eventId).asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun liveTimelineEvent(eventId: String): Observable<TimelineEvent> {
|
||||
return room.liveTimeLineEvent(eventId).asObservable()
|
||||
return room.liveTimeLineEvent(eventId).asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,23 +22,24 @@ 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.sync.SyncState
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
class RxSession(private val session: Session) {
|
||||
|
||||
fun liveRoomSummaries(fetchLastEvents: Boolean): Observable<List<RoomSummary>> {
|
||||
return session.liveRoomSummaries(fetchLastEvents).asObservable()
|
||||
return session.liveRoomSummaries(fetchLastEvents).asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun liveGroupSummaries(): Observable<List<GroupSummary>> {
|
||||
return session.liveGroupSummaries().asObservable()
|
||||
return session.liveGroupSummaries().asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun liveSyncState(): Observable<SyncState> {
|
||||
return session.syncState().asObservable()
|
||||
return session.syncState().asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun livePushers(): Observable<List<Pusher>> {
|
||||
return session.livePushers().asObservable()
|
||||
return session.livePushers().asObservable().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package im.vector.riotx.core.utils
|
|||
|
||||
import com.jakewharton.rxrelay2.BehaviorRelay
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
open class RxStore<T>(defaultValue: T? = null) {
|
||||
|
||||
|
@ -28,7 +29,7 @@ open class RxStore<T>(defaultValue: T? = null) {
|
|||
}
|
||||
|
||||
fun observe(): Observable<T> {
|
||||
return storeSubject.hide().distinctUntilChanged()
|
||||
return storeSubject.hide().observeOn(Schedulers.computation())
|
||||
}
|
||||
|
||||
fun post(value: T) {
|
||||
|
|
|
@ -24,6 +24,7 @@ import com.squareup.inject.assisted.AssistedInject
|
|||
import im.vector.matrix.android.api.session.Session
|
||||
import im.vector.matrix.rx.rx
|
||||
import im.vector.riotx.core.platform.VectorViewModel
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
/**
|
||||
* View model used to update the home bottom bar notification counts
|
||||
|
@ -68,6 +69,7 @@ class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: Ho
|
|||
private fun observeRoomSummaries() {
|
||||
homeRoomListStore
|
||||
.observe()
|
||||
.observeOn(Schedulers.computation())
|
||||
.subscribe { list ->
|
||||
list.let { summaries ->
|
||||
val peopleNotifications = summaries
|
||||
|
|
|
@ -21,18 +21,9 @@ import im.vector.riotx.core.utils.RxStore
|
|||
import im.vector.riotx.features.home.room.list.RoomListDisplayModeFilter
|
||||
import im.vector.riotx.features.home.room.list.RoomListFragment
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class HomeRoomListObservableStore @Inject constructor() : RxStore<List<RoomSummary>>() {
|
||||
|
||||
fun observeFilteredBy(displayMode: RoomListFragment.DisplayMode): Observable<List<RoomSummary>> {
|
||||
return observe()
|
||||
.flatMapSingle {
|
||||
Observable.fromIterable(it).filter(RoomListDisplayModeFilter(displayMode)).toList()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
class HomeRoomListObservableStore @Inject constructor() : RxStore<List<RoomSummary>>()
|
||||
|
|
|
@ -23,6 +23,9 @@ import io.reactivex.functions.Predicate
|
|||
class RoomListDisplayModeFilter(private val displayMode: RoomListFragment.DisplayMode) : Predicate<RoomSummary> {
|
||||
|
||||
override fun test(roomSummary: RoomSummary): Boolean {
|
||||
if (roomSummary.membership.isLeft()) {
|
||||
return false
|
||||
}
|
||||
return when (displayMode) {
|
||||
RoomListFragment.DisplayMode.HOME -> roomSummary.notificationCount > 0 || roomSummary.membership == Membership.INVITE
|
||||
RoomListFragment.DisplayMode.PEOPLE -> roomSummary.isDirect && roomSummary.membership == Membership.JOIN
|
||||
|
|
|
@ -31,6 +31,7 @@ import im.vector.matrix.android.api.session.room.model.tag.RoomTag
|
|||
import im.vector.riotx.core.platform.VectorViewModel
|
||||
import im.vector.riotx.core.utils.LiveEvent
|
||||
import im.vector.riotx.features.home.HomeRoomListObservableStore
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import timber.log.Timber
|
||||
|
||||
class RoomListViewModel @AssistedInject constructor(@Assisted initialState: RoomListViewState,
|
||||
|
@ -55,6 +56,7 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room
|
|||
}
|
||||
|
||||
private val displayMode = initialState.displayMode
|
||||
private val roomListDisplayModeFilter = RoomListDisplayModeFilter(displayMode)
|
||||
|
||||
private val _openRoomLiveData = MutableLiveData<LiveEvent<String>>()
|
||||
val openRoomLiveData: LiveData<LiveEvent<String>>
|
||||
|
@ -95,7 +97,9 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room
|
|||
copy(asyncRooms = asyncRooms)
|
||||
}
|
||||
|
||||
homeRoomListObservableSource.observeFilteredBy(displayMode)
|
||||
homeRoomListObservableSource
|
||||
.observe()
|
||||
.observeOn(Schedulers.computation())
|
||||
.map { buildRoomSummaries(it) }
|
||||
.execute { async ->
|
||||
copy(asyncFilteredRooms = async)
|
||||
|
@ -182,23 +186,24 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room
|
|||
val lowPriorities = ArrayList<RoomSummary>()
|
||||
val serverNotices = ArrayList<RoomSummary>()
|
||||
|
||||
for (room in rooms) {
|
||||
if (room.membership.isLeft()) continue
|
||||
val tags = room.tags.map { it.name }
|
||||
when {
|
||||
room.membership == Membership.INVITE -> invites.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_SERVER_NOTICE) -> serverNotices.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_FAVOURITE) -> favourites.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_LOW_PRIORITY) -> lowPriorities.add(room)
|
||||
room.isDirect -> directChats.add(room)
|
||||
else -> groupRooms.add(room)
|
||||
}
|
||||
}
|
||||
rooms
|
||||
.filter { roomListDisplayModeFilter.test(it) }
|
||||
.forEach { room ->
|
||||
val tags = room.tags.map { it.name }
|
||||
when {
|
||||
room.membership == Membership.INVITE -> invites.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_SERVER_NOTICE) -> serverNotices.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_FAVOURITE) -> favourites.add(room)
|
||||
tags.contains(RoomTag.ROOM_TAG_LOW_PRIORITY) -> lowPriorities.add(room)
|
||||
room.isDirect -> directChats.add(room)
|
||||
else -> groupRooms.add(room)
|
||||
}
|
||||
}
|
||||
|
||||
val roomComparator = when (displayMode) {
|
||||
RoomListFragment.DisplayMode.HOME -> chronologicalRoomComparator
|
||||
RoomListFragment.DisplayMode.PEOPLE -> chronologicalRoomComparator
|
||||
RoomListFragment.DisplayMode.ROOMS -> alphabeticalRoomComparator
|
||||
RoomListFragment.DisplayMode.ROOMS -> chronologicalRoomComparator
|
||||
}
|
||||
|
||||
return RoomSummaries().apply {
|
||||
|
@ -210,6 +215,4 @@ class RoomListViewModel @AssistedInject constructor(@Assisted initialState: Room
|
|||
put(RoomCategory.SERVER_NOTICE, serverNotices.sortedWith(roomComparator))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue