Editor actions fixes
This commit is contained in:
@@ -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
|
||||
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)
|
||||
|
||||
TapGridArea.CENTER -> TapAction.TOGGLE_UI
|
||||
TapGridArea.TOP_RIGHT,
|
||||
TapGridArea.CENTER_RIGHT,
|
||||
TapGridArea.BOTTOM_CENTER,
|
||||
TapGridArea.BOTTOM_RIGHT -> TapAction.PAGE_NEXT
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,11 @@ class TapGridDispatcher(
|
||||
return listener.onGridTouch(getArea(event.rawX, event.rawY))
|
||||
}
|
||||
|
||||
override fun onDoubleTapEvent(e: MotionEvent): Boolean {
|
||||
isDispatching = false // ignore long press after double tap
|
||||
return super.onDoubleTapEvent(e)
|
||||
}
|
||||
|
||||
override fun onLongPress(event: MotionEvent) {
|
||||
if (isDispatching) {
|
||||
listener.onGridLongTouch(getArea(event.rawX, event.rawY))
|
||||
|
||||
@@ -5,6 +5,8 @@ import android.graphics.drawable.ColorDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.LayerDrawable
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.core.graphics.ColorUtils
|
||||
@@ -58,6 +60,22 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
tapGridSettings.observe().observe(this) { updateValues() }
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.opt_tap_grid_config, menu)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
R.id.action_reset -> {
|
||||
confirmReset()
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onWindowInsetsChanged(insets: Insets) {
|
||||
viewBinding.root.updatePadding(
|
||||
left = insets.left,
|
||||
@@ -118,12 +136,21 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun confirmReset() {
|
||||
MaterialAlertDialogBuilder(this)
|
||||
.setMessage(R.string.config_reset_confirm)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.setPositiveButton(R.string.reset) { _, _ ->
|
||||
tapGridSettings.reset()
|
||||
}.show()
|
||||
}
|
||||
|
||||
private fun createBackground(action: TapAction?): Drawable? {
|
||||
val ripple = getThemeDrawable(materialR.attr.selectableItemBackground)
|
||||
return if (action == null) {
|
||||
ripple
|
||||
} else {
|
||||
LayerDrawable(arrayOf(ripple, ColorDrawable(ColorUtils.setAlphaComponent(action.color, 60))))
|
||||
LayerDrawable(arrayOf(ripple, ColorDrawable(ColorUtils.setAlphaComponent(action.color, 40))))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
app/src/main/res/menu/opt_tap_grid_config.xml
Normal file
11
app/src/main/res/menu/opt_tap_grid_config.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_reset"
|
||||
android:title="@string/reset"
|
||||
app:showAsAction="never" />
|
||||
|
||||
</menu>
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user