Improve reader actions editor
This commit is contained in:
@@ -19,7 +19,7 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
||||
|
||||
init {
|
||||
if (!prefs.getBoolean(KEY_INIT, false)) {
|
||||
reset()
|
||||
initPrefs(withDefaultValues = true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,15 +34,25 @@ class TapGridSettings @Inject constructor(@ApplicationContext context: Context)
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
prefs.edit {
|
||||
clear()
|
||||
initDefaultActions(this)
|
||||
putBoolean(KEY_INIT, true)
|
||||
}
|
||||
initPrefs(withDefaultValues = true)
|
||||
}
|
||||
|
||||
fun disableAll() {
|
||||
initPrefs(withDefaultValues = false)
|
||||
}
|
||||
|
||||
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) {
|
||||
area.name + SUFFIX_LONG
|
||||
} else {
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.activity.viewModels
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import androidx.core.graphics.Insets
|
||||
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.observe
|
||||
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.ui.tapgrid.TapAction
|
||||
import java.util.EnumMap
|
||||
import javax.inject.Inject
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
@AndroidEntryPoint
|
||||
class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding>(), View.OnClickListener,
|
||||
View.OnLongClickListener {
|
||||
|
||||
@Inject
|
||||
lateinit var tapGridSettings: TapGridSettings
|
||||
private val viewModel: ReaderTapGridConfigViewModel by viewModels()
|
||||
|
||||
private val controls = EnumMap<TapGridArea, TextView>(TapGridArea::class.java)
|
||||
|
||||
@@ -56,8 +54,8 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
view.setOnClickListener(this)
|
||||
view.setOnLongClickListener(this)
|
||||
}
|
||||
updateValues()
|
||||
tapGridSettings.observe().observe(this) { updateValues() }
|
||||
|
||||
viewModel.content.observe(this, ::onContentChanged)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
@@ -72,6 +70,11 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
true
|
||||
}
|
||||
|
||||
R.id.action_disable_all -> {
|
||||
viewModel.disableAll()
|
||||
true
|
||||
}
|
||||
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
@@ -96,33 +99,37 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
return true
|
||||
}
|
||||
|
||||
private fun updateValues() {
|
||||
private fun onContentChanged(content: Map<TapGridArea, ReaderTapGridConfigViewModel.TapActions>) {
|
||||
controls.forEach { (area, view) ->
|
||||
val actions = content[area]
|
||||
view.text = buildSpannedString {
|
||||
appendLine(getString(R.string.tap_action))
|
||||
bold {
|
||||
appendLine(getTapActionText(area, isLongTap = false))
|
||||
appendLine(actions?.tapAction.getText())
|
||||
}
|
||||
appendLine()
|
||||
appendLine(getString(R.string.long_tap_action))
|
||||
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 {
|
||||
return tapGridSettings.getTapAction(area, isLongTap)?.let {
|
||||
getString(it.nameStringResId)
|
||||
} ?: getString(R.string.none)
|
||||
@Suppress("IfThenToElvis") // lint bug
|
||||
private fun TapAction?.getText(): String = if (this != null) {
|
||||
getString(nameStringResId)
|
||||
} else {
|
||||
getString(R.string.none)
|
||||
}
|
||||
|
||||
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 ->
|
||||
tapGridSettings.setTapAction(area, isLongTap, TapAction.entries.getOrNull(which - 1))
|
||||
viewModel.setTapAction(area, isLongTap, TapAction.entries.getOrNull(which - 1))
|
||||
dialog.dismiss()
|
||||
}
|
||||
val names = arrayOfNulls<String>(TapAction.entries.size + 1)
|
||||
@@ -138,10 +145,11 @@ class ReaderTapGridConfigActivity : BaseActivity<ActivityReaderTapActionsBinding
|
||||
|
||||
private fun confirmReset() {
|
||||
MaterialAlertDialogBuilder(this)
|
||||
.setTitle(R.string.reader_actions)
|
||||
.setMessage(R.string.config_reset_confirm)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.setPositiveButton(R.string.reset) { _, _ ->
|
||||
tapGridSettings.reset()
|
||||
viewModel.reset()
|
||||
}.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:title="@string/reset"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_disable_all"
|
||||
android:title="@string/disable_all"
|
||||
app:showAsAction="never" />
|
||||
|
||||
</menu>
|
||||
|
||||
Reference in New Issue
Block a user