Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add chaining RxJava #2117

Merged
merged 1 commit into from
Jul 26, 2019
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 @@ -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