Skip to content

Commit

Permalink
Merge branch 'InsertKoinIO:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
GrzegorzBobryk authored Oct 27, 2024
2 parents 74f9198 + 4c56aa0 commit 2eb9163
Show file tree
Hide file tree
Showing 297 changed files with 14,627 additions and 6,321 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ jobs:
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ classes/
/build
/captures
yarn.lock

.kotlin/
Binary file removed docs/img/sponsors/stream-logo-2x.png
Binary file not shown.
Binary file removed docs/img/sponsors/stream-logo.png
Binary file not shown.
57 changes: 24 additions & 33 deletions docs/quickstart/android-annotations.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
---
title: Android - Annotations
title: Android & Annotations
---

> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
> You need around __10/15 min__ to do the tutorial.
> You need around __10 min__ to do the tutorial.
:::note
update - 2024-10-21
:::

## Get the code

:::info
[The source code is available at on Github](https://github.com/InsertKoinIO/koin-getting-started/tree/main/android)
[The source code is available at on Github](https://github.com/InsertKoinIO/koin-getting-started/tree/main/android-annotations)
:::

## Gradle Setup

Let's configure the KSP Plugin like this:
Let's configure the KSP Plugin like this, and the following dependencies:

```groovy
apply plugin: 'com.google.devtools.ksp'
android {
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
// For KSP
applicationVariants.configureEach { variant ->
kotlin.sourceSets {
getByName(name) {
kotlin.srcDir("build/generated/ksp/${variant.name}/kotlin")
}
}
}
plugins {
alias(libs.plugins.ksp)
}
```

Add the Koin Android dependency like below:
```groovy
dependencies {
// Koin
implementation "io.insert-koin:koin-android:$koin_version"
implementation "io.insert-koin:koin-annotations:$koin_ksp_version"
ksp "io.insert-koin:koin-ksp-compiler:$koin_ksp_version"
// ...
implementation(libs.koin.annotations)
ksp(libs.koin.ksp)
}
```
// Compile time check
ksp {
arg("KOIN_CONFIG_CHECK","true")
}
```

:::note
See `libs.versions.toml` for current versions
:::

## Application Overview

Expand Down Expand Up @@ -250,18 +244,15 @@ dependencies {

### Checking your modules

The `verify()` function allow to verify the given Koin modules:
The `androidVerify()` function allow to verify the given Koin modules:

```kotlin
class CheckModulesTest : KoinTest {

@Test
fun checkAllModules() {

AppModule().module.verify(
extraTypes = listOf(
SavedStateHandle::class
))
AppModule().module.androidVerify()
}
}
```
Expand Down
49 changes: 33 additions & 16 deletions docs/quickstart/android-compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ title: Android - Jetpack Compose
> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
> You need around __10 min__ to do the tutorial.
:::note
update - 2024-10-21
:::

## Get the code

:::info
Expand Down Expand Up @@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
singleOf(::UserRepositoryImpl) bind UserRepository::class
}
```

Expand All @@ -95,12 +99,12 @@ class UserViewModel(private val repository: UserRepository) : ViewModel() {

> UserRepository is referenced in UserViewModel's constructor
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
We declare `UserViewModel` in our Koin module. We declare it as a `viewModelOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
viewModel { MyViewModel(get()) }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
viewModelOf(::UserViewModel)
}
```

Expand All @@ -125,7 +129,7 @@ The `koinViewModel` function allows us to retrieve a ViewModel instances, create

### The `UserStateHolder` class

Let's write a ViewModel component to display a user:
Let's write a State holder component to display a user:

```kotlin
class UserStateHolder(private val repository: UserRepository) {
Expand All @@ -139,28 +143,28 @@ class UserStateHolder(private val repository: UserRepository) {

> UserRepository is referenced in UserViewModel's constructor
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
We declare `UserStateHolder` in our Koin module. We declare it as a `factoryOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
factory { UserStateHolder(get()) }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
factoryOf(::UserStateHolder)
}
```

### Injecting UserStateHolder in Compose

The `UserViewModel` component will be created, resolving the `UserRepository` instance with it. To get it into our Activity, let's inject it with the `get()` function:
The `UserStateHolder` component will be created, resolving the `UserRepository` instance with it. To get it into our Activity, let's inject it with the `koinInject()` function:

```kotlin
@Composable
fun FactoryInject(userName : String, presenter: UserStateHolder = get()){
fun FactoryInject(userName : String, presenter: UserStateHolder = koinInject()){
Text(text = presenter.sayHello(userName), modifier = Modifier.padding(8.dp))
}
```

:::info
The `get` function allows us to retrieve a ViewModel instances, create the associated ViewModel Factory for you and bind it to the lifecycle
The `koinInject` function allows us to retrieve a ViewModel instances, create the associated ViewModel Factory for you and bind it to the lifecycle
:::


Expand All @@ -186,6 +190,24 @@ class MainApplication : Application(){
The `modules()` function in `startKoin` load the given list of modules
:::

While starting the Compose application we need to link Koin to our current Compose application, with `KoinAndroidContext`:

```kotlin
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
KoinAndroidContext {
App()
}
}
}
}
}
```

## Koin module: classic or constructor DSL?

Here is the Koin moduel declaration for our app:
Expand Down Expand Up @@ -215,11 +237,6 @@ We can ensure that our Koin configuration is good before launching our app, by v
Add the Koin Android dependency like below:

```groovy
// Add Maven Central to your repositories if needed
repositories {
mavenCentral()
}
dependencies {
// Koin for Tests
Expand Down
17 changes: 10 additions & 7 deletions docs/quickstart/android-viewmodel.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ title: Android - ViewModel
---

> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
> You need around __10/15 min__ to do the tutorial.
> You need around __10 min__ to do the tutorial.
:::note
update - 2024-10-21
:::

## Get the code

Expand All @@ -19,7 +23,7 @@ Add the Koin Android dependency like below:
dependencies {
// Koin for Android
implementation "io.insert-koin:koin-android:$koin_version"
implementation("io.insert-koin:koin-android:$koin_version")
}
```

Expand Down Expand Up @@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
}
```

Expand All @@ -93,16 +97,15 @@ class UserViewModel(private val repository: UserRepository) : ViewModel() {

> UserRepository is referenced in UserViewModel`s constructor
We declare `UserViewModel` in our Koin module. We declare it as a `viewModel` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
We declare `UserViewModel` in our Koin module. We declare it as a `viewModelOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
viewModel { MyViewModel(get()) }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
viewModelOf(::UserViewModel)
}
```

> The `get()` function allow to ask Koin to resolve the needed dependency.

## Injecting ViewModel in Android

Expand Down
16 changes: 10 additions & 6 deletions docs/quickstart/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ title: Android
---

> This tutorial lets you write an Android application and use Koin dependency injection to retrieve your components.
> You need around __10/15 min__ to do the tutorial.
> You need around __10 min__ to do the tutorial.
:::note
update - 2024-10-21
:::

## Get the code

Expand All @@ -19,7 +23,7 @@ Add the Koin Android dependency like below:
dependencies {
// Koin for Android
implementation "io.insert-koin:koin-android:$koin_version"
implementation("io.insert-koin:koin-android:$koin_version")
}
```

Expand Down Expand Up @@ -73,7 +77,7 @@ Let's declare our first component. We want a singleton of `UserRepository`, by c

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
}
```

Expand All @@ -93,12 +97,12 @@ class UserPresenter(private val repository: UserRepository) {

> UserRepository is referenced in UserPresenter`s constructor
We declare `UserPresenter` in our Koin module. We declare it as a `factory` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):
We declare `UserPresenter` in our Koin module. We declare it as a `factoryOf` definition, to not keep any instance in memory (avoid any leak with Android lifecycle):

```kotlin
val appModule = module {
single<UserRepository> { UserRepositoryImpl() }
factory { MyPresenter(get()) }
singleOf(::UserRepositoryImpl) { bind<UserRepository>() }
factoryOf(::UserStateHolder)
}
```

Expand Down
Loading

0 comments on commit 2eb9163

Please sign in to comment.