Skip to content

Commit

Permalink
feat: make dialog use device default settings theme outside the app
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Apr 24, 2024
1 parent 31b93d2 commit b0cbeff
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
34 changes: 13 additions & 21 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.osfans.trime.ime.text
import android.text.InputType
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import androidx.appcompat.R.style.Theme_AppCompat_DayNight_Dialog_Alert
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.R
import com.osfans.trime.core.Rime
Expand All @@ -26,16 +25,17 @@ import com.osfans.trime.ime.keyboard.InputFeedbackManager
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.ui.main.colorPicker
import com.osfans.trime.ui.main.soundPicker
import com.osfans.trime.ui.main.themePicker
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.util.ShortcutUtils
import com.osfans.trime.util.startsWithAsciiChar
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import splitties.systemservices.inputMethodManager
import splitties.views.dsl.core.withTheme
import timber.log.Timber
import java.util.Locale

Expand Down Expand Up @@ -71,6 +71,9 @@ class TextInputManager(
}
private val shouldResetAsciiMode get() = trime.shouldResetAsciiMode

// TODO: move things using this context to InputView scope.
private val themedContext = trime.withTheme(android.R.style.Theme_DeviceDefault_Settings)

companion object {
/** Delimiter regex for key property group, their format like `{property_1: value_1, property_2: value_2}` */
private val DELIMITER_PROPERTY_GROUP = """^(\{[^{}]+\}).*$""".toRegex()
Expand Down Expand Up @@ -351,39 +354,28 @@ class TextInputManager(
KeyEvent.KEYCODE_SETTINGS -> { // Settings
trime.lifecycleScope.launch {
when (event.option) {
"theme" ->
trime.inputView?.showDialog(
trime.themePicker(Theme_AppCompat_DayNight_Dialog_Alert),
)
"color" ->
trime.inputView?.showDialog(
trime.colorPicker(Theme_AppCompat_DayNight_Dialog_Alert),
)
"theme" -> trime.inputView?.showDialog(buildThemePickerDialog(themedContext, trime.lifecycleScope))
"color" -> trime.inputView?.showDialog(buildColorPickerDialog(themedContext, trime.lifecycleScope))
"schema" ->
rime.launchOnReady { api ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(AvailableSchemaPickerDialog.build(api, trime))
trime.inputView?.showDialog(AvailableSchemaPickerDialog.build(api, themedContext))
}
}
"sound" ->
trime.inputView?.showDialog(
trime.soundPicker(Theme_AppCompat_DayNight_Dialog_Alert),
)
"sound" -> trime.inputView?.showDialog(buildSoundEffectPickerDialog(themedContext))
else -> ShortcutUtils.launchMainActivity(trime)
}
}
}
KeyEvent.KEYCODE_PROG_RED ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(
trime.colorPicker(Theme_AppCompat_DayNight_Dialog_Alert),
)
trime.inputView?.showDialog(buildColorPickerDialog(themedContext, trime.lifecycleScope))
}
KeyEvent.KEYCODE_MENU -> {
rime.launchOnReady { api ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(
EnabledSchemaPickerDialog.build(api, this, trime) {
EnabledSchemaPickerDialog.build(api, this, themedContext) {
setPositiveButton(R.string.enable_schemata) { _, _ ->
trime.lifecycleScope.launch {
trime.inputView?.showDialog(AvailableSchemaPickerDialog.build(api, context))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.osfans.trime.ui.components

import android.app.AlertDialog
import android.content.Context
import androidx.annotation.StyleRes
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CoroutineChoiceDialog(
context: Context,
@StyleRes themeResId: Int,
) : CoroutineScope by (context as LifecycleOwner).lifecycleScope {
private val builder = AlertDialog.Builder(context, themeResId)
private val scope: LifecycleCoroutineScope,
) {
private val builder = AlertDialog.Builder(context)
var items: Array<CharSequence> = arrayOf()
var checkedItem: Int = -1
var checkedItems: BooleanArray = booleanArrayOf()
Expand All @@ -27,8 +24,6 @@ class CoroutineChoiceDialog(
private lateinit var onInitListener: ActionListener
private lateinit var onPositiveListener: ActionListener

constructor(context: Context) : this(context, 0)

fun interface ActionListener {
fun onAction()
}
Expand Down Expand Up @@ -68,7 +63,7 @@ class CoroutineChoiceDialog(
}
}
setPositiveButton(android.R.string.ok) { _, _ ->
launch {
scope.launch {
positive()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.osfans.trime.data.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.soundPicker
import com.osfans.trime.ui.main.buildSoundEffectPickerDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -29,7 +29,7 @@ class KeyboardFragment :
addPreferencesFromResource(R.xml.keyboard_preference)
findPreference<Preference>("keyboard__key_sound_package")
?.setOnPreferenceClickListener {
lifecycleScope.launch { requireContext().soundPicker().show() }
lifecycleScope.launch { buildSoundEffectPickerDialog(requireContext()).show() }
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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.colorPicker
import com.osfans.trime.ui.main.themePicker
import com.osfans.trime.ui.main.buildColorPickerDialog
import com.osfans.trime.ui.main.buildThemePickerDialog
import kotlinx.coroutines.launch

class ThemeColorFragment : PaddingPreferenceFragment() {
Expand All @@ -22,11 +22,11 @@ class ThemeColorFragment : PaddingPreferenceFragment() {
addPreferencesFromResource(R.xml.theme_color_preference)
with(preferenceScreen) {
get<Preference>("theme_selected_theme")?.setOnPreferenceClickListener {
lifecycleScope.launch { context.themePicker().show() }
lifecycleScope.launch { buildThemePickerDialog(context, lifecycleScope).show() }
true
}
get<Preference>("theme_selected_color")?.setOnPreferenceClickListener {
lifecycleScope.launch { context.colorPicker().show() }
lifecycleScope.launch { buildColorPickerDialog(context, lifecycleScope).show() }
true
}
}
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/com/osfans/trime/ui/main/Pickers.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.osfans.trime.ui.main

import android.app.AlertDialog
import android.content.Context
import androidx.annotation.StyleRes
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.LifecycleCoroutineScope
import com.osfans.trime.R
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.sound.SoundEffect
Expand All @@ -12,11 +12,12 @@ import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ui.components.CoroutineChoiceDialog
import kotlinx.coroutines.Dispatchers

suspend fun Context.themePicker(
@StyleRes themeResId: Int = 0,
suspend fun buildThemePickerDialog(
context: Context,
scope: LifecycleCoroutineScope,
): AlertDialog {
return CoroutineChoiceDialog(this, themeResId).apply {
title = getString(R.string.looks__selected_theme_title)
return CoroutineChoiceDialog(context, scope).apply {
title = context.getString(R.string.looks__selected_theme_title)
initDispatcher = Dispatchers.IO
onInit {
items =
Expand All @@ -37,11 +38,12 @@ suspend fun Context.themePicker(
}.create()
}

suspend fun Context.colorPicker(
@StyleRes themeResId: Int = 0,
suspend fun buildColorPickerDialog(
context: Context,
scope: LifecycleCoroutineScope,
): AlertDialog {
return CoroutineChoiceDialog(this, themeResId).apply {
title = getString(R.string.looks__selected_color_title)
return CoroutineChoiceDialog(context, scope).apply {
title = context.getString(R.string.looks__selected_color_title)
initDispatcher = Dispatchers.Default
val all by lazy { ColorManager.presetColorSchemes }
onInit {
Expand All @@ -58,13 +60,11 @@ suspend fun Context.colorPicker(
}.create()
}

fun Context.soundPicker(
@StyleRes themeResId: Int = 0,
): AlertDialog {
fun buildSoundEffectPickerDialog(context: Context): AlertDialog {
val all = SoundEffectManager.getAllSoundEffects().mapNotNull(SoundEffect::name)
val current = SoundEffectManager.getActiveSoundEffect().getOrNull()?.name ?: ""
var checked = all.indexOf(current)
return AlertDialog.Builder(this, themeResId)
return AlertDialog.Builder(context)
.setTitle(R.string.keyboard__key_sound_package_title)
.setSingleChoiceItems(
all.toTypedArray(),
Expand Down

0 comments on commit b0cbeff

Please sign in to comment.