diff --git a/app/src/main/java/eu/kanade/domain/manga/interactor/UpdateManga.kt b/app/src/main/java/eu/kanade/domain/manga/interactor/UpdateManga.kt index babb7ac41..f52c4bfeb 100644 --- a/app/src/main/java/eu/kanade/domain/manga/interactor/UpdateManga.kt +++ b/app/src/main/java/eu/kanade/domain/manga/interactor/UpdateManga.kt @@ -58,4 +58,8 @@ class UpdateManga( suspend fun awaitUpdateLastUpdate(mangaId: Long): Boolean { return mangaRepository.update(MangaUpdate(id = mangaId, lastUpdate = Date().time)) } + + suspend fun awaitUpdateCoverLastModified(mangaId: Long): Boolean { + return mangaRepository.update(MangaUpdate(id = mangaId, coverLastModified = Date().time)) + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index a7813a5c4..11d1fd339 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -813,7 +813,7 @@ class MangaController : val dataUri = data?.data if (dataUri == null || resultCode != Activity.RESULT_OK) return val activity = activity ?: return - presenter.editCover(manga!!, activity, dataUri) + presenter.editCover(activity, dataUri) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 07285ce5b..a2da0f41d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.Track +import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.data.database.models.toMangaInfo import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download @@ -22,7 +23,6 @@ import eu.kanade.tachiyomi.data.saver.ImageSaver import eu.kanade.tachiyomi.data.track.EnhancedTrackService import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackService -import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSManga @@ -33,8 +33,10 @@ import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.chapter.getChapterSort import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay +import eu.kanade.tachiyomi.util.editCover import eu.kanade.tachiyomi.util.isLocal import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.prepUpdateCover import eu.kanade.tachiyomi.util.removeCovers @@ -299,31 +301,20 @@ class MangaPresenter( /** * Update cover with local file. * - * @param manga the manga edited. * @param context Context. * @param data uri of the cover resource. */ - fun editCover(manga: Manga, context: Context, data: Uri) { - Observable - .fromCallable { - context.contentResolver.openInputStream(data)?.use { - if (manga.isLocal()) { - LocalSource.updateCover(context, manga, it) - manga.updateCoverLastModified(db) - db.insertManga(manga).executeAsBlocking() - } else if (manga.favorite) { - coverCache.setCustomCoverToCache(manga, it) - manga.updateCoverLastModified(db) - } - true + fun editCover(context: Context, data: Uri) { + presenterScope.launchIO { + context.contentResolver.openInputStream(data)?.use { + try { + val result = manga.toDomainManga()!!.editCover(context, it) + launchUI { if (result) view?.onSetCoverSuccess() } + } catch (e: Exception) { + launchUI { view?.onSetCoverError(e) } } } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeFirst( - { view, _ -> view.onSetCoverSuccess() }, - { view, e -> view.onSetCoverError(e) }, - ) + } } fun deleteCustomCover(manga: Manga) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 17c8fa177..11dc5de57 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -877,7 +877,7 @@ class ReaderActivity : BaseRxActivity() { * cover to the presenter. */ fun setAsCover(page: ReaderPage) { - presenter.setAsCover(page) + presenter.setAsCover(this, page) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index 7e075bd41..1a0ac93a9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.reader import android.app.Application +import android.content.Context import android.net.Uri import android.os.Bundle import com.jakewharton.rxrelay.BehaviorRelay @@ -8,9 +9,11 @@ import eu.kanade.domain.chapter.interactor.UpdateChapter import eu.kanade.domain.chapter.model.ChapterUpdate import eu.kanade.domain.history.interactor.UpsertHistory import eu.kanade.domain.history.model.HistoryUpdate +import eu.kanade.domain.manga.model.isLocal import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.saver.Image @@ -19,7 +22,6 @@ import eu.kanade.tachiyomi.data.saver.Location import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.job.DelayedTrackingStore import eu.kanade.tachiyomi.data.track.job.DelayedTrackingUpdateJob -import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter @@ -32,7 +34,7 @@ import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.setting.OrientationType import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType import eu.kanade.tachiyomi.util.chapter.getChapterSort -import eu.kanade.tachiyomi.util.isLocal +import eu.kanade.tachiyomi.util.editCover import eu.kanade.tachiyomi.util.lang.byteSize import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchUI @@ -41,7 +43,6 @@ 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 eu.kanade.tachiyomi.util.updateCoverLastModified import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import logcat.LogPriority @@ -692,36 +693,28 @@ class ReaderPresenter( /** * Sets the image of this [page] as cover and notifies the UI of the result. */ - fun setAsCover(page: ReaderPage) { + fun setAsCover(context: Context, page: ReaderPage) { if (page.status != Page.READY) return - val manga = manga ?: return + val manga = manga?.toDomainManga() ?: return val stream = page.stream ?: return - Observable - .fromCallable { - stream().use { - if (manga.isLocal()) { - val context = Injekt.get() - LocalSource.updateCover(context, manga, it) - manga.updateCoverLastModified(db) - SetAsCoverResult.Success - } else { - if (manga.favorite) { - coverCache.setCustomCoverToCache(manga, it) - manga.updateCoverLastModified(db) - SetAsCoverResult.Success - } else { - SetAsCoverResult.AddToLibraryFirst - } - } - } + presenterScope.launchIO { + val result = try { + manga.editCover(context, stream()) + } catch (e: Exception) { + false } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeFirst( - { view, result -> view.onSetAsCoverResult(result) }, - { view, _ -> view.onSetAsCoverResult(SetAsCoverResult.Error) }, - ) + launchUI { + val resultResult = if (!result) { + SetAsCoverResult.Error + } else if (manga.isLocal() || manga.favorite) { + SetAsCoverResult.Success + } else { + SetAsCoverResult.AddToLibraryFirst + } + view?.onSetAsCoverResult(resultResult) + } + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt index d2c7ce142..f22199a22 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt @@ -1,5 +1,9 @@ package eu.kanade.tachiyomi.util +import android.content.Context +import eu.kanade.domain.manga.interactor.UpdateManga +import eu.kanade.domain.manga.model.isLocal +import eu.kanade.domain.manga.model.toDbManga import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga @@ -8,7 +12,9 @@ import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.model.SManga import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.io.InputStream import java.util.Date +import eu.kanade.domain.manga.model.Manga as DomainManga fun Manga.isLocal() = source == LocalSource.ID @@ -82,3 +88,21 @@ fun Manga.shouldDownloadNewChapters(db: DatabaseHelper, prefs: PreferencesHelper // In included category return categoriesForManga.any { it in includedCategories } } + +suspend fun DomainManga.editCover( + context: Context, + stream: InputStream, + updateManga: UpdateManga = Injekt.get(), + coverCache: CoverCache = Injekt.get(), +): Boolean { + return if (isLocal()) { + LocalSource.updateCover(context, toDbManga(), stream) + updateManga.awaitUpdateCoverLastModified(id) + } else if (favorite) { + coverCache.setCustomCoverToCache(toDbManga(), stream) + updateManga.awaitUpdateCoverLastModified(id) + } else { + // We should never reach this block + false + } +}