Skip to content

Commit

Permalink
refactor: rename RimeEvent to RimeNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Apr 2, 2023
1 parent 77996e2 commit ba27779
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 80 deletions.
23 changes: 9 additions & 14 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Expand Up @@ -52,7 +52,7 @@ class Rime(fullCheck: Boolean) {
private var mContext: RimeContext? = null
private var mStatus: RimeStatus? = null
private var isHandlingRimeNotification = false
private val notificationFlow_ = MutableSharedFlow<RimeEvent>(
private val notificationFlow_ = MutableSharedFlow<RimeNotification>(
extraBufferCapacity = 15,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
)
Expand All @@ -61,13 +61,6 @@ class Rime(fullCheck: Boolean) {
System.loadLibrary("rime_jni")
}

fun initSchema() {
Timber.d("initSchema() RimeSchema")
SchemaManager.init(getCurrentRimeSchema())
Timber.d("initSchema() getStatus")
updateStatus()
}

private fun startup(fullCheck: Boolean) {
isHandlingRimeNotification = false

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

Timber.i("Starting up Rime APIs ...")
startupRime(sharedDataDir, userDataDir, fullCheck)
Timber.i("Updating schema switchers ...")
initSchema()

Timber.i("Initializing schema stuffs after starting up ...")
SchemaManager.init(getCurrentRimeSchema())
updateStatus()
}

fun destroy() {
Expand All @@ -98,7 +93,7 @@ class Rime(fullCheck: Boolean) {
return true
}

private fun updateStatus() {
fun updateStatus() {
SchemaManager.updateSwitchOptions()
measureTimeMillis {
mStatus = getRimeStatus() ?: RimeStatus()
Expand Down Expand Up @@ -431,9 +426,9 @@ class Rime(fullCheck: Boolean) {
messageValue: String,
) {
isHandlingRimeNotification = true
val event = RimeEvent.create(messageType, messageValue)
Timber.d("Handling Rime notification: %s", event)
notificationFlow_.tryEmit(event)
val notification = RimeNotification.create(messageType, messageValue)
Timber.d("Handling Rime notification: $notification")
notificationFlow_.tryEmit(notification)
isHandlingRimeNotification = false
}
}
Expand Down
59 changes: 0 additions & 59 deletions app/src/main/java/com/osfans/trime/core/RimeEvent.kt

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeNotification.kt
@@ -0,0 +1,62 @@
package com.osfans.trime.core

sealed class RimeNotification {

abstract val messageType: MessageType

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

val schemaId get() = messageValue.substringBefore('/')
val schemaName get() = messageValue.substringAfter('/')

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

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

val option = messageValue.substringAfter('!')
val value = !messageValue.startsWith('!')

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

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

val state = messageValue

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

data class UnknownNotification(val messageValue: String) :
RimeNotification() {
override val messageType: MessageType
get() = MessageType.Unknown
}

enum class MessageType {
Schema,
Option,
Deploy,
Unknown,
}

companion object RimeNotificationHandler {
@JvmStatic
fun create(type: String, value: String) =
when (type) {
"schema" -> SchemaNotification(value)
"option" -> OptionNotification(value)
"deploy" -> DeployNotification(value)
else -> UnknownNotification(value)
}
}
}
15 changes: 8 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Expand Up @@ -9,7 +9,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.R
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeEvent
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.core.SchemaListItem
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.schema.SchemaManager
Expand Down Expand Up @@ -247,14 +247,15 @@ class TextInputManager private constructor() :
}
}

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

0 comments on commit ba27779

Please sign in to comment.