Skip to content

Commit 21429cb

Browse files
committed
chore,refactor: introduce Koin dependency injection library
Use Koin to inject dependencies for the UI components, which make it easier to manage those stuffs and avoid the complex templates and long construct arguments.
1 parent 879c7f5 commit 21429cb

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ dependencies {
126126
implementation(libs.androidx.viewpager2)
127127
implementation(libs.flexbox)
128128
implementation(libs.kaml)
129+
implementation(libs.koin.android)
129130
implementation(libs.timber)
130131
implementation(libs.utilcode)
131132
implementation(libs.xxpermissions)

app/src/main/java/com/osfans/trime/ime/bar/QuickBar.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import com.osfans.trime.databinding.CandidateBarBinding
1010
import com.osfans.trime.databinding.TabBarBinding
1111
import com.osfans.trime.ime.core.Trime
1212
import com.osfans.trime.ime.enums.SymbolKeyboardType
13+
import org.koin.core.component.KoinComponent
14+
import org.koin.core.component.inject
1315
import splitties.views.dsl.core.add
1416
import splitties.views.dsl.core.lParams
1517
import splitties.views.dsl.core.matchParent
1618

17-
class QuickBar(private val context: Context, private val service: Trime) {
19+
class QuickBar : KoinComponent {
20+
private val context: Context by inject()
21+
private val service: Trime by inject()
22+
1823
val oldCandidateBar by lazy {
1924
CandidateBarBinding.inflate(LayoutInflater.from(context)).apply {
2025
with(root) {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import androidx.constraintlayout.widget.ConstraintLayout
88
import com.osfans.trime.ime.bar.QuickBar
99
import com.osfans.trime.ime.keyboard.KeyboardWindow
1010
import com.osfans.trime.util.styledFloat
11+
import org.koin.android.ext.koin.androidContext
12+
import org.koin.android.ext.koin.androidLogger
13+
import org.koin.core.context.startKoin
14+
import org.koin.dsl.module
1115
import splitties.views.dsl.constraintlayout.below
1216
import splitties.views.dsl.constraintlayout.bottomOfParent
1317
import splitties.views.dsl.constraintlayout.centerHorizontally
@@ -27,12 +31,27 @@ class InputView(
2731
val service: Trime,
2832
) : ConstraintLayout(service) {
2933
private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
30-
val quickBar = QuickBar(context, service)
31-
val keyboardWindow = KeyboardWindow(context, service)
34+
val quickBar = QuickBar()
35+
val keyboardWindow = KeyboardWindow()
36+
37+
private val module =
38+
module {
39+
single { this@InputView }
40+
single { themedContext }
41+
single { service }
42+
single { keyboardWindow }
43+
single { quickBar }
44+
}
3245

3346
val keyboardView: View
3447

3548
init {
49+
startKoin {
50+
androidLogger()
51+
androidContext(context)
52+
modules(module)
53+
}
54+
3655
keyboardView =
3756
constraintLayout {
3857
isMotionEventSplittingEnabled = true

app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardWindow.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import com.osfans.trime.core.Rime
77
import com.osfans.trime.databinding.MainInputLayoutBinding
88
import com.osfans.trime.databinding.SymbolInputLayoutBinding
99
import com.osfans.trime.ime.core.Trime
10+
import org.koin.core.component.KoinComponent
11+
import org.koin.core.component.inject
1012
import splitties.views.dsl.core.add
1113
import splitties.views.dsl.core.lParams
1214
import splitties.views.dsl.core.matchParent
1315

14-
class KeyboardWindow(private val context: Context, private val service: Trime) {
16+
class KeyboardWindow : KoinComponent {
17+
private val context: Context by inject()
18+
private val service: Trime by inject()
19+
1520
val oldMainInputView by lazy {
1621
MainInputLayoutBinding.inflate(LayoutInflater.from(context)).apply {
1722
with(mainKeyboardView) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class TextInputManager private constructor(private val isDarkMode: Boolean) :
160160
restarting: Boolean,
161161
) {
162162
super.onStartInputView(instance, restarting)
163-
Trime.getService().selectLiquidKeyboard(-1)
163+
trime.selectLiquidKeyboard(-1)
164164
if (restarting) {
165165
trime.performEscape()
166166
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
2727
androidx-viewpager2 = { module = "androidx.viewpager2:viewpager2", version = "1.1.0-beta02" }
2828
flexbox = { module = "com.google.android.flexbox:flexbox", version = "3.0.0" }
2929
kaml = { module = "com.charleskorn.kaml:kaml", version = "0.56.0" }
30+
koin-android = { module = "io.insert-koin:koin-android", version = "3.5.3" }
3031
timber = { module = "com.jakewharton.timber:timber", version = "5.0.1" }
3132
utilcode = { module = "com.blankj:utilcodex", version = "1.31.1" }
3233
xxpermissions = { module = "com.github.getActivity:XXPermissions", version = "18.5" }

0 commit comments

Comments
 (0)