Improve reader actions editor
This commit is contained in:
@@ -19,7 +19,7 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
if (!prefs.getBoolean(KEY_INIT, false)) {
|
if (!prefs.getBoolean(KEY_INIT, false)) {
|
||||||
reset()
|
initPrefs(withDefaultValues = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,15 +34,25 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun reset() {
|
fun reset() {
|
||||||
prefs.edit {
|
initPrefs(withDefaultValues = true)
|
||||||
clear()
|
}
|
||||||
initDefaultActions(this)
|
|
||||||
putBoolean(KEY_INIT, true)
|
fun disableAll() {
|
||||||
}
|
initPrefs(withDefaultValues = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun observe() = prefs.observe().flowOn(Dispatchers.IO)
|
fun observe() = prefs.observe().flowOn(Dispatchers.IO)
|
||||||
|
|
||||||
|
private fun initPrefs(withDefaultValues: Boolean) {
|
||||||
|
prefs.edit {
|
||||||
|
clear()
|
||||||
|
if (withDefaultValues) {
|
||||||
|
initDefaultActions(this)
|
||||||
|
}
|
||||||
|
putBoolean(KEY_INIT, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getPrefKey(area: TapGridArea, isLongTap: Boolean): String = if (isLongTap) {
|
private fun getPrefKey(area: TapGridArea, isLongTap: Boolean): String = if (isLongTap) {
|
||||||
area.name + SUFFIX_LONG
|
area.name + SUFFIX_LONG
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import android.view.Menu
|
|||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.activity.viewModels
|
||||||
import androidx.core.graphics.ColorUtils
|
import androidx.core.graphics.ColorUtils
|
||||||
import androidx.core.graphics.Insets
|
import androidx.core.graphics.Insets
|
||||||
import androidx.core.text.bold
|
import androidx.core.text.bold
|
||||||
@@ -22,19 +23,16 @@ import org.koitharu.kotatsu.core.util.ext.findKeyByValue
|
|||||||
import org.koitharu.kotatsu.core.util.ext.getThemeDrawable
|
import org.koitharu.kotatsu.core.util.ext.getThemeDrawable
|
||||||
import org.koitharu.kotatsu.core.util.ext.observe
|
import org.koitharu.kotatsu.core.util.ext.observe
|
||||||
import org.koitharu.kotatsu.databinding.ActivityReaderTapActionsBinding
|
import org.koitharu.kotatsu.databinding.ActivityReaderTapActionsBinding
|
||||||
import org.koitharu.kotatsu.reader.data.TapGridSettings
|
|
||||||
import org.koitharu.kotatsu.reader.domain.TapGridArea
|
import org.koitharu.kotatsu.reader.domain.TapGridArea
|
||||||
import org.koitharu.kotatsu.reader.ui.tapgrid.TapAction
|
import org.koitharu.kotatsu.reader.ui.tapgrid.TapAction
|
||||||
import java.util.EnumMap
|
import java.util.EnumMap
|
||||||
import javax.inject.Inject
|
|
||||||
import com.google.android.material.R as materialR
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding>(), View.OnClickListener,
|
class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding>(), View.OnClickListener,
|
||||||
View.OnLongClickListener {
|
View.OnLongClickListener {
|
||||||
|
|
||||||
@Inject
|
private val viewModel: ReaderTapGridConfigViewModel by viewModels()
|
||||||
lateinit var tapGridSettings: TapGridSettings
|
|
||||||
|
|
||||||
private val controls = EnumMap<TapGridArea, TextView>(TapGridArea::class.java)
|
private val controls = EnumMap<TapGridArea, TextView>(TapGridArea::class.java)
|
||||||
|
|
||||||
@@ -56,8 +54,8 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
view.setOnClickListener(this)
|
view.setOnClickListener(this)
|
||||||
view.setOnLongClickListener(this)
|
view.setOnLongClickListener(this)
|
||||||
}
|
}
|
||||||
updateValues()
|
|
||||||
tapGridSettings.observe().observe(this) { updateValues() }
|
viewModel.content.observe(this, ::onContentChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
@@ -72,6 +70,11 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
R.id.action_disable_all -> {
|
||||||
|
viewModel.disableAll()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
else -> super.onOptionsItemSelected(item)
|
else -> super.onOptionsItemSelected(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,33 +99,37 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateValues() {
|
private fun onContentChanged(content: Map<TapGridArea, ReaderTapGridConfigViewModel.TapActions>) {
|
||||||
controls.forEach { (area, view) ->
|
controls.forEach { (area, view) ->
|
||||||
|
val actions = content[area]
|
||||||
view.text = buildSpannedString {
|
view.text = buildSpannedString {
|
||||||
appendLine(getString(R.string.tap_action))
|
appendLine(getString(R.string.tap_action))
|
||||||
bold {
|
bold {
|
||||||
appendLine(getTapActionText(area, isLongTap = false))
|
appendLine(actions?.tapAction.getText())
|
||||||
}
|
}
|
||||||
appendLine()
|
appendLine()
|
||||||
appendLine(getString(R.string.long_tap_action))
|
appendLine(getString(R.string.long_tap_action))
|
||||||
bold {
|
bold {
|
||||||
appendLine(getTapActionText(area, isLongTap = true))
|
appendLine(actions?.longTapAction.getText())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
view.background = createBackground(tapGridSettings.getTapAction(area, false))
|
view.background = createBackground(actions?.tapAction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTapActionText(area: TapGridArea, isLongTap: Boolean): String {
|
@Suppress("IfThenToElvis") // lint bug
|
||||||
return tapGridSettings.getTapAction(area, isLongTap)?.let {
|
private fun TapAction?.getText(): String = if (this != null) {
|
||||||
getString(it.nameStringResId)
|
getString(nameStringResId)
|
||||||
} ?: getString(R.string.none)
|
} else {
|
||||||
|
getString(R.string.none)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showActionSelector(area: TapGridArea, isLongTap: Boolean) {
|
private fun showActionSelector(area: TapGridArea, isLongTap: Boolean) {
|
||||||
val selectedItem = tapGridSettings.getTapAction(area, isLongTap)?.ordinal ?: -1
|
val selectedItem = viewModel.content.value[area]?.run {
|
||||||
|
if (isLongTap) longTapAction else tapAction
|
||||||
|
}?.ordinal ?: -1
|
||||||
val listener = DialogInterface.OnClickListener { dialog, which ->
|
val listener = DialogInterface.OnClickListener { dialog, which ->
|
||||||
tapGridSettings.setTapAction(area, isLongTap, TapAction.entries.getOrNull(which - 1))
|
viewModel.setTapAction(area, isLongTap, TapAction.entries.getOrNull(which - 1))
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
val names = arrayOfNulls<String>(TapAction.entries.size + 1)
|
val names = arrayOfNulls<String>(TapAction.entries.size + 1)
|
||||||
@@ -138,10 +145,11 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
|||||||
|
|
||||||
private fun confirmReset() {
|
private fun confirmReset() {
|
||||||
MaterialAlertDialogBuilder(this)
|
MaterialAlertDialogBuilder(this)
|
||||||
|
.setTitle(R.string.reader_actions)
|
||||||
.setMessage(R.string.config_reset_confirm)
|
.setMessage(R.string.config_reset_confirm)
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
.setNegativeButton(android.R.string.cancel, null)
|
||||||
.setPositiveButton(R.string.reset) { _, _ ->
|
.setPositiveButton(R.string.reset) { _, _ ->
|
||||||
tapGridSettings.reset()
|
viewModel.reset()
|
||||||
}.show()
|
}.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package org.koitharu.kotatsu.settings.reader
|
||||||
|
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.onStart
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.plus
|
||||||
|
import org.koitharu.kotatsu.core.ui.BaseViewModel
|
||||||
|
import org.koitharu.kotatsu.reader.data.TapGridSettings
|
||||||
|
import org.koitharu.kotatsu.reader.domain.TapGridArea
|
||||||
|
import org.koitharu.kotatsu.reader.ui.tapgrid.TapAction
|
||||||
|
import java.util.EnumMap
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class ReaderTapGridConfigViewModel @Inject constructor(
|
||||||
|
private val tapGridSettings: TapGridSettings,
|
||||||
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
val content = tapGridSettings.observe()
|
||||||
|
.onStart { emit(null) }
|
||||||
|
.map { getData() }
|
||||||
|
.stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, emptyMap())
|
||||||
|
|
||||||
|
fun reset() {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
tapGridSettings.reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun disableAll() {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
tapGridSettings.disableAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setTapAction(area: TapGridArea, isLongTap: Boolean, action: TapAction?) {
|
||||||
|
launchJob(Dispatchers.Default) {
|
||||||
|
tapGridSettings.setTapAction(area, isLongTap, action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getData(): Map<TapGridArea, TapActions> {
|
||||||
|
val map = EnumMap<TapGridArea, TapActions>(TapGridArea::class.java)
|
||||||
|
for (area in TapGridArea.entries) {
|
||||||
|
map[area] = TapActions(
|
||||||
|
tapAction = tapGridSettings.getTapAction(area, isLongTap = false),
|
||||||
|
longTapAction = tapGridSettings.getTapAction(area, isLongTap = true),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
data class TapActions(
|
||||||
|
val tapAction: TapAction?,
|
||||||
|
val longTapAction: TapAction?,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -7,5 +7,10 @@
|
|||||||
android:id="@+id/action_reset"
|
android:id="@+id/action_reset"
|
||||||
android:title="@string/reset"
|
android:title="@string/reset"
|
||||||
app:showAsAction="never" />
|
app:showAsAction="never" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_disable_all"
|
||||||
|
android:title="@string/disable_all"
|
||||||
|
app:showAsAction="never" />
|
||||||
|
|
||||||
</menu>
|
</menu>
|
||||||
|
|||||||
Reference in New Issue
Block a user