From 05771ddf6dc7cf61540c51ba1c2e1d4008778f64 Mon Sep 17 00:00:00 2001 From: Riztard Lanthorn Date: Sat, 19 Jun 2021 23:36:29 +0700 Subject: [PATCH] add start download now (#5386) * add start download now download now for selected chapter from j2k Co-Authored-By: Jays2Kings * change string to action * move to bottom * oopsie Co-authored-by: Jays2Kings --- .../tachiyomi/data/download/DownloadManager.kt | 17 +++++++++++++++++ .../tachiyomi/data/download/DownloadService.kt | 11 +++++++++++ .../tachiyomi/data/download/Downloader.kt | 5 +++++ .../tachiyomi/ui/manga/MangaController.kt | 5 +++++ .../kanade/tachiyomi/ui/manga/MangaPresenter.kt | 4 ++++ .../ui/manga/chapter/base/BaseChapterHolder.kt | 9 ++++++++- .../manga/chapter/base/BaseChaptersAdapter.kt | 1 + .../ui/recent/updates/UpdatesController.kt | 5 +++++ .../ui/recent/updates/UpdatesPresenter.kt | 5 +++++ app/src/main/res/menu/chapter_download.xml | 4 ++++ app/src/main/res/values/strings.xml | 1 + 11 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 53a153d6e8..8e1f148eb1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -95,6 +95,23 @@ class DownloadManager(private val context: Context) { downloader.clearQueue(isNotification) } + fun startDownloadNow(chapter: Chapter) { + val download = downloader.queue.find { it.chapter.id == chapter.id } ?: return + val queue = downloader.queue.toMutableList() + queue.remove(download) + queue.add(0, download) + reorderQueue(queue) + if (isPaused()) { + if (DownloadService.isRunning(context)) { + downloader.start() + } else { + DownloadService.start(context) + } + } + } + + fun isPaused() = downloader.isPaused() + /** * Reorders the download queue. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index fa500d9c3e..46daf5ce1e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -19,6 +19,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.system.acquireWakeLock import eu.kanade.tachiyomi.util.system.connectivityManager +import eu.kanade.tachiyomi.util.system.isServiceRunning import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.toast import rx.android.schedulers.AndroidSchedulers @@ -58,6 +59,16 @@ class DownloadService : Service() { fun stop(context: Context) { context.stopService(Intent(context, DownloadService::class.java)) } + + /** + * Returns the status of the service. + * + * @param context the application context. + * @return true if the service is running, false otherwise. + */ + fun isRunning(context: Context): Boolean { + return context.isServiceRunning(DownloadService::class.java) + } } private val downloadManager: DownloadManager by injectLazy() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index e7b64f4c1d..1a3d49e2a4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -157,6 +157,11 @@ class Downloader( notifier.paused = true } + /** + * Check if downloader is paused + */ + fun isPaused() = !isRunning + /** * Removes everything from the queue. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 30252949fe..6c9399995b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -1057,6 +1057,11 @@ class MangaController : Timber.e(error) } + override fun startDownloadNow(position: Int) { + val chapter = chaptersAdapter?.getItem(position) ?: return + presenter.startDownloadingNow(chapter) + } + // OVERFLOW MENU DIALOGS private fun downloadChapters(choice: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index e8b6433948..a56d7c4bc1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -517,6 +517,10 @@ class MangaPresenter( } } + fun startDownloadingNow(chapter: Chapter) { + downloadManager.startDownloadNow(chapter) + } + /** * Mark the selected chapter list as read/unread. * @param selectedChapters the list of selected chapters. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt index 4506ab59d2..1241d02cad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChapterHolder.kt @@ -26,9 +26,16 @@ open class BaseChapterHolder( // Download.State.DOWNLOADING, Download.State.QUEUE findItem(R.id.cancel_download).isVisible = item.status != Download.State.DOWNLOADED + + // Download.State.QUEUE + findItem(R.id.start_download).isVisible = item.status == Download.State.QUEUE }, onMenuItemClick = { - adapter.clickListener.deleteChapter(position) + if (itemId == R.id.start_download) { + adapter.clickListener.startDownloadNow(position) + } else { + adapter.clickListener.deleteChapter(position) + } } ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChaptersAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChaptersAdapter.kt index 9beaef71f1..a4f7300ac9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChaptersAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/base/BaseChaptersAdapter.kt @@ -18,5 +18,6 @@ abstract class BaseChaptersAdapter>( interface OnChapterClickListener { fun downloadChapter(position: Int) fun deleteChapter(position: Int) + fun startDownloadNow(position: Int) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index 33077968ae..edf04ede65 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -318,6 +318,11 @@ class UpdatesController : adapter?.updateItem(item) } + override fun startDownloadNow(position: Int) { + val chapter = adapter?.getItem(position) as? UpdatesItem ?: return + presenter.startDownloadingNow(chapter) + } + /** * Called when ActionMode created. * @param mode the ActionMode object diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt index 2ba0c7dfaf..dd2b501f04 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.recent.updates import android.os.Bundle import eu.kanade.tachiyomi.data.database.DatabaseHelper +import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.MangaChapter import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download @@ -134,6 +135,10 @@ class UpdatesPresenter( } } + fun startDownloadingNow(chapter: Chapter) { + downloadManager.startDownloadNow(chapter) + } + /** * Mark selected chapter as read * diff --git a/app/src/main/res/menu/chapter_download.xml b/app/src/main/res/menu/chapter_download.xml index f1fd0573c3..b3a508416a 100644 --- a/app/src/main/res/menu/chapter_download.xml +++ b/app/src/main/res/menu/chapter_download.xml @@ -1,6 +1,10 @@ + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8487d812a4..149ca93f71 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -120,6 +120,7 @@ Back Forward Refresh + Start downloading now Loading…