@ -1,33 +1,39 @@
package org.koitharu.kotatsu.reader.ui
import android.content.SharedPreferences
import android.view.KeyEvent
import android.view.SoundEffectConstants
import android.view.View
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
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
class ReaderControlDelegate (
scope : LifecycleCoroutineScope ,
settings : AppSettings ,
private val settings : AppSettings ,
private val listener : OnInteractionListener ,
) {
owner : LifecycleOwner ,
) : DefaultLifecycleObserver , SharedPreferences . OnSharedPreferenceChangeListener {
private var isTapSwitchEnabled : Boolean = true
private var isVolumeKeysSwitchEnabled : Boolean = false
private var isReaderTapsAdaptive : Boolean = true
init {
settings . observeAsFlow ( AppSettings . KEY _READER _SWITCHERS ) { readerPageSwitch }
. flowOn ( Dispatchers . Default )
. onEach {
isTapSwitchEnabled = AppSettings . PAGE _SWITCH _TAPS in it
isVolumeKeysSwitchEnabled = AppSettings . PAGE _SWITCH _VOLUME _KEYS in it
} . launchIn ( scope )
owner . lifecycle . addObserver ( this )
settings . subscribe ( this )
updateSettings ( )
}
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 ) {
@ -41,7 +47,7 @@ class ReaderControlDelegate(
view . playSoundEffect ( SoundEffectConstants . NAVIGATION _UP )
}
GridTouchHelper . AREA _LEFT -> if ( isTapSwitchEnabled ) {
listener . switchPageBy ( - 1 )
listener . switchPageBy ( if ( isReaderTapsReversed ( ) ) 1 else - 1 )
view . playSoundEffect ( SoundEffectConstants . NAVIGATION _LEFT )
}
GridTouchHelper . AREA _BOTTOM -> if ( isTapSwitchEnabled ) {
@ -49,7 +55,7 @@ class ReaderControlDelegate(
view . playSoundEffect ( SoundEffectConstants . NAVIGATION _DOWN )
}
GridTouchHelper . AREA _RIGHT -> if ( isTapSwitchEnabled ) {
listener . switchPageBy ( 1 )
listener . switchPageBy ( if ( isReaderTapsReversed ( ) ) - 1 else 1 )
view . playSoundEffect ( SoundEffectConstants . NAVIGATION _RIGHT )
}
}
@ -72,19 +78,25 @@ class ReaderControlDelegate(
KeyEvent . KEYCODE _PAGE _DOWN ,
KeyEvent . KEYCODE _SYSTEM _NAVIGATION _DOWN ,
KeyEvent . KEYCODE _DPAD _DOWN ,
KeyEvent . KEYCODE _DPAD _RIGHT ,
-> {
listener . switchPageBy ( 1 )
true
}
KeyEvent . KEYCODE _DPAD _RIGHT -> {
listener . switchPageBy ( if ( isReaderTapsReversed ( ) ) - 1 else 1 )
true
}
KeyEvent . KEYCODE _PAGE _UP ,
KeyEvent . KEYCODE _SYSTEM _NAVIGATION _UP ,
KeyEvent . KEYCODE _DPAD _UP ,
KeyEvent . KEYCODE _DPAD _LEFT ,
-> {
listener . switchPageBy ( - 1 )
true
}
KeyEvent . KEYCODE _DPAD _LEFT -> {
listener . switchPageBy ( if ( isReaderTapsReversed ( ) ) 1 else - 1 )
true
}
KeyEvent . KEYCODE _DPAD _CENTER -> {
listener . toggleUiVisibility ( )
true
@ -99,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 {
val readerMode : ReaderMode ?
fun switchPageBy ( delta : Int )
fun toggleUiVisibility ( )