Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class AuthService(
}
}

fun syncProfile(id: Long = authHolder.getId()): Single<User> {
return authApi.getProfile(id)
.map {
userDao.insertUser(it)
it
}
}

fun sendResetPasswordEmail(email: String): Single<RequestTokenResponse> {
val requestToken = RequestToken(Email(email))
return authApi.requestToken(requestToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class EditProfileFragment : Fragment(), ComplexBackPressFragment {
}
})

profileViewModel.fetchProfile()
profileViewModel.getProfile()

editProfileViewModel.progress
.nonNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ class ProfileFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
rootView = inflater.inflate(R.layout.fragment_profile, container, false)
if (profileViewModel.isLoggedIn())
profileViewModel.fetchProfile()
if (profileViewModel.isLoggedIn()) {
profileViewModel.getProfile()
profileViewModel.syncProfile()
}

val progressDialog = progressDialog(context, getString(R.string.loading_message))
profileViewModel.progress
Expand All @@ -95,31 +97,42 @@ class ProfileFragment : Fragment() {
.nonNull()
.observe(viewLifecycleOwner, Observer {
user = it
rootView.name.text = "${it.firstName.nullToEmpty()} ${it.lastName.nullToEmpty()}"
rootView.email.text = it.email
emailSettings = it.email
rootView.verifiedTick.isVisible = it.isVerified
rootView.resendEmailTextView.isVisible = !it.isVerified
rootView.verifiedTextView.text =
if (it.isVerified) getString(R.string.verified) else getString(R.string.not_verified)
if (it.isVerified)
rootView.verifiedTextView.setTextColor(
resources.getColorStateList(android.R.color.holo_green_light)
)

Picasso.get()
.load(it.avatarUrl)
.placeholder(requireDrawable(requireContext(), R.drawable.ic_account_circle_grey))
.transform(CircleTransform())
.into(rootView.avatar)

rootView.editProfileRL.setOnClickListener {
findNavController(rootView).navigate(ProfileFragmentDirections.actionProfileToEditProfile())
}
updateProfile(it)
})

profileViewModel.updatedUser
.nonNull()
.observe(viewLifecycleOwner, Observer {
user = it
updateProfile(it)
})

rootView.editProfileRL.setOnClickListener {
findNavController(rootView).navigate(ProfileFragmentDirections.actionProfileToEditProfile())
}
return rootView
}

private fun updateProfile(it: User) {
rootView.name.text = "${it.firstName.nullToEmpty()} ${it.lastName.nullToEmpty()}"
rootView.email.text = it.email
emailSettings = it.email
rootView.verifiedTick.isVisible = it.isVerified
rootView.resendEmailTextView.isVisible = !it.isVerified
rootView.verifiedTextView.text =
if (it.isVerified) getString(R.string.verified) else getString(R.string.not_verified)
if (it.isVerified)
rootView.verifiedTextView.setTextColor(
resources.getColorStateList(android.R.color.holo_green_light)
)

Picasso.get()
.load(it.avatarUrl)
.placeholder(requireDrawable(requireContext(), R.drawable.ic_account_circle_grey))
.transform(CircleTransform())
.into(rootView.avatar)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ProfileViewModel(private val authService: AuthService, private val resourc
val user: LiveData<User> = mutableUser
private val mutableMessage = SingleLiveEvent<String>()
val message: LiveData<String> = mutableMessage
private val mutableUpdatedUser = MutableLiveData<User>()
val updatedUser: LiveData<User> = mutableUpdatedUser

fun isLoggedIn() = authService.isLoggedIn()

Expand All @@ -34,7 +36,7 @@ class ProfileViewModel(private val authService: AuthService, private val resourc
}
}

fun fetchProfile() {
fun getProfile() {
compositeDisposable += authService.getProfile()
.withDefaultSchedulers()
.doOnSubscribe {
Expand All @@ -50,6 +52,17 @@ class ProfileViewModel(private val authService: AuthService, private val resourc
}
}

fun syncProfile() {
compositeDisposable += authService.syncProfile()
.withDefaultSchedulers()
.subscribe({ user ->
Timber.d("Response Success")
this.mutableUpdatedUser.value = user
}) {
Timber.e(it, "Failure")
}
}

fun resendVerificationEmail(email: String) {
compositeDisposable += authService.resendVerificationEmail(email)
.withDefaultSchedulers()
Expand Down