Last active
October 17, 2019 09:37
-
-
Save KoMinkyu/e135e2e4525e5f0ca5b17ac10754483d to your computer and use it in GitHub Desktop.
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
data class ViewState( | |
val loginButtonEnabled: ObservableBoolean = ObservableBoolean(false) | |
) : ViewStateType | |
sealed class Mutation : MutationType { | |
class LoginButtonEnabled(val enabled: Boolean) : Mutation() | |
} | |
sealed class Action : ActionType { | |
object LoginButtonClicks : Action() | |
data class EmailInputs(val inputs: String) : Action() | |
data class PasswordInputs(val inputs: String) : Action() | |
data class AccountItemAction(val action: AccountItemAction) : Action() | |
} | |
interface MainViewModel { | |
class ViewModel(application: Application) : BaseViewModel<ViewState>(application) { | |
init { | |
val emailAndPassword = Observables.combineLatest( | |
actions().filterType<Action.EmailInputs>() | |
.map { it.inputs }, | |
actions().filterType<Action.PasswordInputs>() | |
.map { it.inputs } | |
) | |
// test | |
val loginButtonClicks = actions().filterType<Action.LoginButtonClicks>() | |
.share() | |
act(Action.LoginButtonClicks) | |
// handle success and failure with notification | |
val loginRequestNotification = loginButtonClicks | |
.withLatestFrom(emailAndPassword) { _, pair -> pair } | |
.switchMap { login(it.first, it.second) } | |
.materialize() | |
// mutate login button enabled state | |
emailAndPassword | |
.map { !it.first.isNullOrEmpty() && !it.second.isNullOrEmpty() } | |
.map { enabled -> Mutation.LoginButtonEnabled(enabled) } | |
.autoDispose() | |
.subscribe { mutate(it) } | |
} | |
private fun login(email: String, password: String): Observable<Unit> { | |
return Observable.empty() | |
} | |
override fun reduce(mutation: MutationType, viewState: ViewState): ViewState { | |
return when (mutation) { | |
is Mutation.LoginButtonEnabled -> { | |
viewState.apply { | |
this.loginButtonEnabled.set(mutation.enabled) | |
} | |
} | |
else -> viewState | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment