Added delegate callback for removing downloads

Should close #551 and other related A11 issues
pull/3963/head
Jay 4 years ago
parent ad57086d8c
commit e3e18ea775

@ -10,6 +10,9 @@ import eu.kanade.tachiyomi.data.download.model.DownloadQueue
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import rx.Observable import rx.Observable
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -228,28 +231,34 @@ class DownloadManager(val context: Context) {
* @param source the source of the chapters. * @param source the source of the chapters.
*/ */
fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) { fun deleteChapters(chapters: List<Chapter>, manga: Manga, source: Source) {
val wasPaused = isPaused() GlobalScope.launch(Dispatchers.IO) {
if (chapters.isEmpty()) { val wasPaused = isPaused()
DownloadService.stop(context) if (chapters.isEmpty()) {
downloader.queue.clear() DownloadService.stop(context)
return downloader.queue.clear()
} return@launch
downloader.pause() }
downloader.queue.remove(chapters) downloader.pause()
if (!wasPaused && downloader.queue.isNotEmpty()) { downloader.queue.remove(chapters)
downloader.start() if (!wasPaused && downloader.queue.isNotEmpty()) {
} else if (downloader.queue.isEmpty() && DownloadService.isRunning(context)) { downloader.start()
DownloadService.stop(context) } else if (downloader.queue.isEmpty() && DownloadService.isRunning(context)) {
} else if (downloader.queue.isEmpty()) { DownloadService.stop(context)
DownloadService.callListeners(false) } else if (downloader.queue.isEmpty()) {
downloader.stop() DownloadService.callListeners(false)
} downloader.stop()
queue.remove(chapters) }
val chapterDirs = provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(chapters, manga, source) queue.remove(chapters)
chapterDirs.forEach { it.delete() } val chapterDirs =
cache.removeChapters(chapters, manga) provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(
if (cache.getDownloadCount(manga, true) == 0) { // Delete manga directory if empty chapters, manga, source
chapterDirs.firstOrNull()?.parentFile?.delete() )
chapterDirs.forEach { it.delete() }
cache.removeChapters(chapters, manga)
if (cache.getDownloadCount(manga, true) == 0) { // Delete manga directory if empty
chapterDirs.firstOrNull()?.parentFile?.delete()
}
queue.updateListeners()
} }
} }

@ -57,6 +57,7 @@ class DownloadService : Service() {
it.downloadStatusChanged(downloading ?: downloadManager.hasQueue()) it.downloadStatusChanged(downloading ?: downloadManager.hasQueue())
} }
} }
/** /**
* Starts this service. * Starts this service.
* *

@ -45,6 +45,10 @@ List<Download> by queue {
} }
} }
fun updateListeners() {
downloadListeners.forEach { it.updateDownloads() }
}
fun remove(chapter: Chapter) { fun remove(chapter: Chapter) {
find { it.chapter.id == chapter.id }?.let { remove(it) } find { it.chapter.id == chapter.id }?.let { remove(it) }
} }
@ -138,5 +142,6 @@ List<Download> by queue {
interface DownloadListener { interface DownloadListener {
fun updateDownload(download: Download) fun updateDownload(download: Download)
fun updateDownloads()
} }
} }

@ -165,6 +165,15 @@ class MangaDetailsPresenter(
} }
} }
override fun updateDownloads() {
scope.launch(Dispatchers.Default) {
updateChapters(chapters)
withContext(Dispatchers.Main) {
controller.updateChapters(chapters)
}
}
}
/** /**
* Converts a chapter from the database to an extended model, allowing to store new fields. * Converts a chapter from the database to an extended model, allowing to store new fields.
*/ */
@ -266,12 +275,8 @@ class MangaDetailsPresenter(
*/ */
fun deleteChapter(chapter: ChapterItem) { fun deleteChapter(chapter: ChapterItem) {
downloadManager.deleteChapters(listOf(chapter), manga, source) downloadManager.deleteChapters(listOf(chapter), manga, source)
val downloads = downloadManager.queue.toMutableList()
downloads.remove(chapter.download)
downloadManager.reorderQueue(downloads)
this.chapters.find { it.id == chapter.id }?.apply { this.chapters.find { it.id == chapter.id }?.apply {
status = Download.NOT_DOWNLOADED status = Download.QUEUE
download = null download = null
} }
@ -284,10 +289,9 @@ class MangaDetailsPresenter(
*/ */
fun deleteChapters(chapters: List<ChapterItem>, update: Boolean = true) { fun deleteChapters(chapters: List<ChapterItem>, update: Boolean = true) {
downloadManager.deleteChapters(chapters, manga, source) downloadManager.deleteChapters(chapters, manga, source)
chapters.forEach { chapter -> chapters.forEach { chapter ->
this.chapters.find { it.id == chapter.id }?.apply { this.chapters.find { it.id == chapter.id }?.apply {
status = Download.NOT_DOWNLOADED status = Download.QUEUE
download = null download = null
} }
} }

@ -82,6 +82,15 @@ class RecentChaptersPresenter(
} }
} }
override fun updateDownloads() {
scope.launch {
setDownloadedChapters(chapters)
withContext(Dispatchers.Main) {
controller.onNextRecentChapters(chapters)
}
}
}
override fun onUpdateManga(manga: LibraryManga) { override fun onUpdateManga(manga: LibraryManga) {
getUpdates() getUpdates()
} }

@ -241,6 +241,15 @@ class RecentsPresenter(
} }
} }
override fun updateDownloads() {
scope.launch {
setDownloadedChapters(recentItems)
withContext(Dispatchers.Main) {
controller.showLists(recentItems)
}
}
}
override fun onUpdateManga(manga: LibraryManga) { override fun onUpdateManga(manga: LibraryManga) {
if (manga.id == null && !LibraryUpdateService.isRunning()) { if (manga.id == null && !LibraryUpdateService.isRunning()) {
scope.launch(Dispatchers.Main) { controller.setRefreshing(false) } scope.launch(Dispatchers.Main) { controller.setRefreshing(false) }

Loading…
Cancel
Save