using isLoading boolean instead of stateless async result for the display name and profile picture updates

This commit is contained in:
Adam Brown 2022-03-08 11:06:35 +00:00
parent adf2c642da
commit e3df9c4cef
4 changed files with 20 additions and 24 deletions

View File

@ -828,20 +828,20 @@ class OnboardingViewModel @AssistedInject constructor(
} }
private fun updateDisplayName(displayName: String) { private fun updateDisplayName(displayName: String) {
setState { copy(asyncDisplayName = Loading()) } setState { copy(isLoading = true) }
viewModelScope.launch { viewModelScope.launch {
val activeSession = activeSessionHolder.getActiveSession() val activeSession = activeSessionHolder.getActiveSession()
try { try {
activeSession.setDisplayName(activeSession.myUserId, displayName) activeSession.setDisplayName(activeSession.myUserId, displayName)
setState { setState {
copy( copy(
asyncDisplayName = Success(Unit), isLoading = false,
personalizationState = personalizationState.copy(displayName = displayName) personalizationState = personalizationState.copy(displayName = displayName)
) )
} }
handleDisplayNameStepComplete() handleDisplayNameStepComplete()
} catch (error: Throwable) { } catch (error: Throwable) {
setState { copy(asyncDisplayName = Fail(error)) } setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(error)) _viewEvents.post(OnboardingViewEvents.Failure(error))
} }
} }
@ -883,7 +883,7 @@ class OnboardingViewModel @AssistedInject constructor(
when (val pictureUri = state.personalizationState.selectedPictureUri) { when (val pictureUri = state.personalizationState.selectedPictureUri) {
null -> _viewEvents.post(OnboardingViewEvents.Failure(NullPointerException("picture uri is missing from state"))) null -> _viewEvents.post(OnboardingViewEvents.Failure(NullPointerException("picture uri is missing from state")))
else -> { else -> {
setState { copy(asyncProfilePicture = Loading()) } setState { copy(isLoading = true) }
viewModelScope.launch { viewModelScope.launch {
val activeSession = activeSessionHolder.getActiveSession() val activeSession = activeSessionHolder.getActiveSession()
try { try {
@ -894,12 +894,12 @@ class OnboardingViewModel @AssistedInject constructor(
) )
setState { setState {
copy( copy(
asyncProfilePicture = Success(Unit), isLoading = false,
) )
} }
onProfilePictureSaved() onProfilePictureSaved()
} catch (error: Throwable) { } catch (error: Throwable) {
setState { copy(asyncProfilePicture = Fail(error)) } setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(error)) _viewEvents.post(OnboardingViewEvents.Failure(error))
} }
} }

View File

@ -34,8 +34,7 @@ data class OnboardingViewState(
val asyncResetPassword: Async<Unit> = Uninitialized, val asyncResetPassword: Async<Unit> = Uninitialized,
val asyncResetMailConfirmed: Async<Unit> = Uninitialized, val asyncResetMailConfirmed: Async<Unit> = Uninitialized,
val asyncRegistration: Async<Unit> = Uninitialized, val asyncRegistration: Async<Unit> = Uninitialized,
val asyncDisplayName: Async<Unit> = Uninitialized, val isLoading: Boolean = false,
val asyncProfilePicture: Async<Unit> = Uninitialized,
@PersistState @PersistState
val onboardingFlow: OnboardingFlow? = null, val onboardingFlow: OnboardingFlow? = null,
@ -73,14 +72,12 @@ data class OnboardingViewState(
val personalizationState: PersonalizationState = PersonalizationState() val personalizationState: PersonalizationState = PersonalizationState()
) : MavericksState { ) : MavericksState {
fun isLoading(): Boolean { fun legacyIsLoading(): Boolean {
return asyncLoginAction is Loading || return asyncLoginAction is Loading ||
asyncHomeServerLoginFlowRequest is Loading || asyncHomeServerLoginFlowRequest is Loading ||
asyncResetPassword is Loading || asyncResetPassword is Loading ||
asyncResetMailConfirmed is Loading || asyncResetMailConfirmed is Loading ||
asyncRegistration is Loading || asyncRegistration is Loading
asyncDisplayName is Loading ||
asyncProfilePicture is Loading
} }
} }

View File

@ -121,7 +121,7 @@ class FtueAuthVariant(
private fun updateWithState(viewState: OnboardingViewState) { private fun updateWithState(viewState: OnboardingViewState) {
isForceLoginFallbackEnabled = viewState.isForceLoginFallbackEnabled isForceLoginFallbackEnabled = viewState.isForceLoginFallbackEnabled
views.loginLoading.isVisible = viewState.isLoading() views.loginLoading.isVisible = viewState.isLoading || viewState.legacyIsLoading()
} }
override fun setIsLoading(isLoading: Boolean) = Unit override fun setIsLoading(isLoading: Boolean) = Unit

View File

@ -17,7 +17,6 @@
package im.vector.app.features.onboarding package im.vector.app.features.onboarding
import android.net.Uri import android.net.Uri
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import com.airbnb.mvrx.Uninitialized import com.airbnb.mvrx.Uninitialized
@ -260,8 +259,8 @@ class OnboardingViewModelTest {
test test
.assertStatesChanges( .assertStatesChanges(
initialState, initialState,
{ copy(asyncDisplayName = Loading()) }, { copy(isLoading = true) },
{ copy(asyncDisplayName = Fail(AN_ERROR)) }, { copy(isLoading = false) },
) )
.assertEvents(OnboardingViewEvents.Failure(AN_ERROR)) .assertEvents(OnboardingViewEvents.Failure(AN_ERROR))
.finish() .finish()
@ -307,7 +306,7 @@ class OnboardingViewModelTest {
viewModel.handle(OnboardingAction.SaveSelectedProfilePicture) viewModel.handle(OnboardingAction.SaveSelectedProfilePicture)
test test
.assertStates(expectedProfilePictureFailureStates(initialStateWithPicture, AN_ERROR)) .assertStates(expectedProfilePictureFailureStates(initialStateWithPicture))
.assertEvents(OnboardingViewEvents.Failure(AN_ERROR)) .assertEvents(OnboardingViewEvents.Failure(AN_ERROR))
.finish() .finish()
} }
@ -362,20 +361,20 @@ class OnboardingViewModelTest {
private fun expectedProfilePictureSuccessStates(state: OnboardingViewState) = listOf( private fun expectedProfilePictureSuccessStates(state: OnboardingViewState) = listOf(
state, state,
state.copy(asyncProfilePicture = Loading()), state.copy(isLoading = true),
state.copy(asyncProfilePicture = Success(Unit)) state.copy(isLoading = false)
) )
private fun expectedProfilePictureFailureStates(state: OnboardingViewState, cause: Exception) = listOf( private fun expectedProfilePictureFailureStates(state: OnboardingViewState) = listOf(
state, state,
state.copy(asyncProfilePicture = Loading()), state.copy(isLoading = true),
state.copy(asyncProfilePicture = Fail(cause)) state.copy(isLoading = false)
) )
private fun expectedSuccessfulDisplayNameUpdateStates(): List<OnboardingViewState.() -> OnboardingViewState> { private fun expectedSuccessfulDisplayNameUpdateStates(): List<OnboardingViewState.() -> OnboardingViewState> {
return listOf( return listOf(
{ copy(asyncDisplayName = Loading()) }, { copy(isLoading = true) },
{ copy(asyncDisplayName = Success(Unit), personalizationState = personalizationState.copy(displayName = A_DISPLAY_NAME)) } { copy(isLoading = false, personalizationState = personalizationState.copy(displayName = A_DISPLAY_NAME)) }
) )
} }