Using an enum to represent download states

no need for the value int in upsteam, since enums can compare already

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2021-05-09 14:21:01 -04:00
parent 8cadb283f8
commit 22f2ef18b1
12 changed files with 73 additions and 68 deletions

View File

@ -111,8 +111,8 @@ class Downloader(
notifier.paused = false
if (!subscriptions.hasSubscriptions()) 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 }
downloadsRelay.call(pending)
return pending.isNotEmpty()
@ -124,8 +124,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)
@ -149,8 +149,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
}
@ -170,8 +170,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.dismiss()
@ -185,8 +185,8 @@ class Downloader(
fun clearQueue(manga: Manga, isNotification: Boolean = false) {
// Needed to update the chapter view
if (isNotification) {
queue.filter { it.status == Download.QUEUE && it.manga.id == manga.id }
.forEach { it.status = Download.NOT_DOWNLOADED }
queue.filter { it.status == Download.State.QUEUE && it.manga.id == manga.id }
.forEach { it.status = Download.State.NOT_DOWNLOADED }
}
queue.remove(manga)
if (queue.isEmpty()) {
@ -294,7 +294,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.couldnt_download_low_space), download.chapter.name)
return@defer Observable.just(download)
}
@ -323,7 +323,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) }
@ -338,7 +338,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
}
@ -491,13 +491,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)
@ -510,7 +510,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)
}
@ -527,7 +527,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 <= Download.State.DOWNLOADING }
}
companion object {

View File

@ -18,7 +18,7 @@ class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) {
var downloadedImages: Int = 0
@Volatile @Transient
var status: Int = 0
var status: State = State.default
set(status) {
field = status
statusSubject?.onNext(this)
@ -49,12 +49,17 @@ class Download(val source: HttpSource, val manga: Manga, val chapter: Chapter) {
statusCallback = f
}
enum class State {
CHECKED,
NOT_DOWNLOADED,
QUEUE,
DOWNLOADING,
DOWNLOADED,
ERROR
;
companion object {
const val CHECKED = -1
const val NOT_DOWNLOADED = 0
const val QUEUE = 1
const val DOWNLOADING = 2
const val DOWNLOADED = 3
const val ERROR = 4
val default = NOT_DOWNLOADED
}
}
}

View File

@ -24,7 +24,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)
@ -36,8 +36,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
}
downloadListeners.forEach { it.updateDownload(download) }
if (removed) {
@ -65,8 +65,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
}
downloadListeners.forEach { it.updateDownload(download) }
}
@ -76,7 +76,7 @@ class DownloadQueue(
}
private fun setPagesFor(download: Download) {
if (download.status == Download.DOWNLOADING) {
if (download.status == Download.State.DOWNLOADING) {
if (download.pages != null) {
for (page in download.pages!!)
page.setStatusCallback {
@ -84,9 +84,9 @@ class DownloadQueue(
}
}
callListeners(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)
if (download.status == Download.ERROR) {
if (download.status == Download.State.ERROR) {
callListeners(download)
}
} else {

View File

@ -76,19 +76,19 @@ class DownloadButton @JvmOverloads constructor(context: Context, attrs: Attribut
binding = DownloadButtonBinding.bind(this)
}
fun setDownloadStatus(state: Int, progress: Int = 0, animated: Boolean = false) {
if (state != Download.DOWNLOADING) {
fun setDownloadStatus(state: Download.State, progress: Int = 0, animated: Boolean = false) {
if (state != Download.State.DOWNLOADING) {
iconAnimation?.cancel()
binding.downloadIcon.alpha = 1f
isAnimating = false
}
binding.downloadIcon.setImageDrawable(
if (state == Download.CHECKED) {
if (state == Download.State.CHECKED) {
checkDrawable
} else downloadDrawable
)
when (state) {
Download.CHECKED -> {
Download.State.CHECKED -> {
binding.downloadProgress.isVisible = false
binding.downloadBorder.isVisible = true
binding.downloadProgressIndeterminate.isVisible = false
@ -96,7 +96,7 @@ class DownloadButton @JvmOverloads constructor(context: Context, attrs: Attribut
binding.downloadBorder.drawable.setTint(activeColor)
binding.downloadIcon.drawable.setTint(Color.WHITE)
}
Download.NOT_DOWNLOADED -> {
Download.State.NOT_DOWNLOADED -> {
binding.downloadBorder.isVisible = true
binding.downloadProgress.isVisible = false
binding.downloadProgressIndeterminate.isVisible = false
@ -104,14 +104,14 @@ class DownloadButton @JvmOverloads constructor(context: Context, attrs: Attribut
binding.downloadBorder.drawable.setTint(activeColor)
binding.downloadIcon.drawable.setTint(activeColor)
}
Download.QUEUE -> {
Download.State.QUEUE -> {
binding.downloadBorder.isVisible = false
binding.downloadProgress.isVisible = false
binding.downloadProgressIndeterminate.isVisible = true
binding.downloadProgress.isIndeterminate = true
binding.downloadIcon.drawable.setTint(disabledColor)
}
Download.DOWNLOADING -> {
Download.State.DOWNLOADING -> {
binding.downloadBorder.isVisible = true
binding.downloadProgress.isVisible = true
binding.downloadProgressIndeterminate.isVisible = false
@ -131,7 +131,7 @@ class DownloadButton @JvmOverloads constructor(context: Context, attrs: Attribut
isAnimating = true
}
}
Download.DOWNLOADED -> {
Download.State.DOWNLOADED -> {
binding.downloadProgress.isVisible = false
binding.downloadBorder.isVisible = true
binding.downloadProgressIndeterminate.isVisible = false
@ -159,7 +159,7 @@ class DownloadButton @JvmOverloads constructor(context: Context, attrs: Attribut
binding.downloadIcon.drawable.setTint(downloadedTextColor)
}
}
Download.ERROR -> {
Download.State.ERROR -> {
binding.downloadProgress.isVisible = false
binding.downloadBorder.isVisible = true
binding.downloadProgressIndeterminate.isVisible = false

View File

@ -629,7 +629,7 @@ class MangaDetailsController :
(binding.recycler.findViewHolderForAdapterPosition(position) as? BaseFlexibleViewHolder)
?.toggleActivation()
(binding.recycler.findViewHolderForAdapterPosition(position) as? ChapterHolder)
?.notifyStatus(Download.CHECKED, false, 0)
?.notifyStatus(Download.State.CHECKED, false, 0)
startingRangeChapterPos = position
actionMode?.invalidate()
} else {
@ -1148,10 +1148,10 @@ class MangaDetailsController :
onItemClick(null, position)
return
}
if (chapter.status != Download.NOT_DOWNLOADED && chapter.status != Download.ERROR) {
if (chapter.status != Download.State.NOT_DOWNLOADED && chapter.status != Download.State.ERROR) {
presenter.deleteChapter(chapter)
} else {
if (chapter.status == Download.ERROR) {
if (chapter.status == Download.State.ERROR) {
DownloadService.start(view.context)
} else {
downloadChapters(listOf(chapter))
@ -1352,7 +1352,7 @@ class MangaDetailsController :
if (startingRangeChapterPos != null && rangeMode == RangeMode.Download) {
val item = adapter?.getItem(startingRangeChapterPos!!) as? ChapterItem
(binding.recycler.findViewHolderForAdapterPosition(startingRangeChapterPos!!) as? ChapterHolder)?.notifyStatus(
item?.status ?: Download.NOT_DOWNLOADED,
item?.status ?: Download.State.NOT_DOWNLOADED,
false,
0
)

View File

@ -162,10 +162,10 @@ class MangaDetailsPresenter(
private fun setDownloadedChapters(chapters: List<ChapterItem>) {
for (chapter in chapters) {
if (downloadManager.isChapterDownloaded(chapter, manga)) {
chapter.status = Download.DOWNLOADED
chapter.status = Download.State.DOWNLOADED
} else if (downloadManager.hasQueue()) {
chapter.status = downloadManager.queue.find { it.chapter.id == chapter.id }
?.status ?: 0
?.status ?: Download.State.default
}
}
}
@ -267,7 +267,7 @@ class MangaDetailsPresenter(
fun hasDownloads(): Boolean = allChapters.any { it.isDownloaded }
fun getUnreadChaptersSorted() =
allChapters.filter { !it.read && it.status == Download.NOT_DOWNLOADED }.distinctBy { it.name }
allChapters.filter { !it.read && it.status == Download.State.NOT_DOWNLOADED }.distinctBy { it.name }
.sortedByDescending { it.source_order }
fun startDownloadingNow(chapter: Chapter) {
@ -289,7 +289,7 @@ class MangaDetailsPresenter(
fun deleteChapter(chapter: ChapterItem) {
downloadManager.deleteChapters(listOf(chapter), manga, source)
this.chapters.find { it.id == chapter.id }?.apply {
status = Download.QUEUE
status = Download.State.QUEUE
download = null
}
@ -310,7 +310,7 @@ class MangaDetailsPresenter(
}
chapters.forEach { chapter ->
this.chapters.find { it.id == chapter.id }?.apply {
status = Download.QUEUE
status = Download.State.QUEUE
download = null
}
}

View File

@ -19,7 +19,7 @@ open class BaseChapterHolder(
val chapter = adapter.getItem(flexibleAdapterPosition) as? BaseChapterItem<*, *> ?: return
val downloadButton = itemView.findViewById<View>(R.id.download_button) ?: return
if (chapter.status == Download.NOT_DOWNLOADED || chapter.status == Download.ERROR) {
if (chapter.status == Download.State.NOT_DOWNLOADED || chapter.status == Download.State.ERROR) {
adapter.baseDelegate.downloadChapter(flexibleAdapterPosition)
} else {
downloadButton.post {
@ -29,10 +29,10 @@ open class BaseChapterHolder(
// Inflate our menu resource into the PopupMenu's Menu
popup.menuInflater.inflate(R.menu.chapter_download, popup.menu)
popup.menu.findItem(R.id.action_start).isVisible = chapter.status == Download.QUEUE
popup.menu.findItem(R.id.action_start).isVisible = chapter.status == Download.State.QUEUE
// Hide download and show delete if the chapter is downloaded
if (chapter.status != Download.DOWNLOADED) popup.menu.findItem(R.id.action_delete).title = downloadButton.context.getString(
if (chapter.status != Download.State.DOWNLOADED) popup.menu.findItem(R.id.action_delete).title = downloadButton.context.getString(
R.string.cancel
)

View File

@ -14,7 +14,7 @@ abstract class BaseChapterItem<T : BaseChapterHolder, H : AbstractHeaderItem<*>>
AbstractSectionableItem<T, H?>(header),
Chapter by chapter {
private var _status: Int = 0
private var _status: Download.State = Download.State.default
val progress: Int
get() {
@ -22,14 +22,14 @@ abstract class BaseChapterItem<T : BaseChapterHolder, H : AbstractHeaderItem<*>>
return pages.map(Page::progress).average().toInt()
}
var status: Int
var status: Download.State
get() = download?.status ?: _status
set(value) { _status = value }
@Transient var download: Download? = null
val isDownloaded: Boolean
get() = status == Download.DOWNLOADED
get() = status == Download.State.DOWNLOADED
override fun equals(other: Any?): Boolean {
if (this === other) return true

View File

@ -91,7 +91,7 @@ class ChapterHolder(
binding.chapterScanlator.text = statuses.joinToString("")
val status = when {
adapter.isSelected(flexibleAdapterPosition) -> Download.CHECKED
adapter.isSelected(flexibleAdapterPosition) -> Download.State.CHECKED
else -> item.status
}
@ -149,7 +149,7 @@ class ChapterHolder(
if (binding.frontView.translationX != 0f) itemView.post { adapter.notifyItemChanged(flexibleAdapterPosition) }
}
fun notifyStatus(status: Int, locked: Boolean, progress: Int, animated: Boolean = false) = with(binding.downloadButton.downloadButton) {
fun notifyStatus(status: Download.State, locked: Boolean, progress: Int, animated: Boolean = false) = with(binding.downloadButton.downloadButton) {
if (locked) {
isVisible = false
return

View File

@ -142,7 +142,7 @@ class RecentMangaHolder(
}
if (!item.mch.manga.isLocal()) {
notifyStatus(
if (adapter.isSelected(flexibleAdapterPosition)) Download.CHECKED else item.status,
if (adapter.isSelected(flexibleAdapterPosition)) Download.State.CHECKED else item.status,
item.progress,
item.chapter.read
)
@ -160,7 +160,7 @@ class RecentMangaHolder(
return item.mch.history.id != null
}
fun notifyStatus(status: Int, progress: Int, isRead: Boolean, animated: Boolean = false) {
fun notifyStatus(status: Download.State, progress: Int, isRead: Boolean, animated: Boolean = false) {
binding.downloadButton.downloadButton.setDownloadStatus(status, progress, animated)
val isChapterRead =
if (adapter.showDownloads == RecentMangaAdapter.ShowRecentsDLs.UnreadOrDownloaded) isRead else false
@ -168,7 +168,7 @@ class RecentMangaHolder(
when (adapter.showDownloads) {
RecentMangaAdapter.ShowRecentsDLs.UnreadOrDownloaded,
RecentMangaAdapter.ShowRecentsDLs.OnlyDownloaded ->
status !in Download.CHECKED..Download.NOT_DOWNLOADED || !isChapterRead
status !in Download.State.CHECKED..Download.State.NOT_DOWNLOADED || !isChapterRead
else -> binding.downloadButton.downloadButton.isVisible
}
}

View File

@ -523,10 +523,10 @@ class RecentsController(bundle: Bundle? = null) :
val item = adapter.getItem(position) as? RecentMangaItem ?: return
val chapter = item.chapter
val manga = item.mch.manga
if (item.status != Download.NOT_DOWNLOADED && item.status != Download.ERROR) {
if (item.status != Download.State.NOT_DOWNLOADED && item.status != Download.State.ERROR) {
presenter.deleteChapter(chapter, manga)
} else {
if (item.status == Download.ERROR) DownloadService.start(view.context)
if (item.status == Download.State.ERROR) DownloadService.start(view.context)
else presenter.downloadChapter(manga, chapter)
}
}

View File

@ -353,10 +353,10 @@ class RecentsPresenter(
private fun setDownloadedChapters(chapters: List<RecentMangaItem>) {
for (item in chapters.filter { it.chapter.id != null }) {
if (downloadManager.isChapterDownloaded(item.chapter, item.mch.manga)) {
item.status = Download.DOWNLOADED
item.status = Download.State.DOWNLOADED
} else if (downloadManager.hasQueue()) {
item.status = downloadManager.queue.find { it.chapter.id == item.chapter.id }
?.status ?: 0
?.status ?: Download.State.default
}
}
}
@ -405,7 +405,7 @@ class RecentsPresenter(
if (update) {
val item = recentItems.find { it.chapter.id == chapter.id } ?: return
item.apply {
status = Download.NOT_DOWNLOADED
status = Download.State.NOT_DOWNLOADED
download = null
}