Fix BrowseSourceController losing scroll position when navigating back

This commit is contained in:
arkon 2020-04-27 16:21:23 -04:00
parent 850654dccc
commit 2a41e4ce68
3 changed files with 8 additions and 18 deletions

View File

@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.anilist.Anilist import eu.kanade.tachiyomi.data.track.anilist.Anilist
import eu.kanade.tachiyomi.util.lang.startWithCurrentValue
import java.io.File import java.io.File
import java.text.DateFormat import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -29,7 +28,6 @@ fun <T> RxPreference<T>.getOrDefault(): T = get() ?: defaultValue()!!
fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> { fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> {
block(get()) block(get())
return asFlow() return asFlow()
.startWithCurrentValue { get() }
.onEach { block(it) } .onEach { block(it) }
} }

View File

@ -48,6 +48,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -190,12 +191,12 @@ open class BrowseSourceController(bundle: Bundle) :
} else { } else {
(binding.catalogueView.inflate(R.layout.source_recycler_autofit) as AutofitRecyclerView).apply { (binding.catalogueView.inflate(R.layout.source_recycler_autofit) as AutofitRecyclerView).apply {
numColumnsScope = CoroutineScope(Job() + Dispatchers.Main) numColumnsScope = CoroutineScope(Job() + Dispatchers.Main)
getColumnsPreferenceForCurrentOrientation().asImmediateFlow { getColumnsPreferenceForCurrentOrientation().asImmediateFlow { spanCount = it }
spanCount = it .drop(1)
.onEach {
// Set the adapter again to recalculate the covers height // Set the adapter again to recalculate the covers height
adapter = this@BrowseSourceController.adapter adapter = this@BrowseSourceController.adapter
} }
.launchIn(numColumnsScope!!) .launchIn(numColumnsScope!!)
(layoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { (layoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
@ -452,7 +453,7 @@ open class BrowseSourceController(bundle: Bundle) :
* *
* @return the preference. * @return the preference.
*/ */
fun getColumnsPreferenceForCurrentOrientation(): Preference<Int> { private fun getColumnsPreferenceForCurrentOrientation(): Preference<Int> {
return if (resources?.configuration?.orientation == Configuration.ORIENTATION_PORTRAIT) { return if (resources?.configuration?.orientation == Configuration.ORIENTATION_PORTRAIT) {
preferences.portraitColumns() preferences.portraitColumns()
} else { } else {

View File

@ -6,8 +6,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
fun launchUI(block: suspend CoroutineScope.() -> Unit): Job = fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
@ -19,10 +17,3 @@ fun launchIO(block: suspend CoroutineScope.() -> Unit): Job =
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
fun launchNow(block: suspend CoroutineScope.() -> Unit): Job = fun launchNow(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED, block) GlobalScope.launch(Dispatchers.Main, CoroutineStart.UNDISPATCHED, block)
@OptIn(ExperimentalCoroutinesApi::class)
fun <T> Flow<T>.startWithCurrentValue(block: () -> T?): Flow<T> {
return onStart {
block()?.let { emit(it) }
}
}