Close form only if necessary

This commit is contained in:
Benoit Marty 2020-11-24 08:34:01 +01:00 committed by Benoit Marty
parent c5e6e004dd
commit f5ae95d7f1
1 changed files with 15 additions and 9 deletions

View File

@ -164,7 +164,7 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
is RoomAliasAction.SetNewLocalAliasLocalPart -> handleSetNewLocalAliasLocalPart(action) is RoomAliasAction.SetNewLocalAliasLocalPart -> handleSetNewLocalAliasLocalPart(action)
RoomAliasAction.AddLocalAlias -> handleAddLocalAlias() RoomAliasAction.AddLocalAlias -> handleAddLocalAlias()
is RoomAliasAction.RemoveLocalAlias -> handleRemoveLocalAlias(action) is RoomAliasAction.RemoveLocalAlias -> handleRemoveLocalAlias(action)
is RoomAliasAction.PublishAlias -> handleAddAliasManually(action) is RoomAliasAction.PublishAlias -> handlePublishAlias(action)
}.exhaustive }.exhaustive
} }
@ -212,39 +212,45 @@ class RoomAliasViewModel @AssistedInject constructor(@Assisted initialState: Roo
val newAlias = (state.publishManuallyState as? RoomAliasViewState.AddAliasState.Editing)?.value ?: return@withState val newAlias = (state.publishManuallyState as? RoomAliasViewState.AddAliasState.Editing)?.value ?: return@withState
updateCanonicalAlias( updateCanonicalAlias(
canonicalAlias = state.canonicalAlias, canonicalAlias = state.canonicalAlias,
alternativeAliases = state.alternativeAliases + newAlias alternativeAliases = state.alternativeAliases + newAlias,
closeForm = true
) )
} }
private fun handleAddAliasManually(action: RoomAliasAction.PublishAlias) = withState { state -> private fun handlePublishAlias(action: RoomAliasAction.PublishAlias) = withState { state ->
updateCanonicalAlias( updateCanonicalAlias(
canonicalAlias = state.canonicalAlias, canonicalAlias = state.canonicalAlias,
alternativeAliases = state.alternativeAliases + action.alias alternativeAliases = state.alternativeAliases + action.alias,
closeForm = false
) )
} }
private fun handleRemoveAlias(action: RoomAliasAction.UnpublishAlias) = withState { state -> private fun handleRemoveAlias(action: RoomAliasAction.UnpublishAlias) = withState { state ->
updateCanonicalAlias( updateCanonicalAlias(
canonicalAlias = state.canonicalAlias, canonicalAlias = state.canonicalAlias,
alternativeAliases = state.alternativeAliases - action.alias alternativeAliases = state.alternativeAliases - action.alias,
) closeForm = false )
} }
private fun handleSetCanonicalAlias(action: RoomAliasAction.SetCanonicalAlias) = withState { state -> private fun handleSetCanonicalAlias(action: RoomAliasAction.SetCanonicalAlias) = withState { state ->
updateCanonicalAlias( updateCanonicalAlias(
canonicalAlias = action.canonicalAlias, canonicalAlias = action.canonicalAlias,
alternativeAliases = state.alternativeAliases alternativeAliases = state.alternativeAliases,
closeForm = false
) )
} }
private fun updateCanonicalAlias(canonicalAlias: String?, alternativeAliases: List<String>) { private fun updateCanonicalAlias(canonicalAlias: String?, alternativeAliases: List<String>, closeForm: Boolean) {
postLoading(true) postLoading(true)
room.updateCanonicalAlias(canonicalAlias, alternativeAliases, object : MatrixCallback<Unit> { room.updateCanonicalAlias(canonicalAlias, alternativeAliases, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
setState { setState {
copy( copy(
isLoading = false, isLoading = false,
publishManuallyState = RoomAliasViewState.AddAliasState.Closed publishManuallyState = if (closeForm) RoomAliasViewState.AddAliasState.Closed else publishManuallyState,
// Local echo
canonicalAlias = canonicalAlias,
alternativeAliases = alternativeAliases
) )
} }
} }