creating a common type for the edit/selecting of homeserver urls to avoid duplication in the model

This commit is contained in:
Adam Brown 2022-04-08 11:54:57 +01:00
parent f70d61321c
commit 6304d5dc93
5 changed files with 22 additions and 12 deletions

View File

@ -29,8 +29,14 @@ sealed interface OnboardingAction : VectorViewModelAction {
data class OnIAlreadyHaveAnAccount(val resetLoginConfig: Boolean, val onboardingFlow: OnboardingFlow) : OnboardingAction
data class UpdateServerType(val serverType: ServerType) : OnboardingAction
data class SelectHomeServer(val homeServerUrl: String) : OnboardingAction
data class EditHomeServer(val homeServerUrl: String) : OnboardingAction
sealed interface HomeServerChange : OnboardingAction {
val homeServerUrl: String
data class SelectHomeServer(override val homeServerUrl: String) : HomeServerChange
data class EditHomeServer(override val homeServerUrl: String) : HomeServerChange
}
data class UpdateUseCase(val useCase: FtueUseCase) : OnboardingAction
object ResetUseCase : OnboardingAction
data class UpdateSignMode(val signMode: SignMode) : OnboardingAction

View File

@ -140,8 +140,7 @@ class OnboardingViewModel @AssistedInject constructor(
is OnboardingAction.UpdateServerType -> handleUpdateServerType(action)
is OnboardingAction.UpdateSignMode -> handleUpdateSignMode(action)
is OnboardingAction.InitWith -> handleInitWith(action)
is OnboardingAction.SelectHomeServer -> run { lastAction = action }.also { handleHomeserverChange(action.homeServerUrl) }
is OnboardingAction.EditHomeServer -> run { lastAction = action }.also { handleHomeserverChange(action.homeServerUrl) }
is OnboardingAction.HomeServerChange -> withAction(action) { handleHomeserverChange(action.homeServerUrl) }
is OnboardingAction.LoginOrRegister -> handleLoginOrRegister(action).also { lastAction = action }
is OnboardingAction.Register -> handleRegisterWith(action).also { lastAction = action }
is OnboardingAction.LoginWithToken -> handleLoginWithToken(action)
@ -163,6 +162,11 @@ class OnboardingViewModel @AssistedInject constructor(
}
}
private fun withAction(action: OnboardingAction, block: (OnboardingAction) -> Unit) {
lastAction = action
block(action)
}
private fun handleSplashAction(resetConfig: Boolean, onboardingFlow: OnboardingFlow) {
if (resetConfig) {
loginConfig = null
@ -202,7 +206,7 @@ class OnboardingViewModel @AssistedInject constructor(
// It happens when we get the login flow, or during direct authentication.
// So alter the homeserver config and retrieve again the login flow
when (val finalLastAction = lastAction) {
is OnboardingAction.SelectHomeServer -> {
is OnboardingAction.HomeServerChange.SelectHomeServer -> {
currentHomeServerConnectionConfig
?.let { it.copy(allowedFingerprints = it.allowedFingerprints + action.fingerprint) }
?.let { startAuthenticationFlow(it) }
@ -350,7 +354,7 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleUpdateUseCase(action: OnboardingAction.UpdateUseCase) {
setState { copy(useCase = action.useCase) }
when (vectorFeatures.isOnboardingCombinedRegisterEnabled()) {
true -> handle(OnboardingAction.SelectHomeServer(defaultHomeserverUrl))
true -> handle(OnboardingAction.HomeServerChange.SelectHomeServer(defaultHomeserverUrl))
false -> _viewEvents.post(OnboardingViewEvents.OpenServerSelection)
}
}
@ -370,7 +374,7 @@ class OnboardingViewModel @AssistedInject constructor(
ServerType.Unknown -> Unit /* Should not happen */
ServerType.MatrixOrg ->
// Request login flow here
handle(OnboardingAction.SelectHomeServer(matrixOrgUrl))
handle(OnboardingAction.HomeServerChange.SelectHomeServer(matrixOrgUrl))
ServerType.EMS,
ServerType.Other -> _viewEvents.post(OnboardingViewEvents.OnServerSelectionDone(action.serverType))
}
@ -632,8 +636,8 @@ class OnboardingViewModel @AssistedInject constructor(
private fun onAuthenticationStartedSuccess() {
withState {
when (lastAction) {
is OnboardingAction.EditHomeServer -> _viewEvents.post(OnboardingViewEvents.OnHomeserverEdited)
is OnboardingAction.SelectHomeServer -> {
is OnboardingAction.HomeServerChange.EditHomeServer -> _viewEvents.post(OnboardingViewEvents.OnHomeserverEdited)
is OnboardingAction.HomeServerChange.SelectHomeServer -> {
if (it.selectedHomeserver.preferredLoginMode.supportsSignModeScreen()) {
when (it.onboardingFlow) {
OnboardingFlow.SignIn -> handleUpdateSignMode(OnboardingAction.UpdateSignMode(SignMode.SignIn))

View File

@ -66,7 +66,7 @@ class FtueAuthCombinedServerSelectionFragment @Inject constructor() : AbstractFt
}
private fun updateServerUrl() {
viewModel.handle(OnboardingAction.EditHomeServer(views.chooseServerInput.content().ensureProtocol().ensureTrailingSlash()))
viewModel.handle(OnboardingAction.HomeServerChange.EditHomeServer(views.chooseServerInput.content().ensureProtocol().ensureTrailingSlash()))
}
override fun resetViewModel() {

View File

@ -139,7 +139,7 @@ class FtueAuthServerUrlFormFragment @Inject constructor() : AbstractFtueAuthFrag
}
else -> {
views.loginServerUrlFormHomeServerUrl.setText(serverUrl, false /* to avoid completion dialog flicker*/)
viewModel.handle(OnboardingAction.SelectHomeServer(serverUrl))
viewModel.handle(OnboardingAction.HomeServerChange.SelectHomeServer(serverUrl))
}
}
}

View File

@ -241,7 +241,7 @@ class OnboardingViewModelTest {
fakeStartAuthenticationFlowUseCase.givenResult(A_HOMESERVER_CONFIG, StartAuthenticationResult(false, SELECTED_HOMESERVER_STATE))
fakeHomeServerHistoryService.expectUrlToBeAdded(A_HOMESERVER_CONFIG.homeServerUri.toString())
viewModel.handle(OnboardingAction.EditHomeServer(A_HOMESERVER_URL))
viewModel.handle(OnboardingAction.HomeServerChange.EditHomeServer(A_HOMESERVER_URL))
test
.assertStatesChanges(