Skip to content

Commit

Permalink
perf: reduce unnecessary cache refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
nopdan committed Feb 7, 2024
1 parent 9a474b5 commit a4ace6e
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ object KeyboardSwitcher {
private var lastKeyboardId = ".default"
private var lastLockKeyboardId = ".default"
private val keyboardPrefs = KeyboardPrefs()
private var currentTheme = ""
private var currentColor = ""
private val prefs get() = AppPrefs.defaultInstance()

lateinit var currentKeyboard: Keyboard

Expand All @@ -39,7 +42,13 @@ object KeyboardSwitcher {
currentKeyboardId = ".default"
lastKeyboardId = ".default"
lastLockKeyboardId = ".default"
keyboardCache.clear()
if (currentTheme != prefs.theme.selectedTheme ||
currentColor != prefs.theme.selectedColor
) {
currentTheme = prefs.theme.selectedTheme
currentColor = prefs.theme.selectedColor
keyboardCache.clear()
}
switchKeyboard(currentKeyboardId)
}

Expand All @@ -48,7 +57,7 @@ object KeyboardSwitcher {
val currentIdx = theme.allKeyboardIds.indexOf(currentKeyboardId)
var mappedName =
when (name) {
".default" -> autoMatch(name)
".default" -> autoMatch()
".prior" ->
try {
theme.allKeyboardIds[currentIdx - 1]
Expand All @@ -72,10 +81,8 @@ object KeyboardSwitcher {
}
}
else -> {
if (name.isNullOrEmpty()) {
name.ifEmpty {
if (currentKeyboard.isLock) currentKeyboardId else lastLockKeyboardId
} else {
name
}
}
}
Expand All @@ -92,6 +99,10 @@ object KeyboardSwitcher {
mappedName = landscapeKeyboard
}
}
if (mappedName == currentKeyboardId) {
Timber.d("Keyboard is already $mappedName, no need to switch.")
return
}
// 应用键盘布局
Timber.i(
"Switched keyboard from $currentKeyboardId " +
Expand All @@ -109,7 +120,7 @@ object KeyboardSwitcher {
/**
* .default 自动匹配键盘布局
* */
private fun autoMatch(name: String): String {
private fun autoMatch(): String {
// 主题的布局中包含方案id,直接采用
val currentSchemaId = Rime.getCurrentRimeSchema()
if (currentSchemaId in allKeyboardIds) {
Expand Down

0 comments on commit a4ace6e

Please # to comment.