Remove use of RxJava from TrackPresenter

This commit is contained in:
arkon 2021-01-04 14:47:23 -05:00
parent 8a792e6d76
commit 7eb0868791

View File

@ -11,12 +11,10 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.lang.await import eu.kanade.tachiyomi.util.lang.await
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.runAsObservable
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import rx.Observable import kotlinx.coroutines.Job
import rx.Subscription import kotlinx.coroutines.async
import rx.android.schedulers.AndroidSchedulers import kotlinx.coroutines.awaitAll
import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -33,59 +31,57 @@ class TrackPresenter(
private val loggedServices by lazy { trackManager.services.filter { it.isLogged } } private val loggedServices by lazy { trackManager.services.filter { it.isLogged } }
private var trackSubscription: Subscription? = null private var trackJob: Job? = null
private var searchJob: Job? = null
private var searchSubscription: Subscription? = null private var refreshJob: Job? = null
private var refreshSubscription: Subscription? = null
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState) super.onCreate(savedState)
fetchTrackings() fetchTrackings()
} }
fun fetchTrackings() { private fun fetchTrackings() {
trackSubscription?.let { remove(it) } trackJob?.cancel()
trackSubscription = db.getTracks(manga) trackJob = launchIO {
.asRxObservable() val tracks = db.getTracks(manga).await()
.map { tracks -> trackList = loggedServices.map { service ->
loggedServices.map { service -> TrackItem(tracks.find { it.sync_id == service.id }, service)
TrackItem(tracks.find { it.sync_id == service.id }, service)
}
} }
.observeOn(AndroidSchedulers.mainThread()) view?.onNextTrackings(trackList)
.doOnNext { trackList = it } }
.subscribeLatestCache(TrackController::onNextTrackings)
} }
fun refresh() { fun refresh() {
refreshSubscription?.let { remove(it) } refreshJob?.cancel()
refreshSubscription = Observable.from(trackList) refreshJob = launchIO {
.filter { it.track != null } try {
.flatMap { item -> trackList
runAsObservable({ item.service.refresh(item.track!!) }) .filter { it.track != null }
.flatMap { db.insertTrack(it).asRxObservable() } .map {
.map { item } async {
.onErrorReturn { item } val track = it.service.refresh(it.track!!)
db.insertTrack(track).await()
}
}
.awaitAll()
view?.onRefreshDone()
} catch (e: Throwable) {
view?.onRefreshError(e)
} }
.toList() }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeFirst(
{ view, _ -> view.onRefreshDone() },
TrackController::onRefreshError
)
} }
fun search(query: String, service: TrackService) { fun search(query: String, service: TrackService) {
searchSubscription?.let { remove(it) } searchJob?.cancel()
searchSubscription = runAsObservable({ service.search(query) }) searchJob = launchIO {
.subscribeOn(Schedulers.io()) try {
.observeOn(AndroidSchedulers.mainThread()) val results = service.search(query)
.subscribeLatestCache( launchUI { view?.onSearchResults(results) }
TrackController::onSearchResults, } catch (e: Throwable) {
TrackController::onSearchResultsError launchUI { view?.onSearchResultsError(e) }
) }
}
} }
fun registerTracking(item: Track?, service: TrackService) { fun registerTracking(item: Track?, service: TrackService) {
@ -115,12 +111,10 @@ class TrackPresenter(
db.insertTrack(track).await() db.insertTrack(track).await()
view?.onRefreshDone() view?.onRefreshDone()
} catch (e: Throwable) { } catch (e: Throwable) {
launchUI { launchUI { view?.onRefreshError(e) }
view?.onRefreshError(e)
// Restart on error to set old values // Restart on error to set old values
fetchTrackings() fetchTrackings()
}
} }
} }
} }