Skip to content

Commit

Permalink
refactor(enums): tidy enum classes
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Mar 8, 2024
1 parent 3a1b043 commit 318d65b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 69 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.blankj.utilcode.util.PathUtils
import com.osfans.trime.R
import com.osfans.trime.ime.enums.InlineModeType
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.keyboard.KeyboardPrefs
import com.osfans.trime.ime.landscapeinput.LandscapeInputUIMode
import com.osfans.trime.util.appContext
import java.lang.ref.WeakReference
import java.util.Calendar
Expand Down Expand Up @@ -184,11 +184,11 @@ class AppPrefs(
const val SHOULD_LONG_CLICK_DELETE_CANDIDATE = "keyboard__long_click_delete_candidate"
}

var inlinePreedit: InlineModeType
get() = InlineModeType.fromString(prefs.getPref(INLINE_PREEDIT_MODE, "preview"))
var inlinePreedit: InlinePreeditMode
get() = InlinePreeditMode.fromString(prefs.getPref(INLINE_PREEDIT_MODE, "preview"))
set(v) = prefs.setPref(INLINE_PREEDIT_MODE, v)
var fullscreenMode: LandscapeInputUIMode
get() = LandscapeInputUIMode.fromString(prefs.getPref(FULLSCREEN_MODE, "auto_show"))
var fullscreenMode: FullscreenMode
get() = FullscreenMode.fromString(prefs.getPref(FULLSCREEN_MODE, "auto_show"))
set(v) = prefs.setPref(FULLSCREEN_MODE, v)
var softCursorEnabled: Boolean = false
get() = prefs.getPref(SOFT_CURSOR_ENABLED, true)
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/osfans/trime/ime/core/EditorInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import android.view.inputmethod.InputConnection
import com.osfans.trime.core.Rime
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.ime.enums.InlineModeType
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.text.TextInputManager
import timber.log.Timber

Expand Down Expand Up @@ -68,9 +68,9 @@ class EditorInstance(private val ims: InputMethodService) {
val ic = inputConnection ?: return
val composingText =
when (prefs.keyboard.inlinePreedit) {
InlineModeType.INLINE_PREVIEW -> Rime.composingText
InlineModeType.INLINE_COMPOSITION -> Rime.compositionText
InlineModeType.INLINE_INPUT -> Rime.getRimeRawInput() ?: ""
InlinePreeditMode.PREVIEW -> Rime.composingText
InlinePreeditMode.COMPOSITION -> Rime.compositionText
InlinePreeditMode.INPUT -> Rime.getRimeRawInput() ?: ""
else -> ""
}
if (ic.getSelectedText(0).isNullOrEmpty() || composingText.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.osfans.trime.data.db.DraftHelper
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.Keycode
import com.osfans.trime.ime.enums.SymbolKeyboardType
import com.osfans.trime.ime.keyboard.Event
Expand All @@ -58,7 +59,6 @@ import com.osfans.trime.ime.keyboard.Key
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.landscapeinput.LandscapeInputUIMode
import com.osfans.trime.ime.lifecycle.LifecycleInputMethodService
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.symbol.TabView
Expand Down Expand Up @@ -933,7 +933,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
val config = resources.configuration
if (config == null || config.orientation != Configuration.ORIENTATION_LANDSCAPE) return false
return when (prefs.keyboard.fullscreenMode) {
LandscapeInputUIMode.AUTO_SHOW -> {
FullscreenMode.AUTO_SHOW -> {
Timber.d("FullScreen: Auto")
val ei = currentInputEditorInfo
if (ei != null && ei.imeOptions and EditorInfo.IME_FLAG_NO_FULLSCREEN != 0) {
Expand All @@ -943,12 +943,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
true
}

LandscapeInputUIMode.ALWAYS_SHOW -> {
FullscreenMode.ALWAYS_SHOW -> {
Timber.d("FullScreen: Always")
true
}

LandscapeInputUIMode.NEVER_SHOW -> {
FullscreenMode.NEVER_SHOW -> {
Timber.d("FullScreen: Never")
false
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/enums/FullscreenMode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.osfans.trime.ime.enums

enum class FullscreenMode {
AUTO_SHOW,
ALWAYS_SHOW,
NEVER_SHOW,
;

companion object {
fun fromString(mode: String): FullscreenMode {
return runCatching {
valueOf(mode.uppercase())
}.getOrDefault(AUTO_SHOW)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
package com.osfans.trime.ime.enums

/** 嵌入模式枚举 */
enum class InlineModeType {
INLINE_NONE,
INLINE_PREVIEW,
INLINE_COMPOSITION,
INLINE_INPUT,
enum class InlinePreeditMode {
NONE,
PREVIEW,
COMPOSITION,
INPUT,
;

companion object {
fun fromString(string: String): InlineModeType {
return when (string) {
"preview", "preedit", "true" -> INLINE_PREVIEW
"composition" -> INLINE_COMPOSITION
"input" -> INLINE_INPUT
else -> INLINE_NONE
}
fun fromString(string: String): InlinePreeditMode {
return runCatching {
InlinePreeditMode.valueOf(string.uppercase())
}.getOrDefault(NONE)
}
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/osfans/trime/ime/enums/Keycode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ enum class Keycode {
private val reverseMap: EnumMap<Keycode, String> = EnumMap(Keycode::class.java)

init {
for (type in values()) {
for (type in entries) {
convertMap[type.toString()] = type
}

Expand Down Expand Up @@ -436,7 +436,7 @@ enum class Keycode {
mask: Int = 0,
): IntArray {
val event = IntArray(2)
if (keycode !in values().indices) return event
if (keycode !in entries.indices) return event
if (keycode < A.ordinal) {
event[0] = keycode
event[1] = mask
Expand Down Expand Up @@ -470,8 +470,8 @@ enum class Keycode {
}

private fun hasSymbolLabel(keycode: Int): Boolean {
if (keycode !in values().indices) return false
return keycode >= A.ordinal || reverseMap.containsKey(values()[keycode])
if (keycode !in entries.indices) return false
return keycode >= A.ordinal || reverseMap.containsKey(entries[keycode])
}

fun getSymbolLabel(keycode: Keycode): String {
Expand Down Expand Up @@ -519,7 +519,7 @@ enum class Keycode {
@JvmStatic
fun valueOf(ordinal: Int): Keycode {
return runCatching {
values()[ordinal]
entries[ordinal]
}.getOrDefault(VoidSymbol)
}

Expand Down
20 changes: 1 addition & 19 deletions app/src/main/java/com/osfans/trime/ime/enums/PopupPosition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,13 @@ enum class PopupPosition {
;

companion object {
@JvmStatic
fun fromString(code: String): PopupPosition {
return runCatching {
valueOf(code.uppercase())
}.getOrDefault(FIXED)
}

@JvmStatic
fun fromString(
code: String,
default: PopupPosition,
default: PopupPosition = FIXED,
): PopupPosition {
return runCatching {
valueOf(code.uppercase())
}.getOrDefault(default)
}

/**
* 解析候选栏文字,按键文字,按键气泡文字的定位方式
*/
@JvmStatic
fun parseKeyPosition(code: String): PopupPosition {
return runCatching {
valueOf(code.uppercase())
}.getOrDefault(CENTER)
}
}
}

This file was deleted.

0 comments on commit 318d65b

Please sign in to comment.