Convert to ViewEvents -> GroupListViewModel

This commit is contained in:
Benoit Marty 2020-02-07 18:02:42 +01:00
parent 24667f38b8
commit 33b123f719
3 changed files with 39 additions and 15 deletions

View File

@ -26,7 +26,7 @@ import im.vector.matrix.android.api.session.group.model.GroupSummary
import im.vector.riotx.R
import im.vector.riotx.core.extensions.cleanup
import im.vector.riotx.core.extensions.configureWith
import im.vector.riotx.core.extensions.observeEvent
import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.platform.StateView
import im.vector.riotx.core.platform.VectorBaseFragment
import im.vector.riotx.features.home.HomeActivitySharedAction
@ -51,8 +51,10 @@ class GroupListFragment @Inject constructor(
stateView.contentView = groupListView
groupListView.configureWith(groupController)
viewModel.subscribe { renderState(it) }
viewModel.openGroupLiveData.observeEvent(this) {
sharedActionViewModel.post(HomeActivitySharedAction.OpenGroup)
viewModel.observeViewEvents {
when (it) {
is GroupListViewEvents.OpenGroupSummary -> sharedActionViewModel.post(HomeActivitySharedAction.OpenGroup)
}.exhaustive
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2020 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.
*/
package im.vector.riotx.features.grouplist
import im.vector.riotx.core.platform.VectorViewEvents
/**
* Transient events for group list screen
*/
sealed class GroupListViewEvents : VectorViewEvents {
// data class Loading(val message: CharSequence? = null) : RoomDirectoryViewEvents()
// data class Failure(val throwable: Throwable) : GroupListViewEvents()
object OpenGroupSummary : GroupListViewEvents()
}

View File

@ -17,8 +17,6 @@
package im.vector.riotx.features.grouplist
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import arrow.core.Option
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
@ -32,11 +30,8 @@ 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.extensions.postLiveEvent
import im.vector.riotx.core.platform.EmptyViewEvents
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.LiveEvent
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
@ -46,7 +41,7 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
private val selectedGroupStore: SelectedGroupDataSource,
private val session: Session,
private val stringProvider: StringProvider
) : VectorViewModel<GroupListViewState, GroupListAction, EmptyViewEvents>(initialState) {
) : VectorViewModel<GroupListViewState, GroupListAction, GroupListViewEvents>(initialState) {
@AssistedInject.Factory
interface Factory {
@ -62,9 +57,7 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
}
}
private val _openGroupLiveData = MutableLiveData<LiveEvent<GroupSummary>>()
val openGroupLiveData: LiveData<LiveEvent<GroupSummary>>
get() = _openGroupLiveData
private var currentGroupId = ""
init {
observeGroupSummaries()
@ -74,10 +67,10 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro
private fun observeSelectionState() {
selectSubscribe(GroupListViewState::selectedGroup) { groupSummary ->
if (groupSummary != null) {
val selectedGroup = _openGroupLiveData.value?.peekContent()
// We only want to open group if the updated selectedGroup is a different one.
if (selectedGroup?.groupId != groupSummary.groupId) {
_openGroupLiveData.postLiveEvent(groupSummary)
if (currentGroupId != groupSummary.groupId) {
currentGroupId = groupSummary.groupId
_viewEvents.post(GroupListViewEvents.OpenGroupSummary)
}
val optionGroup = Option.just(groupSummary)
selectedGroupStore.post(optionGroup)