Skip to content

Commit

Permalink
fix login info not display error.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingmei2 committed Jul 22, 2019
1 parent 7eb6377 commit 6f31c53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ class LoginFragment : BaseFragment() {
401 -> "username or password failure."
else -> "network failure"
}
else -> ""
else -> "network failure"
}.also { str ->
if (str != "") toast { str }
toast { str }
}
}

mProgressBar.visibility = if (state.isLoading) View.VISIBLE else View.GONE

if (state.autoLoginEvent != null) {
if (state.autoLoginEvent != null && state.useAutoLoginEvent) {
tvUsername.setText(state.autoLoginEvent.username, TextView.BufferType.EDITABLE)
tvPassword.setText(state.autoLoginEvent.password, TextView.BufferType.EDITABLE)
mViewModel.onAutoLoginEventUsed()
}

if (state.loginInfo != null) {
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/qingmei2/sample/ui/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class LoginViewModel(
return mViewStateSubject.hide().distinctUntilChanged()
}

fun onAutoLoginEventUsed() {
mViewStateSubject.copyMap { state ->
state.copy(isLoading = false, throwable = null, useAutoLoginEvent = false, loginInfo = null)
}
}

fun login(username: String?, password: String?) {
when (username.isNullOrEmpty() || password.isNullOrEmpty()) {
true -> mViewStateSubject.copyMap { state ->
Expand All @@ -66,16 +72,16 @@ class LoginViewModel(
.subscribe { state ->
when (state) {
is Result.Loading -> mViewStateSubject.copyMap {
it.copy(isLoading = true, throwable = null, loginInfo = null, autoLoginEvent = null)
it.copy(isLoading = true, throwable = null, loginInfo = null)
}
is Result.Idle -> mViewStateSubject.copyMap {
it.copy(isLoading = false, throwable = null, loginInfo = null, autoLoginEvent = null)
it.copy(isLoading = false, throwable = null, loginInfo = null)
}
is Result.Failure -> mViewStateSubject.copyMap {
it.copy(isLoading = true, throwable = state.error, loginInfo = null, autoLoginEvent = null)
it.copy(isLoading = true, throwable = state.error, loginInfo = null)
}
is Result.Success -> mViewStateSubject.copyMap {
it.copy(isLoading = true, throwable = null, loginInfo = state.data, autoLoginEvent = null)
it.copy(isLoading = true, throwable = null, loginInfo = state.data)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ data class LoginViewState(
val isLoading: Boolean,
val throwable: Throwable?,
val loginInfo: UserInfo?,
val autoLoginEvent: AutoLoginEvent?
val autoLoginEvent: AutoLoginEvent?,
val useAutoLoginEvent: Boolean // the flag ensure login info display one time.
) {

companion object {
Expand All @@ -16,7 +17,8 @@ data class LoginViewState(
isLoading = false,
throwable = null,
loginInfo = null,
autoLoginEvent = null
autoLoginEvent = null,
useAutoLoginEvent = true
)
}
}
Expand All @@ -31,6 +33,7 @@ data class LoginViewState(
if (throwable != other.throwable) return false
if (loginInfo != other.loginInfo) return false
if (autoLoginEvent != other.autoLoginEvent) return false
if (useAutoLoginEvent != other.useAutoLoginEvent) return false

return true
}
Expand All @@ -40,6 +43,7 @@ data class LoginViewState(
result = 31 * result + (throwable?.hashCode() ?: 0)
result = 31 * result + (loginInfo?.hashCode() ?: 0)
result = 31 * result + (autoLoginEvent?.hashCode() ?: 0)
result = 31 * result + useAutoLoginEvent.hashCode()
return result
}
}

0 comments on commit 6f31c53

Please sign in to comment.