Skip to content

Commit

Permalink
refactor: merge schemaItemCached and inputStatusCached into `stat…
Browse files Browse the repository at this point in the history
…usCached`

It's unnecessary to split them out.
  • Loading branch information
WhiredPlanck committed Jan 19, 2025
1 parent 3128121 commit ebfda6e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 60 deletions.
23 changes: 4 additions & 19 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ class Rime :
override val isReady: Boolean
get() = lifecycle.currentStateFlow.value == RimeLifecycle.State.READY

override var schemaItemCached = SchemaItem(".default")
private set

override var inputStatusCached = InputStatus()
override var statusCached = RimeProto.Status()
private set

override var compositionCached = RimeProto.Context.Composition()
Expand Down Expand Up @@ -148,12 +145,6 @@ class Rime :

override suspend fun selectSchema(schemaId: String) = withRimeContext { selectRimeSchema(schemaId) }

override suspend fun currentSchema() =
withRimeContext {
val schema = getRimeStatus()?.let { SchemaItem(it.schemaId, it.schemaName) }
schema ?: schemaItemCached
}

override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { if (it) requireResponse() } }

override suspend fun clearComposition() =
Expand Down Expand Up @@ -186,12 +177,12 @@ class Rime :
private fun handleRimeMessage(it: RimeMessage<*>) {
when (it) {
is RimeMessage.SchemaMessage -> {
schemaItemCached = it.data
statusCached = getRimeStatus()!!
SchemaManager.init(it.data.id)
}
is RimeMessage.OptionMessage -> {
getRimeStatus()?.let {
inputStatusCached = InputStatus.fromStatus(it)
statusCached = it
inputStatus = it // for compatibility
}
SchemaManager.updateSwitchOptions()
Expand All @@ -204,14 +195,8 @@ class Rime :
is RimeMessage.ResponseMessage ->
it.data.let event@{ data ->
data.status.let {
val status = InputStatus.fromStatus(it)
inputStatusCached = status
statusCached = it
inputStatus = it // for compatibility

val item = SchemaItem.fromStatus(it)
if (item != schemaItemCached) {
schemaItemCached = item
}
}
inputContext = data.context // for compatibility
compositionCached = data.context.composition
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ interface RimeApi {

val isReady: Boolean

val schemaItemCached: SchemaItem

val inputStatusCached: InputStatus
val statusCached: RimeProto.Status

val compositionCached: RimeProto.Context.Composition

Expand Down Expand Up @@ -57,8 +55,6 @@ interface RimeApi {

suspend fun selectSchema(schemaId: String): Boolean

suspend fun currentSchema(): SchemaItem

suspend fun commitComposition(): Boolean

suspend fun clearComposition()
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/osfans/trime/core/RimeProto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ class RimeProto {
}

data class Status(
val schemaId: String,
val schemaName: String,
val isDisabled: Boolean,
val isComposing: Boolean,
val isAsciiMode: Boolean,
val isFullShape: Boolean,
val isSimplified: Boolean,
val isTraditional: Boolean,
val isAsciiPunch: Boolean,
val schemaId: String = "",
val schemaName: String = "",
val isDisabled: Boolean = true,
val isComposing: Boolean = false,
val isAsciiMode: Boolean = true,
val isFullShape: Boolean = false,
val isSimplified: Boolean = false,
val isTraditional: Boolean = false,
val isAsciiPunch: Boolean = true,
)
}
21 changes: 1 addition & 20 deletions app/src/main/java/com/osfans/trime/core/Structs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,9 @@ package com.osfans.trime.core
data class SchemaItem(
val id: String,
val name: String = "",
) {
companion object {
fun fromStatus(status: RimeProto.Status) = status.run { SchemaItem(schemaId, schemaName) }
}
}
)

data class CandidateItem(
val comment: String,
val text: String,
)

data class InputStatus(
val isDisabled: Boolean = true,
val isComposing: Boolean = false,
val isAsciiMode: Boolean = true,
val isFullShape: Boolean = false,
val isSimplified: Boolean = false,
val isTraditional: Boolean = false,
val isAsciiPunch: Boolean = true,
) {
companion object {
fun fromStatus(status: RimeProto.Status) =
status.run { InputStatus(isDisabled, isComposing, isAsciiMode, isFullShape, isSimplified, isTraditional, isAsciiPunch) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class KeyboardWindow(

override fun onCreateView(): View {
keyboardView = context.frameLayout(R.id.keyboard_view)
attachKeyboard(evalKeyboard(rime.run { schemaItemCached.id }))
attachKeyboard(evalKeyboard(rime.run { statusCached }.schemaId))
return keyboardView
}

Expand All @@ -112,7 +112,7 @@ class KeyboardWindow(
}
if (it.isLock) lastLockKeyboardId = target
dispatchCapsState(it::setShifted)
val isAsciiMode = rime.run { inputStatusCached }.isAsciiMode
val isAsciiMode = rime.run { statusCached }.isAsciiMode
if (isAsciiMode != it.currentAsciiMode) {
service.postRimeJob { setRuntimeOption("ascii_mode", it.currentAsciiMode) }
}
Expand All @@ -132,9 +132,9 @@ class KeyboardWindow(

private fun smartMatchKeyboard(): String {
// 主题的布局中包含方案id,直接采用
val currentSchema = rime.run { schemaItemCached }
if (presetKeyboardIds.contains(currentSchema.id)) {
return currentSchema.id
val currentSchema = rime.run { statusCached }.schemaId
if (presetKeyboardIds.contains(currentSchema)) {
return currentSchema
}
val alphabet = SchemaManager.activeSchema.alphabet ?: return "default"
val layout =
Expand Down Expand Up @@ -237,7 +237,7 @@ class KeyboardWindow(
}
switchKeyboard(targetKeyboard)
currentKeyboard?.let {
val isAsciiMode = rime.run { inputStatusCached }.isAsciiMode
val isAsciiMode = rime.run { statusCached }.isAsciiMode
if (tempAsciiMode) {
if (!isAsciiMode) {
service.postRimeJob { setRuntimeOption("ascii_mode", true) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class LiquidKeyboard(
private fun triggerSymbolInput(symbol: String) {
commonKeyboardActionListener.listener.onPress(KeyEvent.KEYCODE_UNKNOWN)
rime.launchOnReady {
val (isAsciiMode, isAsciiPunch) = it.inputStatusCached.run { isAsciiMode to isAsciiPunch }
val (isAsciiMode, isAsciiPunch) = it.statusCached.run { isAsciiMode to isAsciiPunch }
if (isAsciiMode) it.setRuntimeOption("ascii_mode", false)
if (isAsciiPunch) it.setRuntimeOption("ascii_punch", false)
commonKeyboardActionListener.listener.onText("{Escape}$symbol")
Expand Down

0 comments on commit ebfda6e

Please sign in to comment.