Skip to content

Commit

Permalink
Add ProfileViewState.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingmei2 committed Jul 22, 2019
1 parent 7fd6f33 commit 3241980
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.qingmei2.rhine.ext.reactivex.clicksThrottleFirst
import com.qingmei2.rhine.image.GlideApp
import com.qingmei2.rhine.util.RxSchedulers
import com.qingmei2.sample.R
import com.qingmei2.sample.entity.UserInfo
import com.qingmei2.sample.utils.toast
import com.uber.autodispose.autoDisposable
import kotlinx.android.synthetic.main.fragment_profile.*
Expand All @@ -32,24 +31,30 @@ class ProfileFragment : BaseFragment() {
}

private fun binds() {
mViewModel.userInfoSubject
mViewModel.observeViewState()
.observeOn(RxSchedulers.ui)
.autoDisposable(scopeProvider)
.subscribe { renderUserOwner(it) }
.subscribe(::onNewState)

mBtnEdit.clicksThrottleFirst()
.autoDisposable(scopeProvider)
.subscribe { toast { "coming soon..." } }
}

private fun renderUserOwner(userInfo: UserInfo) {
GlideApp.with(context!!)
.load(userInfo.avatarUrl)
.apply(RequestOptions().circleCrop())
.into(mIvAvatar)

mTvNickname.text = userInfo.name
mTvBio.text = userInfo.bio
mTvLocation.text = userInfo.location
private fun onNewState(state: ProfileViewState) {
if (state.throwable != null) {
// handle throwable.
}

if (state.userInfo != null) {
GlideApp.with(context!!)
.load(state.userInfo.avatarUrl)
.apply(RequestOptions().circleCrop())
.into(mIvAvatar)

mTvNickname.text = state.userInfo.name
mTvBio.text = state.userInfo.bio
mTvLocation.text = state.userInfo.location
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,18 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.qingmei2.rhine.base.viewmodel.BaseViewModel
import com.qingmei2.rhine.util.SingletonHolderSingleArg
import com.qingmei2.sample.entity.UserInfo
import com.qingmei2.sample.manager.UserManager
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject

class ProfileViewModel(
private val repo: ProfileRepository
) : BaseViewModel() {

private val mErrorEventSubject = BehaviorSubject.create<Throwable>()
private val loadingStateChangedEventSubject = BehaviorSubject.create<Boolean>()
val userInfoSubject = BehaviorSubject.create<UserInfo>()
private val mViewStateSubject: BehaviorSubject<ProfileViewState> =
BehaviorSubject.createDefault(ProfileViewState.initial())

init {
applyState(user = UserManager.INSTANCE)
}

private fun applyState(isLoading: Boolean = false,
user: UserInfo? = null,
error: Throwable? = null) {
this.loadingStateChangedEventSubject.onNext(isLoading)
error?.apply { mErrorEventSubject.onNext(this) }
user?.apply { userInfoSubject.onNext(this) }
fun observeViewState(): Observable<ProfileViewState> {
return mViewStateSubject.hide().distinctUntilChanged()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.qingmei2.sample.ui.main.profile

import com.qingmei2.sample.entity.UserInfo
import com.qingmei2.sample.manager.UserManager

data class ProfileViewState(
val isLoading: Boolean,
val throwable: Throwable?,
val userInfo: UserInfo?
) {

companion object {

fun initial(): ProfileViewState {
return ProfileViewState(
isLoading = false,
throwable = null,
userInfo = UserManager.INSTANCE
)
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ProfileViewState

if (isLoading != other.isLoading) return false
if (throwable != other.throwable) return false
if (userInfo != other.userInfo) return false

return true
}

override fun hashCode(): Int {
var result = isLoading.hashCode()
result = 31 * result + (throwable?.hashCode() ?: 0)
result = 31 * result + (userInfo?.hashCode() ?: 0)
return result
}
}

0 comments on commit 3241980

Please sign in to comment.