mirror of
https://github.com/LiveFastEatTrashRaccoon/RaccoonForLemmy.git
synced 2025-02-02 13:46:53 +01:00
chore: add koin for dependency injection
This commit is contained in:
parent
e47020303c
commit
2510933c6a
@ -47,4 +47,6 @@ dependencies {
|
||||
implementation(libs.material3)
|
||||
implementation(libs.material3.windowsize)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.koin.core)
|
||||
implementation(libs.koin.android)
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:name=".MainApplication"
|
||||
android:allowBackup="false"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
|
@ -6,14 +6,14 @@ import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import com.github.diegoberaldin.raccoonforlemmy.Greeting
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.presentation.GreetPresenter
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.ui.GreetingView
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.ui.theme.AppTheme
|
||||
import org.koin.android.ext.android.inject
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
@ -22,22 +22,10 @@ class MainActivity : ComponentActivity() {
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colorScheme.background
|
||||
) {
|
||||
GreetingView(Greeting().greet())
|
||||
val presenter: GreetPresenter by inject()
|
||||
GreetingView(presenter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GreetingView(text: String) {
|
||||
Text(text = text)
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
fun DefaultPreview() {
|
||||
AppTheme {
|
||||
GreetingView("Hello, Android!")
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.android
|
||||
|
||||
import android.app.Application
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.di.androidModule
|
||||
import com.github.diegoberaldin.raccoonforlemmy.di.appModule
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.android.ext.koin.androidLogger
|
||||
import org.koin.core.context.startKoin
|
||||
|
||||
class MainApplication : Application() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
startKoin {
|
||||
androidContext(this@MainApplication)
|
||||
androidLogger()
|
||||
modules(
|
||||
appModule() + androidModule,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.android.di
|
||||
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.presentation.GreetPresenter
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val androidModule = module {
|
||||
singleOf(::GreetPresenter)
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.android.presentation
|
||||
|
||||
import com.github.diegoberaldin.raccoonforlemmy.Greeting
|
||||
|
||||
class GreetPresenter(
|
||||
private val greeting: Greeting,
|
||||
) {
|
||||
|
||||
fun print() = greeting.greet()
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.android.ui
|
||||
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.github.diegoberaldin.raccoonforlemmy.android.presentation.GreetPresenter
|
||||
|
||||
@Composable
|
||||
fun GreetingView(presenter: GreetPresenter) {
|
||||
val text = presenter.print()
|
||||
Text(text = text)
|
||||
}
|
@ -2,17 +2,23 @@
|
||||
androidx_activity_compose = "1.7.2"
|
||||
android_gradle = "7.4.2"
|
||||
compose = "1.4.3"
|
||||
koin = "3.2.0"
|
||||
kotlin = "1.8.22"
|
||||
material3 = "1.1.1"
|
||||
|
||||
[libraries]
|
||||
|
||||
androidx_activity_compose = { module = "androidx.activity:activity-compose", version.ref = "androidx.activity.compose" }
|
||||
|
||||
compose_ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
|
||||
compose_tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
|
||||
compose_preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
|
||||
compose_foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
|
||||
|
||||
koin_core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
|
||||
koin_test = { module = "io.insert-koin:koin-test", version.ref = "koin" }
|
||||
koin_android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
|
||||
|
||||
material3 = { module = "androidx.compose.material3:material3", version.ref = "material3" }
|
||||
material3_windowsize = { module = "androidx.compose.material3:material3-window-size-class", version.ref = "material3" }
|
||||
|
||||
|
@ -2,7 +2,7 @@ import SwiftUI
|
||||
import shared
|
||||
|
||||
struct ContentView: View {
|
||||
let greet = Greeting().greet()
|
||||
let greet = GreetingHelper().greet()
|
||||
|
||||
var body: some View {
|
||||
Text(greet)
|
||||
|
@ -2,6 +2,11 @@ import SwiftUI
|
||||
|
||||
@main
|
||||
struct iOSApp: App {
|
||||
|
||||
init() {
|
||||
HelperKt.doInitKoin()
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
|
@ -23,7 +23,12 @@ kotlin {
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
val commonMain by getting
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
api(libs.koin.core)
|
||||
api(libs.koin.test)
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
dependencies {
|
||||
implementation(kotlin("test"))
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy
|
||||
|
||||
class Greeting {
|
||||
private val platform: Platform = getPlatform()
|
||||
class Greeting(
|
||||
private val platform: Platform,
|
||||
) {
|
||||
|
||||
fun greet(): String {
|
||||
return "Hello, ${platform.name}!"
|
||||
|
@ -0,0 +1,3 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.di
|
||||
|
||||
fun appModule() = listOf(commonModule, platformModule)
|
@ -0,0 +1,9 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.di
|
||||
|
||||
import com.github.diegoberaldin.raccoonforlemmy.Greeting
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val commonModule = module {
|
||||
singleOf(::Greeting)
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy.di
|
||||
|
||||
import com.github.diegoberaldin.raccoonforlemmy.getPlatform
|
||||
import org.koin.core.module.dsl.singleOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val platformModule = module {
|
||||
singleOf(::getPlatform)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.github.diegoberaldin.raccoonforlemmy
|
||||
|
||||
import com.github.diegoberaldin.raccoonforlemmy.di.appModule
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import org.koin.core.context.startKoin
|
||||
|
||||
class GreetingHelper : KoinComponent {
|
||||
private val greeting: Greeting by inject()
|
||||
fun greet(): String = greeting.greet()
|
||||
}
|
||||
|
||||
fun initKoin() {
|
||||
startKoin {
|
||||
modules(appModule())
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user