Skip to content

Commit 82c4aa0

Browse files
committed
refactor: move RimeResponse into RimeEvent as IpcResponse(Event)
1 parent 19005e6 commit 82c4aa0

File tree

5 files changed

+72
-64
lines changed

5 files changed

+72
-64
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Rime :
5454

5555
lifecycleImpl.emitState(RimeLifecycle.State.READY)
5656

57-
requestRimeResponse()
57+
ipcResponseCallback()
5858
SchemaManager.init(getCurrentRimeSchema())
5959
}
6060

@@ -81,7 +81,7 @@ class Rime :
8181
withRimeContext {
8282
processRimeKey(value, modifiers.toInt()).also {
8383
if (it) {
84-
requestRimeResponse()
84+
ipcResponseCallback()
8585
} else {
8686
keyEventCallback(KeyValue(value), KeyModifiers(modifiers))
8787
}
@@ -95,7 +95,7 @@ class Rime :
9595
withRimeContext {
9696
processRimeKey(value.value, modifiers.toInt()).also {
9797
if (it) {
98-
requestRimeResponse()
98+
ipcResponseCallback()
9999
} else {
100100
keyEventCallback(value, modifiers)
101101
}
@@ -104,12 +104,12 @@ class Rime :
104104

105105
override suspend fun selectCandidate(idx: Int): Boolean =
106106
withRimeContext {
107-
selectRimeCandidate(idx).also { if (it) requestRimeResponse() }
107+
selectRimeCandidate(idx).also { if (it) ipcResponseCallback() }
108108
}
109109

110110
override suspend fun forgetCandidate(idx: Int): Boolean =
111111
withRimeContext {
112-
forgetRimeCandidate(idx).also { if (it) requestRimeResponse() }
112+
forgetRimeCandidate(idx).also { if (it) ipcResponseCallback() }
113113
}
114114

115115
override suspend fun availableSchemata(): Array<SchemaItem> = withRimeContext { getAvailableRimeSchemaList() }
@@ -130,12 +130,12 @@ class Rime :
130130
schema ?: schemaItemCached
131131
}
132132

133-
override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { if (it) requestRimeResponse() } }
133+
override suspend fun commitComposition(): Boolean = withRimeContext { commitRimeComposition().also { if (it) ipcResponseCallback() } }
134134

135135
override suspend fun clearComposition() =
136136
withRimeContext {
137137
clearRimeComposition()
138-
requestRimeResponse()
138+
ipcResponseCallback()
139139
}
140140

141141
override suspend fun setRuntimeOption(
@@ -177,19 +177,20 @@ class Rime :
177177
"start" -> OpenCCDictManager.buildOpenCCDict()
178178
}
179179
}
180-
is RimeResponse -> {
181-
it.status?.let {
182-
val status = InputStatus.fromStatus(it)
183-
inputStatusCached = status
184-
inputStatus = it // for compatibility
185-
186-
val item = SchemaItem.fromStatus(it)
187-
if (item != schemaItemCached) {
188-
schemaItemCached = item
180+
is RimeEvent.IpcResponseEvent ->
181+
it.data.let event@{ data ->
182+
data.status?.let {
183+
val status = InputStatus.fromStatus(it)
184+
inputStatusCached = status
185+
inputStatus = it // for compatibility
186+
187+
val item = SchemaItem.fromStatus(it)
188+
if (item != schemaItemCached) {
189+
schemaItemCached = item
190+
}
189191
}
192+
data.context?.let { inputContext = it } // for compatibility
190193
}
191-
it.context?.let { inputContext = it } // for compatibility
192-
}
193194
else -> {}
194195
}
195196
}
@@ -299,7 +300,7 @@ class Rime :
299300
return processRimeKey(keycode, mask).also {
300301
Timber.d("processKey ${if (it) "success" else "failed"}")
301302
if (it) {
302-
requestRimeResponse()
303+
ipcResponseCallback()
303304
} else {
304305
keyEventCallback(KeyValue(keycode), KeyModifiers.of(mask))
305306
}
@@ -314,7 +315,7 @@ class Rime :
314315
sequence.toString().replace("{}", "{braceleft}{braceright}"),
315316
).also {
316317
Timber.d("simulateKeySequence ${if (it) "success" else "failed"}")
317-
if (it) requestRimeResponse()
318+
if (it) ipcResponseCallback()
318319
}
319320
}
320321

@@ -334,7 +335,7 @@ class Rime :
334335
@JvmStatic
335336
fun setCaretPos(caretPos: Int) {
336337
setRimeCaretPos(caretPos)
337-
requestRimeResponse()
338+
ipcResponseCallback()
338339
}
339340

340341
// init
@@ -498,10 +499,11 @@ class Rime :
498499
callbackFlow_.tryEmit(notification)
499500
}
500501

501-
fun requestRimeResponse() {
502-
val response = RimeResponse(getRimeCommit(), getRimeContext(), getRimeStatus())
503-
Timber.d("Got Rime response: $response")
504-
callbackFlow_.tryEmit(response)
502+
private fun ipcResponseCallback() {
503+
handleRimeEvent(
504+
RimeEvent.EventType.IpcResponse,
505+
RimeEvent.IpcResponseEvent.Data(getRimeCommit(), getRimeContext(), getRimeStatus()),
506+
)
505507
}
506508

507509
private fun keyEventCallback(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ sealed class RimeEvent<T>(
1010
) : RimeCallback {
1111
abstract val eventType: EventType
1212

13+
data class IpcResponseEvent(
14+
override val data: Data,
15+
) : RimeEvent<IpcResponseEvent.Data>(data) {
16+
override val eventType = EventType.IpcResponse
17+
18+
data class Data(
19+
val commit: RimeProto.Commit?,
20+
val context: RimeProto.Context?,
21+
val status: RimeProto.Status?,
22+
)
23+
}
24+
1325
data class KeyEvent(
1426
override val data: Data,
1527
) : RimeEvent<KeyEvent.Data>(data) {
@@ -22,6 +34,7 @@ sealed class RimeEvent<T>(
2234
}
2335

2436
enum class EventType {
37+
IpcResponse,
2538
Key,
2639
}
2740

@@ -30,6 +43,9 @@ sealed class RimeEvent<T>(
3043
type: EventType,
3144
data: T,
3245
) = when (type) {
46+
EventType.IpcResponse -> {
47+
IpcResponseEvent(data as IpcResponseEvent.Data)
48+
}
3349
EventType.Key ->
3450
KeyEvent(data as KeyEvent.Data)
3551
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

app/src/main/java/com/osfans/trime/ime/core/InputView.kt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import androidx.core.view.updateLayoutParams
2424
import androidx.lifecycle.lifecycleScope
2525
import com.osfans.trime.core.CandidateItem
2626
import com.osfans.trime.core.RimeCallback
27+
import com.osfans.trime.core.RimeEvent
2728
import com.osfans.trime.core.RimeNotification
28-
import com.osfans.trime.core.RimeResponse
2929
import com.osfans.trime.daemon.RimeSession
3030
import com.osfans.trime.data.prefs.AppPrefs
3131
import com.osfans.trime.data.theme.ColorManager
@@ -334,25 +334,26 @@ class InputView(
334334
}
335335
}
336336
}
337-
is RimeResponse -> {
338-
val ctx = it.context
339-
if (ctx != null) {
340-
broadcaster.onInputContextUpdate(ctx)
341-
val candidates = ctx.menu.candidates.map { CandidateItem(it.comment ?: "", it.text) }
342-
val isLastPage = ctx.menu.isLastPage
343-
val previous = ctx.menu.run { pageSize * pageNumber }
344-
val highlightedIdx = ctx.menu.highlightedCandidateIndex
345-
if (composition.isPopupWindowEnabled) {
346-
val sticky = composition.composition.update(ctx)
347-
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx, sticky)
348-
} else {
349-
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx)
350-
}
351-
if (candidates.isEmpty()) {
352-
compactCandidate.refreshUnrolled()
337+
is RimeEvent.IpcResponseEvent ->
338+
it.data.let event@{
339+
val ctx = it.context
340+
if (ctx != null) {
341+
broadcaster.onInputContextUpdate(ctx)
342+
val candidates = ctx.menu.candidates.map { CandidateItem(it.comment ?: "", it.text) }
343+
val isLastPage = ctx.menu.isLastPage
344+
val previous = ctx.menu.run { pageSize * pageNumber }
345+
val highlightedIdx = ctx.menu.highlightedCandidateIndex
346+
if (composition.isPopupWindowEnabled) {
347+
val sticky = composition.composition.update(ctx)
348+
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx, sticky)
349+
} else {
350+
compactCandidate.adapter.updateCandidates(candidates, isLastPage, previous, highlightedIdx)
351+
}
352+
if (candidates.isEmpty()) {
353+
compactCandidate.refreshUnrolled()
354+
}
353355
}
354356
}
355-
}
356357
else -> {}
357358
}
358359
}

app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import com.osfans.trime.core.RimeEvent
4242
import com.osfans.trime.core.RimeKeyMapping
4343
import com.osfans.trime.core.RimeNotification
4444
import com.osfans.trime.core.RimeProto
45-
import com.osfans.trime.core.RimeResponse
4645
import com.osfans.trime.daemon.RimeDaemon
4746
import com.osfans.trime.daemon.RimeSession
4847
import com.osfans.trime.data.db.DraftHelper
@@ -285,16 +284,17 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
285284
}
286285
}
287286
}
288-
is RimeResponse -> {
289-
val (commit, ctx) = it
290-
if (commit?.text?.isNotEmpty() == true) {
291-
commitText(commit.text)
292-
}
293-
if (ctx != null) {
294-
updateComposingText(ctx)
287+
is RimeEvent.IpcResponseEvent ->
288+
it.data.let event@{
289+
val (commit, ctx) = it
290+
if (commit?.text?.isNotEmpty() == true) {
291+
commitText(commit.text)
292+
}
293+
if (ctx != null) {
294+
updateComposingText(ctx)
295+
}
296+
updateComposing()
295297
}
296-
updateComposing()
297-
}
298298
is RimeEvent.KeyEvent ->
299299
it.data.let event@{
300300
val keyCode = it.value.keyCode

0 commit comments

Comments
 (0)