-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MBL-1077: Coroutines in ChangePasswordViewModel + Tests (#1923)
* - ChangePasswordViewModel is now using Coroutines and fully tested * - linter * - Adopted UIStates types of architecture * - Stable versions, rather than release candidate --------- Co-authored-by: mtgriego <matthew.t.griego@gmail.com>
- Loading branch information
Showing
4 changed files
with
175 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 55 additions & 151 deletions
206
app/src/main/java/com/kickstarter/viewmodels/ChangePasswordViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,69 @@ | ||
package com.kickstarter.viewmodels | ||
|
||
import UpdateUserPasswordMutation | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import com.kickstarter.libs.Environment | ||
import com.kickstarter.libs.rx.transformers.Transformers.errorsV2 | ||
import com.kickstarter.libs.rx.transformers.Transformers.takeWhenV2 | ||
import com.kickstarter.libs.rx.transformers.Transformers.valuesV2 | ||
import com.kickstarter.libs.utils.extensions.addToDisposable | ||
import com.kickstarter.libs.utils.extensions.newPasswordValidationWarnings | ||
import com.kickstarter.libs.utils.extensions.validPassword | ||
import io.reactivex.Observable | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.subjects.BehaviorSubject | ||
import io.reactivex.subjects.PublishSubject | ||
|
||
interface ChangePasswordViewModel { | ||
|
||
interface Inputs { | ||
/** Call when the user clicks the change password button. */ | ||
fun changePasswordClicked() | ||
|
||
/** Call when the current password field changes. */ | ||
fun confirmPassword(confirmPassword: String) | ||
|
||
/** Call when the current password field changes. */ | ||
fun currentPassword(currentPassword: String) | ||
|
||
/** Call when the new password field changes. */ | ||
fun newPassword(newPassword: String) | ||
} | ||
|
||
interface Outputs { | ||
/** Emits when the password update was unsuccessful. */ | ||
fun error(): Observable<String> | ||
|
||
/** Emits when the progress bar should be visible. */ | ||
fun progressBarIsVisible(): Observable<Boolean> | ||
|
||
/** Emits when the password update was successful. */ | ||
fun success(): Observable<String> | ||
} | ||
|
||
class ChangePasswordViewModel(val environment: Environment) : ViewModel(), Inputs, Outputs { | ||
|
||
private val changePasswordClicked = PublishSubject.create<Unit>() | ||
private val confirmPassword = PublishSubject.create<String>() | ||
private val currentPassword = PublishSubject.create<String>() | ||
private val newPassword = PublishSubject.create<String>() | ||
|
||
private val error = BehaviorSubject.create<String>() | ||
private val progressBarIsVisible = BehaviorSubject.create<Boolean>() | ||
private val success = BehaviorSubject.create<String>() | ||
|
||
val inputs: Inputs = this | ||
val outputs: Outputs = this | ||
|
||
private val apolloClient = requireNotNull(this.environment.apolloClientV2()) | ||
private val analytics = this.environment.analytics() | ||
|
||
private val disposables = CompositeDisposable() | ||
|
||
init { | ||
|
||
val changePassword = Observable.combineLatest( | ||
this.currentPassword.startWith(""), | ||
this.newPassword.startWith(""), | ||
this.confirmPassword.startWith("") | ||
) { current, new, confirm -> ChangePassword(current, new, confirm) } | ||
|
||
val changePasswordNotification = changePassword | ||
.compose(takeWhenV2(this.changePasswordClicked)) | ||
.switchMap { cp -> submit(cp).materialize() } | ||
.share() | ||
|
||
changePasswordNotification | ||
.compose(errorsV2()) | ||
.subscribe { error -> | ||
error?.localizedMessage?.let { message -> | ||
this.error.onNext(message) | ||
} | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.rx2.asFlow | ||
|
||
data class UpdatePasswordUIState( | ||
val isLoading: Boolean = false, | ||
val errorMessage: String? = null, | ||
val email: String? = null | ||
) | ||
class ChangePasswordViewModel(val environment: Environment) : ViewModel() { | ||
|
||
private val apolloClient = requireNotNull(this.environment.apolloClientV2()) | ||
private val analytics = requireNotNull(this.environment.analytics()) | ||
|
||
private val mutableUIState = MutableStateFlow(UpdatePasswordUIState()) | ||
val uiState: StateFlow<UpdatePasswordUIState> get() = | ||
mutableUIState.asStateFlow() | ||
.stateIn( | ||
scope = viewModelScope, | ||
started = WhileSubscribed(), | ||
initialValue = UpdatePasswordUIState(isLoading = true) | ||
) | ||
|
||
fun updatePassword(oldPassword: String, newPassword: String) { | ||
viewModelScope.launch { | ||
// TODO: Avoid using GraphQL generated types such as UpdateUserPasswordMutation.Data, return data model defined within the app. | ||
apolloClient.updateUserPassword(oldPassword, newPassword, newPassword) | ||
.asFlow() | ||
.onStart { | ||
mutableUIState.emit(UpdatePasswordUIState(isLoading = true)) | ||
} | ||
.addToDisposable(disposables) | ||
|
||
changePasswordNotification | ||
.compose(valuesV2()) | ||
.map { it.updateUserAccount()?.user()?.email() } | ||
.subscribe { email -> | ||
this.analytics?.reset() | ||
email?.let { | ||
this.success.onNext(it) | ||
} | ||
.map { | ||
analytics.reset() | ||
mutableUIState.emit(UpdatePasswordUIState(isLoading = false, email = it.updateUserAccount()?.user()?.email() ?: "")) | ||
} | ||
.addToDisposable(disposables) | ||
} | ||
|
||
private fun submit(changePassword: ChangePassword): Observable<UpdateUserPasswordMutation.Data> { | ||
return this.apolloClient.updateUserPassword(changePassword.currentPassword, changePassword.newPassword, changePassword.confirmPassword) | ||
.doOnSubscribe { this.progressBarIsVisible.onNext(true) } | ||
.doAfterTerminate { this.progressBarIsVisible.onNext(false) } | ||
} | ||
|
||
fun updatePasswordData(oldPassword: String, newPassword: String) { | ||
this.currentPassword.onNext(oldPassword) | ||
this.newPassword.onNext(newPassword) | ||
this.confirmPassword.onNext(newPassword) | ||
} | ||
|
||
fun resetError() { | ||
this.error.onNext("") | ||
} | ||
|
||
override fun changePasswordClicked() { | ||
this.changePasswordClicked.onNext(Unit) | ||
} | ||
|
||
override fun confirmPassword(confirmPassword: String) { | ||
this.confirmPassword.onNext(confirmPassword) | ||
} | ||
|
||
override fun currentPassword(currentPassword: String) { | ||
this.currentPassword.onNext(currentPassword) | ||
} | ||
|
||
override fun newPassword(newPassword: String) { | ||
this.newPassword.onNext(newPassword) | ||
} | ||
|
||
override fun error(): Observable<String> { | ||
return this.error | ||
} | ||
|
||
override fun progressBarIsVisible(): Observable<Boolean> { | ||
return this.progressBarIsVisible | ||
} | ||
|
||
override fun success(): Observable<String> { | ||
return this.success | ||
} | ||
|
||
data class ChangePassword(val currentPassword: String, val newPassword: String, val confirmPassword: String) { | ||
fun isValid(): Boolean { | ||
return this.currentPassword.validPassword() && | ||
this.newPassword.validPassword() && | ||
this.confirmPassword.validPassword() && | ||
this.confirmPassword == this.newPassword | ||
} | ||
|
||
fun warning(): Int = | ||
newPassword.newPasswordValidationWarnings(confirmPassword) ?: 0 | ||
.catch { | ||
mutableUIState.emit(UpdatePasswordUIState(errorMessage = it.message ?: "", isLoading = false)) | ||
} | ||
.collect() | ||
} | ||
} | ||
|
||
override fun onCleared() { | ||
disposables.clear() | ||
super.onCleared() | ||
fun resetError() { | ||
viewModelScope.launch { | ||
mutableUIState.emit(UpdatePasswordUIState(errorMessage = null)) | ||
} | ||
} | ||
} | ||
|
||
class Factory(private val environment: Environment) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return ChangePasswordViewModel(environment) as T | ||
} | ||
class ChangePasswordViewModelFactory(private val environment: Environment) : | ||
ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return ChangePasswordViewModel(environment) as T | ||
} | ||
} |
Oops, something went wrong.