Convert edit cover function to suspend function (#7330)

This commit is contained in:
Ivan Iskandar 2022-06-18 21:01:03 +07:00 committed by GitHub
parent dd983c803b
commit 28e7009b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 52 deletions

View File

@ -58,4 +58,8 @@ class UpdateManga(
suspend fun awaitUpdateLastUpdate(mangaId: Long): Boolean { suspend fun awaitUpdateLastUpdate(mangaId: Long): Boolean {
return mangaRepository.update(MangaUpdate(id = mangaId, lastUpdate = Date().time)) return mangaRepository.update(MangaUpdate(id = mangaId, lastUpdate = Date().time))
} }
suspend fun awaitUpdateCoverLastModified(mangaId: Long): Boolean {
return mangaRepository.update(MangaUpdate(id = mangaId, coverLastModified = Date().time))
}
} }

View File

@ -813,7 +813,7 @@ class MangaController :
val dataUri = data?.data val dataUri = data?.data
if (dataUri == null || resultCode != Activity.RESULT_OK) return if (dataUri == null || resultCode != Activity.RESULT_OK) return
val activity = activity ?: return val activity = activity ?: return
presenter.editCover(manga!!, activity, dataUri) presenter.editCover(activity, dataUri)
} }
} }

View File

@ -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.Manga
import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.MangaCategory
import eu.kanade.tachiyomi.data.database.models.Track 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.database.models.toMangaInfo
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.download.model.Download 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.EnhancedTrackService
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSChapter
import eu.kanade.tachiyomi.source.model.toSManga 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.getChapterSort
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay
import eu.kanade.tachiyomi.util.editCover
import eu.kanade.tachiyomi.util.isLocal import eu.kanade.tachiyomi.util.isLocal
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.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.prepUpdateCover import eu.kanade.tachiyomi.util.prepUpdateCover
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
@ -299,31 +301,20 @@ class MangaPresenter(
/** /**
* Update cover with local file. * Update cover with local file.
* *
* @param manga the manga edited.
* @param context Context. * @param context Context.
* @param data uri of the cover resource. * @param data uri of the cover resource.
*/ */
fun editCover(manga: Manga, context: Context, data: Uri) { fun editCover(context: Context, data: Uri) {
Observable presenterScope.launchIO {
.fromCallable { context.contentResolver.openInputStream(data)?.use {
context.contentResolver.openInputStream(data)?.use { try {
if (manga.isLocal()) { val result = manga.toDomainManga()!!.editCover(context, it)
LocalSource.updateCover(context, manga, it) launchUI { if (result) view?.onSetCoverSuccess() }
manga.updateCoverLastModified(db) } catch (e: Exception) {
db.insertManga(manga).executeAsBlocking() launchUI { view?.onSetCoverError(e) }
} else if (manga.favorite) {
coverCache.setCustomCoverToCache(manga, it)
manga.updateCoverLastModified(db)
}
true
} }
} }
.subscribeOn(Schedulers.io()) }
.observeOn(AndroidSchedulers.mainThread())
.subscribeFirst(
{ view, _ -> view.onSetCoverSuccess() },
{ view, e -> view.onSetCoverError(e) },
)
} }
fun deleteCustomCover(manga: Manga) { fun deleteCustomCover(manga: Manga) {

View File

@ -877,7 +877,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
* cover to the presenter. * cover to the presenter.
*/ */
fun setAsCover(page: ReaderPage) { fun setAsCover(page: ReaderPage) {
presenter.setAsCover(page) presenter.setAsCover(this, page)
} }
/** /**

View File

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.ui.reader package eu.kanade.tachiyomi.ui.reader
import android.app.Application import android.app.Application
import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import com.jakewharton.rxrelay.BehaviorRelay 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.chapter.model.ChapterUpdate
import eu.kanade.domain.history.interactor.UpsertHistory import eu.kanade.domain.history.interactor.UpsertHistory
import eu.kanade.domain.history.model.HistoryUpdate 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.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga 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.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.saver.Image 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.TrackManager
import eu.kanade.tachiyomi.data.track.job.DelayedTrackingStore import eu.kanade.tachiyomi.data.track.job.DelayedTrackingStore
import eu.kanade.tachiyomi.data.track.job.DelayedTrackingUpdateJob 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.SourceManager
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter 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.OrientationType
import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
import eu.kanade.tachiyomi.util.chapter.getChapterSort 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.byteSize
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
@ -41,7 +43,6 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.storage.cacheImageDir import eu.kanade.tachiyomi.util.storage.cacheImageDir
import eu.kanade.tachiyomi.util.system.isOnline import eu.kanade.tachiyomi.util.system.isOnline
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import eu.kanade.tachiyomi.util.updateCoverLastModified
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import logcat.LogPriority import logcat.LogPriority
@ -692,36 +693,28 @@ class ReaderPresenter(
/** /**
* Sets the image of this [page] as cover and notifies the UI of the result. * 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 if (page.status != Page.READY) return
val manga = manga ?: return val manga = manga?.toDomainManga() ?: return
val stream = page.stream ?: return val stream = page.stream ?: return
Observable presenterScope.launchIO {
.fromCallable { val result = try {
stream().use { manga.editCover(context, stream())
if (manga.isLocal()) { } catch (e: Exception) {
val context = Injekt.get<Application>() false
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
}
}
}
} }
.subscribeOn(Schedulers.io()) launchUI {
.observeOn(AndroidSchedulers.mainThread()) val resultResult = if (!result) {
.subscribeFirst( SetAsCoverResult.Error
{ view, result -> view.onSetAsCoverResult(result) }, } else if (manga.isLocal() || manga.favorite) {
{ view, _ -> view.onSetAsCoverResult(SetAsCoverResult.Error) }, SetAsCoverResult.Success
) } else {
SetAsCoverResult.AddToLibraryFirst
}
view?.onSetAsCoverResult(resultResult)
}
}
} }
/** /**

View File

@ -1,5 +1,9 @@
package eu.kanade.tachiyomi.util 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.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga 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 eu.kanade.tachiyomi.source.model.SManga
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.io.InputStream
import java.util.Date import java.util.Date
import eu.kanade.domain.manga.model.Manga as DomainManga
fun Manga.isLocal() = source == LocalSource.ID fun Manga.isLocal() = source == LocalSource.ID
@ -82,3 +88,21 @@ fun Manga.shouldDownloadNewChapters(db: DatabaseHelper, prefs: PreferencesHelper
// In included category // In included category
return categoriesForManga.any { it in includedCategories } 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
}
}