From 33b123f719cc7fb128e0b46d6064309324bdf151 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 7 Feb 2020 18:02:42 +0100 Subject: [PATCH] Convert to ViewEvents -> GroupListViewModel --- .../features/grouplist/GroupListFragment.kt | 8 +++-- .../features/grouplist/GroupListViewEvents.kt | 29 +++++++++++++++++++ .../features/grouplist/GroupListViewModel.kt | 17 ++++------- 3 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewEvents.kt diff --git a/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListFragment.kt b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListFragment.kt index c9c98a58ab..e884761cdf 100644 --- a/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListFragment.kt @@ -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 } } diff --git a/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewEvents.kt b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewEvents.kt new file mode 100644 index 0000000000..92e4a5c6e3 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewEvents.kt @@ -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() +} diff --git a/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewModel.kt b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewModel.kt index c31a30b432..f14583c5d5 100644 --- a/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/grouplist/GroupListViewModel.kt @@ -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(initialState) { +) : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { @@ -62,9 +57,7 @@ class GroupListViewModel @AssistedInject constructor(@Assisted initialState: Gro } } - private val _openGroupLiveData = MutableLiveData>() - val openGroupLiveData: LiveData> - 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)