Convert to ViewEvents -> CreateDirectRoomViewModel

This commit is contained in:
Benoit Marty 2020-02-07 18:11:53 +01:00
parent 33b123f719
commit 35054dc5e8
3 changed files with 47 additions and 22 deletions

View file

@ -31,7 +31,11 @@ import com.google.android.material.chip.ChipGroup
import com.jakewharton.rxbinding3.widget.textChanges
import im.vector.matrix.android.api.session.user.model.User
import im.vector.riotx.R
import im.vector.riotx.core.extensions.*
import im.vector.riotx.core.extensions.cleanup
import im.vector.riotx.core.extensions.configureWith
import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.extensions.hideKeyboard
import im.vector.riotx.core.extensions.setupAsSearch
import im.vector.riotx.core.platform.VectorBaseFragment
import im.vector.riotx.core.utils.DimensionConverter
import kotlinx.android.synthetic.main.fragment_create_direct_room.*
@ -57,8 +61,10 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor(
setupFilterView()
setupAddByMatrixIdView()
setupCloseView()
viewModel.selectUserEvent.observeEvent(this) {
updateChipsView(it)
viewModel.observeViewEvents {
when (it) {
is CreateDirectViewEvents.SelectUserAction -> updateChipsView(it)
}.exhaustive
}
viewModel.selectSubscribe(this, CreateDirectRoomViewState::selectedUsers) {
renderSelectedUsers(it)
@ -132,7 +138,7 @@ class CreateDirectRoomKnownUsersFragment @Inject constructor(
knownUsersController.setData(it)
}
private fun updateChipsView(data: SelectUserAction) {
private fun updateChipsView(data: CreateDirectViewEvents.SelectUserAction) {
if (data.isAdded) {
addChipToGroup(data.user, chipGroup)
} else {

View file

@ -18,8 +18,6 @@
package im.vector.riotx.features.createdirect
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import arrow.core.Option
import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
@ -32,10 +30,7 @@ import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams
import im.vector.matrix.android.api.session.user.model.User
import im.vector.matrix.android.api.util.toMatrixItem
import im.vector.matrix.rx.rx
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.utils.LiveEvent
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit
@ -43,16 +38,10 @@ import java.util.concurrent.TimeUnit
private typealias KnowUsersFilter = String
private typealias DirectoryUsersSearch = String
data class SelectUserAction(
val user: User,
val isAdded: Boolean,
val index: Int
)
class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
initialState: CreateDirectRoomViewState,
private val session: Session)
: VectorViewModel<CreateDirectRoomViewState, CreateDirectRoomAction, EmptyViewEvents>(initialState) {
: VectorViewModel<CreateDirectRoomViewState, CreateDirectRoomAction, CreateDirectViewEvents>(initialState) {
@AssistedInject.Factory
interface Factory {
@ -62,10 +51,6 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
private val knownUsersFilter = BehaviorRelay.createDefault<Option<KnowUsersFilter>>(Option.empty())
private val directoryUsersSearch = BehaviorRelay.create<DirectoryUsersSearch>()
private val _selectUserEvent = MutableLiveData<LiveEvent<SelectUserAction>>()
val selectUserEvent: LiveData<LiveEvent<SelectUserAction>>
get() = _selectUserEvent
companion object : MvRxViewModelFactory<CreateDirectRoomViewModel, CreateDirectRoomViewState> {
@JvmStatic
@ -109,7 +94,7 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
val index = state.selectedUsers.indexOfFirst { it.userId == action.user.userId }
val selectedUsers = state.selectedUsers.minus(action.user)
setState { copy(selectedUsers = selectedUsers) }
_selectUserEvent.postLiveEvent(SelectUserAction(action.user, false, index))
_viewEvents.post(CreateDirectViewEvents.SelectUserAction(action.user, false, index))
}
private fun handleSelectUser(action: CreateDirectRoomAction.SelectUser) = withState { state ->
@ -129,7 +114,7 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
isAddOperation = false
}
setState { copy(selectedUsers = selectedUsers) }
_selectUserEvent.postLiveEvent(SelectUserAction(action.user, isAddOperation, changeIndex))
_viewEvents.post(CreateDirectViewEvents.SelectUserAction(action.user, isAddOperation, changeIndex))
}
private fun observeDirectoryUsers() {

View file

@ -0,0 +1,34 @@
/*
* 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.createdirect
import im.vector.matrix.android.api.session.user.model.User
import im.vector.riotx.core.platform.VectorViewEvents
/**
* Transient events for create direct room screen
*/
sealed class CreateDirectViewEvents : VectorViewEvents {
// data class Loading(val message: CharSequence? = null) : RoomDirectoryViewEvents()
// data class Failure(val throwable: Throwable) : CreateDirectViewEvents()
data class SelectUserAction(
val user: User,
val isAdded: Boolean,
val index: Int
) : CreateDirectViewEvents()
}