Annotations for Koin



Preparation


build.gradle
plugins {
	...
    ...
    id "com.google.devtools.ksp" version "1.6.10-1.0.2"
}

apply plugin: 'com.google.devtools.ksp'
...
...

dependencies {
    ...
    ...
    // koin
    implementation "io.insert-koin:koin-android:3.2.0"
    implementation "io.insert-koin:koin-annotations:1.0.0-beta-2"
    ksp "io.insert-koin:koin-ksp-compiler:1.0.0-beta-2"
}
application.kt
package com.aplikasi.android

import android.app.Application
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.GlobalContext.startKoin
import org.koin.ksp.generated.defaultModule

class Aplikasi : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@Aplikasi)
            modules(defaultModule)
        }
    }
}

Usage

Factory

package com.aplikasi.android.presentation.adapter

import androidx.recyclerview.widget.RecyclerView
import org.koin.core.annotation.Factory

@Factory
class GridAdapter : RecyclerView.Adapter<GridAdapter.GridVH>() {
	...
}

Single

package com.aplikasi.android.data.repo

import org.koin.core.annotation.Single
import com.aplikasi.android.data.network.service.DataService

@Single
class ConfigRepoImpl(
    val service: DataService
) : ConfigRepo {

}

ViewModel

package com.aplikasi.android.presentation.screen.main

import org.koin.android.annotation.KoinViewModel
import com.aplikasi.android.data.repo.ConfigRepo
import share.core.base.BaseViewModel

@KoinViewModel
class MainVM(
    private val configRepo: ConfigRepo
): BaseViewModel() {
   ...
}

Implementation

package com.aplikasi.android.presentation.screen.main

import org.koin.android.ext.android.inject
import com.aplikasi.android.databinding.ActivityMainBinding
import com.aplikasi.android.presentation.adapter.GridAdapter
import share.core.base.BaseActivity

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
    private val vm: MainVM by inject()
    private val gridAdapter: GridAdapter by inject()
}

Komentar