Skip to content

Commit

Permalink
refactor: refactors to picker dialog
Browse files Browse the repository at this point in the history
Enhance the UX
  • Loading branch information
WhiredPlanck committed May 19, 2024
1 parent 560658f commit 751563b
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object SoundEffectManager {

private fun getSound(name: String) = userSounds.find { it.name == name }

private val userSounds: MutableList<SoundEffect> = listSounds()
private val userSounds: MutableList<SoundEffect> get() = listSounds()

@JvmStatic
fun switchSound(name: String) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/osfans/trime/data/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Theme(name: String) {
private const val DEFAULT_THEME_NAME = "trime"

private fun deploy(active: String): Config? {
val nameWithExtension = "$active.yaml"
val ext = if (active == DEFAULT_THEME_NAME) ".yaml" else ".trime.yaml"
val nameWithExtension = "$active$ext"
val isDeployed: Boolean
measureTimeMillis {
isDeployed = Rime.deployRimeConfigFile(nameWithExtension, VERSION_KEY)
Expand All @@ -46,7 +47,7 @@ class Theme(name: String) {
Timber.w("Failed to deploy theme file '$nameWithExtension'")
}
}
return Config.create(active)
return Config.create(nameWithExtension.removeSuffix(".yaml"))
}
}

Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ object ThemeManager {
DataManager.addOnChangedListener(onDataDirChange)
}

private val suffixRegex = Regex("(.*?)(\\.trime\\.yaml$|\\.yaml$)")

private fun listThemes(path: File): MutableList<String> {
return path.listFiles { _, name -> name.endsWith("trime.yaml") }
?.map { f -> f.nameWithoutExtension }
?.mapNotNull { f ->
suffixRegex.matchEntire(f.name)?.let { result ->
val basename = if (result.groups[2]?.value == ".trime.yaml") result.groupValues[1] else f.nameWithoutExtension
basename
}
}
?.toMutableList() ?: mutableListOf()
}

private val sharedThemes: MutableList<String> = listThemes(DataManager.sharedDataDir)
private val sharedThemes: MutableList<String> get() = listThemes(DataManager.sharedDataDir)

private val userThemes: MutableList<String> = listThemes(DataManager.userDataDir)
private val userThemes: MutableList<String> get() = listThemes(DataManager.userDataDir)

fun getAllThemes(): List<String> {
if (DataManager.sharedDataDir.absolutePath == DataManager.userDataDir.absolutePath) {
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import com.osfans.trime.ime.keyboard.Keyboard
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.symbol.SymbolBoardType
import com.osfans.trime.ui.main.buildColorPickerDialog
import com.osfans.trime.ui.main.buildSoundEffectPickerDialog
import com.osfans.trime.ui.main.buildThemePickerDialog
import com.osfans.trime.ui.main.settings.ColorPickerDialog
import com.osfans.trime.ui.main.settings.KeySoundEffectPickerDialog
import com.osfans.trime.ui.main.settings.ThemePickerDialog
import com.osfans.trime.util.ShortcutUtils
import com.osfans.trime.util.startsWithAsciiChar
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -368,22 +368,22 @@ class TextInputManager(
KeyEvent.KEYCODE_SETTINGS -> { // Settings
trime.lifecycleScope.launch {
when (event.option) {
"theme" -> trime.inputView?.showDialog(buildThemePickerDialog(themedContext, trime.lifecycleScope))
"color" -> trime.inputView?.showDialog(buildColorPickerDialog(themedContext, trime.lifecycleScope))
"theme" -> trime.inputView?.showDialog(ThemePickerDialog.build(trime.lifecycleScope, themedContext))
"color" -> trime.inputView?.showDialog(ColorPickerDialog.build(trime.lifecycleScope, themedContext))
"schema" ->
rime.launchOnReady { api ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(AvailableSchemaPickerDialog.build(api, trime.lifecycleScope, themedContext))
}
}
"sound" -> trime.inputView?.showDialog(buildSoundEffectPickerDialog(themedContext))
"sound" -> trime.inputView?.showDialog(KeySoundEffectPickerDialog.build(trime.lifecycleScope, themedContext))
else -> ShortcutUtils.launchMainActivity(trime)
}
}
}
KeyEvent.KEYCODE_PROG_RED ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(buildColorPickerDialog(themedContext, trime.lifecycleScope))
trime.inputView?.showDialog(ColorPickerDialog.build(trime.lifecycleScope, themedContext))
}
KeyEvent.KEYCODE_MENU -> {
rime.launchOnReady { api ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.osfans.trime.data.base.DataManager
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ui.components.PaddingPreferenceFragment
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.ui.main.buildSoundEffectPickerDialog
import com.osfans.trime.ui.main.settings.KeySoundEffectPickerDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -33,7 +33,7 @@ class KeyboardFragment :
addPreferencesFromResource(R.xml.keyboard_preference)
findPreference<Preference>("keyboard__key_sound_package")
?.setOnPreferenceClickListener {
lifecycleScope.launch { buildSoundEffectPickerDialog(requireContext()).show() }
lifecycleScope.launch { KeySoundEffectPickerDialog.build(lifecycleScope, requireContext()).show() }
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import androidx.preference.get
import com.osfans.trime.R
import com.osfans.trime.ui.components.PaddingPreferenceFragment
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.ui.main.buildColorPickerDialog
import com.osfans.trime.ui.main.buildThemePickerDialog
import com.osfans.trime.ui.main.settings.ColorPickerDialog
import com.osfans.trime.ui.main.settings.ThemePickerDialog
import kotlinx.coroutines.launch

class ThemeColorFragment : PaddingPreferenceFragment() {
Expand All @@ -26,11 +26,11 @@ class ThemeColorFragment : PaddingPreferenceFragment() {
addPreferencesFromResource(R.xml.theme_color_preference)
with(preferenceScreen) {
get<Preference>("theme_selected_theme")?.setOnPreferenceClickListener {
lifecycleScope.launch { buildThemePickerDialog(context, lifecycleScope).show() }
lifecycleScope.launch { ThemePickerDialog.build(lifecycleScope, context).show() }
true
}
get<Preference>("theme_selected_color")?.setOnPreferenceClickListener {
lifecycleScope.launch { buildColorPickerDialog(context, lifecycleScope).show() }
lifecycleScope.launch { ColorPickerDialog.build(lifecycleScope, context).show() }
true
}
}
Expand Down
82 changes: 0 additions & 82 deletions app/src/main/java/com/osfans/trime/ui/main/Pickers.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package com.osfans.trime.ui.main

import android.app.AlertDialog
import android.content.Intent
import android.os.Build
import android.os.Bundle
Expand All @@ -13,7 +14,6 @@ import android.view.MenuItem
import android.view.ViewGroup
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.ViewCompat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.osfans.trime.ui.main.settings

import android.app.AlertDialog
import android.content.Context
import androidx.lifecycle.LifecycleCoroutineScope
import com.osfans.trime.R
import com.osfans.trime.data.theme.ColorManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

object ColorPickerDialog {
suspend fun build(
scope: LifecycleCoroutineScope,
context: Context,
): AlertDialog {
val all = withContext(Dispatchers.Default) { ColorManager.presetColorSchemes }
val allIds = all.keys
val allNames = all.values.mapNotNull { it["name"] }
val currentId = ColorManager.selectedColor
val currentIndex = all.keys.indexOfFirst { it == currentId }
return AlertDialog.Builder(context).apply {
setTitle(R.string.looks__selected_color_title)
if (all.isEmpty()) {
setMessage(R.string.no_color_to_select)
} else {
setSingleChoiceItems(
allNames.toTypedArray(),
currentIndex,
) { dialog, which ->
scope.launch {
if (which != currentIndex) {
ColorManager.setColorScheme(allIds.elementAt(which))
}
dialog.dismiss()
}
}
}
setNegativeButton(android.R.string.cancel, null)
}.create()
}
}
Loading

0 comments on commit 751563b

Please sign in to comment.