From 04a178e7dac15b20e763d24b63f471c20d0717d0 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 22 Apr 2020 21:46:02 -0400 Subject: [PATCH] Move common view configs to parent class --- .../ui/reader/viewer/ViewerConfig.kt | 47 +++++++++++++++- .../ui/reader/viewer/pager/PagerConfig.kt | 54 +------------------ .../ui/reader/viewer/webtoon/WebtoonConfig.kt | 53 +++--------------- 3 files changed, 55 insertions(+), 99 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt index 926efeb053..b645432e94 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt @@ -1,14 +1,59 @@ 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.flow.onEach +import rx.subscriptions.CompositeSubscription -abstract class ViewerConfig { +abstract class ViewerConfig(preferences: PreferencesHelper) { + + private val subscriptions = CompositeSubscription() var imagePropertyChangedListener: (() -> Unit)? = null + var tappingEnabled = true + var longTapEnabled = true + var volumeKeysEnabled = false + var volumeKeysInverted = false + var alwaysShowChapterTransition = true + + init { + preferences.readWithTapping() + .register({ tappingEnabled = it }) + + preferences.readWithLongTap() + .register({ longTapEnabled = it }) + + preferences.readWithVolumeKeys() + .register({ volumeKeysEnabled = it }) + + preferences.readWithVolumeKeysInverted() + .register({ volumeKeysInverted = it }) + + preferences.alwaysShowChapterTransition() + .register({ alwaysShowChapterTransition = it }) + } + + fun unsubscribe() { + subscriptions.unsubscribe() + } + + fun com.f2prateek.rx.preferences.Preference.register( + valueAssignment: (T) -> Unit, + onChanged: (T) -> Unit = {} + ) { + asObservable() + .doOnNext(valueAssignment) + .skip(1) + .distinctUntilChanged() + .doOnNext(onChanged) + .subscribe() + .addTo(subscriptions) + } + fun Preference.register( valueAssignment: (T) -> Unit, onChanged: (T) -> Unit = {} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt index 14e458f7c4..552b76af2f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt @@ -1,32 +1,15 @@ package eu.kanade.tachiyomi.ui.reader.viewer.pager -import com.f2prateek.rx.preferences.Preference import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.reader.viewer.ViewerConfig -import eu.kanade.tachiyomi.util.lang.addTo -import rx.subscriptions.CompositeSubscription import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get /** * Configuration used by pager viewers. */ - - private val subscriptions = CompositeSubscription() class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelper = Injekt.get()) : - ViewerConfig() { - - var tappingEnabled = true - private set - - var longTapEnabled = true - private set - - var volumeKeysEnabled = false - private set - - var volumeKeysInverted = false - private set + ViewerConfig(preferences) { var usePageTransitions = false private set @@ -43,16 +26,7 @@ class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelpe var doubleTapAnimDuration = 500 private set - var alwaysShowChapterTransition = true - private set - init { - preferences.readWithTapping() - .register({ tappingEnabled = it }) - - preferences.readWithLongTap() - .register({ longTapEnabled = it }) - preferences.pageTransitions() .register({ usePageTransitions = it }) @@ -67,32 +41,6 @@ class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelpe preferences.doubleTapAnimSpeed() .register({ doubleTapAnimDuration = it }) - - preferences.readWithVolumeKeys() - .register({ volumeKeysEnabled = it }) - - preferences.readWithVolumeKeysInverted() - .register({ volumeKeysInverted = it }) - - preferences.alwaysShowChapterTransition() - .register({ alwaysShowChapterTransition = it }) - } - - fun unsubscribe() { - subscriptions.unsubscribe() - } - - private fun Preference.register( - valueAssignment: (T) -> Unit, - onChanged: (T) -> Unit = {} - ) { - asObservable() - .doOnNext(valueAssignment) - .skip(1) - .distinctUntilChanged() - .doOnNext(onChanged) - .subscribe() - .addTo(subscriptions) } private fun zoomTypeFromPreference(value: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt index 471b817db8..569d52605b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt @@ -1,31 +1,14 @@ package eu.kanade.tachiyomi.ui.reader.viewer.webtoon -import com.f2prateek.rx.preferences.Preference import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.reader.viewer.ViewerConfig -import eu.kanade.tachiyomi.util.lang.addTo -import rx.subscriptions.CompositeSubscription import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get /** * Configuration used by webtoon viewers. */ - - private val subscriptions = CompositeSubscription() -class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) : ViewerConfig() { - - var tappingEnabled = true - private set - - var longTapEnabled = true - private set - - var volumeKeysEnabled = false - private set - - var volumeKeysInverted = false - private set +class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) : ViewerConfig(preferences) { var imageCropBorders = false private set @@ -33,52 +16,32 @@ class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) : ViewerConfi var doubleTapAnimDuration = 500 private set - var alwaysShowChapterTransition = true - private set - var sidePadding = 0 private set init { preferences.readWithTapping() - .register({ tappingEnabled = it }) + .register({ tappingEnabled = it }) preferences.readWithLongTap() - .register({ longTapEnabled = it }) + .register({ longTapEnabled = it }) preferences.cropBordersWebtoon() - .register({ imageCropBorders = it }, { imagePropertyChangedListener?.invoke() }) + .register({ imageCropBorders = it }, { imagePropertyChangedListener?.invoke() }) preferences.doubleTapAnimSpeed() - .register({ doubleTapAnimDuration = it }) + .register({ doubleTapAnimDuration = it }) preferences.readWithVolumeKeys() - .register({ volumeKeysEnabled = it }) + .register({ volumeKeysEnabled = it }) preferences.readWithVolumeKeysInverted() - .register({ volumeKeysInverted = it }) + .register({ volumeKeysInverted = it }) preferences.alwaysShowChapterTransition() - .register({ alwaysShowChapterTransition = it }) + .register({ alwaysShowChapterTransition = it }) preferences.webtoonSidePadding() .register({ sidePadding = it }, { imagePropertyChangedListener?.invoke() }) } - - fun unsubscribe() { - subscriptions.unsubscribe() - } - - private fun Preference.register( - valueAssignment: (T) -> Unit, - onChanged: (T) -> Unit = {} - ) { - asObservable() - .doOnNext(valueAssignment) - .skip(1) - .distinctUntilChanged() - .doOnNext(onChanged) - .subscribe() - .addTo(subscriptions) - } }