|
@@ -37,7 +37,6 @@ import eu.kanade.tachiyomi.data.track.TrackManager
|
|
|
import eu.kanade.tachiyomi.source.SourceManager
|
|
|
import eu.kanade.tachiyomi.source.model.Page
|
|
|
import eu.kanade.tachiyomi.source.online.HttpSource
|
|
|
-import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
|
|
|
import eu.kanade.tachiyomi.ui.reader.loader.ChapterLoader
|
|
|
import eu.kanade.tachiyomi.ui.reader.loader.DownloadPageLoader
|
|
|
import eu.kanade.tachiyomi.ui.reader.loader.HttpPageLoader
|
|
@@ -60,17 +59,20 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil
|
|
|
import eu.kanade.tachiyomi.util.storage.cacheImageDir
|
|
|
import eu.kanade.tachiyomi.util.system.isOnline
|
|
|
import eu.kanade.tachiyomi.util.system.logcat
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
+import kotlinx.coroutines.MainScope
|
|
|
import kotlinx.coroutines.async
|
|
|
import kotlinx.coroutines.awaitAll
|
|
|
+import kotlinx.coroutines.cancel
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
import logcat.LogPriority
|
|
|
+import nucleus.presenter.RxPresenter
|
|
|
import rx.Observable
|
|
|
import rx.Subscription
|
|
|
import rx.android.schedulers.AndroidSchedulers
|
|
|
import rx.schedulers.Schedulers
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
import uy.kohesive.injekt.api.get
|
|
|
-import uy.kohesive.injekt.injectLazy
|
|
|
import java.util.Date
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
import eu.kanade.domain.manga.model.Manga as DomainManga
|
|
@@ -82,6 +84,7 @@ class ReaderPresenter(
|
|
|
private val sourceManager: SourceManager = Injekt.get(),
|
|
|
private val downloadManager: DownloadManager = Injekt.get(),
|
|
|
private val downloadProvider: DownloadProvider = Injekt.get(),
|
|
|
+ private val imageSaver: ImageSaver = Injekt.get(),
|
|
|
preferences: BasePreferences = Injekt.get(),
|
|
|
private val downloadPreferences: DownloadPreferences = Injekt.get(),
|
|
|
private val readerPreferences: ReaderPreferences = Injekt.get(),
|
|
@@ -95,7 +98,9 @@ class ReaderPresenter(
|
|
|
private val upsertHistory: UpsertHistory = Injekt.get(),
|
|
|
private val updateChapter: UpdateChapter = Injekt.get(),
|
|
|
private val setMangaViewerFlags: SetMangaViewerFlags = Injekt.get(),
|
|
|
-) : BasePresenter<ReaderActivity>() {
|
|
|
+) : RxPresenter<ReaderActivity>() {
|
|
|
+
|
|
|
+ private val coroutineScope: CoroutineScope = MainScope()
|
|
|
|
|
|
/**
|
|
|
* The manga loaded in the reader. It can be null when instantiated for a short time.
|
|
@@ -133,8 +138,6 @@ class ReaderPresenter(
|
|
|
*/
|
|
|
private val isLoadingAdjacentChapterRelay = BehaviorRelay.create<Boolean>()
|
|
|
|
|
|
- private val imageSaver: ImageSaver by injectLazy()
|
|
|
-
|
|
|
private var chapterToDownload: Download? = null
|
|
|
|
|
|
/**
|
|
@@ -205,6 +208,7 @@ class ReaderPresenter(
|
|
|
*/
|
|
|
override fun onDestroy() {
|
|
|
super.onDestroy()
|
|
|
+ coroutineScope.cancel()
|
|
|
val currentChapters = viewerChaptersRelay.value
|
|
|
if (currentChapters != null) {
|
|
|
currentChapters.unref()
|
|
@@ -242,7 +246,7 @@ class ReaderPresenter(
|
|
|
*/
|
|
|
fun onSaveInstanceStateNonConfigurationChange() {
|
|
|
val currentChapter = getCurrentChapter() ?: return
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
saveChapterProgress(currentChapter)
|
|
|
}
|
|
|
}
|
|
@@ -261,7 +265,7 @@ class ReaderPresenter(
|
|
|
fun init(mangaId: Long, initialChapterId: Long) {
|
|
|
if (!needsInit()) return
|
|
|
|
|
|
- presenterScope.launchIO {
|
|
|
+ coroutineScope.launchIO {
|
|
|
try {
|
|
|
val manga = getManga.await(mangaId)
|
|
|
withUIContext {
|
|
@@ -467,7 +471,7 @@ class ReaderPresenter(
|
|
|
if (getCurrentChapter()?.pageLoader !is DownloadPageLoader) return
|
|
|
val nextChapter = viewerChaptersRelay.value?.nextChapter?.chapter ?: return
|
|
|
|
|
|
- presenterScope.launchIO {
|
|
|
+ coroutineScope.launchIO {
|
|
|
val isNextChapterDownloaded = downloadManager.isChapterDownloaded(
|
|
|
nextChapter.name,
|
|
|
nextChapter.scanlator,
|
|
@@ -523,7 +527,7 @@ class ReaderPresenter(
|
|
|
* Called when reader chapter is changed in reader or when activity is paused.
|
|
|
*/
|
|
|
private fun saveReadingProgress(readerChapter: ReaderChapter) {
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
saveChapterProgress(readerChapter)
|
|
|
saveChapterHistory(readerChapter)
|
|
|
}
|
|
@@ -613,7 +617,7 @@ class ReaderPresenter(
|
|
|
fun bookmarkCurrentChapter(bookmarked: Boolean) {
|
|
|
val chapter = getCurrentChapter()?.chapter ?: return
|
|
|
chapter.bookmark = bookmarked // Otherwise the bookmark icon doesn't update
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
updateChapter.await(
|
|
|
ChapterUpdate(
|
|
|
id = chapter.id!!.toLong(),
|
|
@@ -726,7 +730,7 @@ class ReaderPresenter(
|
|
|
|
|
|
// Copy file in background.
|
|
|
try {
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
val uri = imageSaver.save(
|
|
|
image = Image.Page(
|
|
|
inputStream = page.stream!!,
|
|
@@ -762,7 +766,7 @@ class ReaderPresenter(
|
|
|
val filename = generateFilename(manga, page)
|
|
|
|
|
|
try {
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
destDir.deleteRecursively()
|
|
|
val uri = imageSaver.save(
|
|
|
image = Image.Page(
|
|
@@ -788,7 +792,7 @@ class ReaderPresenter(
|
|
|
val manga = manga?.toDomainManga() ?: return
|
|
|
val stream = page.stream ?: return
|
|
|
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
try {
|
|
|
manga.editCover(context, stream())
|
|
|
withUIContext {
|
|
@@ -834,7 +838,7 @@ class ReaderPresenter(
|
|
|
val trackManager = Injekt.get<TrackManager>()
|
|
|
val context = Injekt.get<Application>()
|
|
|
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
getTracks.await(manga.id!!)
|
|
|
.mapNotNull { track ->
|
|
|
val service = trackManager.getService(track.syncId)
|
|
@@ -874,7 +878,7 @@ class ReaderPresenter(
|
|
|
if (!chapter.chapter.read) return
|
|
|
val manga = manga ?: return
|
|
|
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
downloadManager.enqueueChaptersToDelete(listOf(chapter.chapter.toDomainChapter()!!), manga.toDomainManga()!!)
|
|
|
}
|
|
|
}
|
|
@@ -884,11 +888,17 @@ class ReaderPresenter(
|
|
|
* are ignored.
|
|
|
*/
|
|
|
private fun deletePendingChapters() {
|
|
|
- presenterScope.launchNonCancellable {
|
|
|
+ coroutineScope.launchNonCancellable {
|
|
|
downloadManager.deletePendingChapters()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // We're trying to avoid using Rx, so we "undeprecate" this
|
|
|
+ @Suppress("DEPRECATION")
|
|
|
+ override fun getView(): ReaderActivity? {
|
|
|
+ return super.getView()
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Subscribes an observable with [deliverFirst] and adds it to the presenter's lifecycle
|
|
|
* subscription list.
|