switch to check folders for downloads whenever delete chapters is called so the manga folder doesnt get deleted by accident

This commit is contained in:
Carlos 2020-05-24 12:30:06 -04:00 committed by Jay
parent 4849cc6bd2
commit a5c9411f29
2 changed files with 38 additions and 25 deletions

View File

@ -48,8 +48,8 @@ class DownloadCache(
init {
preferences.downloadsDirectory().asObservable().skip(1).subscribe {
lastRenew = 0L // invalidate cache
}
lastRenew = 0L // invalidate cache
}
}
/**
@ -88,11 +88,24 @@ class DownloadCache(
*
* @param manga the manga to check.
*/
fun getDownloadCount(manga: Manga): Int {
fun getDownloadCount(manga: Manga, forceCheckFolder: Boolean = false): Int {
checkRenew()
val files = mangaFiles[manga.id] ?: return 0
return files.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }.size
if (forceCheckFolder) {
val mangaDir = provider.findMangaDir(manga, sourceManager.getMangadex())
if (mangaDir != null) {
val listFiles = mangaDir.listFiles { dir, filename -> !filename.endsWith(Downloader.TMP_DIR_SUFFIX) }
if (!listFiles.isNullOrEmpty()) {
return listFiles.size
}
}
return 0
} else {
val files = mangaFiles[manga.id] ?: return 0
return files.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }.size
}
}
/**
@ -126,9 +139,9 @@ class DownloadCache(
sourceMangasRaw.filterNot { it.favorite })
val sourceDir = sourceValue.value
val mangaDirs = sourceDir.dir.listFiles().orEmpty().mapNotNull {
val name = it.name ?: return@mapNotNull null
name to MangaDirectory(it)
}.toMap()
val name = it.name ?: return@mapNotNull null
name to MangaDirectory(it)
}.toMap()
mangaDirs.values.forEach { mangaDir ->
val chapterDirs =
@ -200,18 +213,18 @@ class DownloadCache(
}
}
/*fun renameFolder(from: String, to: String, source: Long) {
val sourceDir = rootDir.files[source] ?: return
val list = sourceDir.files.toMutableMap()
val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return
val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil
.buildValidFilename(to))) ?: return
val newDir = MangaDirectory(newFile)
newDir.files = mangaFiles.files
list.remove(DiskUtil.buildValidFilename(from))
list[to] = newDir
sourceDir.files = list
}*/
/*fun renameFolder(from: String, to: String, source: Long) {
val sourceDir = rootDir.files[source] ?: return
val list = sourceDir.files.toMutableMap()
val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return
val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil
.buildValidFilename(to))) ?: return
val newDir = MangaDirectory(newFile)
newDir.files = mangaFiles.files
list.remove(DiskUtil.buildValidFilename(from))
list[to] = newDir
sourceDir.files = list
}*/
/**
* Removes a manga that has been deleted from this cache.

View File

@ -169,16 +169,16 @@ class DownloadManager(val context: Context) {
private fun buildPageList(chapterDir: UniFile?): Observable<List<Page>> {
return Observable.fromCallable {
val files = chapterDir?.listFiles().orEmpty()
.filter { "image" in it.type.orEmpty() }
.filter { "image" in it.type.orEmpty() }
if (files.isEmpty()) {
throw Exception("Page list is empty")
}
files.sortedBy { it.name }
.mapIndexed { i, file ->
Page(i, uri = file.uri).apply { status = Page.READY }
}
.mapIndexed { i, file ->
Page(i, uri = file.uri).apply { status = Page.READY }
}
}
}
@ -247,7 +247,7 @@ class DownloadManager(val context: Context) {
val chapterDirs = provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(chapters, manga, source)
chapterDirs.forEach { it.delete() }
cache.removeChapters(chapters, manga)
if (cache.getDownloadCount(manga) == 0) { // Delete manga directory if empty
if (cache.getDownloadCount(manga, true) == 0) { // Delete manga directory if empty
chapterDirs.firstOrNull()?.parentFile?.delete()
}
}