diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 175e83f..5d57dd8 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -32,4 +32,8 @@ dependencies { implementation("com.google.android.material:material:1.9.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.constraintlayout:constraintlayout:2.1.4") + + //Koin + implementation("io.insert-koin:koin-android:3.4.2") + implementation("io.insert-koin:koin-core:3.4.2") } \ No newline at end of file diff --git a/androidApp/src/main/AndroidManifest.xml b/androidApp/src/main/AndroidManifest.xml index 28db67d..7e204d7 100644 --- a/androidApp/src/main/AndroidManifest.xml +++ b/androidApp/src/main/AndroidManifest.xml @@ -2,18 +2,18 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/KmmApplication.kt b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/KmmApplication.kt new file mode 100644 index 0000000..102e500 --- /dev/null +++ b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/KmmApplication.kt @@ -0,0 +1,23 @@ +package com.jetbrains.kmm.androidApp + +import android.app.Application +import com.jetbrains.kmm.androidApp.di.androidAppModule +import com.jetbrains.kmm.shared.di.appModule +import org.koin.android.ext.koin.androidContext +import org.koin.android.ext.koin.androidLogger +//import org.koin.core.context.GlobalContext.startKoin +import org.koin.core.context.startKoin +import org.koin.core.logger.Level + +class KmmApplication : Application() { + + override fun onCreate() { + super.onCreate() + + startKoin { + androidContext(this@KmmApplication) + androidLogger(Level.DEBUG) + modules(appModule() + androidAppModule) + } + } +} \ No newline at end of file diff --git a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/di/androidAppModule.kt b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/di/androidAppModule.kt new file mode 100644 index 0000000..8cdcca3 --- /dev/null +++ b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/di/androidAppModule.kt @@ -0,0 +1,9 @@ +package com.jetbrains.kmm.androidApp.di + +import org.koin.core.module.dsl.singleOf +import org.koin.dsl.module +import com.jetbrains.kmm.androidApp.presenter.GreetingPresenter + +val androidAppModule = module { + singleOf(::GreetingPresenter) +} \ No newline at end of file diff --git a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/presenter/GreetingPresenter.kt b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/presenter/GreetingPresenter.kt new file mode 100644 index 0000000..484fa97 --- /dev/null +++ b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/presenter/GreetingPresenter.kt @@ -0,0 +1,7 @@ +package com.jetbrains.kmm.androidApp.presenter + +import com.jetbrains.kmm.shared.Greeting + +class GreetingPresenter(private val greeting: Greeting) { + fun sayHello() = greeting.sayHello() +} \ No newline at end of file diff --git a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/ui/MainActivity.kt similarity index 67% rename from androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt rename to androidApp/src/main/java/com/jetbrains/kmm/androidApp/ui/MainActivity.kt index b8621cb..3d332e0 100644 --- a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt +++ b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/ui/MainActivity.kt @@ -1,4 +1,4 @@ -package com.jetbrains.kmm.androidApp +package com.jetbrains.kmm.androidApp.ui import androidx.appcompat.app.AppCompatActivity import android.os.Bundle @@ -9,31 +9,40 @@ import com.jetbrains.kmm.shared.Greeting import com.jetbrains.kmm.shared.Calculator import android.widget.TextView import com.jetbrains.androidApp.R +import com.jetbrains.kmm.androidApp.presenter.GreetingPresenter +import org.koin.android.ext.android.inject +//create instance of greeting() Without DI fun greet(): String { return Greeting().greeting() } class MainActivity : AppCompatActivity() { + + //create instance of GreetingPresenter using DI + private val presenter: GreetingPresenter by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val tv: TextView = findViewById(R.id.textView) - tv.text = greet() + val tv1: TextView = findViewById(R.id.textView1) + val tv2: TextView = findViewById(R.id.textView2) + + tv1.text = greet() + tv2.text = presenter.sayHello() val numATV: EditText = findViewById(R.id.editTextNumberDecimalA) val numBTV: EditText = findViewById(R.id.editTextNumberDecimalB) val sumTV: TextView = findViewById(R.id.textViewSum) - val textWatcher = object: TextWatcher { + val textWatcher = object : TextWatcher { override fun afterTextChanged(s: Editable?) { try { val numA = Integer.parseInt(numATV.text.toString()) val numB = Integer.parseInt(numBTV.text.toString()) - sumTV.text = "= " + Calculator.sum(numA, numB).toString() - } catch(e: NumberFormatException) { + sumTV.text = "= " + Calculator.sum(numA, numB).toString() + } catch (e: NumberFormatException) { sumTV.text = "= 🤔" } } diff --git a/androidApp/src/main/res/layout/activity_main.xml b/androidApp/src/main/res/layout/activity_main.xml index 4c4af60..3c9e69f 100644 --- a/androidApp/src/main/res/layout/activity_main.xml +++ b/androidApp/src/main/res/layout/activity_main.xml @@ -1,59 +1,67 @@ - - - - + tools:context="com.jetbrains.kmm.androidApp.ui.MainActivity"> + - - - - - + android:layout_height="wrap_content" + android:orientation="vertical" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + + + + + + + + + + + + + + + - \ No newline at end of file diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml new file mode 100644 index 0000000..7387a21 --- /dev/null +++ b/androidApp/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + + = 🤔 + KMM Sample + \ No newline at end of file diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index a952df1..7683191 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -2,6 +2,11 @@ import SwiftUI @main struct SampleApp: App { + + init() { + HelperKt.doInitKoin() + } + var body: some Scene { WindowGroup { ContentView() diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 59d347b..fc4b3f6 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -25,6 +25,11 @@ kotlin { } sourceSets { + val commonMain by getting { + dependencies { + implementation("io.insert-koin:koin-android:3.4.2") + } + } val commonTest by getting { dependencies { implementation(kotlin("test-common")) diff --git a/shared/src/androidMain/kotlin/com/jetbrains/kmm/shared/Platform.kt b/shared/src/androidMain/kotlin/com/jetbrains/kmm/shared/Platform.kt index 92c2ab1..a51935a 100644 --- a/shared/src/androidMain/kotlin/com/jetbrains/kmm/shared/Platform.kt +++ b/shared/src/androidMain/kotlin/com/jetbrains/kmm/shared/Platform.kt @@ -1,5 +1,9 @@ package com.jetbrains.kmm.shared actual class Platform actual constructor() { - actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT}" + //without DI + actual val platform: String = "Android ${android.os.Build.VERSION.SDK_INT} without DI" + + // With DI + actual val newVal = "from Android using DI!" } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Greeting.kt b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Greeting.kt index 5846d75..a529bbe 100644 --- a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Greeting.kt +++ b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Greeting.kt @@ -4,4 +4,8 @@ class Greeting { fun greeting(): String { return "Hello, ${Platform().platform}!" } + + fun sayHello():String{ + return "Hello ${Platform().newVal}" + } } diff --git a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Platform.kt b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Platform.kt index 914f3f1..b7caba3 100644 --- a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Platform.kt +++ b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/Platform.kt @@ -2,4 +2,5 @@ package com.jetbrains.kmm.shared expect class Platform() { val platform: String + val newVal:String } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/AppModule.kt b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/AppModule.kt new file mode 100644 index 0000000..a366f05 --- /dev/null +++ b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/AppModule.kt @@ -0,0 +1,3 @@ +package com.jetbrains.kmm.shared.di + +fun appModule() = listOf(platformModule, commonModule) \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/CommonModule.kt b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/CommonModule.kt new file mode 100644 index 0000000..dce29fb --- /dev/null +++ b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/CommonModule.kt @@ -0,0 +1,9 @@ +package com.jetbrains.kmm.shared.di + +import org.koin.core.module.dsl.singleOf +import org.koin.dsl.module +import com.jetbrains.kmm.shared.Greeting + +val commonModule = module { + singleOf(::Greeting) +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/PlatformModule.kt b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/PlatformModule.kt new file mode 100644 index 0000000..ff15a3d --- /dev/null +++ b/shared/src/commonMain/kotlin/com/jetbrains/kmm/shared/di/PlatformModule.kt @@ -0,0 +1,10 @@ +package com.jetbrains.kmm.shared.di + +import org.koin.dsl.module +import com.jetbrains.kmm.shared.Platform +import org.koin.core.module.dsl.singleOf + + +val platformModule = module { + singleOf(::Platform) +} \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Helper.kt b/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Helper.kt new file mode 100644 index 0000000..4c4cdeb --- /dev/null +++ b/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Helper.kt @@ -0,0 +1,17 @@ +package com.jetbrains.kmm.shared + +import com.jetbrains.kmm.shared.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.greeting() +} + +fun initKoin() { + startKoin { + modules(appModule()) + } +} \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Platform.kt b/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Platform.kt index 4c5a30c..a5b8706 100644 --- a/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Platform.kt +++ b/shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Platform.kt @@ -4,4 +4,5 @@ import platform.UIKit.UIDevice actual class Platform actual constructor() { actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion + actual val newVal = "from iOS!" } \ No newline at end of file