Replace some usages of RxJava

This commit is contained in:
arkon
2022-07-10 19:48:00 -04:00
parent cbcab5a545
commit 788583e66f
6 changed files with 144 additions and 145 deletions
@@ -51,15 +51,13 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import logcat.LogPriority
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Date
@@ -112,7 +110,7 @@ open class BrowseSourcePresenter(
/**
* Subscription for the pager.
*/
private var pagerSubscription: Subscription? = null
private var pagerJob: Job? = null
/**
* Subscription for one request from the pager.
@@ -129,7 +127,6 @@ open class BrowseSourcePresenter(
super.onCreate(savedState)
source = sourceManager.get(sourceId) as? CatalogueSource ?: return
sourceFilters = source.getFilterList()
if (savedState != null) {
@@ -158,25 +155,37 @@ open class BrowseSourcePresenter(
pager = createPager(query, filters)
val sourceId = source.id
val sourceDisplayMode = prefs.sourceDisplayMode()
// Prepare the pager.
pagerSubscription?.let { remove(it) }
pagerSubscription = pager.results()
.observeOn(Schedulers.io())
.map { (first, second) -> first to second.map { networkToLocalManga(it, sourceId).toDomainManga()!! } }
.doOnNext { initializeMangas(it.second) }
.map { (first, second) -> first to second.map { SourceItem(it, sourceDisplayMode) } }
.observeOn(AndroidSchedulers.mainThread())
.subscribeReplay(
{ view, (page, mangas) ->
view.onAddPage(page, mangas)
},
{ _, error ->
pagerJob?.cancel()
pagerJob = presenterScope.launchIO {
pager.asFlow()
.map { (first, second) ->
first to second.map {
networkToLocalManga(
it,
sourceId,
).toDomainManga()!!
}
}
.onEach { initializeMangas(it.second) }
.map { (first, second) ->
first to second.map {
SourceItem(
it,
sourceDisplayMode,
)
}
}
.catch { error ->
logcat(LogPriority.ERROR, error)
},
)
}
.collectLatest { (page, mangas) ->
withUIContext {
view?.onAddPage(page, mangas)
}
}
}
// Request first page.
requestNext()
@@ -1,9 +1,10 @@
package eu.kanade.tachiyomi.ui.browse.source.browse
import com.jakewharton.rxrelay.PublishRelay
import eu.kanade.core.util.asFlow
import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.SManga
import rx.Observable
import kotlinx.coroutines.flow.Flow
/**
* A general pager for source requests (latest updates, popular, search)
@@ -15,8 +16,8 @@ abstract class Pager(var currentPage: Int = 1) {
protected val results: PublishRelay<Pair<Int, List<SManga>>> = PublishRelay.create()
fun results(): Observable<Pair<Int, List<SManga>>> {
return results.asObservable()
fun asFlow(): Flow<Pair<Int, List<SManga>>> {
return results.asObservable().asFlow()
}
abstract suspend fun requestNextPage()