Skip to content

Commit

Permalink
feat: Add chaining RxJava (#2117)
Browse files Browse the repository at this point in the history
Fixes: #2116
  • Loading branch information
anhanh11001 authored and iamareebjamal committed Jul 26, 2019
1 parent 6a20b94 commit 573e2ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AboutEventViewModel(private val eventService: EventService, private val re
val error: LiveData<String> = mutableError

fun loadEvent(id: Long) {
if (id.equals(-1)) {
if (id == -1L) {
mutableError.value = Resource().getString(R.string.error_fetching_event_message)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.fossasia.openevent.general.auth
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers
Expand Down Expand Up @@ -35,30 +36,15 @@ class EditProfileViewModel(
fun isLoggedIn() = authService.isLoggedIn()

fun updateProfile(user: User) {
if (encodedImage.isNullOrEmpty()) {
updateUser(user)
return
}
compositeDisposable += authService.uploadImage(UploadImage(encodedImage))
.withDefaultSchedulers()
.doOnSubscribe {
mutableProgress.value = true
}
.doFinally {
mutableProgress.value = false
}
.subscribe({
updateUser(user.copy(avatarUrl = it.url))
mutableMessage.value = resource.getString(R.string.image_upload_success_message)
Timber.d("Image uploaded ${it.url}")
}) {
mutableMessage.value = resource.getString(R.string.image_upload_error_message)
Timber.e(it, "Error uploading user!")
}
}
val updateProfileObservable: Single<User> =
if (encodedImage.isNullOrEmpty())
authService.updateUser(user)
else
authService.uploadImage(UploadImage(encodedImage)).flatMap {
authService.updateUser(user.copy(avatarUrl = it.url))
}

private fun updateUser(user: User) {
compositeDisposable += authService.updateUser(user)
compositeDisposable += updateProfileObservable
.withDefaultSchedulers()
.doOnSubscribe {
mutableProgress.value = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ class EventDetailsViewModel(
val range = StringBuilder()
if (maxPrice == minPrice) {
if (maxPrice == 0f)
range.append("Free")
range.append(resource.getString(R.string.free))
else {
range.append(paymentCurrency)
range.append(" ")
range.append("%.2f".format(minPrice))
}
} else {
if (minPrice == 0f)
range.append("Free")
range.append(resource.getString(R.string.free))
else {
range.append(paymentCurrency)
range.append(" ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,20 @@ class OrderDetailsViewModel(
fun loadAttendeeDetails(orderId: Long) {
if (orderId == -1L) return

compositeDisposable += orderService.getOrderById(orderId)
compositeDisposable += orderService
.getOrderById(orderId)
.flatMap { order ->
orderService.getAttendeesUnderOrder(order.identifier ?: "", order.attendees.map { it.id })
}
.withDefaultSchedulers()
.doOnSubscribe {
mutableProgress.value = true
}.subscribe({
loadAttendeeUnderOrder(it)
}, {
Timber.e(it, "Error fetching attendee details")
}.doFinally {
mutableProgress.value = false
message.value = resource.getString(R.string.error_fetching_attendee_details_message)
})
}

private fun loadAttendeeUnderOrder(order: Order) {
val orderIdentifier = order.identifier ?: return

compositeDisposable += orderService
.getAttendeesUnderOrder(orderIdentifier, order.attendees.map { it.id })
.withDefaultSchedulers()
.subscribe({
}.subscribe({
mutableAttendees.value = it
mutableProgress.value = false
}, {
Timber.e(it, "Error fetching attendee details")
mutableProgress.value = false
message.value = resource.getString(R.string.error_fetching_attendee_details_message)
})
}
Expand Down

0 comments on commit 573e2ca

Please sign in to comment.