From 84ae61f72c86a8d578b78b000765a31f38e1d6ab Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 27 Dec 2020 10:20:14 -0500 Subject: [PATCH] Refactor download states into enum --- .../tachiyomi/data/download/Downloader.kt | 32 +++++++++---------- .../tachiyomi/data/download/model/Download.kt | 14 ++++---- .../data/download/model/DownloadQueue.kt | 20 ++++++------ .../ui/download/DownloadController.kt | 6 ++-- .../tachiyomi/ui/manga/MangaController.kt | 2 +- .../tachiyomi/ui/manga/MangaPresenter.kt | 8 ++--- .../ui/manga/chapter/ChapterDownloadView.kt | 19 ++++------- .../ui/manga/chapter/ChapterHolder.kt | 13 +------- .../tachiyomi/ui/manga/chapter/ChapterItem.kt | 6 ++-- .../ui/recent/updates/UpdatesHolder.kt | 15 ++------- .../ui/recent/updates/UpdatesItem.kt | 6 ++-- .../ui/recent/updates/UpdatesPresenter.kt | 6 ++-- app/src/main/res/layout/chapters_item.xml | 2 ++ app/src/main/res/layout/updates_item.xml | 17 +++++----- 14 files changed, 69 insertions(+), 97 deletions(-) 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 d107f4cde..e28f1e922 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 @@ -114,8 +114,8 @@ class Downloader( initializeSubscriptions() } - val pending = queue.filter { it.status != Download.DOWNLOADED } - pending.forEach { if (it.status != Download.QUEUE) it.status = Download.QUEUE } + val pending = queue.filter { it.status != Download.State.DOWNLOADED } + pending.forEach { if (it.status != Download.State.QUEUE) it.status = Download.State.QUEUE } notifier.paused = false @@ -129,8 +129,8 @@ class Downloader( fun stop(reason: String? = null) { destroySubscriptions() queue - .filter { it.status == Download.DOWNLOADING } - .forEach { it.status = Download.ERROR } + .filter { it.status == Download.State.DOWNLOADING } + .forEach { it.status = Download.State.ERROR } if (reason != null) { notifier.onWarning(reason) @@ -151,8 +151,8 @@ class Downloader( fun pause() { destroySubscriptions() queue - .filter { it.status == Download.DOWNLOADING } - .forEach { it.status = Download.QUEUE } + .filter { it.status == Download.State.DOWNLOADING } + .forEach { it.status = Download.State.QUEUE } notifier.paused = true } @@ -167,8 +167,8 @@ class Downloader( // Needed to update the chapter view if (isNotification) { queue - .filter { it.status == Download.QUEUE } - .forEach { it.status = Download.NOT_DOWNLOADED } + .filter { it.status == Download.State.QUEUE } + .forEach { it.status = Download.State.NOT_DOWNLOADED } } queue.clear() notifier.dismissProgress() @@ -271,7 +271,7 @@ class Downloader( val availSpace = DiskUtil.getAvailableStorageSpace(mangaDir) if (availSpace != -1L && availSpace < MIN_DISK_SPACE) { - download.status = Download.ERROR + download.status = Download.State.ERROR notifier.onError(context.getString(R.string.download_insufficient_space), download.chapter.name) return@defer Observable.just(download) } @@ -301,7 +301,7 @@ class Downloader( ?.forEach { it.delete() } download.downloadedImages = 0 - download.status = Download.DOWNLOADING + download.status = Download.State.DOWNLOADING } // Get all the URLs to the source images, fetch pages if necessary .flatMap { download.source.fetchAllImageUrlsFromPageList(it) } @@ -317,7 +317,7 @@ class Downloader( .doOnNext { ensureSuccessfulDownload(download, mangaDir, tmpDir, chapterDirname) } // If the page list threw, it will resume here .onErrorReturn { error -> - download.status = Download.ERROR + download.status = Download.State.ERROR notifier.onError(error.message, download.chapter.name) download } @@ -457,13 +457,13 @@ class Downloader( val downloadedImages = tmpDir.listFiles().orEmpty().filterNot { it.name!!.endsWith(".tmp") } download.status = if (downloadedImages.size == download.pages!!.size) { - Download.DOWNLOADED + Download.State.DOWNLOADED } else { - Download.ERROR + Download.State.ERROR } // Only rename the directory if it's downloaded. - if (download.status == Download.DOWNLOADED) { + if (download.status == Download.State.DOWNLOADED) { tmpDir.renameTo(dirname) cache.addChapter(dirname, mangaDir, download.manga) @@ -476,7 +476,7 @@ class Downloader( */ private fun completeDownload(download: Download) { // Delete successful downloads from queue - if (download.status == Download.DOWNLOADED) { + if (download.status == Download.State.DOWNLOADED) { // remove downloaded chapter from queue queue.remove(download) } @@ -489,7 +489,7 @@ class Downloader( * Returns true if all the queued downloads are in DOWNLOADED or ERROR state. */ private fun areAllDownloadsFinished(): Boolean { - return queue.none { it.status <= Download.DOWNLOADING } + return queue.none { it.status.value <= Download.State.DOWNLOADING.value } } companion object { 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 5bd9b15be..7ebd7a9a5 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 @@ -20,7 +20,7 @@ class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) { @Volatile @Transient - var status: Int = 0 + var status: State = State.NOT_DOWNLOADED set(status) { field = status statusSubject?.onNext(this) @@ -47,11 +47,11 @@ class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) { statusCallback = f } - companion object { - const val NOT_DOWNLOADED = 0 - const val QUEUE = 1 - const val DOWNLOADING = 2 - const val DOWNLOADED = 3 - const val ERROR = 4 + enum class State(val value: Int) { + NOT_DOWNLOADED(0), + QUEUE(1), + DOWNLOADING(2), + DOWNLOADED(3), + ERROR(4), } } 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 90207b9e1..af4982bb9 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 @@ -22,7 +22,7 @@ class DownloadQueue( downloads.forEach { download -> download.setStatusSubject(statusSubject) download.setStatusCallback(::setPagesFor) - download.status = Download.QUEUE + download.status = Download.State.QUEUE } queue.addAll(downloads) store.addAll(downloads) @@ -34,8 +34,8 @@ class DownloadQueue( store.remove(download) download.setStatusSubject(null) download.setStatusCallback(null) - if (download.status == Download.DOWNLOADING || download.status == Download.QUEUE) { - download.status = Download.NOT_DOWNLOADED + if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { + download.status = Download.State.NOT_DOWNLOADED } if (removed) { updatedRelay.call(Unit) @@ -60,8 +60,8 @@ class DownloadQueue( queue.forEach { download -> download.setStatusSubject(null) download.setStatusCallback(null) - if (download.status == Download.DOWNLOADING || download.status == Download.QUEUE) { - download.status = Download.NOT_DOWNLOADED + if (download.status == Download.State.DOWNLOADING || download.status == Download.State.QUEUE) { + download.status = Download.State.NOT_DOWNLOADED } } queue.clear() @@ -70,7 +70,7 @@ class DownloadQueue( } fun getActiveDownloads(): Observable = - Observable.from(this).filter { download -> download.status == Download.DOWNLOADING } + Observable.from(this).filter { download -> download.status == Download.State.DOWNLOADING } fun getStatusObservable(): Observable = statusSubject.onBackpressureBuffer() @@ -79,7 +79,7 @@ class DownloadQueue( .map { this } private fun setPagesFor(download: Download) { - if (download.status == Download.DOWNLOADED || download.status == Download.ERROR) { + if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) { setPagesSubject(download.pages, null) } } @@ -88,19 +88,19 @@ class DownloadQueue( return statusSubject.onBackpressureBuffer() .startWith(getActiveDownloads()) .flatMap { download -> - if (download.status == Download.DOWNLOADING) { + if (download.status == Download.State.DOWNLOADING) { val pageStatusSubject = PublishSubject.create() setPagesSubject(download.pages, pageStatusSubject) return@flatMap pageStatusSubject .onBackpressureBuffer() .filter { it == Page.READY } .map { download } - } else if (download.status == Download.DOWNLOADED || download.status == Download.ERROR) { + } else if (download.status == Download.State.DOWNLOADED || download.status == Download.State.ERROR) { setPagesSubject(download.pages, null) } Observable.just(download) } - .filter { it.status == Download.DOWNLOADING } + .filter { it.status == Download.State.DOWNLOADING } } private fun setPagesSubject(pages: List?, subject: PublishSubject?) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt index 5fb2d934a..2622318b5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt @@ -172,17 +172,17 @@ class DownloadController : */ private fun onStatusChange(download: Download) { when (download.status) { - Download.DOWNLOADING -> { + Download.State.DOWNLOADING -> { observeProgress(download) // Initial update of the downloaded pages onUpdateDownloadedPages(download) } - Download.DOWNLOADED -> { + Download.State.DOWNLOADED -> { unsubscribeProgress(download) onUpdateProgress(download) onUpdateDownloadedPages(download) } - Download.ERROR -> unsubscribeProgress(download) + Download.State.ERROR -> unsubscribeProgress(download) } } 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 7481678d1..4f306b603 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 @@ -957,7 +957,7 @@ class MangaController : // OVERFLOW MENU DIALOGS private fun getUnreadChaptersSorted() = presenter.chapters - .filter { !it.read && it.status == Download.NOT_DOWNLOADED } + .filter { !it.read && it.status == Download.State.NOT_DOWNLOADED } .distinctBy { it.name } .sortedByDescending { it.source_order } 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 32ea0ad0e..d5c548b5c 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 @@ -328,7 +328,7 @@ class MangaPresenter( private fun setDownloadedChapters(chapters: List) { chapters .filter { downloadManager.isChapterDownloaded(it, manga) } - .forEach { it.status = Download.DOWNLOADED } + .forEach { it.status = Download.State.DOWNLOADED } } /** @@ -416,7 +416,7 @@ class MangaPresenter( */ private fun onDownloadStatusChange(download: Download) { // Assign the download to the model object. - if (download.status == Download.QUEUE) { + if (download.status == Download.State.QUEUE) { chapters.find { it.id == download.chapter.id }?.let { if (it.download == null) { it.download = download @@ -425,7 +425,7 @@ class MangaPresenter( } // Force UI update if downloaded filter active and download finished. - if (onlyDownloaded() != State.IGNORE && download.status == Download.DOWNLOADED) { + if (onlyDownloaded() != State.IGNORE && download.status == Download.State.DOWNLOADED) { refreshChapters() } } @@ -514,7 +514,7 @@ class MangaPresenter( private fun deleteChaptersInternal(chapters: List) { downloadManager.deleteChapters(chapters, manga, source).forEach { if (it is ChapterItem) { - it.status = Download.NOT_DOWNLOADED + it.status = Download.State.NOT_DOWNLOADED it.download = null } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDownloadView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDownloadView.kt index 47f4b1395..15848c0f9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDownloadView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterDownloadView.kt @@ -5,6 +5,7 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.widget.FrameLayout import androidx.core.view.isVisible +import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.databinding.ChapterDownloadViewBinding class ChapterDownloadView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : @@ -17,20 +18,12 @@ class ChapterDownloadView @JvmOverloads constructor(context: Context, attrs: Att addView(binding.root) } - fun setState(state: State) { - binding.downloadIconBorder.isVisible = state == State.DOWNLOAD || state == State.ERROR - binding.downloadIcon.isVisible = state == State.DOWNLOAD || state == State.DOWNLOADING + fun setState(state: Download.State) { + binding.downloadIconBorder.isVisible = state == Download.State.NOT_DOWNLOADED || state == Download.State.ERROR + binding.downloadIcon.isVisible = state == Download.State.NOT_DOWNLOADED || state == Download.State.DOWNLOADING - binding.downloadProgress.isVisible = state == State.DOWNLOADING || state == State.QUEUED + binding.downloadProgress.isVisible = state == Download.State.DOWNLOADING || state == Download.State.QUEUE - binding.downloadedIcon.isVisible = state == State.DOWNLOADED - } - - enum class State { - DOWNLOAD, - QUEUED, - DOWNLOADING, - ERROR, - DOWNLOADED, + binding.downloadedIcon.isVisible = state == Download.State.DOWNLOADED } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt index cd8e84b8c..5f7766dc7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterHolder.kt @@ -8,7 +8,6 @@ import androidx.core.view.isVisible import eu.davidea.viewholders.FlexibleViewHolder import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.databinding.ChaptersItemBinding import java.util.Date @@ -68,16 +67,6 @@ class ChapterHolder( binding.chapterDescription.text = "" } - notifyStatus(item.status) - } - - private fun notifyStatus(status: Int) = with(binding.download) { - when (status) { - Download.QUEUE -> setState(ChapterDownloadView.State.QUEUED) - Download.DOWNLOADING -> setState(ChapterDownloadView.State.DOWNLOADING) - Download.DOWNLOADED -> setState(ChapterDownloadView.State.DOWNLOADED) - Download.ERROR -> setState(ChapterDownloadView.State.ERROR) - else -> setState(ChapterDownloadView.State.DOWNLOAD) - } + binding.download.setState(item.status) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterItem.kt index 649cab753..678fb241e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChapterItem.kt @@ -14,9 +14,9 @@ class ChapterItem(val chapter: Chapter, val manga: Manga) : AbstractFlexibleItem(), Chapter by chapter { - private var _status: Int = 0 + private var _status: Download.State = Download.State.NOT_DOWNLOADED - var status: Int + var status: Download.State get() = download?.status ?: _status set(value) { _status = value @@ -26,7 +26,7 @@ class ChapterItem(val chapter: Chapter, val manga: Manga) : var download: Download? = null val isDownloaded: Boolean - get() = status == Download.DOWNLOADED + get() = status == Download.State.DOWNLOADED override fun getLayoutRes(): Int { return R.layout.chapters_item diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt index e94417ef7..1abb83190 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesHolder.kt @@ -81,18 +81,7 @@ class UpdatesHolder(private val view: View, private val adapter: UpdatesAdapter) .into(binding.mangaCover) } - /** - * Updates chapter status in view. - * - * @param status download status - */ - fun notifyStatus(status: Int) = with(binding.downloadText) { - when (status) { - Download.QUEUE -> setText(R.string.chapter_queued) - Download.DOWNLOADING -> setText(R.string.chapter_downloading) - Download.DOWNLOADED -> setText(R.string.chapter_downloaded) - Download.ERROR -> setText(R.string.chapter_error) - else -> text = "" - } + fun notifyStatus(state: Download.State) { + binding.download.setState(state) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesItem.kt index ecfc486ec..3ac1d108d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesItem.kt @@ -14,9 +14,9 @@ import eu.kanade.tachiyomi.ui.recent.DateSectionItem class UpdatesItem(val chapter: Chapter, val manga: Manga, header: DateSectionItem) : AbstractSectionableItem(header) { - private var _status: Int = 0 + private var _status: Download.State = Download.State.NOT_DOWNLOADED - var status: Int + var status: Download.State get() = download?.status ?: _status set(value) { _status = value @@ -26,7 +26,7 @@ class UpdatesItem(val chapter: Chapter, val manga: Manga, header: DateSectionIte var download: Download? = null val isDownloaded: Boolean - get() = status == Download.DOWNLOADED + get() = status == Download.State.DOWNLOADED override fun getLayoutRes(): Int { return R.layout.updates_item 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 84b1c87e1..18970dfc0 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 @@ -108,7 +108,7 @@ class UpdatesPresenter( val chapter = item.chapter if (downloadManager.isChapterDownloaded(chapter, manga)) { - item.status = Download.DOWNLOADED + item.status = Download.State.DOWNLOADED } } } @@ -120,7 +120,7 @@ class UpdatesPresenter( */ private fun onDownloadStatusChange(download: Download) { // Assign the download to the model object. - if (download.status == Download.QUEUE) { + if (download.status == Download.State.QUEUE) { val chapter = chapters.find { it.chapter.id == download.chapter.id } if (chapter != null && chapter.download == null) { chapter.download = download @@ -188,7 +188,7 @@ class UpdatesPresenter( downloadManager.deleteChapters(chapters, manga, source) items.forEach { - it.status = Download.NOT_DOWNLOADED + it.status = Download.State.NOT_DOWNLOADED it.download = null } } diff --git a/app/src/main/res/layout/chapters_item.xml b/app/src/main/res/layout/chapters_item.xml index 1c092d9d4..05072e58a 100644 --- a/app/src/main/res/layout/chapters_item.xml +++ b/app/src/main/res/layout/chapters_item.xml @@ -31,6 +31,7 @@ android:layout_marginTop="12dp" android:ellipsize="end" android:maxLines="1" + android:paddingEnd="8dp" app:layout_constraintEnd_toStartOf="@+id/download" app:layout_constraintStart_toEndOf="@+id/bookmark_icon" app:layout_constraintTop_toTopOf="parent" @@ -43,6 +44,7 @@ android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:ellipsize="end" + android:paddingEnd="8dp" android:singleLine="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/download" diff --git a/app/src/main/res/layout/updates_item.xml b/app/src/main/res/layout/updates_item.xml index 36c533b59..cdd267b92 100644 --- a/app/src/main/res/layout/updates_item.xml +++ b/app/src/main/res/layout/updates_item.xml @@ -27,9 +27,10 @@ android:layout_marginStart="16dp" android:ellipsize="end" android:maxLines="1" + android:paddingEnd="8dp" android:textAppearance="@style/TextAppearance.Regular.Body1" app:layout_constraintBottom_toTopOf="@+id/chapter_title" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@+id/download" app:layout_constraintStart_toEndOf="@+id/manga_cover" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_chainStyle="packed" @@ -43,22 +44,20 @@ android:layout_marginEnd="8dp" android:ellipsize="end" android:maxLines="1" + android:paddingEnd="8dp" android:textAppearance="@style/TextAppearance.Regular.Caption" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toStartOf="@+id/download_text" + app:layout_constraintEnd_toStartOf="@+id/download" app:layout_constraintStart_toEndOf="@+id/manga_cover" app:layout_constraintTop_toBottomOf="@+id/manga_title" tools:text="Chapter title" /> - + app:layout_constraintEnd_toEndOf="parent" />