Skip to content

Commit ba27779

Browse files
WhiredPlanckBambooin
authored andcommitted
refactor: rename RimeEvent to RimeNotification
1 parent 77996e2 commit ba27779

File tree

4 files changed

+79
-80
lines changed

4 files changed

+79
-80
lines changed

app/src/main/java/com/osfans/trime/core/Rime.kt

+9-14
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Rime(fullCheck: Boolean) {
5252
private var mContext: RimeContext? = null
5353
private var mStatus: RimeStatus? = null
5454
private var isHandlingRimeNotification = false
55-
private val notificationFlow_ = MutableSharedFlow<RimeEvent>(
55+
private val notificationFlow_ = MutableSharedFlow<RimeNotification>(
5656
extraBufferCapacity = 15,
5757
onBufferOverflow = BufferOverflow.DROP_OLDEST,
5858
)
@@ -61,13 +61,6 @@ class Rime(fullCheck: Boolean) {
6161
System.loadLibrary("rime_jni")
6262
}
6363

64-
fun initSchema() {
65-
Timber.d("initSchema() RimeSchema")
66-
SchemaManager.init(getCurrentRimeSchema())
67-
Timber.d("initSchema() getStatus")
68-
updateStatus()
69-
}
70-
7164
private fun startup(fullCheck: Boolean) {
7265
isHandlingRimeNotification = false
7366

@@ -78,8 +71,10 @@ class Rime(fullCheck: Boolean) {
7871

7972
Timber.i("Starting up Rime APIs ...")
8073
startupRime(sharedDataDir, userDataDir, fullCheck)
81-
Timber.i("Updating schema switchers ...")
82-
initSchema()
74+
75+
Timber.i("Initializing schema stuffs after starting up ...")
76+
SchemaManager.init(getCurrentRimeSchema())
77+
updateStatus()
8378
}
8479

8580
fun destroy() {
@@ -98,7 +93,7 @@ class Rime(fullCheck: Boolean) {
9893
return true
9994
}
10095

101-
private fun updateStatus() {
96+
fun updateStatus() {
10297
SchemaManager.updateSwitchOptions()
10398
measureTimeMillis {
10499
mStatus = getRimeStatus() ?: RimeStatus()
@@ -431,9 +426,9 @@ class Rime(fullCheck: Boolean) {
431426
messageValue: String,
432427
) {
433428
isHandlingRimeNotification = true
434-
val event = RimeEvent.create(messageType, messageValue)
435-
Timber.d("Handling Rime notification: %s", event)
436-
notificationFlow_.tryEmit(event)
429+
val notification = RimeNotification.create(messageType, messageValue)
430+
Timber.d("Handling Rime notification: $notification")
431+
notificationFlow_.tryEmit(notification)
437432
isHandlingRimeNotification = false
438433
}
439434
}

app/src/main/java/com/osfans/trime/core/RimeEvent.kt

-59
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.osfans.trime.core
2+
3+
sealed class RimeNotification {
4+
5+
abstract val messageType: MessageType
6+
7+
data class SchemaNotification(val messageValue: String) :
8+
RimeNotification() {
9+
override val messageType: MessageType
10+
get() = MessageType.Schema
11+
12+
val schemaId get() = messageValue.substringBefore('/')
13+
val schemaName get() = messageValue.substringAfter('/')
14+
15+
override fun toString() = "SchemaEvent(schemaId=$schemaId, schemaName=$schemaName"
16+
}
17+
18+
data class OptionNotification(val messageValue: String) :
19+
RimeNotification() {
20+
override val messageType: MessageType
21+
get() = MessageType.Option
22+
23+
val option = messageValue.substringAfter('!')
24+
val value = !messageValue.startsWith('!')
25+
26+
override fun toString() = "OptionNotification(option=$option, value=$value)"
27+
}
28+
29+
data class DeployNotification(val messageValue: String) :
30+
RimeNotification() {
31+
override val messageType: MessageType
32+
get() = MessageType.Deploy
33+
34+
val state = messageValue
35+
36+
override fun toString() = "DeployNotification(state=$state)"
37+
}
38+
39+
data class UnknownNotification(val messageValue: String) :
40+
RimeNotification() {
41+
override val messageType: MessageType
42+
get() = MessageType.Unknown
43+
}
44+
45+
enum class MessageType {
46+
Schema,
47+
Option,
48+
Deploy,
49+
Unknown,
50+
}
51+
52+
companion object RimeNotificationHandler {
53+
@JvmStatic
54+
fun create(type: String, value: String) =
55+
when (type) {
56+
"schema" -> SchemaNotification(value)
57+
"option" -> OptionNotification(value)
58+
"deploy" -> DeployNotification(value)
59+
else -> UnknownNotification(value)
60+
}
61+
}
62+
}

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import androidx.appcompat.app.AlertDialog
99
import androidx.lifecycle.lifecycleScope
1010
import com.osfans.trime.R
1111
import com.osfans.trime.core.Rime
12-
import com.osfans.trime.core.RimeEvent
12+
import com.osfans.trime.core.RimeNotification
1313
import com.osfans.trime.core.SchemaListItem
1414
import com.osfans.trime.data.AppPrefs
1515
import com.osfans.trime.data.schema.SchemaManager
@@ -247,14 +247,15 @@ class TextInputManager private constructor() :
247247
}
248248
}
249249

250-
private fun handleRimeNotification(event: RimeEvent) {
251-
if (event is RimeEvent.SchemaEvent) {
252-
Rime.initSchema()
250+
private fun handleRimeNotification(notification: RimeNotification) {
251+
if (notification is RimeNotification.SchemaNotification) {
252+
SchemaManager.init(notification.schemaId)
253+
Rime.updateStatus()
253254
trime.initKeyboard()
254-
} else if (event is RimeEvent.OptionEvent) {
255+
} else if (notification is RimeNotification.OptionNotification) {
255256
Rime.updateContext() // 切換中英文、簡繁體時更新候選
256-
val value = event.value
257-
when (val option = event.option) {
257+
val value = notification.value
258+
when (val option = notification.option) {
258259
"ascii_mode" -> {
259260
trime.inputFeedbackManager.ttsLanguage =
260261
locales[if (value) 1 else 0]

0 commit comments

Comments
 (0)