|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package org.koitharu.kotatsu.reader.data
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import androidx.core.content.edit
|
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
@ -14,15 +15,17 @@ import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
class TapGridSettings @Inject constructor(@ApplicationContext context: Context) {
|
|
|
|
|
|
|
|
|
|
private val prefs = context.getSharedPreferences("tap_grid", Context.MODE_PRIVATE)
|
|
|
|
|
private val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
if (!prefs.getBoolean(KEY_INIT, false)) {
|
|
|
|
|
reset()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getTapAction(area: TapGridArea, isLongTap: Boolean): TapAction? {
|
|
|
|
|
val key = getPrefKey(area, isLongTap)
|
|
|
|
|
return if (!isLongTap && key !in prefs) {
|
|
|
|
|
getDefaultTapAction(area)
|
|
|
|
|
} else {
|
|
|
|
|
prefs.getEnumValue(key, TapAction::class.java)
|
|
|
|
|
}
|
|
|
|
|
return prefs.getEnumValue(key, TapAction::class.java)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun setTapAction(area: TapGridArea, isLongTap: Boolean, action: TapAction?) {
|
|
|
|
|
@ -30,24 +33,41 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
|
|
|
|
prefs.edit { putEnumValue(key, action) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun reset() {
|
|
|
|
|
prefs.edit {
|
|
|
|
|
clear()
|
|
|
|
|
initDefaultActions(this)
|
|
|
|
|
putBoolean(KEY_INIT, true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun observe() = prefs.observe().flowOn(Dispatchers.IO)
|
|
|
|
|
|
|
|
|
|
private fun getPrefKey(area: TapGridArea, isLongTap: Boolean): String = if (isLongTap) {
|
|
|
|
|
area.name + "_long"
|
|
|
|
|
area.name + SUFFIX_LONG
|
|
|
|
|
} else {
|
|
|
|
|
area.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getDefaultTapAction(area: TapGridArea): TapAction = when (area) {
|
|
|
|
|
TapGridArea.TOP_LEFT,
|
|
|
|
|
TapGridArea.TOP_CENTER,
|
|
|
|
|
TapGridArea.CENTER_LEFT,
|
|
|
|
|
TapGridArea.BOTTOM_LEFT -> TapAction.PAGE_PREV
|
|
|
|
|
|
|
|
|
|
TapGridArea.CENTER -> TapAction.TOGGLE_UI
|
|
|
|
|
TapGridArea.TOP_RIGHT,
|
|
|
|
|
TapGridArea.CENTER_RIGHT,
|
|
|
|
|
TapGridArea.BOTTOM_CENTER,
|
|
|
|
|
TapGridArea.BOTTOM_RIGHT -> TapAction.PAGE_NEXT
|
|
|
|
|
private fun initDefaultActions(editor: SharedPreferences.Editor) {
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_LEFT, false), TapAction.PAGE_PREV)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_CENTER, false), TapAction.PAGE_PREV)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER_LEFT, false), TapAction.PAGE_PREV)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.BOTTOM_LEFT, false), TapAction.PAGE_PREV)
|
|
|
|
|
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER, false), TapAction.TOGGLE_UI)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER, true), TapAction.SHOW_MENU)
|
|
|
|
|
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_RIGHT, false), TapAction.PAGE_NEXT)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER_RIGHT, false), TapAction.PAGE_NEXT)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.BOTTOM_CENTER, false), TapAction.PAGE_NEXT)
|
|
|
|
|
editor.putEnumValue(getPrefKey(TapGridArea.BOTTOM_RIGHT, false), TapAction.PAGE_NEXT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private companion object {
|
|
|
|
|
|
|
|
|
|
private const val PREFS_NAME = "tap_grid"
|
|
|
|
|
private const val KEY_INIT = "_init"
|
|
|
|
|
private const val SUFFIX_LONG = "_long"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|