Let the View model compute the state

This commit is contained in:
Benoit Marty 2021-05-27 14:35:29 +02:00
parent f62ab84e59
commit 96720af52f
3 changed files with 22 additions and 9 deletions

View file

@ -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<RoomDirectoryPickerViewState>() {
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) }
)

View file

@ -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<RoomDirectoryPickerViewState, RoomDirectoryPickerAction, EmptyViewEvents>(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 {

View file

@ -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<String> = emptySet(),
val inEditMode: Boolean = false,
val enteredServer: String = "",
val addServerAsync: Async<Unit> = Uninitialized
val addServerAsync: Async<Unit> = Uninitialized,
// computed
val directories: List<RoomDirectoryServer> = emptyList()
) : MvRxState