DownloadProvider: Only provide necessary info and not whole chapter/manga class (#7411)

(cherry picked from commit e1525a5125f83fb419d5e5f05834cffcbcd7fa07)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt
This commit is contained in:
AntsyLich 2022-06-30 19:20:55 +06:00 committed by Jobobby04
parent 299ac1ca2d
commit 8bf3426afd
11 changed files with 96 additions and 72 deletions

View File

@ -97,7 +97,7 @@ class SyncChaptersWithSource(
toAdd.add(toAddChapter)
} else {
if (shouldUpdateDbChapter.await(dbChapter, chapter)) {
if (dbChapter.name != chapter.name && downloadManager.isChapterDownloaded(dbChapter.toDbChapter(), manga.toDbManga())) {
if (dbChapter.name != chapter.name && downloadManager.isChapterDownloaded(dbChapter.name, dbChapter.scanlator, /* SY --> */ manga.ogTitle /* SY <-- */, manga.source)) {
downloadManager.renameChapter(source, manga.toDbManga(), dbChapter.toDbChapter(), chapter.toDbChapter())
}
var toChangeChapter = dbChapter.copy(

View File

@ -65,23 +65,31 @@ class DownloadCache(
/**
* Returns true if the chapter is downloaded.
*
* @param chapter the chapter to check.
* @param manga the manga of the chapter.
* @param chapterName the name of the chapter to query.
* @param chapterScanlator scanlator of the chapter to query
* @param mangaTitle the title of the manga to query.
* @param sourceId the id of the source of the chapter.
* @param skipCache whether to skip the directory cache and check in the filesystem.
*/
fun isChapterDownloaded(chapter: Chapter, manga: Manga, skipCache: Boolean): Boolean {
fun isChapterDownloaded(
chapterName: String,
chapterScanlator: String?,
mangaTitle: String,
sourceId: Long,
skipCache: Boolean,
): Boolean {
if (skipCache) {
val source = sourceManager.getOrStub(manga.source)
return provider.findChapterDir(chapter, manga, source) != null
val source = sourceManager.getOrStub(sourceId)
return provider.findChapterDir(chapterName, chapterScanlator, mangaTitle, source) != null
}
checkRenew()
val sourceDir = rootDir.files[manga.source]
val sourceDir = rootDir.files[sourceId]
if (sourceDir != null) {
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)]
val mangaDir = sourceDir.files[provider.getMangaDirName(mangaTitle)]
if (mangaDir != null) {
return provider.getValidChapterDirNames(chapter).any { it in mangaDir.files }
return provider.getValidChapterDirNames(chapterName, chapterScanlator).any { it in mangaDir.files }
}
}
return false
@ -97,7 +105,7 @@ class DownloadCache(
val sourceDir = rootDir.files[manga.source]
if (sourceDir != null) {
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)]
val mangaDir = sourceDir.files[provider.getMangaDirName(/* SY --> */ manga.originalTitle /* SY <-- */)]
if (mangaDir != null) {
return mangaDir.files
.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }
@ -176,7 +184,7 @@ class DownloadCache(
}
// Retrieve the cached manga directory or cache a new one
val mangaDirName = provider.getMangaDirName(manga)
val mangaDirName = provider.getMangaDirName(/* SY --> */ manga.originalTitle /* SY <-- */)
var mangaDir = sourceDir.files[mangaDirName]
if (mangaDir == null) {
mangaDir = MangaDirectory(mangaUniFile)
@ -196,8 +204,8 @@ class DownloadCache(
@Synchronized
fun removeChapter(chapter: Chapter, manga: Manga) {
val sourceDir = rootDir.files[manga.source] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
provider.getValidChapterDirNames(chapter).forEach {
val mangaDir = sourceDir.files[provider.getMangaDirName(/* SY --> */ manga.originalTitle /* SY <-- */)] ?: return
provider.getValidChapterDirNames(chapter.name, chapter.scanlator).forEach {
if (it in mangaDir.files) {
mangaDir.files -= it
}
@ -207,7 +215,7 @@ class DownloadCache(
// SY -->
fun removeFolders(folders: List<String>, manga: Manga) {
val sourceDir = rootDir.files[manga.source] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga.originalTitle)] ?: return
folders.forEach { chapter ->
if (chapter in mangaDir.files) {
mangaDir.files -= chapter
@ -226,9 +234,9 @@ class DownloadCache(
@Synchronized
fun removeChapters(chapters: List<Chapter>, manga: Manga) {
val sourceDir = rootDir.files[manga.source] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(/* SY --> */ manga.originalTitle /* SY <-- */)] ?: return
chapters.forEach { chapter ->
provider.getValidChapterDirNames(chapter).forEach {
provider.getValidChapterDirNames(chapter.name, chapter.scanlator).forEach {
if (it in mangaDir.files) {
mangaDir.files -= it
}
@ -244,7 +252,7 @@ class DownloadCache(
@Synchronized
fun removeManga(manga: Manga) {
val sourceDir = rootDir.files[manga.source] ?: return
val mangaDirName = provider.getMangaDirName(manga)
val mangaDirName = provider.getMangaDirName(/* SY --> */ manga.originalTitle /* SY <-- */)
if (mangaDirName in sourceDir.files) {
sourceDir.files -= mangaDirName
}

View File

@ -165,7 +165,7 @@ class DownloadManager(
* @return an observable containing the list of pages from the chapter.
*/
fun buildPageList(source: Source, manga: Manga, chapter: Chapter): Observable<List<Page>> {
return buildPageList(provider.findChapterDir(chapter, manga, source))
return buildPageList(provider.findChapterDir(chapter.name, chapter.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, source))
}
/**
@ -193,12 +193,20 @@ class DownloadManager(
/**
* Returns true if the chapter is downloaded.
*
* @param chapter the chapter to check.
* @param manga the manga of the chapter.
* @param chapterName the name of the chapter to query.
* @param chapterScanlator scanlator of the chapter to query
* @param mangaTitle the title of the manga to query.
* @param sourceId the id of the source of the chapter.
* @param skipCache whether to skip the directory cache and check in the filesystem.
*/
fun isChapterDownloaded(chapter: Chapter, manga: Manga, skipCache: Boolean = false): Boolean {
return cache.isChapterDownloaded(chapter, manga, skipCache)
fun isChapterDownloaded(
chapterName: String,
chapterScanlator: String?,
mangaTitle: String,
sourceId: Long,
skipCache: Boolean = false,
): Boolean {
return cache.isChapterDownloaded(chapterName, chapterScanlator, mangaTitle, sourceId, skipCache)
}
/**
@ -304,7 +312,7 @@ class DownloadManager(
var cleaned = 0
if (removeNonFavorite && !manga.favorite) {
val mangaFolder = provider.getMangaDir(manga, source)
val mangaFolder = provider.getMangaDir(manga.originalTitle, source)
cleaned += 1 + mangaFolder.listFiles().orEmpty().size
mangaFolder.delete()
cache.removeManga(manga)
@ -325,12 +333,12 @@ class DownloadManager(
}
if (cache.getDownloadCount(manga) == 0) {
val mangaFolder = provider.getMangaDir(manga, source)
val mangaFolder = provider.getMangaDir(manga.originalTitle, source)
if (!mangaFolder.listFiles().isNullOrEmpty()) {
mangaFolder.delete()
cache.removeManga(manga)
} else {
xLogE("Cache and download folder doesn't match for " + manga.title)
xLogE("Cache and download folder doesn't match for " + manga.originalTitle)
}
}
return cleaned
@ -346,7 +354,7 @@ class DownloadManager(
fun deleteManga(manga: Manga, source: Source) {
launchIO {
downloader.queue.remove(manga)
provider.findMangaDir(manga, source)?.delete()
provider.findMangaDir(/* SY --> */ manga.originalTitle /* SY <-- */, source)?.delete()
cache.removeManga(manga)
}
}
@ -381,15 +389,15 @@ class DownloadManager(
* @param newChapter the target chapter with the new name.
*/
fun renameChapter(source: Source, manga: Manga, oldChapter: Chapter, newChapter: Chapter) {
val oldNames = provider.getValidChapterDirNames(oldChapter)
val mangaDir = provider.getMangaDir(manga, source)
val oldNames = provider.getValidChapterDirNames(oldChapter.name, oldChapter.scanlator)
val mangaDir = provider.getMangaDir(/* SY --> */ manga.originalTitle /* SY <-- */, source)
// Assume there's only 1 version of the chapter name formats present
val oldDownload = oldNames.asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull() ?: return
var newName = provider.getChapterDirName(newChapter)
var newName = provider.getChapterDirName(newChapter.name, newChapter.scanlator)
if (oldDownload.isFile && oldDownload.name?.endsWith(".cbz") == true) {
newName += ".cbz"
}

View File

@ -46,14 +46,14 @@ class DownloadProvider(private val context: Context) {
/**
* Returns the download directory for a manga. For internal use only.
*
* @param manga the manga to query.
* @param mangaTitle the title of the manga to query.
* @param source the source of the manga.
*/
internal fun getMangaDir(manga: Manga, source: Source): UniFile {
internal fun getMangaDir(mangaTitle: String, source: Source): UniFile {
try {
return downloadsDir
.createDirectory(getSourceDirName(source))
.createDirectory(getMangaDirName(manga))
.createDirectory(getMangaDirName(mangaTitle))
} catch (e: Throwable) {
logcat(LogPriority.ERROR, e) { "Invalid download directory" }
throw Exception(context.getString(R.string.invalid_download_dir))
@ -72,24 +72,25 @@ class DownloadProvider(private val context: Context) {
/**
* Returns the download directory for a manga if it exists.
*
* @param manga the manga to query.
* @param mangaTitle the title of the manga to query.
* @param source the source of the manga.
*/
fun findMangaDir(manga: Manga, source: Source): UniFile? {
fun findMangaDir(mangaTitle: String, source: Source): UniFile? {
val sourceDir = findSourceDir(source)
return sourceDir?.findFile(getMangaDirName(manga), true)
return sourceDir?.findFile(getMangaDirName(mangaTitle), true)
}
/**
* Returns the download directory for a chapter if it exists.
*
* @param chapter the chapter to query.
* @param manga the manga of the chapter.
* @param chapterName the name of the chapter to query.
* @param chapterScanlator scanlator of the chapter to query
* @param mangaTitle the title of the manga to query.
* @param source the source of the chapter.
*/
fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? {
val mangaDir = findMangaDir(manga, source)
return getValidChapterDirNames(chapter).asSequence()
fun findChapterDir(chapterName: String, chapterScanlator: String?, mangaTitle: String, source: Source): UniFile? {
val mangaDir = findMangaDir(mangaTitle, source)
return getValidChapterDirNames(chapterName, chapterScanlator).asSequence()
.mapNotNull { mangaDir?.findFile(it, true) }
.firstOrNull()
}
@ -102,9 +103,9 @@ class DownloadProvider(private val context: Context) {
* @param source the source of the chapter.
*/
fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): List<UniFile> {
val mangaDir = findMangaDir(manga, source) ?: return emptyList()
val mangaDir = findMangaDir(/* SY --> */ manga.originalTitle /* SY <-- */, source) ?: return emptyList()
return chapters.mapNotNull { chapter ->
getValidChapterDirNames(chapter).asSequence()
getValidChapterDirNames(chapter.name, chapter.scanlator).asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull()
}
@ -123,10 +124,10 @@ class DownloadProvider(private val context: Context) {
manga: Manga,
source: Source,
): List<UniFile> {
val mangaDir = findMangaDir(manga, source) ?: return emptyList()
val mangaDir = findMangaDir(manga.originalTitle, source) ?: return emptyList()
return mangaDir.listFiles().orEmpty().asList().filter {
chapters.find { chp ->
getValidChapterDirNames(chp).any { dir ->
getValidChapterDirNames(chp.name, chp.scanlator).any { dir ->
mangaDir.findFile(dir) != null
}
} == null || it.name?.endsWith(Downloader.TMP_DIR_SUFFIX) == true
@ -146,24 +147,23 @@ class DownloadProvider(private val context: Context) {
/**
* Returns the download directory name for a manga.
*
* @param manga the manga to query.
* @param mangaTitle the title of the manga to query.
*/
fun getMangaDirName(manga: Manga): String {
// SY -->
return DiskUtil.buildValidFilename(manga.originalTitle)
// SY <--
fun getMangaDirName(mangaTitle: String): String {
return DiskUtil.buildValidFilename(mangaTitle)
}
/**
* Returns the chapter directory name for a chapter.
*
* @param chapter the chapter to query.
* @param chapterName the name of the chapter to query.
* @param chapterScanlator scanlator of the chapter to query
*/
fun getChapterDirName(chapter: Chapter): String {
fun getChapterDirName(chapterName: String, chapterScanlator: String?): String {
return DiskUtil.buildValidFilename(
when {
chapter.scanlator != null -> "${chapter.scanlator}_${chapter.name}"
else -> chapter.name
chapterScanlator != null -> "${chapterScanlator}_$chapterName"
else -> chapterName
},
)
}
@ -171,19 +171,20 @@ class DownloadProvider(private val context: Context) {
/**
* Returns valid downloaded chapter directory names.
*
* @param chapter the chapter to query.
* @param chapterName the name of the chapter to query.
* @param chapterScanlator scanlator of the chapter to query
*/
fun getValidChapterDirNames(chapter: Chapter): List<String> {
val chapterName = getChapterDirName(chapter)
fun getValidChapterDirNames(chapterName: String, chapterScanlator: String?): List<String> {
val chapterDirName = getChapterDirName(chapterName, chapterScanlator)
return listOf(
// Folder of images
chapterName,
chapterDirName,
// Archived chapters
"$chapterName.cbz",
"$chapterDirName.cbz",
// Legacy chapter directory name used in v0.9.2 and before
DiskUtil.buildValidFilename(chapter.name),
DiskUtil.buildValidFilename(chapterName),
)
}
}

View File

@ -254,7 +254,7 @@ class Downloader(
val chaptersWithoutDir = async {
chapters
// Filter out those already downloaded.
.filter { provider.findChapterDir(it, manga, source) == null }
.filter { provider.findChapterDir(it.name, it.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, source) == null }
// Add chapters to queue from the start.
.sortedByDescending { it.source_order }
}
@ -305,7 +305,7 @@ class Downloader(
* @param download the chapter to be downloaded.
*/
private fun downloadChapter(download: Download): Observable<Download> = Observable.defer {
val mangaDir = provider.getMangaDir(download.manga, download.source)
val mangaDir = provider.getMangaDir(/* SY --> */ download.manga.originalTitle /* SY <-- */, download.source)
val availSpace = DiskUtil.getAvailableStorageSpace(mangaDir)
if (availSpace != -1L && availSpace < MIN_DISK_SPACE) {
@ -314,7 +314,7 @@ class Downloader(
return@defer Observable.just(download)
}
val chapterDirname = provider.getChapterDirName(download.chapter)
val chapterDirname = provider.getChapterDirName(download.chapter.name, download.chapter.scanlator)
val tmpDir = mangaDir.createDirectory(chapterDirname + TMP_DIR_SUFFIX)
val pageListObservable = if (download.pages == null) {

View File

@ -474,6 +474,7 @@ class LibrarySettingsSheet(
item.checked = !item.checked
when (item) {
startReadingButton -> preferences.startReadingButton().set((item.checked))
else -> Unit
}
adapter.notifyItemChanged(item)
}

View File

@ -783,11 +783,14 @@ class MangaPresenter(
private fun List<DomainChapter>.toChapterItems(manga: DomainManga, mergedData: MergedMangaData?): List<ChapterItem> {
return map { chapter ->
val activeDownload = downloadManager.queue.find { chapter.id == it.chapter.id }
val chapter = chapter.let { if (mergedData != null) it.toMergedDownloadedChapter() else it }
val manga = mergedData?.manga?.get(chapter.mangaId) ?: manga
val downloaded = downloadManager.isChapterDownloaded(
// SY -->
chapter.let { if (mergedData != null) it.toMergedDownloadedChapter() else it }
.toDbChapter(),
(mergedData?.manga?.get(chapter.mangaId) ?: manga).toDbManga(),
chapter.name,
chapter.scanlator,
manga.ogTitle,
manga.source,
// SY <--
)
val downloadState = when {

View File

@ -163,8 +163,8 @@ class ReaderPresenter(
preferences.skipFiltered() -> {
(manga.readFilter == Manga.CHAPTER_SHOW_READ && !it.read) ||
(manga.readFilter == Manga.CHAPTER_SHOW_UNREAD && it.read) ||
(manga.downloadedFilter == Manga.CHAPTER_SHOW_DOWNLOADED && !downloadManager.isChapterDownloaded(it, manga)) ||
(manga.downloadedFilter == Manga.CHAPTER_SHOW_NOT_DOWNLOADED && downloadManager.isChapterDownloaded(it, manga)) ||
(manga.downloadedFilter == Manga.CHAPTER_SHOW_DOWNLOADED && !downloadManager.isChapterDownloaded(it.name, it.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, manga.source)) ||
(manga.downloadedFilter == Manga.CHAPTER_SHOW_NOT_DOWNLOADED && downloadManager.isChapterDownloaded(it.name, it.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, manga.source)) ||
(manga.bookmarkedFilter == Manga.CHAPTER_SHOW_BOOKMARKED && !it.bookmark) ||
// SY -->
(filteredScanlators != null && MdUtil.getScanlators(it.scanlator).none { group -> filteredScanlators.contains(group) })
@ -440,7 +440,8 @@ class ReaderPresenter(
private fun preload(chapter: ReaderChapter) {
if (chapter.pageLoader is HttpPageLoader) {
val manga = manga ?: return
val isDownloaded = downloadManager.isChapterDownloaded(chapter.chapter, manga)
val dbChapter = chapter.chapter
val isDownloaded = downloadManager.isChapterDownloaded(dbChapter.name, dbChapter.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, manga.source)
if (isDownloaded) {
chapter.state = ReaderChapter.State.Wait
}

View File

@ -87,14 +87,15 @@ class ChapterLoader(
* Returns the page loader to use for this [chapter].
*/
private fun getPageLoader(chapter: ReaderChapter): PageLoader {
val isDownloaded = downloadManager.isChapterDownloaded(chapter.chapter, manga, true)
val dbChapter = chapter.chapter
val isDownloaded = downloadManager.isChapterDownloaded(dbChapter.name, dbChapter.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, manga.source, skipCache = true)
return when {
// SY -->
source is MergedSource -> {
val mangaReference = mergedReferences.firstOrNull { it.mangaId == chapter.chapter.manga_id } ?: error("Merge reference null")
val source = sourceManager.get(mangaReference.mangaSourceId) ?: error("Source ${mangaReference.mangaSourceId} was null")
val manga = mergedManga[chapter.chapter.manga_id] ?: error("Manga for merged chapter was null")
val isMergedMangaDownloaded = downloadManager.isChapterDownloaded(chapter.chapter, manga, true)
val isMergedMangaDownloaded = downloadManager.isChapterDownloaded(chapter.chapter.name, chapter.chapter.scanlator, manga.originalTitle, manga.source, true)
when {
isMergedMangaDownloaded -> DownloadPageLoader(chapter, manga, source, downloadManager)
source is HttpSource -> HttpPageLoader(chapter, source)

View File

@ -30,7 +30,8 @@ class DownloadPageLoader(
* Returns an observable containing the pages found on this downloaded chapter.
*/
override fun getPages(): Observable<List<ReaderPage>> {
val chapterPath = downloadManager.provider.findChapterDir(chapter.chapter, manga, source)
val dbChapter = chapter.chapter
val chapterPath = downloadManager.provider.findChapterDir(dbChapter.name, dbChapter.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, source)
return if (chapterPath?.isFile == true) {
getPagesFromArchive(chapterPath)
} else {

View File

@ -121,7 +121,7 @@ class UpdatesPresenter : BasePresenter<UpdatesController>() {
val manga = item.manga
val chapter = item.chapter
if (downloadManager.isChapterDownloaded(chapter, manga)) {
if (downloadManager.isChapterDownloaded(chapter.name, chapter.scanlator, /* SY --> */ manga.originalTitle /* SY <-- */, manga.source)) {
item.status = Download.State.DOWNLOADED
}
}