Added delegate callback for removing downloads

Should close #551 and other related A11 issues
This commit is contained in:
Jay 2020-08-09 17:22:47 -04:00
parent ad57086d8c
commit e3e18ea775
6 changed files with 66 additions and 29 deletions

View File

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

View File

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

View File

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

View File

@ -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.
*/
@ -266,12 +275,8 @@ class MangaDetailsPresenter(
*/
fun deleteChapter(chapter: ChapterItem) {
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 {
status = Download.NOT_DOWNLOADED
status = Download.QUEUE
download = null
}
@ -284,10 +289,9 @@ class MangaDetailsPresenter(
*/
fun deleteChapters(chapters: List<ChapterItem>, update: Boolean = true) {
downloadManager.deleteChapters(chapters, manga, source)
chapters.forEach { chapter ->
this.chapters.find { it.id == chapter.id }?.apply {
status = Download.NOT_DOWNLOADED
status = Download.QUEUE
download = null
}
}

View File

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

View File

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