Skip to content

Commit

Permalink
refactor: wrap the message value of a rime notification into data class
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Mar 11, 2024
1 parent 8825177 commit f767aec
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Rime(fullCheck: Boolean) {
private var mStatus: RimeStatus? = null
private var isHandlingRimeNotification = false
private val notificationFlow_ =
MutableSharedFlow<RimeNotification>(
MutableSharedFlow<RimeNotification<*>>(
extraBufferCapacity = 15,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
Expand Down
45 changes: 25 additions & 20 deletions app/src/main/java/com/osfans/trime/core/RimeNotification.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
package com.osfans.trime.core

sealed class RimeNotification {
sealed class RimeNotification<T>(open val value: T) {
abstract val messageType: MessageType

data class SchemaNotification(val messageValue: String) :
RimeNotification() {
data class SchemaNotification(override val value: Value) :
RimeNotification<SchemaNotification.Value>(value) {
override val messageType: MessageType
get() = MessageType.Schema

val schemaId get() = messageValue.substringBefore('/')
val schemaName get() = messageValue.substringAfter('/')
data class Value(val schemaId: String, val schemaName: String)

override fun toString() = "SchemaEvent(schemaId=$schemaId, schemaName=$schemaName"
override fun toString() = "SchemaEvent(schemaId=${value.schemaId}, schemaName=${value.schemaName})"
}

data class OptionNotification(val messageValue: String) :
RimeNotification() {
data class OptionNotification(override val value: Value) :
RimeNotification<OptionNotification.Value>(value) {
override val messageType: MessageType
get() = MessageType.Option

val option = messageValue.substringAfter('!')
val value = !messageValue.startsWith('!')
data class Value(val option: String, val value: Boolean)

override fun toString() = "OptionNotification(option=$option, value=$value)"
override fun toString() = "OptionNotification(option=${value.option}, value=${value.value})"
}

data class DeployNotification(val messageValue: String) :
RimeNotification() {
data class DeployNotification(override val value: String) :
RimeNotification<String>(value) {
override val messageType: MessageType
get() = MessageType.Deploy

val state = messageValue

override fun toString() = "DeployNotification(state=$state)"
override fun toString() = "DeployNotification(state=$value)"
}

data class UnknownNotification(val messageValue: String) :
RimeNotification() {
data class UnknownNotification(override val value: String) :
RimeNotification<String>(value) {
override val messageType: MessageType
get() = MessageType.Unknown
}
Expand All @@ -54,8 +50,17 @@ sealed class RimeNotification {
type: String,
value: String,
) = when (type) {
"schema" -> SchemaNotification(value)
"option" -> OptionNotification(value)
"schema" -> {
val (id, name) = value.split('/', limit = 2)
SchemaNotification(SchemaNotification.Value(id, name))
}
"option" ->
OptionNotification(
OptionNotification.Value(
value.substringAfter('!'),
!value.startsWith('!'),
),
)
"deploy" -> DeployNotification(value)
else -> UnknownNotification(value)
}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,21 @@ class InputView(
}
}

private fun handleRimeNotification(it: RimeNotification) {
private fun handleRimeNotification(it: RimeNotification<*>) {
when (it) {
is RimeNotification.OptionNotification -> {
when (it.option) {
when (it.value.option) {
"_hide_comment" -> {
quickBar.oldCandidateBar.candidates.shouldShowComment = !it.value
quickBar.oldCandidateBar.candidates.shouldShowComment = !it.value.value
}
"_hide_bar",
"_hide_candidate",
-> {
quickBar.view.visibility =
if (it.value) View.GONE else View.VISIBLE
if (it.value.value) View.GONE else View.VISIBLE
}
"_hide_key_hint" -> keyboardWindow.oldMainInputView.mainKeyboardView.showKeyHint = !it.value
"_hide_key_symbol" -> keyboardWindow.oldMainInputView.mainKeyboardView.showKeySymbol = !it.value
"_hide_key_hint" -> keyboardWindow.oldMainInputView.mainKeyboardView.showKeyHint = !it.value.value
"_hide_key_symbol" -> keyboardWindow.oldMainInputView.mainKeyboardView.showKeySymbol = !it.value.value
}
keyboardWindow.oldMainInputView.mainKeyboardView.invalidateAllKeys()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ class TextInputManager private constructor() :
}
}

private fun handleRimeNotification(notification: RimeNotification) {
private fun handleRimeNotification(notification: RimeNotification<*>) {
if (notification is RimeNotification.SchemaNotification) {
SchemaManager.init(notification.schemaId)
SchemaManager.init(notification.value.schemaId)
Rime.updateStatus()
trime.recreateInputView()
} else if (notification is RimeNotification.OptionNotification) {
Rime.updateContext() // 切換中英文、簡繁體時更新候選
val value = notification.value
when (val option = notification.option) {
val value = notification.value.value
when (val option = notification.value.option) {
"ascii_mode" -> {
InputFeedbackManager.ttsLanguage =
locales[if (value) 1 else 0]
Expand Down

0 comments on commit f767aec

Please sign in to comment.