2019-01-18 10:12:08 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* * Copyright 2019 New Vector Ltd
|
|
|
|
* *
|
|
|
|
* * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* * you may not use this file except in compliance with the License.
|
|
|
|
* * You may obtain a copy of the License at
|
|
|
|
* *
|
|
|
|
* * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* *
|
|
|
|
* * Unless required by applicable law or agreed to in writing, software
|
|
|
|
* * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* * See the License for the specific language governing permissions and
|
|
|
|
* * limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-29 18:57:38 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.list
|
|
|
|
|
|
|
|
import arrow.core.Option
|
|
|
|
import com.airbnb.mvrx.MvRxViewModelFactory
|
2019-01-16 18:25:43 +00:00
|
|
|
import com.airbnb.mvrx.ViewModelContext
|
2018-12-29 18:57:38 +00:00
|
|
|
import im.vector.matrix.android.api.Matrix
|
|
|
|
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.RoomSummary
|
|
|
|
import im.vector.matrix.rx.rx
|
|
|
|
import im.vector.riotredesign.core.platform.RiotViewModel
|
|
|
|
import im.vector.riotredesign.features.home.group.SelectedGroupHolder
|
2019-01-11 11:07:38 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.VisibleRoomHolder
|
2018-12-29 18:57:38 +00:00
|
|
|
import io.reactivex.Observable
|
|
|
|
import io.reactivex.functions.BiFunction
|
2019-01-11 11:07:38 +00:00
|
|
|
import io.reactivex.rxkotlin.subscribeBy
|
2018-12-29 18:57:38 +00:00
|
|
|
import org.koin.android.ext.android.get
|
|
|
|
|
|
|
|
class RoomListViewModel(initialState: RoomListViewState,
|
|
|
|
private val session: Session,
|
|
|
|
private val selectedGroupHolder: SelectedGroupHolder,
|
2019-01-11 11:07:38 +00:00
|
|
|
private val visibleRoomHolder: VisibleRoomHolder,
|
2018-12-29 18:57:38 +00:00
|
|
|
private val roomSelectionRepository: RoomSelectionRepository)
|
|
|
|
: RiotViewModel<RoomListViewState>(initialState) {
|
|
|
|
|
2019-01-16 18:25:43 +00:00
|
|
|
companion object : MvRxViewModelFactory<RoomListViewModel, RoomListViewState> {
|
2018-12-29 18:57:38 +00:00
|
|
|
|
|
|
|
@JvmStatic
|
2019-01-16 18:25:43 +00:00
|
|
|
override fun create(viewModelContext: ViewModelContext, state: RoomListViewState): RoomListViewModel? {
|
2018-12-29 18:57:38 +00:00
|
|
|
val currentSession = Matrix.getInstance().currentSession
|
2019-01-16 18:25:43 +00:00
|
|
|
val roomSelectionRepository = viewModelContext.activity.get<RoomSelectionRepository>()
|
|
|
|
val selectedGroupHolder = viewModelContext.activity.get<SelectedGroupHolder>()
|
|
|
|
val visibleRoomHolder = viewModelContext.activity.get<VisibleRoomHolder>()
|
2019-01-11 11:07:38 +00:00
|
|
|
return RoomListViewModel(state, currentSession, selectedGroupHolder, visibleRoomHolder, roomSelectionRepository)
|
2018-12-29 18:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init {
|
|
|
|
observeRoomSummaries()
|
2019-01-11 11:07:38 +00:00
|
|
|
observeVisibleRoom()
|
2018-12-29 18:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun accept(action: RoomListActions) {
|
|
|
|
when (action) {
|
|
|
|
is RoomListActions.SelectRoom -> handleSelectRoom(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PRIVATE METHODS *****************************************************************************
|
|
|
|
|
|
|
|
private fun handleSelectRoom(action: RoomListActions.SelectRoom) = withState { state ->
|
|
|
|
if (state.selectedRoomId != action.roomSummary.roomId) {
|
|
|
|
roomSelectionRepository.saveLastSelectedRoom(action.roomSummary.roomId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:07:38 +00:00
|
|
|
private fun observeVisibleRoom() {
|
|
|
|
visibleRoomHolder.visibleRoom()
|
|
|
|
.subscribeBy {
|
|
|
|
setState { copy(selectedRoomId = it) }
|
|
|
|
}
|
|
|
|
.disposeOnClear()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:57:38 +00:00
|
|
|
private fun observeRoomSummaries() {
|
|
|
|
Observable.combineLatest<List<RoomSummary>, Option<GroupSummary>, RoomSummaries>(
|
|
|
|
session.rx().liveRoomSummaries(),
|
|
|
|
selectedGroupHolder.selectedGroup(),
|
|
|
|
BiFunction { rooms, selectedGroupOption ->
|
|
|
|
val selectedGroup = selectedGroupOption.orNull()
|
|
|
|
|
|
|
|
val filteredDirectRooms = rooms
|
|
|
|
.filter { it.isDirect }
|
|
|
|
.filter {
|
|
|
|
if (selectedGroup == null) {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
it.otherMemberIds
|
|
|
|
.intersect(selectedGroup.userIds)
|
|
|
|
.isNotEmpty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val filteredGroupRooms = rooms
|
|
|
|
.filter { !it.isDirect }
|
|
|
|
.filter {
|
|
|
|
selectedGroup?.roomIds?.contains(it.roomId) ?: true
|
|
|
|
}
|
|
|
|
RoomSummaries(filteredDirectRooms, filteredGroupRooms)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.execute { async ->
|
|
|
|
copy(
|
2019-01-11 11:07:38 +00:00
|
|
|
asyncRooms = async
|
2018-12-29 18:57:38 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|