Fix zoom changing

This commit is contained in:
Koitharu
2020-12-20 16:49:51 +02:00
parent 6e1fdcb19a
commit a1545fd889
5 changed files with 13 additions and 68 deletions

View File

@@ -72,10 +72,7 @@ class ReaderViewModel(
.flowOn(Dispatchers.IO)
.asLiveData(viewModelScope.coroutineContext)
val onZoomChanged = settings.observe()
.filter { it == AppSettings.KEY_ZOOM_MODE }
.flowOn(Dispatchers.IO)
.asLiveEvent(viewModelScope.coroutineContext)
val onZoomChanged = SingleLiveEvent<Unit>()
init {
loadingJob = launchLoadingJob(Dispatchers.Default) {
@@ -113,6 +110,8 @@ class ReaderViewModel(
shortcutsRepository.updateShortcuts()
}
}
subscribeToSettings()
}
fun switchMode(newMode: ReaderMode) {
@@ -255,6 +254,13 @@ class ReaderViewModel(
}
}
private fun subscribeToSettings() {
settings.observe()
.filter { it == AppSettings.KEY_ZOOM_MODE }
.onEach { onZoomChanged.postCall(Unit) }
.launchIn(viewModelScope + Dispatchers.IO)
}
private companion object : KoinComponent {
const val BOUNDS_PAGE_OFFSET = 2

View File

@@ -42,8 +42,7 @@ class ReversedReaderFragment : BaseReader<FragmentReaderStandardBinding>() {
}
}
viewModel.onZoomChanged.observe(viewLifecycleOwner) {
pagerAdapter = ReversedPagesAdapter(loader, get())
binding.pager.swapAdapter(pagerAdapter)
pagerAdapter?.notifyDataSetChanged()
}
}

View File

@@ -42,8 +42,7 @@ class PagerReaderFragment : BaseReader<FragmentReaderStandardBinding>() {
}
}
viewModel.onZoomChanged.observe(viewLifecycleOwner) {
pagesAdapter = PagesAdapter(loader, get())
binding.pager.swapAdapter(pagesAdapter)
pagesAdapter?.notifyDataSetChanged()
}
}

View File

@@ -1,54 +0,0 @@
package org.koitharu.kotatsu.utils
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.CoroutineContext
class FlowLiveEvent<T>(
private val source: Flow<T>,
private val context: CoroutineContext
) : LiveData<T>() {
private val scope = CoroutineScope(
Dispatchers.Main.immediate + context + SupervisorJob(context[Job])
)
private val pending = AtomicBoolean(false)
private var collectJob: Job? = null
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
super.observe(owner) {
if (pending.compareAndSet(true, false)) {
observer.onChanged(it)
}
}
}
override fun onActive() {
super.onActive()
if (collectJob == null) {
collectJob = source.onEach {
setValue(it)
}.launchIn(scope)
}
}
override fun onInactive() {
collectJob?.cancel()
collectJob = null
super.onInactive()
}
override fun setValue(value: T) {
pending.set(true)
super.setValue(value)
}
}

View File

@@ -7,7 +7,6 @@ import androidx.lifecycle.liveData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import org.koitharu.kotatsu.utils.BufferedObserver
import org.koitharu.kotatsu.utils.FlowLiveEvent
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
@@ -37,8 +36,4 @@ fun <T> Flow<T>.asLiveData(
collect {
emit(it)
}
}
fun <T> Flow<T>.asLiveEvent(
context: CoroutineContext = EmptyCoroutineContext
): LiveData<T> = FlowLiveEvent(this, context)
}