Show download progress in download icons

This commit is contained in:
arkon 2020-12-27 16:50:25 -05:00
parent 3cb64669e4
commit 0e2b8b10d1
9 changed files with 55 additions and 37 deletions

View file

@ -705,7 +705,7 @@ class MangaController :
}
}
fun onChapterStatusChange(download: Download) {
fun onChapterDownloadUpdate(download: Download) {
chaptersAdapter?.currentItems?.find { it.id == download.chapter.id }?.let {
chaptersAdapter?.updateItem(it)
chaptersAdapter?.notifyDataSetChanged()

View file

@ -79,7 +79,8 @@ class MangaPresenter(
/**
* Subscription to observe download status changes.
*/
private var observeDownloadsSubscription: Subscription? = null
private var observeDownloadsStatusSubscription: Subscription? = null
private var observeDownloadsPageSubscription: Subscription? = null
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
@ -293,12 +294,20 @@ class MangaPresenter(
// Chapters list - start
private fun observeDownloads() {
observeDownloadsSubscription?.let { remove(it) }
observeDownloadsSubscription = downloadManager.queue.getStatusObservable()
observeDownloadsStatusSubscription?.let { remove(it) }
observeDownloadsStatusSubscription = downloadManager.queue.getStatusObservable()
.observeOn(AndroidSchedulers.mainThread())
.filter { download -> download.manga.id == manga.id }
.doOnNext { onDownloadStatusChange(it) }
.subscribeLatestCache(MangaController::onChapterStatusChange) { _, error ->
.subscribeLatestCache(MangaController::onChapterDownloadUpdate) { _, error ->
Timber.e(error)
}
observeDownloadsPageSubscription?.let { remove(it) }
observeDownloadsPageSubscription = downloadManager.queue.getProgressObservable()
.observeOn(AndroidSchedulers.mainThread())
.filter { download -> download.manga.id == manga.id }
.subscribeLatestCache(MangaController::onChapterDownloadUpdate) { _, error ->
Timber.e(error)
}
}

View file

@ -22,7 +22,7 @@ class ChapterDownloadView @JvmOverloads constructor(context: Context, attrs: Att
addView(binding.root)
}
fun setState(state: Download.State) {
fun setState(state: Download.State, progress: Int = 0) {
binding.downloadIconBorder.isVisible = state == Download.State.NOT_DOWNLOADED
binding.downloadIcon.isVisible = state == Download.State.NOT_DOWNLOADED || state == Download.State.DOWNLOADING
@ -44,7 +44,13 @@ class ChapterDownloadView @JvmOverloads constructor(context: Context, attrs: Att
}
binding.downloadProgress.isVisible = state == Download.State.DOWNLOADING || state == Download.State.QUEUE
// TODO: show actual download progress
// Spinner when queued
val isDownloading = state == Download.State.DOWNLOADING || (state == Download.State.QUEUE && progress > 0)
binding.downloadProgress.isIndeterminate = !isDownloading
// Actual progress when downloading or partially downloaded
if (isDownloading) {
binding.downloadProgress.progress = progress
}
binding.downloadedIcon.isVisible = state == Download.State.DOWNLOADED

View file

@ -69,6 +69,6 @@ class ChapterHolder(
}
binding.download.isVisible = item.manga.source != LocalSource.ID
binding.download.setState(item.status)
binding.download.setState(item.status, item.progress)
}
}

View file

@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.source.model.Page
class ChapterItem(val chapter: Chapter, val manga: Manga) :
AbstractFlexibleItem<ChapterHolder>(),
@ -22,6 +23,12 @@ class ChapterItem(val chapter: Chapter, val manga: Manga) :
_status = value
}
val progress: Int
get() {
val pages = download?.pages ?: return 0
return pages.map(Page::progress).average().toInt()
}
@Transient
var download: Download? = null

View file

@ -229,16 +229,13 @@ class UpdatesController :
* Update download status of chapter
* @param download [Download] object containing download progress.
*/
fun onChapterStatusChange(download: Download) {
getHolder(download)?.notifyStatus(download.status)
fun onChapterDownloadUpdate(download: Download) {
adapter?.currentItems
?.filterIsInstance<UpdatesItem>()
?.find { it.chapter.id == download.chapter.id }?.let {
adapter?.updateItem(it)
adapter?.notifyDataSetChanged()
}
/**
* Returns holder belonging to chapter
* @param download [Download] object containing download progress.
*/
private fun getHolder(download: Download): UpdatesHolder? {
return binding.recycler.findViewHolderForItemId(download.chapter.id!!) as? UpdatesHolder
}
/**

View file

@ -8,7 +8,6 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import eu.davidea.viewholders.FlexibleViewHolder
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.glide.GlideApp
import eu.kanade.tachiyomi.data.glide.toMangaThumbnail
import eu.kanade.tachiyomi.databinding.UpdatesItemBinding
@ -52,7 +51,7 @@ class UpdatesHolder(private val view: View, private val adapter: UpdatesAdapter)
// Set chapter status
binding.download.isVisible = item.manga.source != LocalSource.ID
notifyStatus(item.status)
binding.download.setState(item.status, item.progress)
// Set cover
GlideApp.with(itemView.context).clear(binding.mangaCover)
@ -66,8 +65,4 @@ class UpdatesHolder(private val view: View, private val adapter: UpdatesAdapter)
.dontAnimate()
.into(binding.mangaCover)
}
fun notifyStatus(state: Download.State) {
binding.download.setState(state)
}
}

View file

@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.ui.recent.DateSectionItem
class UpdatesItem(val chapter: Chapter, val manga: Manga, header: DateSectionItem) :
@ -22,6 +23,12 @@ class UpdatesItem(val chapter: Chapter, val manga: Manga, header: DateSectionIte
_status = value
}
val progress: Int
get() {
val pages = download?.pages ?: return 0
return pages.map(Page::progress).average().toInt()
}
@Transient
var download: Download? = null

View file

@ -39,8 +39,16 @@ class UpdatesPresenter(
.observeOn(AndroidSchedulers.mainThread())
.subscribeLatestCache(UpdatesController::onNextRecentChapters)
getChapterStatusObservable()
.subscribeLatestCache(UpdatesController::onChapterStatusChange) { _, error ->
downloadManager.queue.getStatusObservable()
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { onDownloadStatusChange(it) }
.subscribeLatestCache(UpdatesController::onChapterDownloadUpdate) { _, error ->
Timber.e(error)
}
downloadManager.queue.getProgressObservable()
.observeOn(AndroidSchedulers.mainThread())
.subscribeLatestCache(UpdatesController::onChapterDownloadUpdate) { _, error ->
Timber.e(error)
}
}
@ -86,17 +94,6 @@ class UpdatesPresenter(
}
}
/**
* Returns observable containing chapter status.
*
* @return download object containing download progress.
*/
private fun getChapterStatusObservable(): Observable<Download> {
return downloadManager.queue.getStatusObservable()
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { download -> onDownloadStatusChange(download) }
}
/**
* Finds and assigns the list of downloaded chapters.
*