From 96720af52f185b51557f95b83a197a286a9edd15 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 27 May 2021 14:35:29 +0200 Subject: [PATCH] Let the View model compute the state --- .../picker/RoomDirectoryPickerController.kt | 9 ++------- .../picker/RoomDirectoryPickerViewModel.kt | 17 ++++++++++++++++- .../picker/RoomDirectoryPickerViewState.kt | 5 ++++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerController.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerController.kt index d6a78d435e..8f45422c29 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerController.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerController.kt @@ -51,8 +51,7 @@ class RoomDirectoryPickerController @Inject constructor( private val stringProvider: StringProvider, private val colorProvider: ColorProvider, private val dimensionConverter: DimensionConverter, - private val errorFormatter: ErrorFormatter, - private val roomDirectoryListCreator: RoomDirectoryListCreator + private val errorFormatter: ErrorFormatter ) : TypedEpoxyController() { var currentRoomDirectoryData: RoomDirectoryData? = null @@ -65,11 +64,7 @@ class RoomDirectoryPickerController @Inject constructor( when (val asyncThirdPartyProtocol = data.asyncThirdPartyRequest) { is Success -> { - val directories = roomDirectoryListCreator.computeDirectories( - asyncThirdPartyProtocol(), - data.customHomeservers - ) - directories.join( + data.directories.join( each = { _, roomDirectoryServer -> buildDirectory(roomDirectoryServer) }, between = { idx, _ -> buildDivider(idx) } ) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt index 049293cafd..f790aa7d67 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewModel.kt @@ -38,7 +38,8 @@ import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsPa class RoomDirectoryPickerViewModel @AssistedInject constructor( @Assisted initialState: RoomDirectoryPickerViewState, private val session: Session, - private val uiStateRepository: UiStateRepository + private val uiStateRepository: UiStateRepository, + private val roomDirectoryListCreator: RoomDirectoryListCreator ) : VectorViewModel(initialState) { @AssistedFactory @@ -56,10 +57,24 @@ class RoomDirectoryPickerViewModel @AssistedInject constructor( } init { + observeAndCompute() load() loadCustomRoomDirectoryHomeservers() } + private fun observeAndCompute() { + selectSubscribe( + RoomDirectoryPickerViewState::asyncThirdPartyRequest, + RoomDirectoryPickerViewState::customHomeservers + ) { async, custom -> + async()?.let { + setState { + copy(directories = roomDirectoryListCreator.computeDirectories(it, custom)) + } + } + } + } + private fun load() { viewModelScope.launch { setState { diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewState.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewState.kt index c78d4ac55c..5cdee862ab 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/picker/RoomDirectoryPickerViewState.kt @@ -19,6 +19,7 @@ package im.vector.app.features.roomdirectory.picker import com.airbnb.mvrx.Async import com.airbnb.mvrx.MvRxState import com.airbnb.mvrx.Uninitialized +import im.vector.app.features.roomdirectory.RoomDirectoryServer import org.matrix.android.sdk.api.session.room.model.thirdparty.ThirdPartyProtocol data class RoomDirectoryPickerViewState( @@ -26,5 +27,7 @@ data class RoomDirectoryPickerViewState( val customHomeservers: Set = emptySet(), val inEditMode: Boolean = false, val enteredServer: String = "", - val addServerAsync: Async = Uninitialized + val addServerAsync: Async = Uninitialized, + // computed + val directories: List = emptyList() ) : MvRxState