|
|
|
@ -1,33 +1,39 @@
|
|
|
|
package org.koitharu.kotatsu.reader.ui
|
|
|
|
package org.koitharu.kotatsu.reader.ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.view.KeyEvent
|
|
|
|
import android.view.KeyEvent
|
|
|
|
import android.view.SoundEffectConstants
|
|
|
|
import android.view.SoundEffectConstants
|
|
|
|
import android.view.View
|
|
|
|
import android.view.View
|
|
|
|
import androidx.lifecycle.LifecycleCoroutineScope
|
|
|
|
import androidx.lifecycle.DefaultLifecycleObserver
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
import kotlinx.coroutines.flow.flowOn
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ReaderMode
|
|
|
|
import org.koitharu.kotatsu.utils.GridTouchHelper
|
|
|
|
import org.koitharu.kotatsu.utils.GridTouchHelper
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderControlDelegate(
|
|
|
|
class ReaderControlDelegate(
|
|
|
|
scope: LifecycleCoroutineScope,
|
|
|
|
private val settings: AppSettings,
|
|
|
|
settings: AppSettings,
|
|
|
|
private val listener: OnInteractionListener,
|
|
|
|
private val listener: OnInteractionListener
|
|
|
|
owner: LifecycleOwner,
|
|
|
|
) {
|
|
|
|
) : DefaultLifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
|
|
|
|
|
|
|
|
private var isTapSwitchEnabled: Boolean = true
|
|
|
|
private var isTapSwitchEnabled: Boolean = true
|
|
|
|
private var isVolumeKeysSwitchEnabled: Boolean = false
|
|
|
|
private var isVolumeKeysSwitchEnabled: Boolean = false
|
|
|
|
|
|
|
|
private var isReaderTapsAdaptive: Boolean = true
|
|
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
init {
|
|
|
|
settings.observeAsFlow(AppSettings.KEY_READER_SWITCHERS) { readerPageSwitch }
|
|
|
|
owner.lifecycle.addObserver(this)
|
|
|
|
.flowOn(Dispatchers.Default)
|
|
|
|
settings.subscribe(this)
|
|
|
|
.onEach {
|
|
|
|
updateSettings()
|
|
|
|
isTapSwitchEnabled = AppSettings.PAGE_SWITCH_TAPS in it
|
|
|
|
}
|
|
|
|
isVolumeKeysSwitchEnabled = AppSettings.PAGE_SWITCH_VOLUME_KEYS in it
|
|
|
|
|
|
|
|
}.launchIn(scope)
|
|
|
|
override fun onDestroy(owner: LifecycleOwner) {
|
|
|
|
|
|
|
|
settings.unsubscribe(this)
|
|
|
|
|
|
|
|
owner.lifecycle.removeObserver(this)
|
|
|
|
|
|
|
|
super.onDestroy(owner)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
|
|
|
|
|
|
updateSettings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun onGridTouch(area: Int, view: View) {
|
|
|
|
fun onGridTouch(area: Int, view: View) {
|
|
|
|
@ -41,7 +47,7 @@ class ReaderControlDelegate(
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_UP)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_UP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GridTouchHelper.AREA_LEFT -> if (isTapSwitchEnabled) {
|
|
|
|
GridTouchHelper.AREA_LEFT -> if (isTapSwitchEnabled) {
|
|
|
|
listener.switchPageBy(-1)
|
|
|
|
listener.switchPageBy(if (isReaderTapsReversed()) 1 else -1)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GridTouchHelper.AREA_BOTTOM -> if (isTapSwitchEnabled) {
|
|
|
|
GridTouchHelper.AREA_BOTTOM -> if (isTapSwitchEnabled) {
|
|
|
|
@ -49,7 +55,7 @@ class ReaderControlDelegate(
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GridTouchHelper.AREA_RIGHT -> if (isTapSwitchEnabled) {
|
|
|
|
GridTouchHelper.AREA_RIGHT -> if (isTapSwitchEnabled) {
|
|
|
|
listener.switchPageBy(1)
|
|
|
|
listener.switchPageBy(if (isReaderTapsReversed()) -1 else 1)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT)
|
|
|
|
view.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -72,17 +78,25 @@ class ReaderControlDelegate(
|
|
|
|
KeyEvent.KEYCODE_PAGE_DOWN,
|
|
|
|
KeyEvent.KEYCODE_PAGE_DOWN,
|
|
|
|
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN,
|
|
|
|
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN,
|
|
|
|
KeyEvent.KEYCODE_DPAD_DOWN,
|
|
|
|
KeyEvent.KEYCODE_DPAD_DOWN,
|
|
|
|
KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
|
|
|
-> {
|
|
|
|
listener.switchPageBy(1)
|
|
|
|
listener.switchPageBy(1)
|
|
|
|
true
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_RIGHT -> {
|
|
|
|
|
|
|
|
listener.switchPageBy(if (isReaderTapsReversed()) -1 else 1)
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
KeyEvent.KEYCODE_PAGE_UP,
|
|
|
|
KeyEvent.KEYCODE_PAGE_UP,
|
|
|
|
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP,
|
|
|
|
KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP,
|
|
|
|
KeyEvent.KEYCODE_DPAD_UP,
|
|
|
|
KeyEvent.KEYCODE_DPAD_UP,
|
|
|
|
KeyEvent.KEYCODE_DPAD_LEFT -> {
|
|
|
|
-> {
|
|
|
|
listener.switchPageBy(-1)
|
|
|
|
listener.switchPageBy(-1)
|
|
|
|
true
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyEvent.KEYCODE_DPAD_LEFT -> {
|
|
|
|
|
|
|
|
listener.switchPageBy(if (isReaderTapsReversed()) 1 else -1)
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
KeyEvent.KEYCODE_DPAD_CENTER -> {
|
|
|
|
KeyEvent.KEYCODE_DPAD_CENTER -> {
|
|
|
|
listener.toggleUiVisibility()
|
|
|
|
listener.toggleUiVisibility()
|
|
|
|
true
|
|
|
|
true
|
|
|
|
@ -97,8 +111,21 @@ class ReaderControlDelegate(
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun updateSettings() {
|
|
|
|
|
|
|
|
val switch = settings.readerPageSwitch
|
|
|
|
|
|
|
|
isTapSwitchEnabled = AppSettings.PAGE_SWITCH_TAPS in switch
|
|
|
|
|
|
|
|
isVolumeKeysSwitchEnabled = AppSettings.PAGE_SWITCH_VOLUME_KEYS in switch
|
|
|
|
|
|
|
|
isReaderTapsAdaptive = settings.isReaderTapsAdaptive
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun isReaderTapsReversed(): Boolean {
|
|
|
|
|
|
|
|
return isReaderTapsAdaptive && listener.readerMode == ReaderMode.REVERSED
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface OnInteractionListener {
|
|
|
|
interface OnInteractionListener {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val readerMode: ReaderMode?
|
|
|
|
|
|
|
|
|
|
|
|
fun switchPageBy(delta: Int)
|
|
|
|
fun switchPageBy(delta: Int)
|
|
|
|
|
|
|
|
|
|
|
|
fun toggleUiVisibility()
|
|
|
|
fun toggleUiVisibility()
|
|
|
|
|