From 1382ab793386e85f2df844ec4ea7a73de3457254 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 30 Jan 2024 09:43:52 +0200 Subject: [PATCH] Improve reader actions editor --- .../kotatsu/reader/data/TapGridSettings.kt | 22 +++++-- .../reader/ReaderTapGridConfigActivity.kt | 42 +++++++------ .../reader/ReaderTapGridConfigViewModel.kt | 61 +++++++++++++++++++ app/src/main/res/menu/opt_tap_grid_config.xml | 5 ++ 4 files changed, 107 insertions(+), 23 deletions(-) create mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigViewModel.kt diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/data/TapGridSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/data/TapGridSettings.kt index 740f6493d..d3fc3dab1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/data/TapGridSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/data/TapGridSettings.kt @@ -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 { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigActivity.kt index d3e08ef5a..d0894370a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigActivity.kt @@ -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(), View.OnClickListener, View.OnLongClickListener { - @Inject - lateinit var tapGridSettings: TapGridSettings + private val viewModel: ReaderTapGridConfigViewModel by viewModels() private val controls = EnumMap(TapGridArea::class.java) @@ -56,8 +54,8 @@ class ReaderTapGridConfigActivity : BaseActivity { + viewModel.disableAll() + true + } + else -> super.onOptionsItemSelected(item) } } @@ -96,33 +99,37 @@ class ReaderTapGridConfigActivity : BaseActivity) { 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(TapAction.entries.size + 1) @@ -138,10 +145,11 @@ class ReaderTapGridConfigActivity : BaseActivity - tapGridSettings.reset() + viewModel.reset() }.show() } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigViewModel.kt new file mode 100644 index 000000000..e31068d3a --- /dev/null +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/reader/ReaderTapGridConfigViewModel.kt @@ -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 { + val map = EnumMap(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?, + ) +} diff --git a/app/src/main/res/menu/opt_tap_grid_config.xml b/app/src/main/res/menu/opt_tap_grid_config.xml index 5ce3e7652..ccaea0bee 100644 --- a/app/src/main/res/menu/opt_tap_grid_config.xml +++ b/app/src/main/res/menu/opt_tap_grid_config.xml @@ -7,5 +7,10 @@ android:id="@+id/action_reset" android:title="@string/reset" app:showAsAction="never" /> + +