Skip to content

Commit

Permalink
refactor: add schemata stuffs to RimeApi
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Apr 24, 2024
1 parent d903b31 commit 199bfd5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 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 @@ -26,6 +26,7 @@ import com.osfans.trime.util.isStorageAvailable
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.withContext
import timber.log.Timber
import kotlin.system.measureTimeMillis

Expand Down Expand Up @@ -71,6 +72,28 @@ class Rime : RimeApi, RimeLifecycleOwner {
},
)

private suspend inline fun <T> withRimeContext(crossinline block: suspend () -> T): T =
withContext(dispatcher) {
block()
}

override suspend fun isEmpty(): Boolean =
withRimeContext {
getCurrentRimeSchema() == ".default" // 無方案
}

override suspend fun availableSchemata(): Array<SchemaListItem> = withRimeContext { getAvailableRimeSchemaList() }

override suspend fun enabledSchemata(): Array<SchemaListItem> = withRimeContext { getSelectedRimeSchemaList() }

override suspend fun setEnabledSchemata(schemaIds: Array<String>) = withRimeContext { selectRimeSchemas(schemaIds) }

override suspend fun selectedSchemata(): Array<SchemaListItem> = withRimeContext { getRimeSchemaList() }

override suspend fun selectedSchemaId(): String = withRimeContext { getCurrentRimeSchema() }

override suspend fun selectSchema(schemaId: String) = withRimeContext { selectRimeSchema(schemaId) }

fun startup(fullCheck: Boolean) {
if (lifecycle.stateFlow.value != RimeLifecycle.State.STOPPED) {
Timber.w("Skip starting rime: not at stopped state!")
Expand Down
14 changes: 14 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 @@ -8,4 +8,18 @@ interface RimeApi {
val stateFlow: SharedFlow<RimeLifecycle.State>

val isReady: Boolean

suspend fun isEmpty(): Boolean

suspend fun availableSchemata(): Array<SchemaListItem>

suspend fun enabledSchemata(): Array<SchemaListItem>

suspend fun setEnabledSchemata(schemaIds: Array<String>): Boolean

suspend fun selectedSchemata(): Array<SchemaListItem>

suspend fun selectedSchemaId(): String

suspend fun selectSchema(schemaId: String): Boolean
}

0 comments on commit 199bfd5

Please sign in to comment.