Editor actions fixes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package org.koitharu.kotatsu.reader.data
|
package org.koitharu.kotatsu.reader.data
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -14,15 +15,17 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
class TapGridSettings @Inject constructor(@ApplicationContext context: Context) {
|
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? {
|
fun getTapAction(area: TapGridArea, isLongTap: Boolean): TapAction? {
|
||||||
val key = getPrefKey(area, isLongTap)
|
val key = getPrefKey(area, isLongTap)
|
||||||
return if (!isLongTap && key !in prefs) {
|
return prefs.getEnumValue(key, TapAction::class.java)
|
||||||
getDefaultTapAction(area)
|
|
||||||
} else {
|
|
||||||
prefs.getEnumValue(key, TapAction::class.java)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setTapAction(area: TapGridArea, isLongTap: Boolean, action: TapAction?) {
|
fun setTapAction(area: TapGridArea, isLongTap: Boolean, action: TapAction?) {
|
||||||
@@ -30,24 +33,41 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
|||||||
prefs.edit { putEnumValue(key, action) }
|
prefs.edit { putEnumValue(key, action) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
prefs.edit {
|
||||||
|
clear()
|
||||||
|
initDefaultActions(this)
|
||||||
|
putBoolean(KEY_INIT, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun observe() = prefs.observe().flowOn(Dispatchers.IO)
|
fun observe() = prefs.observe().flowOn(Dispatchers.IO)
|
||||||
|
|
||||||
private fun getPrefKey(area: TapGridArea, isLongTap: Boolean): String = if (isLongTap) {
|
private fun getPrefKey(area: TapGridArea, isLongTap: Boolean): String = if (isLongTap) {
|
||||||
area.name + "_long"
|
area.name + SUFFIX_LONG
|
||||||
} else {
|
} else {
|
||||||
area.name
|
area.name
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getDefaultTapAction(area: TapGridArea): TapAction = when (area) {
|
private fun initDefaultActions(editor: SharedPreferences.Editor) {
|
||||||
TapGridArea.TOP_LEFT,
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_LEFT, false), TapAction.PAGE_PREV)
|
||||||
TapGridArea.TOP_CENTER,
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_CENTER, false), TapAction.PAGE_PREV)
|
||||||
TapGridArea.CENTER_LEFT,
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER_LEFT, false), TapAction.PAGE_PREV)
|
||||||
TapGridArea.BOTTOM_LEFT -> TapAction.PAGE_PREV
|
editor.putEnumValue(getPrefKey(TapGridArea.BOTTOM_LEFT, false), TapAction.PAGE_PREV)
|
||||||
|
|
||||||
TapGridArea.CENTER -> TapAction.TOGGLE_UI
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER, false), TapAction.TOGGLE_UI)
|
||||||
TapGridArea.TOP_RIGHT,
|
editor.putEnumValue(getPrefKey(TapGridArea.CENTER, true), TapAction.SHOW_MENU)
|
||||||
TapGridArea.CENTER_RIGHT,
|
|
||||||
TapGridArea.BOTTOM_CENTER,
|
editor.putEnumValue(getPrefKey(TapGridArea.TOP_RIGHT, false), TapAction.PAGE_NEXT)
|
||||||
TapGridArea.BOTTOM_RIGHT -> 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))
|
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) {
|
override fun onLongPress(event: MotionEvent) {
|
||||||
if (isDispatching) {
|
if (isDispatching) {
|
||||||
listener.onGridLongTouch(getArea(event.rawX, event.rawY))
|
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.Drawable
|
||||||
import android.graphics.drawable.LayerDrawable
|
import android.graphics.drawable.LayerDrawable
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.core.graphics.ColorUtils
|
import androidx.core.graphics.ColorUtils
|
||||||
@@ -58,6 +60,22 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
tapGridSettings.observe().observe(this) { updateValues() }
|
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) {
|
override fun onWindowInsetsChanged(insets: Insets) {
|
||||||
viewBinding.root.updatePadding(
|
viewBinding.root.updatePadding(
|
||||||
left = insets.left,
|
left = insets.left,
|
||||||
@@ -118,12 +136,21 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
.show()
|
.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? {
|
private fun createBackground(action: TapAction?): Drawable? {
|
||||||
val ripple = getThemeDrawable(materialR.attr.selectableItemBackground)
|
val ripple = getThemeDrawable(materialR.attr.selectableItemBackground)
|
||||||
return if (action == null) {
|
return if (action == null) {
|
||||||
ripple
|
ripple
|
||||||
} else {
|
} 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