Create base coroutine classes for controller/presenters

Using so far for recents and extensions presenter
This commit is contained in:
Jays2Kings 2021-04-26 15:36:57 -04:00
parent f28419ce3f
commit a9125cc591
5 changed files with 67 additions and 38 deletions

View File

@ -0,0 +1,21 @@
package eu.kanade.tachiyomi.ui.base.controller
import android.os.Bundle
import android.view.View
import androidx.viewbinding.ViewBinding
import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
abstract class BaseCoroutineController<VB : ViewBinding, PS : BaseCoroutinePresenter>(bundle: Bundle? = null) :
BaseController<VB>(bundle) {
abstract val presenter: PS
override fun onViewCreated(view: View) {
super.onViewCreated(view)
presenter.onCreate()
}
override fun onDestroyView(view: View) {
super.onDestroyView(view)
presenter.onDestroy()
}
}

View File

@ -0,0 +1,18 @@
package eu.kanade.tachiyomi.ui.base.presenter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
open class BaseCoroutinePresenter {
var presenterScope = CoroutineScope(Job() + Dispatchers.Default)
open fun onCreate() {
presenterScope = CoroutineScope(Job() + Dispatchers.Default)
}
open fun onDestroy() {
presenterScope.cancel()
}
}

View File

@ -12,14 +12,13 @@ import eu.kanade.tachiyomi.extension.model.InstallStep
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
import eu.kanade.tachiyomi.ui.migration.MangaItem
import eu.kanade.tachiyomi.ui.migration.SelectionHeader
import eu.kanade.tachiyomi.ui.migration.SourceItem
import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
@ -38,8 +37,7 @@ class ExtensionBottomPresenter(
private val bottomSheet: ExtensionBottomSheet,
private val extensionManager: ExtensionManager = Injekt.get(),
private val preferences: PreferencesHelper = Injekt.get()
) : ExtensionsChangedListener {
private var scope = CoroutineScope(Job() + Dispatchers.Default)
) : BaseCoroutinePresenter(), ExtensionsChangedListener {
private var extensions = emptyList<ExtensionItem>()
@ -56,8 +54,9 @@ class ExtensionBottomPresenter(
private var selectedSource: Long? = null
private val db: DatabaseHelper = Injekt.get()
fun onCreate() {
scope.launch {
override fun onCreate() {
super.onCreate()
presenterScope.launch {
val extensionJob = async {
extensionManager.findAvailableExtensionsAsync()
extensions = toItems(
@ -102,12 +101,13 @@ class ExtensionBottomPresenter(
return library.filter { it.source == sourceId }.map(::MangaItem)
}
fun onDestroy() {
override fun onDestroy() {
super.onDestroy()
extensionManager.removeListener(this)
}
fun refreshExtensions() {
scope.launch {
presenterScope.launch {
extensions = toItems(
Triple(
extensionManager.installedExtensions,
@ -120,7 +120,7 @@ class ExtensionBottomPresenter(
}
fun refreshMigrations() {
scope.launch {
presenterScope.launch {
val favs = db.getFavoriteMangas().executeOnIO()
sourceItems = findSourcesWithManga(favs)
mangaItems = HashMap(
@ -241,14 +241,14 @@ class ExtensionBottomPresenter(
fun setSelectedSource(source: Source) {
selectedSource = source.id
scope.launch {
presenterScope.launch {
withContext(Dispatchers.Main) { bottomSheet.setMigrationManga(mangaItems[source.id]) }
}
}
fun deselectSource() {
selectedSource = null
scope.launch {
presenterScope.launch {
withContext(Dispatchers.Main) { bottomSheet.setMigrationSources(sourceItems) }
}
}

View File

@ -29,7 +29,7 @@ import eu.kanade.tachiyomi.data.download.DownloadService
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.databinding.RecentsControllerBinding
import eu.kanade.tachiyomi.ui.base.controller.BaseController
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.main.BottomSheetController
import eu.kanade.tachiyomi.ui.main.FloatingSearchInterface
@ -65,7 +65,7 @@ import kotlin.math.min
* UI related actions should be called from here.
*/
class RecentsController(bundle: Bundle? = null) :
BaseController<RecentsControllerBinding>(bundle),
BaseCoroutineController<RecentsControllerBinding, RecentsPresenter>(bundle),
RecentMangaAdapter.RecentsInterface,
FlexibleAdapter.OnItemClickListener,
FlexibleAdapter.OnItemLongClickListener,
@ -92,7 +92,7 @@ class RecentsController(bundle: Bundle? = null) :
var displaySheet: TabbedRecentsOptionsSheet? = null
private var progressItem: ProgressItem? = null
private var presenter = RecentsPresenter(this)
override var presenter = RecentsPresenter(this)
private var snack: Snackbar? = null
private var lastChapterId: Long? = null
private var showingDownloads = false
@ -192,8 +192,6 @@ class RecentsController(bundle: Bundle? = null) :
binding.fakeAppBar.alpha = if (isExpanded) 1f else 0f
}
presenter.onCreate()
if (presenter.recentItems.isNotEmpty()) {
adapter.updateDataSet(presenter.recentItems)
} else {
@ -375,7 +373,6 @@ class RecentsController(bundle: Bundle? = null) :
override fun onDestroy() {
super.onDestroy()
snack?.dismiss()
presenter.onDestroy()
snack = null
}
@ -383,7 +380,6 @@ class RecentsController(bundle: Bundle? = null) :
super.onDestroyView(view)
displaySheet?.dismiss()
displaySheet = null
presenter.cancelScope()
}
fun refresh() = presenter.getRecents()

View File

@ -13,8 +13,9 @@ import eu.kanade.tachiyomi.data.library.LibraryServiceListener
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.base.presenter.BaseCoroutinePresenter
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import eu.kanade.tachiyomi.util.system.launchUI
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
@ -36,9 +37,7 @@ class RecentsPresenter(
val preferences: PreferencesHelper = Injekt.get(),
val downloadManager: DownloadManager = Injekt.get(),
private val db: DatabaseHelper = Injekt.get()
) : DownloadQueue.DownloadListener, LibraryServiceListener {
private var scope = CoroutineScope(Job() + Dispatchers.Default)
) : BaseCoroutinePresenter(), DownloadQueue.DownloadListener, LibraryServiceListener {
private var recentsJob: Job? = null
var recentItems = listOf<RecentMangaItem>()
@ -73,7 +72,8 @@ class RecentsPresenter(
private val isOnFirstPage: Boolean
get() = pageOffset == 0
fun onCreate() {
override fun onCreate() {
super.onCreate()
downloadManager.addListener(this)
LibraryUpdateService.setListener(this)
if (lastRecents != null) {
@ -83,7 +83,6 @@ class RecentsPresenter(
lastRecents = null
}
getRecents()
scope = CoroutineScope(Job() + Dispatchers.Default)
listOf(
preferences.groupChaptersHistory(),
preferences.showReadInAllRecents(),
@ -95,14 +94,14 @@ class RecentsPresenter(
resetOffsets()
getRecents()
}
.launchIn(scope)
.launchIn(presenterScope)
}
}
fun getRecents(updatePageCount: Boolean = false) {
val oldQuery = query
recentsJob?.cancel()
recentsJob = scope.launch {
recentsJob = presenterScope.launch {
runRecents(oldQuery, updatePageCount)
}
}
@ -324,16 +323,13 @@ class RecentsPresenter(
}
}
fun onDestroy() {
override fun onDestroy() {
super.onDestroy()
downloadManager.removeListener(this)
LibraryUpdateService.removeListener(this)
lastRecents = recentItems
}
fun cancelScope() {
scope.cancel()
}
fun toggleGroupRecents(pref: Int, updatePref: Boolean = true) {
if (updatePref) {
preferences.recentsViewType().set(pref)
@ -361,13 +357,11 @@ class RecentsPresenter(
override fun updateDownload(download: Download) {
recentItems.find { it.chapter.id == download.chapter.id }?.download = download
scope.launch(Dispatchers.Main) {
controller?.updateChapterDownload(download)
}
presenterScope.launchUI { controller?.updateChapterDownload(download) }
}
override fun updateDownloads() {
scope.launch {
presenterScope.launch {
setDownloadedChapters(recentItems)
withContext(Dispatchers.Main) {
controller?.showLists(recentItems, true)
@ -377,9 +371,9 @@ class RecentsPresenter(
override fun onUpdateManga(manga: Manga?) {
if (manga == null && !LibraryUpdateService.isRunning()) {
scope.launch(Dispatchers.Main) { controller?.setRefreshing(false) }
presenterScope.launchUI { controller?.setRefreshing(false) }
} else if (manga == null) {
scope.launch(Dispatchers.Main) { controller?.setRefreshing(true) }
presenterScope.launchUI { controller?.setRefreshing(true) }
} else {
getRecents()
}
@ -443,7 +437,7 @@ class RecentsPresenter(
lastRead: Int? = null,
pagesLeft: Int? = null
) {
scope.launch(Dispatchers.IO) {
presenterScope.launch(Dispatchers.IO) {
chapter.apply {
this.read = read
if (!read) {