Fixes to the download status/fab/errors in ui

This commit is contained in:
Jays2Kings 2021-05-09 13:51:55 -04:00
parent e669f4eba5
commit 5927ca6976
3 changed files with 38 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import android.util.AttributeSet
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.core.view.isInvisible
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -87,12 +88,13 @@ class DownloadBottomSheet @JvmOverloads constructor(
} }
} }
binding.downloadFab.setOnClickListener { binding.downloadFab.setOnClickListener {
if (!isRunning) { if (controller.presenter.downloadManager.isPaused()) {
DownloadService.start(context) DownloadService.start(context)
} else { } else {
DownloadService.stop(context) DownloadService.stop(context)
presenter.pauseDownloads() presenter.pauseDownloads()
} }
updateFab()
} }
update() update()
setInformationView() setInformationView()
@ -105,7 +107,7 @@ class DownloadBottomSheet @JvmOverloads constructor(
fun update() { fun update() {
presenter.getItems() presenter.getItems()
onQueueStatusChange(!presenter.downloadManager.isPaused()) onQueueStatusChange(!presenter.downloadManager.isPaused())
binding.downloadFab.isVisible = presenter.downloadQueue.isNotEmpty() binding.downloadFab.isInvisible = presenter.downloadQueue.isEmpty()
} }
private fun updateDLTitle() { private fun updateDLTitle() {
@ -125,7 +127,8 @@ class DownloadBottomSheet @JvmOverloads constructor(
private fun onQueueStatusChange(running: Boolean) { private fun onQueueStatusChange(running: Boolean) {
val oldRunning = isRunning val oldRunning = isRunning
isRunning = running isRunning = running
binding.downloadFab.isVisible = presenter.downloadQueue.isNotEmpty() binding.downloadFab.isInvisible = presenter.downloadQueue.isEmpty()
updateFab()
if (oldRunning != running) { if (oldRunning != running) {
activity?.invalidateOptionsMenu() activity?.invalidateOptionsMenu()
@ -191,9 +194,7 @@ class DownloadBottomSheet @JvmOverloads constructor(
} }
fun prepareMenu(menu: Menu) { fun prepareMenu(menu: Menu) {
binding.downloadFab.text = context.getString(if (isRunning) R.string.pause else R.string.resume) updateFab()
binding.downloadFab.setIconResource(if (isRunning) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp)
// Set clear button visibility. // Set clear button visibility.
menu.findItem(R.id.clear_queue)?.isVisible = !presenter.downloadQueue.isEmpty() menu.findItem(R.id.clear_queue)?.isVisible = !presenter.downloadQueue.isEmpty()
@ -201,6 +202,11 @@ class DownloadBottomSheet @JvmOverloads constructor(
menu.findItem(R.id.reorder)?.isVisible = !presenter.downloadQueue.isEmpty() menu.findItem(R.id.reorder)?.isVisible = !presenter.downloadQueue.isEmpty()
} }
private fun updateFab() {
binding.downloadFab.text = context.getString(if (isRunning) R.string.pause else R.string.resume)
binding.downloadFab.setIconResource(if (isRunning) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp)
}
fun onOptionsItemSelected(item: MenuItem): Boolean { fun onOptionsItemSelected(item: MenuItem): Boolean {
val context = activity ?: return false val context = activity ?: return false
when (item.itemId) { when (item.itemId) {
@ -263,6 +269,7 @@ class DownloadBottomSheet @JvmOverloads constructor(
val adapter = adapter ?: return val adapter = adapter ?: return
val downloads = (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download } val downloads = (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download }
presenter.reorder(downloads) presenter.reorder(downloads)
controller.updateChapterDownload(download, false)
} }
/** /**

View File

@ -304,6 +304,7 @@ class RecentsController(bundle: Bundle? = null) :
} }
if (presenter.downloadManager.hasQueue()) { if (presenter.downloadManager.hasQueue()) {
binding.downloadBottomSheet.downloadFab.alpha = 1f
if (state == BottomSheetBehavior.STATE_EXPANDED) { if (state == BottomSheetBehavior.STATE_EXPANDED) {
binding.downloadBottomSheet.downloadFab.show() binding.downloadBottomSheet.downloadFab.show()
} else { } else {
@ -488,16 +489,22 @@ class RecentsController(bundle: Bundle? = null) :
} }
} }
fun updateChapterDownload(download: Download) { fun updateChapterDownload(download: Download, updateDLSheet: Boolean = true) {
if (view == null) return if (view == null) return
if (updateDLSheet) {
binding.downloadBottomSheet.dlBottomSheet.update() binding.downloadBottomSheet.dlBottomSheet.update()
binding.downloadBottomSheet.dlBottomSheet.onUpdateProgress(download) binding.downloadBottomSheet.dlBottomSheet.onUpdateProgress(download)
binding.downloadBottomSheet.dlBottomSheet.onUpdateDownloadedPages(download) binding.downloadBottomSheet.dlBottomSheet.onUpdateDownloadedPages(download)
}
val id = download.chapter.id ?: return val id = download.chapter.id ?: return
val holder = binding.recycler.findViewHolderForItemId(id) as? RecentMangaHolder ?: return val holder = binding.recycler.findViewHolderForItemId(id) as? RecentMangaHolder ?: return
holder.notifyStatus(download.status, download.progress, download.chapter.read, true) holder.notifyStatus(download.status, download.progress, download.chapter.read, true)
} }
fun updateDownloadStatus() {
binding.downloadBottomSheet.dlBottomSheet.update()
}
private fun refreshItem(chapterId: Long) { private fun refreshItem(chapterId: Long) {
val recentItemPos = adapter.currentItems.indexOfFirst { val recentItemPos = adapter.currentItems.indexOfFirst {
it is RecentMangaItem && it is RecentMangaItem &&

View File

@ -7,6 +7,8 @@ import eu.kanade.tachiyomi.data.database.models.HistoryImpl
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.download.DownloadService
import eu.kanade.tachiyomi.data.download.DownloadServiceListener
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.download.model.DownloadQueue import eu.kanade.tachiyomi.data.download.model.DownloadQueue
import eu.kanade.tachiyomi.data.library.LibraryServiceListener import eu.kanade.tachiyomi.data.library.LibraryServiceListener
@ -37,7 +39,7 @@ class RecentsPresenter(
val preferences: PreferencesHelper = Injekt.get(), val preferences: PreferencesHelper = Injekt.get(),
val downloadManager: DownloadManager = Injekt.get(), val downloadManager: DownloadManager = Injekt.get(),
private val db: DatabaseHelper = Injekt.get() private val db: DatabaseHelper = Injekt.get()
) : BaseCoroutinePresenter(), DownloadQueue.DownloadListener, LibraryServiceListener { ) : BaseCoroutinePresenter(), DownloadQueue.DownloadListener, LibraryServiceListener, DownloadServiceListener {
private var recentsJob: Job? = null private var recentsJob: Job? = null
var recentItems = listOf<RecentMangaItem>() var recentItems = listOf<RecentMangaItem>()
@ -75,6 +77,7 @@ class RecentsPresenter(
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
downloadManager.addListener(this) downloadManager.addListener(this)
DownloadService.addListener(this)
LibraryUpdateService.setListener(this) LibraryUpdateService.setListener(this)
if (lastRecents != null) { if (lastRecents != null) {
if (recentItems.isEmpty()) { if (recentItems.isEmpty()) {
@ -329,6 +332,7 @@ class RecentsPresenter(
super.onDestroy() super.onDestroy()
downloadManager.removeListener(this) downloadManager.removeListener(this)
LibraryUpdateService.removeListener(this) LibraryUpdateService.removeListener(this)
DownloadService.removeListener(this)
lastRecents = recentItems lastRecents = recentItems
} }
@ -367,6 +371,15 @@ class RecentsPresenter(
setDownloadedChapters(recentItems) setDownloadedChapters(recentItems)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
controller?.showLists(recentItems, true) controller?.showLists(recentItems, true)
controller?.updateDownloadStatus()
}
}
}
override fun downloadStatusChanged(downloading: Boolean) {
presenterScope.launch {
withContext(Dispatchers.Main) {
controller?.updateDownloadStatus()
} }
} }
} }