Last active
April 29, 2020 10:21
-
-
Save binrebin/b826c8219e3ef2d5100d37200c24f4ec to your computer and use it in GitHub Desktop.
viewmodel
This file contains hidden or 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
imports.... | |
class BasicProfileViewModel @Inject constructor(private val basicProfileUseCase: BasicProfileUseCase, | |
private val getUserUseCase: GetUserUseCase) : ViewModel() { | |
private val _profileDataState = MutableLiveData<DataState<BasicProfileModel>>() | |
val profileDataState: LiveData<DataState<BasicProfileModel>> | |
get() = _profileDataState | |
private val _userDataState = MutableLiveData<DataState<BasicProfileModel>>() | |
val userDataState: LiveData<DataState<BasicProfileModel>> | |
get() = _userDataState | |
private var _applySubmitEvent = MutableLiveData<Event<Any>>() | |
val applySubmitEvent: LiveData<Event<Any>> | |
get() = _applySubmitEvent | |
private var _submitEvent = MutableLiveData<Event<BasicProfileModel>>() | |
val submitEvent: LiveData<Event<BasicProfileModel>> | |
get() = _submitEvent | |
private var profile = userDataState.value?.data ?: BasicProfileModel.empty() | |
fun setGender(genderModel: GenderModel) { | |
profile.genderModel = genderModel | |
this._submitEvent.value = Event(profile) | |
} | |
fun setProfile(basicProfileModel: BasicProfileModel) { | |
this.profile = basicProfileModel.copy() | |
this._submitEvent.value = Event(profile) | |
} | |
fun applySubmit() { | |
this._sharedFilter.value = Event(profile) | |
this._applySubmitEvent.value = Event(Any()) | |
submitProfile(profile) | |
} | |
// for updating via NetworkDatastore | |
fun submitProfile(basicProfileModel: BasicProfileModel){ | |
this.profile = basicProfileModel.copy() | |
launchAsync { | |
try { | |
val profileModel = asyncAwait { | |
BasicProfileModel(basicProfileUseCase.run( | |
profile.genderModel?.gender, | |
profile.genderModel?.genderCode)) | |
} | |
_profileDataState.value = DataState.Success(profileModel) | |
} catch (exception: Exception) { | |
_profileDataState.value = DataState.Error(exception, obtainCurrentData()) | |
} | |
} | |
} | |
fun obtainCurrentData() = _profileDataState.value?.data | |
// for updating via Dao | |
fun loadUser(uid: String) { | |
launchAsync { | |
try { | |
val profileUser = asyncAwait { BasicProfileModel(getUserUseCase.run(uid)) } | |
_userDataState.value = DataState.Success(profileUser) | |
} catch (exception: Exception) { | |
_userDataState.value = DataState.Error(exception, obtainCurrentUser()) | |
} | |
} | |
} | |
fun obtainCurrentUser() = _userDataState.value?.data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment