Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Adding Koin DI to KMM Sample Project #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 4 additions & 4 deletions androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name="com.jetbrains.kmm.androidApp.KmmApplication"
android:allowBackup="false"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.jetbrains.kmm.androidApp.MainActivity"
android:name="com.jetbrains.kmm.androidApp.ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jetbrains.kmm.androidApp
package com.jetbrains.kmm.androidApp.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Expand All @@ -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 = "= 🤔"
}
}
Expand Down
106 changes: 57 additions & 49 deletions androidApp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_view"
tools:context="com.jetbrains.kmm.androidApp.MainActivity">

<LinearLayout
android:layout_width="wrap_content"
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
/>
tools:context="com.jetbrains.kmm.androidApp.ui.MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<EditText
android:id="@+id/editTextNumberDecimalA"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:autofillHints="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
/>
<EditText
android:id="@+id/editTextNumberDecimalB"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:autofillHints="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewSum"
android:text="= 🤔"
/>
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">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />


<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<EditText
android:id="@+id/editTextNumberDecimalA"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:inputType="number" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />

<EditText
android:id="@+id/editTextNumberDecimalB"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:inputType="number" />

<TextView
android:id="@+id/textViewSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thinking_smiley" />
</LinearLayout>
</LinearLayout>
</LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 5 additions & 0 deletions androidApp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="thinking_smiley">= 🤔</string>
<string name="app_name">KMM Sample</string>
</resources>
5 changes: 5 additions & 0 deletions iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import SwiftUI

@main
struct SampleApp: App {

init() {
HelperKt.doInitKoin()
}

var body: some Scene {
WindowGroup {
ContentView()
Expand Down
5 changes: 5 additions & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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!"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ class Greeting {
fun greeting(): String {
return "Hello, ${Platform().platform}!"
}

fun sayHello():String{
return "Hello ${Platform().newVal}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.jetbrains.kmm.shared

expect class Platform() {
val platform: String
val newVal:String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.jetbrains.kmm.shared.di

fun appModule() = listOf(platformModule, commonModule)
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions shared/src/iosMain/kotlin/com/jetbrains/kmm/shared/Helper.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}