Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EditProfileViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() {
- private val status = SingleLiveEvent<Status>()
- fun getStatus(): LiveData<Status> {
- return status
- }
- fun handleImage(intent: Intent?) {
- intent?.data?.let {
- avatar.value = it.toString()
- } ?: run { status.value = Status.INVALID_URI }
- }
- fun saveProfile() {
- validateData()
- user.value?.let {
- var photoUri: Uri? = null
- if (avatar.value != user.value?.avatar) {
- photoUri = Uri.parse(avatar.value)
- }
- disposable = userRepository.saveProfile(it, photoUri)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe({
- status.value = it
- }, {
- status.value = Status.SERVER_CONNECTION_ERROR
- })
- }
- }
- private fun validateData() {
- user.value?.let {
- if (it.firstName.isNullOrBlank()) {
- status.value = Status.EMPTY_FIRST_NAME
- return
- }
- if (it.lastName.isNullOrBlank()) {
- status.value = Status.EMPTY_LAST_NAME
- return
- }
- if (it.city == null || it.city?.name.isNullOrBlank()) {
- status.value = Status.EMPTY_CITY
- return
- }
- }
- }
Add Comment
Please, Sign In to add comment