Replace some usages of RxJava in reader

This commit is contained in:
arkon 2022-12-02 23:11:42 -05:00
parent b0dc20e00c
commit bb1e7816e1

View File

@ -64,6 +64,11 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import logcat.LogPriority import logcat.LogPriority
import nucleus.presenter.RxPresenter import nucleus.presenter.RxPresenter
@ -74,7 +79,6 @@ 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
import java.util.Date import java.util.Date
import java.util.concurrent.TimeUnit
import eu.kanade.domain.manga.model.Manga as DomainManga import eu.kanade.domain.manga.model.Manga as DomainManga
/** /**
@ -134,9 +138,9 @@ class ReaderPresenter(
private val viewerChaptersRelay = BehaviorRelay.create<ViewerChapters>() private val viewerChaptersRelay = BehaviorRelay.create<ViewerChapters>()
/** /**
* Relay used when loading prev/next chapter needed to lock the UI (with a dialog). * Used when loading prev/next chapter needed to lock the UI (with a dialog).
*/ */
private val isLoadingAdjacentChapterRelay = BehaviorRelay.create<Boolean>() private val isLoadingAdjacentChapterEvent = Channel<Boolean>()
private var chapterToDownload: Download? = null private var chapterToDownload: Download? = null
@ -295,7 +299,11 @@ class ReaderPresenter(
Observable.just(manga).subscribeLatestCache(ReaderActivity::setManga) Observable.just(manga).subscribeLatestCache(ReaderActivity::setManga)
viewerChaptersRelay.subscribeLatestCache(ReaderActivity::setChapters) viewerChaptersRelay.subscribeLatestCache(ReaderActivity::setChapters)
isLoadingAdjacentChapterRelay.subscribeLatestCache(ReaderActivity::setProgressDialog) coroutineScope.launch {
isLoadingAdjacentChapterEvent.receiveAsFlow().collectLatest {
view?.setProgressDialog(it)
}
}
// Read chapterList from an io thread because it's retrieved lazily and would block main. // Read chapterList from an io thread because it's retrieved lazily and would block main.
activeChapterSubscription?.unsubscribe() activeChapterSubscription?.unsubscribe()
@ -377,8 +385,8 @@ class ReaderPresenter(
activeChapterSubscription?.unsubscribe() activeChapterSubscription?.unsubscribe()
activeChapterSubscription = getLoadObservable(loader, chapter) activeChapterSubscription = getLoadObservable(loader, chapter)
.doOnSubscribe { isLoadingAdjacentChapterRelay.call(true) } .doOnSubscribe { coroutineScope.launch { isLoadingAdjacentChapterEvent.send(true) } }
.doOnUnsubscribe { isLoadingAdjacentChapterRelay.call(false) } .doOnUnsubscribe { coroutineScope.launch { isLoadingAdjacentChapterEvent.send(false) } }
.subscribeFirst( .subscribeFirst(
{ view, _ -> { view, _ ->
view.moveToPageIndex(0) view.moveToPageIndex(0)
@ -649,19 +657,21 @@ class ReaderPresenter(
setMangaViewerFlags.awaitSetMangaReadingMode(manga.id!!.toLong(), readingModeType.toLong()) setMangaViewerFlags.awaitSetMangaReadingMode(manga.id!!.toLong(), readingModeType.toLong())
} }
Observable.timer(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) coroutineScope.launchIO {
.subscribeFirst({ view, _ -> delay(250)
val currChapters = viewerChaptersRelay.value val currChapters = viewerChaptersRelay.value
if (currChapters != null) { if (currChapters != null) {
// Save current page // Save current page
val currChapter = currChapters.currChapter val currChapter = currChapters.currChapter
currChapter.requestedPage = currChapter.chapter.last_page_read currChapter.requestedPage = currChapter.chapter.last_page_read
withUIContext {
// Emit manga and chapters to the new viewer // Emit manga and chapters to the new viewer
view.setManga(manga) view?.setManga(manga)
view.setChapters(currChapters) view?.setChapters(currChapters)
}
}
} }
},)
} }
/** /**
@ -688,13 +698,13 @@ class ReaderPresenter(
logcat(LogPriority.INFO) { "Manga orientation is ${manga.orientationType}" } logcat(LogPriority.INFO) { "Manga orientation is ${manga.orientationType}" }
Observable.timer(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) coroutineScope.launchIO {
.subscribeFirst({ view, _ -> delay(250)
val currChapters = viewerChaptersRelay.value val currChapters = viewerChaptersRelay.value
if (currChapters != null) { if (currChapters != null) {
view.setOrientation(getMangaOrientationType()) withUIContext { view?.setOrientation(getMangaOrientationType()) }
}
} }
},)
} }
/** /**
@ -740,12 +750,12 @@ class ReaderPresenter(
) )
withUIContext { withUIContext {
notifier.onComplete(uri) notifier.onComplete(uri)
view!!.onSaveImageResult(SaveImageResult.Success(uri)) view?.onSaveImageResult(SaveImageResult.Success(uri))
} }
} }
} catch (e: Throwable) { } catch (e: Throwable) {
notifier.onError(e.message) notifier.onError(e.message)
view!!.onSaveImageResult(SaveImageResult.Error(e)) view?.onSaveImageResult(SaveImageResult.Error(e))
} }
} }
@ -776,7 +786,7 @@ class ReaderPresenter(
), ),
) )
withUIContext { withUIContext {
view!!.onShareImageResult(uri, page) view?.onShareImageResult(uri, page)
} }
} }
} catch (e: Throwable) { } catch (e: Throwable) {