Fixes to CoroutineScope

This includes removing what I thought was a good helper method for a list of preferences

Also using a base scope for controllers (others will have to be updated later)
This commit is contained in:
Jays2Kings 2021-04-26 05:29:12 -04:00
parent 8753d188d1
commit 0b4019a1cb
9 changed files with 50 additions and 56 deletions

View File

@ -43,17 +43,6 @@ fun <T> com.tfcporciuncula.flow.Preference<T>.asImmediateFlowIn(scope: Coroutine
.launchIn(scope)
}
fun <T> Collection<com.tfcporciuncula.flow.Preference<out T>>.asFlowsIn(scope: CoroutineScope, dropFirst: Boolean = false, block: () -> Unit): Collection<Job> {
return map { pref ->
pref.asFlow()
.apply {
if (dropFirst) drop(1)
}
.onEach { block() }
.launchIn(scope)
}
}
fun com.tfcporciuncula.flow.Preference<Boolean>.toggle() = set(!get())
private class DateFormatConverter : Preference.Adapter<DateFormat> {

View File

@ -13,12 +13,16 @@ import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.ControllerChangeType
import com.bluelinelabs.conductor.RestoreViewOnCreateController
import eu.kanade.tachiyomi.util.view.removeQueryListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import timber.log.Timber
abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
RestoreViewOnCreateController(bundle) {
lateinit var binding: VB
lateinit var viewScope: CoroutineScope
val isBindingInitialized get() = this::binding.isInitialized
init {
@ -29,6 +33,7 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
}
override fun preCreateView(controller: Controller) {
viewScope = MainScope()
Timber.d("Create view for ${controller.instance()}")
}
@ -41,6 +46,7 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
}
override fun preDestroyView(controller: Controller, view: View) {
viewScope.cancel()
Timber.d("Destroy view for ${controller.instance()}")
}
}

View File

@ -46,7 +46,6 @@ import eu.kanade.tachiyomi.util.view.openInBrowser
import eu.kanade.tachiyomi.util.view.scrollViewWith
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.widget.preference.ListMatPreference
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import uy.kohesive.injekt.Injekt
@ -64,7 +63,6 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
private val preferences: PreferencesHelper = Injekt.get()
private val viewScope = MainScope()
init {
setHasOptionsMenu(true)
}

View File

@ -28,7 +28,6 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.GestureDetectorCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
@ -53,7 +52,6 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asFlowsIn
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.databinding.LibraryControllerBinding
import eu.kanade.tachiyomi.source.LocalSource
@ -99,11 +97,11 @@ import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.updatePaddingRelative
import eu.kanade.tachiyomi.util.view.withFadeTransaction
import eu.kanade.tachiyomi.widget.EndAnimatorListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Locale
@ -150,8 +148,6 @@ class LibraryController(
var singleCategory: Boolean = false
private set
val scope = CoroutineScope(Job() + Dispatchers.Main)
/**
* Library search query.
*/
@ -835,9 +831,14 @@ class LibraryController(
preferences.uniformGrid(),
preferences.gridSize(),
preferences.unreadBadgeType()
).asFlowsIn(scope, true) {
).forEach {
it.asFlow()
.drop(1)
.onEach {
reattachAdapter()
}
.launchIn(viewScope)
}
}
override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) {
@ -885,7 +886,6 @@ class LibraryController(
override fun onDestroy() {
if (::presenter.isInitialized) presenter.onDestroy()
scope.cancel()
super.onDestroy()
}
@ -897,6 +897,7 @@ class LibraryController(
}
displaySheet?.dismiss()
displaySheet = null
presenter.cancelScope()
super.onDestroyView(view)
}
@ -913,11 +914,6 @@ class LibraryController(
)
}
adapter.setItems(mangaMap)
if (binding.libraryGridRecycler.recycler.itemAnimator == null) {
binding.libraryGridRecycler.recycler.post {
binding.libraryGridRecycler.recycler.itemAnimator = DefaultItemAnimator()
}
}
singleCategory = presenter.categories.size <= 1
showDropdown()
binding.progress.isVisible = false
@ -951,9 +947,7 @@ class LibraryController(
listOf(activityBinding?.toolbar, binding.headerTitle).forEach {
it?.setOnClickListener {
val recycler = binding.libraryGridRecycler.recycler
if (singleCategory) {
recycler.scrollToPosition(0)
} else {
if (!singleCategory) {
showCategories(recycler.translationY == 0f)
}
}

View File

@ -33,6 +33,7 @@ import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
@ -86,6 +87,10 @@ class LibraryPresenter(
val libraryIsGrouped
get() = groupType != UNGROUPED
fun cancelScope() {
scope.cancel()
}
/** Save the current list to speed up loading later */
fun onDestroy() {
lastLibraryItems = libraryItems
@ -105,6 +110,7 @@ class LibraryPresenter(
if (categories.isEmpty()) {
categories = lastCategories ?: db.getCategories().executeAsBlocking().toMutableList()
}
scope = CoroutineScope(Job() + Dispatchers.Default)
scope.launch {
val library = withContext(Dispatchers.IO) { getLibraryFromDB() }
library.apply {

View File

@ -181,7 +181,7 @@ class FilterBottomSheet @JvmOverloads constructor(context: Context, attrs: Attri
filterOrder = it
clearFilters()
}
.launchIn(controller.scope)
.launchIn(controller.viewScope)
}
private fun stateChanged(state: Int) {

View File

@ -37,7 +37,6 @@ 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.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asFlowsIn
import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn
import eu.kanade.tachiyomi.data.preference.toggle
import eu.kanade.tachiyomi.databinding.ReaderActivityBinding
@ -637,7 +636,11 @@ class ReaderActivity :
}
listOf(preferences.cropBorders(), preferences.cropBordersWebtoon())
.asFlowsIn(scope) { updateCropBordersShortcut() }
.forEach { pref ->
pref.asFlow()
.onEach { updateCropBordersShortcut() }
.launchIn(scope)
}
preferences.rotation().asImmediateFlowIn(scope) { updateRotationShortcut(it) }
binding.chaptersSheet.shiftPageButton.setOnClickListener {

View File

@ -54,10 +54,6 @@ import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.updateLayoutParams
import eu.kanade.tachiyomi.util.view.updatePaddingRelative
import eu.kanade.tachiyomi.util.view.withFadeTransaction
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import java.util.Locale
import kotlin.math.abs
import kotlin.math.max
@ -89,12 +85,10 @@ class RecentsController(bundle: Bundle? = null) :
presenter.toggleGroupRecents(viewType, false)
}
private val adapterScope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
/**
* Adapter containing the recent manga.
*/
private var adapter = RecentMangaAdapter(this)
private lateinit var adapter: RecentMangaAdapter
var displaySheet: TabbedRecentsOptionsSheet? = null
private var progressItem: ProgressItem? = null
@ -383,14 +377,13 @@ class RecentsController(bundle: Bundle? = null) :
snack?.dismiss()
presenter.onDestroy()
snack = null
adapterScope.cancel()
presenter.cancelScope()
}
override fun onDestroyView(view: View) {
super.onDestroyView(view)
displaySheet?.dismiss()
displaySheet = null
presenter.cancelScope()
}
fun refresh() = presenter.getRecents()
@ -497,7 +490,7 @@ class RecentsController(bundle: Bundle? = null) :
override fun getViewType(): Int = presenter.viewType
override fun scope() = adapterScope
override fun scope() = viewScope
override fun onItemClick(view: View?, position: Int): Boolean {
val item = adapter.getItem(position) ?: return false

View File

@ -12,13 +12,15 @@ import eu.kanade.tachiyomi.data.download.model.DownloadQueue
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.data.preference.asFlowsIn
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
@ -71,17 +73,6 @@ class RecentsPresenter(
private val isOnFirstPage: Boolean
get() = pageOffset == 0
init {
listOf(
preferences.groupChaptersHistory(),
preferences.showReadInAllRecents(),
preferences.groupChaptersUpdates()
).asFlowsIn(scope, true) {
resetOffsets()
getRecents()
}
}
fun onCreate() {
downloadManager.addListener(this)
LibraryUpdateService.setListener(this)
@ -92,6 +83,20 @@ class RecentsPresenter(
lastRecents = null
}
getRecents()
scope = CoroutineScope(Job() + Dispatchers.Default)
listOf(
preferences.groupChaptersHistory(),
preferences.showReadInAllRecents(),
preferences.groupChaptersUpdates()
).forEach {
it.asFlow()
.drop(1)
.onEach {
resetOffsets()
getRecents()
}
.launchIn(scope)
}
}
fun getRecents(updatePageCount: Boolean = false) {