Skip to content

Commit

Permalink
fix: couldn't smart match the keyboard corresponding to the schema id
Browse files Browse the repository at this point in the history
feat(core): add inputStatusCached to new api interface
  • Loading branch information
WhiredPlanck committed Sep 18, 2024
1 parent 90a7960 commit fe9c39d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Rime :
override var schemaItemCached = SchemaItem(".default")
private set

override var inputStatusCached = InputStatus()
private set

private val dispatcher =
RimeDispatcher(
object : RimeDispatcher.RimeLooper {
Expand Down Expand Up @@ -153,13 +156,28 @@ class Rime :
}
}

private fun handleRimeResponse(response: RimeResponse) {
if (response.status != null) {
val (item, status) =
response.status.run {
SchemaItem(schemaId, schemaName) to
InputStatus(isDisabled, isComposing, isAsciiMode, isFullShape, isSimplified, isTraditional, isAsciiPunch)
}
inputStatusCached = status
if (item != schemaItemCached) {
schemaItemCached = item
}
}
}

fun startup(fullCheck: Boolean) {
if (lifecycle.currentStateFlow.value != RimeLifecycle.State.STOPPED) {
Timber.w("Skip starting rime: not at stopped state!")
return
}
if (appContext.isStorageAvailable()) {
registerRimeNotificationHandler(::handleRimeNotification)
registerRimeResponseHandler(::handleRimeResponse)
lifecycleImpl.emitState(RimeLifecycle.State.STARTING)
dispatcher.start(fullCheck)
}
Expand All @@ -179,6 +197,7 @@ class Rime :
}
lifecycleImpl.emitState(RimeLifecycle.State.STOPPED)
unregisterRimeNotificationHandler(::handleRimeNotification)
unregisterRimeResponseHandler(::handleRimeResponse)
}

companion object {
Expand All @@ -198,6 +217,8 @@ class Rime :

private val notificationHandlers = ArrayList<(RimeNotification<*>) -> Unit>()

private val responseHandlers = ArrayList<(RimeResponse) -> Unit>()

init {
System.loadLibrary("rime_jni")
}
Expand Down Expand Up @@ -492,5 +513,14 @@ class Rime :
Timber.d("Got Rime response: $response")
responseFlow_.tryEmit(response)
}

private fun registerRimeResponseHandler(handler: (RimeResponse) -> Unit) {
if (responseHandlers.contains(handler)) return
responseHandlers.add(handler)
}

private fun unregisterRimeResponseHandler(handler: (RimeResponse) -> Unit) {
responseHandlers.remove(handler)
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface RimeApi {

val schemaItemCached: SchemaItem

val inputStatusCached: InputStatus

suspend fun isEmpty(): Boolean

suspend fun processKey(
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/osfans/trime/core/Structs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ 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,
)

0 comments on commit fe9c39d

Please sign in to comment.