DownloadBottomPresenter as a BaseCoroutinePresenter

This commit is contained in:
Jays2Kings 2022-04-22 14:51:52 -04:00
parent 1596bce43e
commit ff30974cf7
4 changed files with 12 additions and 11 deletions

View file

@ -3,9 +3,8 @@ package eu.kanade.tachiyomi.ui.download
import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.DownloadManager
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 kotlinx.coroutines.CoroutineScope import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -13,7 +12,7 @@ import uy.kohesive.injekt.injectLazy
/** /**
* Presenter of [DownloadBottomSheet]. * Presenter of [DownloadBottomSheet].
*/ */
class DownloadBottomPresenter(val sheet: DownloadBottomSheet) { class DownloadBottomPresenter : BaseCoroutinePresenter<DownloadBottomSheet>() {
/** /**
* Download manager. * Download manager.
@ -21,8 +20,6 @@ class DownloadBottomPresenter(val sheet: DownloadBottomSheet) {
val downloadManager: DownloadManager by injectLazy() val downloadManager: DownloadManager by injectLazy()
var items = listOf<DownloadHeaderItem>() var items = listOf<DownloadHeaderItem>()
private var scope = CoroutineScope(Job() + Dispatchers.Default)
/** /**
* Property to get the queue from the download manager. * Property to get the queue from the download manager.
*/ */
@ -30,7 +27,7 @@ class DownloadBottomPresenter(val sheet: DownloadBottomSheet) {
get() = downloadManager.queue get() = downloadManager.queue
fun getItems() { fun getItems() {
scope.launch { presenterScope.launch {
val items = downloadQueue val items = downloadQueue
.groupBy { it.source } .groupBy { it.source }
.map { entry -> .map { entry ->
@ -56,7 +53,7 @@ class DownloadBottomPresenter(val sheet: DownloadBottomSheet) {
} }
this@DownloadBottomPresenter.items = items this@DownloadBottomPresenter.items = items
if (hasChanged) { if (hasChanged) {
withContext(Dispatchers.Main) { sheet.onNextDownloads(items) } withContext(Dispatchers.Main) { controller?.onNextDownloads(items) }
} }
} }
} }

View file

@ -39,7 +39,7 @@ class DownloadBottomSheet @JvmOverloads constructor(
*/ */
private var adapter: DownloadAdapter? = null private var adapter: DownloadAdapter? = null
private val presenter = DownloadBottomPresenter(this) private val presenter = DownloadBottomPresenter()
/** /**
* Whether the download queue is running or not. * Whether the download queue is running or not.
@ -55,6 +55,8 @@ class DownloadBottomSheet @JvmOverloads constructor(
fun onCreate(controller: RecentsController) { fun onCreate(controller: RecentsController) {
// Initialize adapter, scroll listener and recycler views // Initialize adapter, scroll listener and recycler views
presenter.attachView(this)
presenter.onCreate()
adapter = DownloadAdapter(this) adapter = DownloadAdapter(this)
sheetBehavior = BottomSheetBehavior.from(this) sheetBehavior = BottomSheetBehavior.from(this)
activity = controller.activity activity = controller.activity
@ -336,4 +338,8 @@ class DownloadBottomSheet @JvmOverloads constructor(
} }
} }
} }
fun onDestroy() {
presenter.onDestroy()
}
} }

View file

@ -387,9 +387,6 @@ class ExtensionBottomSheet @JvmOverloads constructor(context: Context, attrs: At
presenter.uninstallExtension(pkgName) presenter.uninstallExtension(pkgName)
} }
/**
* Called when the view of this adapter is being destroyed.
*/
fun onDestroy() { fun onDestroy() {
presenter.onDestroy() presenter.onDestroy()
} }

View file

@ -500,6 +500,7 @@ class RecentsController(bundle: Bundle? = null) :
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
super.onDestroyView(view) super.onDestroyView(view)
binding.downloadBottomSheet.root.onDestroy()
displaySheet?.dismiss() displaySheet?.dismiss()
displaySheet = null displaySheet = null
} }