Guest User

Untitled

a guest
Oct 24th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. class EditProfileViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() {
  2. private val status = SingleLiveEvent<Status>()
  3.  
  4. fun getStatus(): LiveData<Status> {
  5. return status
  6. }
  7.  
  8. fun handleImage(intent: Intent?) {
  9. intent?.data?.let {
  10. avatar.value = it.toString()
  11. } ?: run { status.value = Status.INVALID_URI }
  12. }
  13.  
  14. fun saveProfile() {
  15. validateData()
  16. user.value?.let {
  17. var photoUri: Uri? = null
  18. if (avatar.value != user.value?.avatar) {
  19. photoUri = Uri.parse(avatar.value)
  20. }
  21. disposable = userRepository.saveProfile(it, photoUri)
  22. .subscribeOn(Schedulers.io())
  23. .observeOn(AndroidSchedulers.mainThread())
  24. .subscribe({
  25. status.value = it
  26. }, {
  27. status.value = Status.SERVER_CONNECTION_ERROR
  28. })
  29. }
  30. }
  31.  
  32. private fun validateData() {
  33. user.value?.let {
  34. if (it.firstName.isNullOrBlank()) {
  35. status.value = Status.EMPTY_FIRST_NAME
  36. return
  37. }
  38. if (it.lastName.isNullOrBlank()) {
  39. status.value = Status.EMPTY_LAST_NAME
  40. return
  41. }
  42. if (it.city == null || it.city?.name.isNullOrBlank()) {
  43. status.value = Status.EMPTY_CITY
  44. return
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment