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 { init {
preferences.downloadsDirectory().asObservable().skip(1).subscribe { 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. * @param manga the manga to check.
*/ */
fun getDownloadCount(manga: Manga): Int { fun getDownloadCount(manga: Manga, forceCheckFolder: Boolean = false): Int {
checkRenew() checkRenew()
val files = mangaFiles[manga.id] ?: return 0 if (forceCheckFolder) {
return files.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }.size
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 }) sourceMangasRaw.filterNot { it.favorite })
val sourceDir = sourceValue.value val sourceDir = sourceValue.value
val mangaDirs = sourceDir.dir.listFiles().orEmpty().mapNotNull { val mangaDirs = sourceDir.dir.listFiles().orEmpty().mapNotNull {
val name = it.name ?: return@mapNotNull null val name = it.name ?: return@mapNotNull null
name to MangaDirectory(it) name to MangaDirectory(it)
}.toMap() }.toMap()
mangaDirs.values.forEach { mangaDir -> mangaDirs.values.forEach { mangaDir ->
val chapterDirs = val chapterDirs =
@ -200,18 +213,18 @@ class DownloadCache(
} }
} }
/*fun renameFolder(from: String, to: String, source: Long) { /*fun renameFolder(from: String, to: String, source: Long) {
val sourceDir = rootDir.files[source] ?: return val sourceDir = rootDir.files[source] ?: return
val list = sourceDir.files.toMutableMap() val list = sourceDir.files.toMutableMap()
val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return
val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil
.buildValidFilename(to))) ?: return .buildValidFilename(to))) ?: return
val newDir = MangaDirectory(newFile) val newDir = MangaDirectory(newFile)
newDir.files = mangaFiles.files newDir.files = mangaFiles.files
list.remove(DiskUtil.buildValidFilename(from)) list.remove(DiskUtil.buildValidFilename(from))
list[to] = newDir list[to] = newDir
sourceDir.files = list sourceDir.files = list
}*/ }*/
/** /**
* Removes a manga that has been deleted from this cache. * 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>> { private fun buildPageList(chapterDir: UniFile?): Observable<List<Page>> {
return Observable.fromCallable { return Observable.fromCallable {
val files = chapterDir?.listFiles().orEmpty() val files = chapterDir?.listFiles().orEmpty()
.filter { "image" in it.type.orEmpty() } .filter { "image" in it.type.orEmpty() }
if (files.isEmpty()) { if (files.isEmpty()) {
throw Exception("Page list is empty") throw Exception("Page list is empty")
} }
files.sortedBy { it.name } files.sortedBy { it.name }
.mapIndexed { i, file -> .mapIndexed { i, file ->
Page(i, uri = file.uri).apply { status = Page.READY } 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) val chapterDirs = provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(chapters, manga, source)
chapterDirs.forEach { it.delete() } chapterDirs.forEach { it.delete() }
cache.removeChapters(chapters, manga) 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() chapterDirs.firstOrNull()?.parentFile?.delete()
} }
} }