Migrate more preferences
This commit is contained in:
@@ -28,7 +28,6 @@ import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
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.getOrDefault
|
||||
import eu.kanade.tachiyomi.databinding.ReaderActivityBinding
|
||||
import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity
|
||||
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.AddToLibraryFirst
|
||||
@@ -57,9 +56,12 @@ import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.math.abs
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.sample
|
||||
import nucleus.factory.RequiresPresenter
|
||||
import rx.Observable
|
||||
import rx.Subscription
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
import timber.log.Timber
|
||||
@@ -125,7 +127,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
* Called when the activity is created. Initializes the presenter and configuration.
|
||||
*/
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
setTheme(when (preferences.readerTheme().getOrDefault()) {
|
||||
setTheme(when (preferences.readerTheme().get()) {
|
||||
0 -> R.style.Theme_Reader_Light
|
||||
else -> R.style.Theme_Reader
|
||||
})
|
||||
@@ -317,7 +319,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
private fun setMenuVisibility(visible: Boolean, animate: Boolean = true) {
|
||||
menuVisible = visible
|
||||
if (visible) {
|
||||
if (preferences.fullscreen().getOrDefault()) {
|
||||
if (preferences.fullscreen().get()) {
|
||||
window.showBar()
|
||||
} else {
|
||||
resetDefaultMenuAndBar()
|
||||
@@ -338,11 +340,11 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
binding.readerMenuBottom.startAnimation(bottomAnimation)
|
||||
}
|
||||
|
||||
if (preferences.showPageNumber().getOrDefault()) {
|
||||
if (preferences.showPageNumber().get()) {
|
||||
config?.setPageNumberVisibility(false)
|
||||
}
|
||||
} else {
|
||||
if (preferences.fullscreen().getOrDefault()) {
|
||||
if (preferences.fullscreen().get()) {
|
||||
window.hideBar()
|
||||
} else {
|
||||
resetDefaultMenuAndBar()
|
||||
@@ -361,7 +363,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
binding.readerMenuBottom.startAnimation(bottomAnimation)
|
||||
}
|
||||
|
||||
if (preferences.showPageNumber().getOrDefault()) {
|
||||
if (preferences.showPageNumber().get()) {
|
||||
config?.setPageNumberVisibility(true)
|
||||
}
|
||||
}
|
||||
@@ -605,16 +607,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
*/
|
||||
private val subscriptions = CompositeSubscription()
|
||||
|
||||
/**
|
||||
* Custom brightness subscription.
|
||||
*/
|
||||
private var customBrightnessSubscription: Subscription? = null
|
||||
|
||||
/**
|
||||
* Custom color filter subscription.
|
||||
*/
|
||||
private var customFilterColorSubscription: Subscription? = null
|
||||
|
||||
/**
|
||||
* Initializes the reader subscriptions.
|
||||
*/
|
||||
@@ -627,32 +619,40 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
subscriptions += Observable.merge(initialRotation, rotationUpdates)
|
||||
.subscribe { setOrientation(it) }
|
||||
|
||||
subscriptions += preferences.readerTheme().asObservable()
|
||||
.skip(1) // We only care about updates
|
||||
.subscribe { recreate() }
|
||||
preferences.readerTheme().asFlow()
|
||||
.drop(1) // We only care about updates
|
||||
.onEach { recreate() }
|
||||
.launchIn(scope)
|
||||
|
||||
subscriptions += preferences.showPageNumber().asObservable()
|
||||
.subscribe { setPageNumberVisibility(it) }
|
||||
preferences.showPageNumber().asFlow()
|
||||
.onEach { setPageNumberVisibility(it) }
|
||||
.launchIn(scope)
|
||||
|
||||
subscriptions += preferences.trueColor().asObservable()
|
||||
.subscribe { setTrueColor(it) }
|
||||
preferences.trueColor().asFlow()
|
||||
.onEach { setTrueColor(it) }
|
||||
.launchIn(scope)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
subscriptions += preferences.cutoutShort().asObservable()
|
||||
.subscribe { setCutoutShort(it) }
|
||||
preferences.cutoutShort().asFlow()
|
||||
.onEach { setCutoutShort(it) }
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
subscriptions += preferences.keepScreenOn().asObservable()
|
||||
.subscribe { setKeepScreenOn(it) }
|
||||
preferences.keepScreenOn().asFlow()
|
||||
.onEach { setKeepScreenOn(it) }
|
||||
.launchIn(scope)
|
||||
|
||||
subscriptions += preferences.customBrightness().asObservable()
|
||||
.subscribe { setCustomBrightness(it) }
|
||||
preferences.customBrightness().asFlow()
|
||||
.onEach { setCustomBrightness(it) }
|
||||
.launchIn(scope)
|
||||
|
||||
subscriptions += preferences.colorFilter().asObservable()
|
||||
.subscribe { setColorFilter(it) }
|
||||
preferences.colorFilter().asFlow()
|
||||
.onEach { setColorFilter(it) }
|
||||
.launchIn(scope)
|
||||
|
||||
subscriptions += preferences.colorFilterMode().asObservable()
|
||||
.subscribe { setColorFilter(preferences.colorFilter().getOrDefault()) }
|
||||
preferences.colorFilterMode().asFlow()
|
||||
.onEach { setColorFilter(preferences.colorFilter().get()) }
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -660,8 +660,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
*/
|
||||
fun destroy() {
|
||||
subscriptions.unsubscribe()
|
||||
customBrightnessSubscription = null
|
||||
customFilterColorSubscription = null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -732,13 +730,11 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
*/
|
||||
private fun setCustomBrightness(enabled: Boolean) {
|
||||
if (enabled) {
|
||||
customBrightnessSubscription = preferences.customBrightnessValue().asObservable()
|
||||
.sample(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
||||
.subscribe { setCustomBrightnessValue(it) }
|
||||
|
||||
subscriptions.add(customBrightnessSubscription)
|
||||
preferences.customBrightnessValue().asFlow()
|
||||
.sample(100)
|
||||
.onEach { setCustomBrightnessValue(it) }
|
||||
.launchIn(scope)
|
||||
} else {
|
||||
customBrightnessSubscription?.let { subscriptions.remove(it) }
|
||||
setCustomBrightnessValue(0)
|
||||
}
|
||||
}
|
||||
@@ -748,13 +744,11 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
*/
|
||||
private fun setColorFilter(enabled: Boolean) {
|
||||
if (enabled) {
|
||||
customFilterColorSubscription = preferences.colorFilterValue().asObservable()
|
||||
.sample(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
||||
.subscribe { setColorFilterValue(it) }
|
||||
|
||||
subscriptions.add(customFilterColorSubscription)
|
||||
preferences.colorFilterValue().asFlow()
|
||||
.sample(100)
|
||||
.onEach { setColorFilterValue(it) }
|
||||
.launchIn(scope)
|
||||
} else {
|
||||
customFilterColorSubscription?.let { subscriptions.remove(it) }
|
||||
binding.colorOverlay.gone()
|
||||
}
|
||||
}
|
||||
@@ -794,7 +788,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
||||
*/
|
||||
private fun setColorFilterValue(value: Int) {
|
||||
binding.colorOverlay.visible()
|
||||
binding.colorOverlay.setFilterColor(value, preferences.colorFilterMode().getOrDefault())
|
||||
binding.colorOverlay.setFilterColor(value, preferences.colorFilterMode().get())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,10 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialog
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.data.preference.getOrDefault
|
||||
import eu.kanade.tachiyomi.util.lang.plusAssign
|
||||
import eu.kanade.tachiyomi.util.view.gone
|
||||
import eu.kanade.tachiyomi.util.view.visible
|
||||
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
|
||||
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.math.abs
|
||||
import kotlinx.android.synthetic.main.reader_color_filter.brightness_seekbar
|
||||
import kotlinx.android.synthetic.main.reader_color_filter.color_filter_mode
|
||||
@@ -32,54 +29,41 @@ import kotlinx.android.synthetic.main.reader_color_filter.txt_color_filter_green
|
||||
import kotlinx.android.synthetic.main.reader_color_filter.txt_color_filter_red_value
|
||||
import kotlinx.android.synthetic.main.reader_color_filter_sheet.brightness_overlay
|
||||
import kotlinx.android.synthetic.main.reader_color_filter_sheet.color_overlay
|
||||
import rx.Subscription
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.sample
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
/**
|
||||
* Color filter sheet to toggle custom filter and brightness overlay.
|
||||
*/
|
||||
class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activity) {
|
||||
class ReaderColorFilterSheet(private val activity: ReaderActivity) : BottomSheetDialog(activity) {
|
||||
|
||||
private val preferences by injectLazy<PreferencesHelper>()
|
||||
|
||||
private var sheetBehavior: BottomSheetBehavior<*>? = null
|
||||
|
||||
/**
|
||||
* Subscriptions used for this dialog
|
||||
*/
|
||||
private val subscriptions = CompositeSubscription()
|
||||
|
||||
/**
|
||||
* Subscription used for custom brightness overlay
|
||||
*/
|
||||
private var customBrightnessSubscription: Subscription? = null
|
||||
|
||||
/**
|
||||
* Subscription used for color filter overlay
|
||||
*/
|
||||
private var customFilterColorSubscription: Subscription? = null
|
||||
|
||||
init {
|
||||
val view = activity.layoutInflater.inflate(R.layout.reader_color_filter_sheet, null)
|
||||
setContentView(view)
|
||||
|
||||
sheetBehavior = BottomSheetBehavior.from(view.parent as ViewGroup)
|
||||
|
||||
// Initialize subscriptions.
|
||||
subscriptions += preferences.colorFilter().asObservable()
|
||||
.subscribe { setColorFilter(it, view) }
|
||||
preferences.colorFilter().asFlow()
|
||||
.onEach { setColorFilter(it, view) }
|
||||
.launchIn(activity.scope)
|
||||
|
||||
subscriptions += preferences.colorFilterMode().asObservable()
|
||||
.subscribe { setColorFilter(preferences.colorFilter().getOrDefault(), view) }
|
||||
preferences.colorFilterMode().asFlow()
|
||||
.onEach { setColorFilter(preferences.colorFilter().get(), view) }
|
||||
.launchIn(activity.scope)
|
||||
|
||||
subscriptions += preferences.customBrightness().asObservable()
|
||||
.subscribe { setCustomBrightness(it, view) }
|
||||
preferences.customBrightness().asFlow()
|
||||
.onEach { setCustomBrightness(it, view) }
|
||||
.launchIn(activity.scope)
|
||||
|
||||
// Get color and update values
|
||||
val color = preferences.colorFilterValue().getOrDefault()
|
||||
val brightness = preferences.customBrightnessValue().getOrDefault()
|
||||
val color = preferences.colorFilterValue().get()
|
||||
val brightness = preferences.customBrightnessValue().get()
|
||||
|
||||
val argb = setValues(color, view)
|
||||
|
||||
@@ -94,12 +78,12 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
seekbar_color_filter_blue.progress = argb[3]
|
||||
|
||||
// Set listeners
|
||||
switch_color_filter.isChecked = preferences.colorFilter().getOrDefault()
|
||||
switch_color_filter.isChecked = preferences.colorFilter().get()
|
||||
switch_color_filter.setOnCheckedChangeListener { _, isChecked ->
|
||||
preferences.colorFilter().set(isChecked)
|
||||
}
|
||||
|
||||
custom_brightness.isChecked = preferences.customBrightness().getOrDefault()
|
||||
custom_brightness.isChecked = preferences.customBrightness().get()
|
||||
custom_brightness.setOnCheckedChangeListener { _, isChecked ->
|
||||
preferences.customBrightness().set(isChecked)
|
||||
}
|
||||
@@ -107,7 +91,7 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
color_filter_mode.onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
|
||||
preferences.colorFilterMode().set(position)
|
||||
}
|
||||
color_filter_mode.setSelection(preferences.colorFilterMode().getOrDefault(), false)
|
||||
color_filter_mode.setSelection(preferences.colorFilterMode().get(), false)
|
||||
|
||||
seekbar_color_filter_alpha.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
|
||||
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
|
||||
@@ -156,13 +140,6 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
sheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow()
|
||||
subscriptions.unsubscribe()
|
||||
customBrightnessSubscription = null
|
||||
customFilterColorSubscription = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Set enabled status of seekBars belonging to color filter
|
||||
* @param enabled determines if seekBar gets enabled
|
||||
@@ -196,15 +173,11 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
val blue = getBlueFromColor(color)
|
||||
|
||||
// Initialize values
|
||||
with(view) {
|
||||
txt_color_filter_alpha_value.text = alpha.toString()
|
||||
txt_color_filter_alpha_value.text = alpha.toString()
|
||||
txt_color_filter_red_value.text = red.toString()
|
||||
txt_color_filter_green_value.text = green.toString()
|
||||
txt_color_filter_blue_value.text = blue.toString()
|
||||
|
||||
txt_color_filter_red_value.text = red.toString()
|
||||
|
||||
txt_color_filter_green_value.text = green.toString()
|
||||
|
||||
txt_color_filter_blue_value.text = blue.toString()
|
||||
}
|
||||
return arrayOf(alpha, red, green, blue)
|
||||
}
|
||||
|
||||
@@ -215,13 +188,11 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
*/
|
||||
private fun setCustomBrightness(enabled: Boolean, view: View) {
|
||||
if (enabled) {
|
||||
customBrightnessSubscription = preferences.customBrightnessValue().asObservable()
|
||||
.sample(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
||||
.subscribe { setCustomBrightnessValue(it, view) }
|
||||
|
||||
subscriptions.add(customBrightnessSubscription)
|
||||
preferences.customBrightnessValue().asFlow()
|
||||
.sample(100)
|
||||
.onEach { setCustomBrightnessValue(it, view) }
|
||||
.launchIn(activity.scope)
|
||||
} else {
|
||||
customBrightnessSubscription?.let { subscriptions.remove(it) }
|
||||
setCustomBrightnessValue(0, view, true)
|
||||
}
|
||||
setCustomBrightnessSeekBar(enabled, view)
|
||||
@@ -254,13 +225,11 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
*/
|
||||
private fun setColorFilter(enabled: Boolean, view: View) {
|
||||
if (enabled) {
|
||||
customFilterColorSubscription = preferences.colorFilterValue().asObservable()
|
||||
.sample(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
|
||||
.subscribe { setColorFilterValue(it, view) }
|
||||
|
||||
subscriptions.add(customFilterColorSubscription)
|
||||
preferences.colorFilterValue().asFlow()
|
||||
.sample(100)
|
||||
.onEach { setColorFilterValue(it, view) }
|
||||
.launchIn(activity.scope)
|
||||
} else {
|
||||
customFilterColorSubscription?.let { subscriptions.remove(it) }
|
||||
color_overlay.gone()
|
||||
}
|
||||
setColorFilterSeekBar(enabled, view)
|
||||
@@ -273,7 +242,7 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
*/
|
||||
private fun setColorFilterValue(@ColorInt color: Int, view: View) = with(view) {
|
||||
color_overlay.visible()
|
||||
color_overlay.setFilterColor(color, preferences.colorFilterMode().getOrDefault())
|
||||
color_overlay.setFilterColor(color, preferences.colorFilterMode().get())
|
||||
setValues(color, view)
|
||||
}
|
||||
|
||||
@@ -284,7 +253,7 @@ class ReaderColorFilterSheet(activity: ReaderActivity) : BottomSheetDialog(activ
|
||||
* @param bitShift amounts of bits that gets shifted to receive value
|
||||
*/
|
||||
fun setColorValue(color: Int, mask: Long, bitShift: Int) {
|
||||
val currentColor = preferences.colorFilterValue().getOrDefault()
|
||||
val currentColor = preferences.colorFilterValue().get()
|
||||
val updatedColor = (color shl bitShift) or (currentColor and mask.inv().toInt())
|
||||
preferences.colorFilterValue().set(updatedColor)
|
||||
}
|
||||
|
||||
@@ -6,8 +6,9 @@ import android.widget.CompoundButton
|
||||
import android.widget.Spinner
|
||||
import androidx.annotation.ArrayRes
|
||||
import androidx.core.widget.NestedScrollView
|
||||
import com.f2prateek.rx.preferences.Preference
|
||||
import com.f2prateek.rx.preferences.Preference as RxPreference
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialog
|
||||
import com.tfcporciuncula.flow.Preference
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.data.preference.getOrDefault
|
||||
@@ -121,10 +122,20 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia
|
||||
* Binds a checkbox or switch view with a boolean preference.
|
||||
*/
|
||||
private fun CompoundButton.bindToPreference(pref: Preference<Boolean>) {
|
||||
isChecked = pref.getOrDefault()
|
||||
isChecked = pref.get()
|
||||
setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds a spinner to an int preference with an optional offset for the value.
|
||||
*/
|
||||
private fun Spinner.bindToPreference(pref: RxPreference<Int>, offset: Int = 0) {
|
||||
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
|
||||
pref.set(position + offset)
|
||||
}
|
||||
setSelection(pref.getOrDefault() - offset, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds a spinner to an int preference with an optional offset for the value.
|
||||
*/
|
||||
@@ -132,7 +143,7 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia
|
||||
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
|
||||
pref.set(position + offset)
|
||||
}
|
||||
setSelection(pref.getOrDefault() - offset, false)
|
||||
setSelection(pref.get() - offset, false)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,8 +154,8 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia
|
||||
private fun Spinner.bindToIntPreference(pref: Preference<Int>, @ArrayRes intValuesResource: Int) {
|
||||
val intValues = resources.getStringArray(intValuesResource).map { it.toIntOrNull() }
|
||||
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
|
||||
pref.set(intValues[position])
|
||||
pref.set(intValues[position]!!)
|
||||
}
|
||||
setSelection(intValues.indexOf(pref.getOrDefault()), false)
|
||||
setSelection(intValues.indexOf(pref.get()), false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,20 +2,24 @@ package eu.kanade.tachiyomi.ui.reader.viewer
|
||||
|
||||
import com.tfcporciuncula.flow.Preference
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.util.lang.addTo
|
||||
import eu.kanade.tachiyomi.util.lang.launchInUI
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
|
||||
/**
|
||||
* Common configuration for all viewers.
|
||||
*/
|
||||
abstract class ViewerConfig(preferences: PreferencesHelper) {
|
||||
|
||||
private val subscriptions = CompositeSubscription()
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
|
||||
var imagePropertyChangedListener: (() -> Unit)? = null
|
||||
|
||||
var tappingEnabled = true
|
||||
var longTapEnabled = true
|
||||
var doubleTapAnimDuration = 500
|
||||
var volumeKeysEnabled = false
|
||||
var volumeKeysInverted = false
|
||||
var alwaysShowChapterTransition = true
|
||||
@@ -27,6 +31,9 @@ abstract class ViewerConfig(preferences: PreferencesHelper) {
|
||||
preferences.readWithLongTap()
|
||||
.register({ longTapEnabled = it })
|
||||
|
||||
preferences.doubleTapAnimSpeed()
|
||||
.register({ doubleTapAnimDuration = it })
|
||||
|
||||
preferences.readWithVolumeKeys()
|
||||
.register({ volumeKeysEnabled = it })
|
||||
|
||||
@@ -37,31 +44,15 @@ abstract class ViewerConfig(preferences: PreferencesHelper) {
|
||||
.register({ alwaysShowChapterTransition = it })
|
||||
}
|
||||
|
||||
fun unsubscribe() {
|
||||
subscriptions.unsubscribe()
|
||||
}
|
||||
|
||||
fun <T> com.f2prateek.rx.preferences.Preference<T>.register(
|
||||
valueAssignment: (T) -> Unit,
|
||||
onChanged: (T) -> Unit = {}
|
||||
) {
|
||||
asObservable()
|
||||
.doOnNext(valueAssignment)
|
||||
.skip(1)
|
||||
.distinctUntilChanged()
|
||||
.doOnNext(onChanged)
|
||||
.subscribe()
|
||||
.addTo(subscriptions)
|
||||
}
|
||||
|
||||
fun <T> Preference<T>.register(
|
||||
valueAssignment: (T) -> Unit,
|
||||
onChanged: (T) -> Unit = {}
|
||||
) {
|
||||
asFlow()
|
||||
.onEach { valueAssignment(it) }
|
||||
.distinctUntilChanged()
|
||||
.onEach { onChanged(it) }
|
||||
.launchInUI()
|
||||
.onEach {
|
||||
valueAssignment(it)
|
||||
onChanged(it)
|
||||
}
|
||||
.launchIn(scope)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,24 +23,18 @@ class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelpe
|
||||
var imageCropBorders = false
|
||||
private set
|
||||
|
||||
var doubleTapAnimDuration = 500
|
||||
private set
|
||||
|
||||
init {
|
||||
preferences.pageTransitions()
|
||||
.register({ usePageTransitions = it })
|
||||
.register({ usePageTransitions = it })
|
||||
|
||||
preferences.imageScaleType()
|
||||
.register({ imageScaleType = it }, { imagePropertyChangedListener?.invoke() })
|
||||
.register({ imageScaleType = it }, { imagePropertyChangedListener?.invoke() })
|
||||
|
||||
preferences.zoomStart()
|
||||
.register({ zoomTypeFromPreference(it) }, { imagePropertyChangedListener?.invoke() })
|
||||
.register({ zoomTypeFromPreference(it) }, { imagePropertyChangedListener?.invoke() })
|
||||
|
||||
preferences.cropBorders()
|
||||
.register({ imageCropBorders = it }, { imagePropertyChangedListener?.invoke() })
|
||||
|
||||
preferences.doubleTapAnimSpeed()
|
||||
.register({ doubleTapAnimDuration = it })
|
||||
.register({ imageCropBorders = it }, { imagePropertyChangedListener?.invoke() })
|
||||
}
|
||||
|
||||
private fun zoomTypeFromPreference(value: Int) {
|
||||
|
||||
@@ -115,14 +115,6 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
|
||||
return pager
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys this viewer. Called when leaving the reader or swapping viewers.
|
||||
*/
|
||||
override fun destroy() {
|
||||
super.destroy()
|
||||
config.unsubscribe()
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active
|
||||
*/
|
||||
|
||||
@@ -13,34 +13,13 @@ class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) : ViewerConfi
|
||||
var imageCropBorders = false
|
||||
private set
|
||||
|
||||
var doubleTapAnimDuration = 500
|
||||
private set
|
||||
|
||||
var sidePadding = 0
|
||||
private set
|
||||
|
||||
init {
|
||||
preferences.readWithTapping()
|
||||
.register({ tappingEnabled = it })
|
||||
|
||||
preferences.readWithLongTap()
|
||||
.register({ longTapEnabled = it })
|
||||
|
||||
preferences.cropBordersWebtoon()
|
||||
.register({ imageCropBorders = it }, { imagePropertyChangedListener?.invoke() })
|
||||
|
||||
preferences.doubleTapAnimSpeed()
|
||||
.register({ doubleTapAnimDuration = it })
|
||||
|
||||
preferences.readWithVolumeKeys()
|
||||
.register({ volumeKeysEnabled = it })
|
||||
|
||||
preferences.readWithVolumeKeysInverted()
|
||||
.register({ volumeKeysInverted = it })
|
||||
|
||||
preferences.alwaysShowChapterTransition()
|
||||
.register({ alwaysShowChapterTransition = it })
|
||||
|
||||
preferences.webtoonSidePadding()
|
||||
.register({ sidePadding = it }, { imagePropertyChangedListener?.invoke() })
|
||||
}
|
||||
|
||||
@@ -155,7 +155,6 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
|
||||
*/
|
||||
override fun destroy() {
|
||||
super.destroy()
|
||||
config.unsubscribe()
|
||||
subscriptions.unsubscribe()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user