Created
July 8, 2019 17:54
-
-
Save photizzo/4d41b239fb98ad9b5aaf4b2027545609 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dryva.data.boilerplate.source.trip | |
import android.arch.paging.PagedList | |
import com.dryva.data.boilerplate.repository.driver.DryvaCache | |
import com.dryva.data.boilerplate.repository.trip.TripCache | |
import com.dryva.data.boilerplate.repository.trip.TripRemote | |
import com.dryva.domain.model.BookingData | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.schedulers.Schedulers | |
import io.reactivex.subjects.PublishSubject | |
import javax.inject.Inject | |
/** | |
* This boundary callback gets notified when user reaches to the edges of the list | |
* for example when the database cannot provide any more data or is empty. | |
* It then fetching data from the server and stores in the database | |
**/ | |
class BookingHistoryBoundaryCallback @Inject constructor( | |
private val remote: TripRemote, | |
private val cache: TripCache, | |
private val driverCache: DryvaCache | |
) : PagedList.BoundaryCallback<BookingData>() { | |
var compositeDisposable = CompositeDisposable() | |
var networkErrors = PublishSubject.create<String>() | |
// holds the last requested offset. When the request is successful, increment the offset number. | |
private var lastRequestedPage = 0 | |
// avoid triggering multiple requests in the same time | |
private var isRequestInProgress = false | |
/** | |
* Database returned 0 items. We should query the backend for more items. | |
*/ | |
override fun onZeroItemsLoaded() { | |
fetchTripHistoryAndSaveInDb() | |
} | |
/** | |
* When all items in the database were loaded, we need to query the backend for more items. | |
*/ | |
override fun onItemAtEndLoaded(itemAtEnd: BookingData) { | |
fetchTripHistoryAndSaveInDb() | |
} | |
/** | |
* fetches booking data from server and save in db | |
*/ | |
private fun fetchTripHistoryAndSaveInDb() { | |
if (isRequestInProgress) return | |
isRequestInProgress = true | |
val disposable = driverCache.getDriverInfo() | |
.flatMap { driver -> | |
remote.getBookings("Bearer ${driver.token}", driver.id, lastRequestedPage).doOnSuccess { | |
if(lastRequestedPage == 0) { | |
//clear the db of old booking data and start saving fresh booking data | |
cache.clearBookings().subscribe() | |
} | |
lastRequestedPage++ | |
isRequestInProgress = false | |
cache.saveBookings(it).subscribe() | |
cache.setIsBookingCached(true) | |
}.doOnError { throwable -> | |
//check if booking data is from server is empty and clear bookings in the db | |
if (throwable.message.equals("No active booking")) cache.clearBookings().subscribe() | |
networkErrors.onNext(throwable.localizedMessage) | |
} | |
}.subscribeOn(Schedulers.computation()) | |
.subscribe({ | |
}, { throwable -> | |
networkErrors.onNext(throwable.localizedMessage) | |
}) | |
compositeDisposable.add(disposable) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment