From c88b79fa178adb49814bc118a1657fa93665a5fd Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 5 Dec 2022 14:14:50 -0500 Subject: [PATCH] Minor cleanup --- .../tachiyomi/data/download/DownloadStore.kt | 35 +++++++++---------- .../tachiyomi/data/download/Downloader.kt | 9 ++--- .../tachiyomi/data/download/model/Download.kt | 12 ++----- .../data/download/model/DownloadQueue.kt | 34 +++++++++--------- .../data/library/LibraryUpdateNotifier.kt | 2 +- .../kanade/tachiyomi/ui/main/MainActivity.kt | 16 ++++----- 6 files changed, 45 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt index 4d8bf57ca8..dd3e87476c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt @@ -13,16 +13,18 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import uy.kohesive.injekt.injectLazy +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get /** * This class is used to persist active downloads across application restarts. - * - * @param context the application context. */ class DownloadStore( context: Context, - private val sourceManager: SourceManager, + private val sourceManager: SourceManager = Injekt.get(), + private val json: Json = Injekt.get(), + private val getManga: GetManga = Injekt.get(), + private val getChapter: GetChapter = Injekt.get(), ) { /** @@ -30,11 +32,6 @@ class DownloadStore( */ private val preferences = context.getSharedPreferences("active_downloads", Context.MODE_PRIVATE) - private val json: Json by injectLazy() - - private val getManga: GetManga by injectLazy() - private val getChapter: GetChapter by injectLazy() - /** * Counter used to keep the queue order. */ @@ -129,14 +126,14 @@ class DownloadStore( null } } - - /** - * Class used for download serialization - * - * @param mangaId the id of the manga. - * @param chapterId the id of the chapter. - * @param order the order of the download in the queue. - */ - @Serializable - data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int) } + +/** + * Class used for download serialization + * + * @param mangaId the id of the manga. + * @param chapterId the id of the chapter. + * @param order the order of the download in the queue. + */ +@Serializable +private data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int) 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 3e591379f9..8ace3d89e1 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 @@ -40,7 +40,6 @@ import rx.schedulers.Schedulers import rx.subscriptions.CompositeSubscription import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import uy.kohesive.injekt.injectLazy import java.io.BufferedOutputStream import java.io.File import java.util.zip.CRC32 @@ -68,17 +67,13 @@ class Downloader( private val sourceManager: SourceManager = Injekt.get(), private val chapterCache: ChapterCache = Injekt.get(), private val downloadPreferences: DownloadPreferences = Injekt.get(), + private val xml: XML = Injekt.get(), ) { - /** - * xml format used for ComicInfo files - */ - private val xml: XML by injectLazy() - /** * Store for persisting downloads across restarts. */ - private val store = DownloadStore(context, sourceManager) + private val store = DownloadStore(context) /** * Queue where active downloads are kept. diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt index 7dff294b96..5c7263605b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/Download.kt @@ -36,10 +36,10 @@ data class Download( } @Transient - private var statusSubject: PublishSubject? = null + var statusSubject: PublishSubject? = null @Transient - private var statusCallback: ((Download) -> Unit)? = null + var statusCallback: ((Download) -> Unit)? = null val progress: Int get() { @@ -47,14 +47,6 @@ data class Download( return pages.map(Page::progress).average().toInt() } - fun setStatusSubject(subject: PublishSubject?) { - statusSubject = subject - } - - fun setStatusCallback(f: ((Download) -> Unit)?) { - statusCallback = f - } - enum class State(val value: Int) { NOT_DOWNLOADED(0), QUEUE(1), diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt index 2701c2c5dd..07c81075fa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/model/DownloadQueue.kt @@ -31,8 +31,8 @@ class DownloadQueue( fun addAll(downloads: List) { downloads.forEach { download -> - download.setStatusSubject(statusSubject) - download.setStatusCallback(::setPagesFor) + download.statusSubject = statusSubject + download.statusCallback = ::setPagesFor download.status = Download.State.QUEUE } queue.addAll(downloads) @@ -45,8 +45,8 @@ class DownloadQueue( fun remove(download: Download) { val removed = queue.remove(download) store.remove(download) - download.setStatusSubject(null) - download.setStatusCallback(null) + download.statusSubject = null + download.statusCallback = null if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { download.status = Download.State.NOT_DOWNLOADED } @@ -62,9 +62,7 @@ class DownloadQueue( } fun remove(chapters: List) { - for (chapter in chapters) { - remove(chapter) - } + chapters.forEach(::remove) } fun remove(manga: Manga) { @@ -73,8 +71,8 @@ class DownloadQueue( fun clear() { queue.forEach { download -> - download.setStatusSubject(null) - download.setStatusCallback(null) + download.statusSubject = null + download.statusCallback = null if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { download.status = Download.State.NOT_DOWNLOADED } @@ -86,6 +84,10 @@ class DownloadQueue( } } + fun statusFlow(): Flow = getStatusObservable().asFlow() + + fun progressFlow(): Flow = getProgressObservable().asFlow() + private fun getActiveDownloads(): Observable = Observable.from(this).filter { download -> download.status == Download.State.DOWNLOADING } @@ -93,14 +95,6 @@ class DownloadQueue( .startWith(getActiveDownloads()) .onBackpressureBuffer() - fun statusFlow(): Flow = getStatusObservable().asFlow() - - private fun setPagesFor(download: Download) { - if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) { - setPagesSubject(download.pages, null) - } - } - private fun getProgressObservable(): Observable { return statusSubject.onBackpressureBuffer() .startWith(getActiveDownloads()) @@ -120,7 +114,11 @@ class DownloadQueue( .filter { it.status == Download.State.DOWNLOADING } } - fun progressFlow(): Flow = getProgressObservable().asFlow() + private fun setPagesFor(download: Download) { + if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) { + setPagesSubject(download.pages, null) + } + } private fun setPagesSubject(pages: List?, subject: PublishSubject?) { pages?.forEach { it.setStatusSubject(subject) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt index 19ecfb287a..e359be4659 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateNotifier.kt @@ -333,7 +333,7 @@ class LibraryUpdateNotifier(private val context: Context) { private fun getNotificationIntent(): PendingIntent { val intent = Intent(context, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP - action = MainActivity.SHORTCUT_RECENTLY_UPDATED + action = MainActivity.SHORTCUT_UPDATES } return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index d5e27742a9..e47d6544db 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -342,15 +342,15 @@ class MainActivity : BaseActivity() { when (intent.action) { SHORTCUT_LIBRARY -> HomeScreen.openTab(HomeScreen.Tab.Library()) - SHORTCUT_RECENTLY_UPDATED -> HomeScreen.openTab(HomeScreen.Tab.Updates) - SHORTCUT_RECENTLY_READ -> HomeScreen.openTab(HomeScreen.Tab.History) - SHORTCUT_CATALOGUES -> HomeScreen.openTab(HomeScreen.Tab.Browse(false)) - SHORTCUT_EXTENSIONS -> HomeScreen.openTab(HomeScreen.Tab.Browse(true)) SHORTCUT_MANGA -> { val idToOpen = intent.extras?.getLong(Constants.MANGA_EXTRA) ?: return false navigator.popUntilRoot() HomeScreen.openTab(HomeScreen.Tab.Library(idToOpen)) } + SHORTCUT_UPDATES -> HomeScreen.openTab(HomeScreen.Tab.Updates) + SHORTCUT_HISTORY -> HomeScreen.openTab(HomeScreen.Tab.History) + SHORTCUT_SOURCES -> HomeScreen.openTab(HomeScreen.Tab.Browse(false)) + SHORTCUT_EXTENSIONS -> HomeScreen.openTab(HomeScreen.Tab.Browse(true)) SHORTCUT_DOWNLOADS -> { navigator.popUntilRoot() HomeScreen.openTab(HomeScreen.Tab.More(toDownloads = true)) @@ -413,12 +413,12 @@ class MainActivity : BaseActivity() { // Shortcut actions const val SHORTCUT_LIBRARY = "eu.kanade.tachiyomi.SHOW_LIBRARY" - const val SHORTCUT_RECENTLY_UPDATED = "eu.kanade.tachiyomi.SHOW_RECENTLY_UPDATED" - const val SHORTCUT_RECENTLY_READ = "eu.kanade.tachiyomi.SHOW_RECENTLY_READ" - const val SHORTCUT_CATALOGUES = "eu.kanade.tachiyomi.SHOW_CATALOGUES" - const val SHORTCUT_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS" const val SHORTCUT_MANGA = "eu.kanade.tachiyomi.SHOW_MANGA" + const val SHORTCUT_UPDATES = "eu.kanade.tachiyomi.SHOW_RECENTLY_UPDATED" + const val SHORTCUT_HISTORY = "eu.kanade.tachiyomi.SHOW_RECENTLY_READ" + const val SHORTCUT_SOURCES = "eu.kanade.tachiyomi.SHOW_CATALOGUES" const val SHORTCUT_EXTENSIONS = "eu.kanade.tachiyomi.EXTENSIONS" + const val SHORTCUT_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS" const val INTENT_SEARCH = "eu.kanade.tachiyomi.SEARCH" const val INTENT_SEARCH_QUERY = "query"