Skip to content

Commit

Permalink
Merge pull request #321 from mbakgun/feature/koin-inject
Browse files Browse the repository at this point in the history
feature/koin-inject
  • Loading branch information
mbakgun authored Nov 6, 2024
2 parents bc63568 + 6f9d4b4 commit 48ff262
Show file tree
Hide file tree
Showing 22 changed files with 30 additions and 102 deletions.
2 changes: 0 additions & 2 deletions androidApp/src/androidMain/kotlin/com/mbakgun/mj/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mbakgun.mj

import android.app.Application
import com.mbakgun.mj.di.viewModelModule
import di.initKoin
import org.koin.android.ext.koin.androidContext

Expand All @@ -11,7 +10,6 @@ class App : Application() {
super.onCreate()
initKoin {
androidContext(this@App)
modules(viewModelModule)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mbakgun.mj.ui
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.get
import ui.MjImagesApp

class MainActivity : AppCompatActivity() {
Expand All @@ -15,9 +14,7 @@ class MainActivity : AppCompatActivity() {

private fun setComposable() {
setContent {
MjImagesApp(
viewModel = get()
)
MjImagesApp()
}
}
}
2 changes: 0 additions & 2 deletions automotiveApp/src/main/java/com/mbakgun/mj/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mbakgun.mj

import android.app.Application
import com.mbakgun.mj.di.viewModelModule
import di.initKoin
import org.koin.android.ext.koin.androidContext

Expand All @@ -11,7 +10,6 @@ class App : Application() {
super.onCreate()
initKoin {
androidContext(this@App)
modules(viewModelModule)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mbakgun.mj.ui
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.get
import ui.MjImagesApp

class MainActivity : AppCompatActivity() {
Expand All @@ -15,9 +14,7 @@ class MainActivity : AppCompatActivity() {

private fun setComposable() {
setContent {
MjImagesApp(
viewModel = get()
)
MjImagesApp()
}
}
}
11 changes: 2 additions & 9 deletions desktopApp/src/jvmMain/kotlin/com/mbakgun/mj/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ package com.mbakgun.mj
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import di.initKoin
import domain.usecase.MjImagesFetchUseCase
import domain.usecase.MjImagesUseCase
import org.koin.java.KoinJavaComponent.inject
import ui.MjImagesApp
import ui.MjImagesViewModel

fun main() {
System.setProperty("apple.awt.application.appearance", "system")
initKoin { }
val fetchUseCase by inject<MjImagesFetchUseCase>(MjImagesFetchUseCase::class.java)
val useCase by inject<MjImagesUseCase>(MjImagesUseCase::class.java)
val viewModel = MjImagesViewModel(fetchUseCase, useCase)
initKoin()

application {
Window(
onCloseRequest = ::exitApplication,
title = "MjImages",
) {
MjImagesApp(viewModel)
MjImagesApp()
}
}
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ coil-compose = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil3"
coil-compose-core = { module = "io.coil-kt.coil3:coil-compose-core", version.ref = "coil3" }
coil-network-ktor = { module = "io.coil-kt.coil3:coil-network-ktor3", version.ref = "coil3" }
coil-mp = { module = "io.coil-kt.coil3:coil", version.ref = "coil3" }
koin = { module = "io.insert-koin:koin-android", version.ref="koin" }
koinAndroid = { module = "io.insert-koin:koin-android", version.ref="koin" }
koinComposeViewmodel = { module = "io.insert-koin:koin-compose-viewmodel", version.ref = "koin" }
koinCore = { module = "io.insert-koin:koin-core", version.ref="koin" }
koinTest = { module = "io.insert-koin:koin-test", version.ref="koin" }
ktorClient = { module = "io.ktor:ktor-client-android", version.ref="ktorClient" }
Expand Down
7 changes: 1 addition & 6 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import shared
struct ComposeView: UIViewControllerRepresentable {

func makeUIViewController(context: Context) -> UIViewController {
let viewModel = MjImagesViewModel.init(
fetchUseCase: MjImagesFetchUseCase.init(),
useCase: MjImagesUseCase.init()
)
return Main_iosKt.MainViewController(viewModel: viewModel)
return Main_iosKt.MainViewController()
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

struct ContentView: View {

init() {
KoinModuleKt.doInitKoin()
}
Expand Down
3 changes: 2 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ kotlin {

//di
api(libs.koinCore)
api(libs.koinComposeViewmodel)

//network
implementation(libs.ktorClientCore)
Expand All @@ -77,7 +78,7 @@ kotlin {
}

androidMain.dependencies {
api(libs.koin)
api(libs.koinAndroid)
implementation(libs.ktorClient)
implementation(libs.lifecycle.runtime.android)
}
Expand Down
6 changes: 3 additions & 3 deletions shared/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<ID>FunctionNaming:MjImagesApp.kt$@Composable fun PreviewDialog( hqImageUrl: String, onDismissed: () -&gt; Unit, )</ID>
<ID>FunctionNaming:MjImagesApp.kt$@Composable fun PreviewImage(hqImageUrl: String)</ID>
<ID>FunctionNaming:MjImagesApp.kt$@Composable fun ScrollToTopButton( onClick: () -&gt; Unit, modifier: Modifier = Modifier )</ID>
<ID>FunctionNaming:MjImagesApp.kt$@OptIn(ExperimentalMaterialApi::class) @Composable fun MjImagesApp( viewModel: MjImagesViewModel )</ID>
<ID>FunctionNaming:MjImagesApp.kt$@OptIn(ExperimentalMaterialApi::class) @Composable fun MjImagesApp( viewModel: MjImagesViewModel = koinViewModel&lt;MjImagesViewModel&gt;() )</ID>
<ID>FunctionNaming:String0.commonMain.kt$@InternalResourceApi internal fun _collectCommonMainString0Resources(map: MutableMap&lt;String, StringResource&gt;)</ID>
<ID>FunctionNaming:String0.commonMain.kt$private fun init_failed_fetch_message(): StringResource</ID>
<ID>FunctionNaming:String0.commonMain.kt$private fun init_snack_message(): StringResource</ID>
<ID>FunctionNaming:Theme.kt$@Composable fun AppTheme( useDarkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -&gt; Unit )</ID>
<ID>FunctionNaming:main.ios.kt$fun MainViewController(viewModel: MjImagesViewModel): UIViewController</ID>
<ID>LongMethod:MjImagesApp.kt$@OptIn(ExperimentalMaterialApi::class) @Composable fun MjImagesApp( viewModel: MjImagesViewModel )</ID>
<ID>FunctionNaming:main.ios.kt$fun MainViewController(): UIViewController</ID>
<ID>LongMethod:MjImagesApp.kt$@OptIn(ExperimentalMaterialApi::class) @Composable fun MjImagesApp( viewModel: MjImagesViewModel = koinViewModel&lt;MjImagesViewModel&gt;() )</ID>
<ID>MagicNumber:Colors.kt$0xFF1F1B16</ID>
<ID>MagicNumber:Colors.kt$0xFF3E2D16</ID>
<ID>MagicNumber:Colors.kt$0xFF452B00</ID>
Expand Down
9 changes: 8 additions & 1 deletion shared/src/commonMain/kotlin/di/KoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import domain.mapper.MjImagesMapper
import domain.usecase.MjImagesFetchUseCase
import domain.usecase.MjImagesUseCase
import org.koin.core.context.startKoin
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module
import ui.MjImagesViewModel
import util.getDispatcherProvider

private val utilityModule = module {
Expand All @@ -35,7 +37,12 @@ private val useCaseModule = module {
factory { MjImagesUseCase() }
}

private val sharedModules = listOf(useCaseModule, repositoryModule, apiModule, utilityModule)
private val uiModule = module {
viewModelOf(::MjImagesViewModel)
}

private val sharedModules =
listOf(uiModule, useCaseModule, repositoryModule, apiModule, utilityModule)

fun initKoin(appDeclaration: KoinAppDeclaration) = startKoin {
modules(sharedModules)
Expand Down
3 changes: 2 additions & 1 deletion shared/src/commonMain/kotlin/ui/MjImagesApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import kotlinx.coroutines.launch
import midjourneyimagescomposemultiplatform.shared.generated.resources.Res
import midjourneyimagescomposemultiplatform.shared.generated.resources.snack_message
import org.jetbrains.compose.resources.getString
import org.koin.compose.viewmodel.koinViewModel
import ui.theme.AppTheme
import util.OnBottomReached
import util.getAsyncImageLoader
Expand All @@ -96,7 +97,7 @@ import util.getImageProvider
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MjImagesApp(
viewModel: MjImagesViewModel
viewModel: MjImagesViewModel = koinViewModel<MjImagesViewModel>()
) {
setSingletonImageLoaderFactory { context ->
getAsyncImageLoader(context)
Expand Down
5 changes: 2 additions & 3 deletions shared/src/iosMain/kotlin/main.ios.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
import ui.MjImagesApp
import ui.MjImagesViewModel

fun MainViewController(viewModel: MjImagesViewModel): UIViewController =
fun MainViewController(): UIViewController =
ComposeUIViewController {
MjImagesApp(viewModel)
MjImagesApp()
}
2 changes: 0 additions & 2 deletions televisionApp/src/main/java/com/mbakgun/mj/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mbakgun.mj

import android.app.Application
import com.mbakgun.mj.di.viewModelModule
import di.initKoin
import org.koin.android.ext.koin.androidContext

Expand All @@ -11,7 +10,6 @@ class App : Application() {
super.onCreate()
initKoin {
androidContext(this@App)
modules(viewModelModule)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mbakgun.mj.ui
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.get
import ui.MjImagesApp

class MainActivity : AppCompatActivity() {
Expand All @@ -15,9 +14,7 @@ class MainActivity : AppCompatActivity() {

private fun setComposable() {
setContent {
MjImagesApp(
viewModel = get()
)
MjImagesApp()
}
}
}
10 changes: 2 additions & 8 deletions wasmApp/src/wasmJsMain/kotlin/com/mbakgun/mj/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ package com.mbakgun.mj
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import di.initKoin
import domain.usecase.MjImagesFetchUseCase
import domain.usecase.MjImagesUseCase
import kotlinx.browser.document
import ui.MjImagesApp
import ui.MjImagesViewModel

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
val koin = initKoin { }.koin
val fetchUseCase = koin.get<MjImagesFetchUseCase>()
val useCase = koin.get<MjImagesUseCase>()
val viewModel = MjImagesViewModel(fetchUseCase, useCase)
initKoin()

CanvasBasedWindow("MjImagesApp") {
MjImagesApp(viewModel)
MjImagesApp()
}
document.getElementById("loader-container")?.remove()
}
2 changes: 0 additions & 2 deletions wearApp/src/main/java/com/mbakgun/mj/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mbakgun.mj

import android.app.Application
import com.mbakgun.mj.di.viewModelModule
import di.initKoin
import org.koin.android.ext.koin.androidContext

Expand All @@ -11,7 +10,6 @@ class App : Application() {
super.onCreate()
initKoin {
androidContext(this@App)
modules(viewModelModule)
}
}
}
9 changes: 0 additions & 9 deletions wearApp/src/main/java/com/mbakgun/mj/di/ViewModelModule.kt

This file was deleted.

5 changes: 1 addition & 4 deletions wearApp/src/main/java/com/mbakgun/mj/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mbakgun.mj.ui
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.get
import ui.MjImagesApp

class MainActivity : AppCompatActivity() {
Expand All @@ -15,9 +14,7 @@ class MainActivity : AppCompatActivity() {

private fun setComposable() {
setContent {
MjImagesApp(
viewModel = get()
)
MjImagesApp()
}
}
}
11 changes: 2 additions & 9 deletions webApp/src/jsMain/kotlin/com/mbakgun/mj/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@ package com.mbakgun.mj
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import di.initKoin
import domain.usecase.MjImagesFetchUseCase
import domain.usecase.MjImagesUseCase
import kotlinx.browser.document
import org.jetbrains.skiko.wasm.onWasmReady
import ui.MjImagesApp
import ui.MjImagesViewModel

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
val koin = initKoin { }.koin
val fetchUseCase = koin.get<MjImagesFetchUseCase>()
val useCase = koin.get<MjImagesUseCase>()
val viewModel = MjImagesViewModel(fetchUseCase, useCase)

initKoin()
onWasmReady {
CanvasBasedWindow("MjImagesApp") {
MjImagesApp(viewModel)
MjImagesApp()
}
document.getElementById("loader-container")?.remove()
}
Expand Down

0 comments on commit 48ff262

Please sign in to comment.