Skip to content

Latest commit

 

History

History
128 lines (112 loc) · 2.75 KB

part-1.md

File metadata and controls

128 lines (112 loc) · 2.75 KB

Koin (Part 1)

Get dependencies

https://insert-koin.io/docs/setup/koin/#koin-bom

implementation(platform("io.insert-koin:koin-bom:$koin_version"))
implementation("io.insert-koin:koin-android")
implementation("io.insert-koin:koin-core-coroutines")
implementation("io.insert-koin:koin-androidx-workmanager")

Initialise Application class

  • Create application class annotation. We will use this in the last step.
    class BaseApplication: Application() {
        override fun onCreate() {
            super.onCreate()
        }
    }
    
  • Add to manifest.
    <manifest ...
      >
    
      <application
          android:name=".BaseApplication"
          ...
          >
    
          ...
    
      </application>
    
    </manifest>
    

Create classes to be injected

  1. An API class that will be injected in a repository.
    class MyApi() {
        fun callNetwork()
    }
    
  2. A repository interface and its implementation that will be injected in a viewmodel.
    interface MyRepo {
        fun doNetworkCall()
    }
    class MyRepoImpl(
        private val myApi: MyApi
    ): MyRepo() {
        override fun doNetworkCall() {
            myApi.callNetwork()
        }
    }
    
  3. A viewmodel to be injected in an activity.
    class MainViewModel(
        private val repo: MyRepo
    ): ViewModel(){
        fun doNetworkCall(){
            repo.doNetworkCall()
        }
    }
    

Create how to inject

Create a file AppModule.kt.

val appModule = module {
    single {
        // create MyApi using Retrofit or something else
        Retrofit.Builder()
            .baseUrl("https://something")
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
            .create(MyApi::class.java)
    }
    // OLD
    //single<MyRepo> {
        // Provide an implementation of MyRepo.
        // "get()" will automatically fetch the api from above "single" block.
        MyRepoImpl(get())
    //}
    singleOf(::MyRepoImpl) { bind<MyRepo>() }
    viewModel {
        // Notice different block for viewmodel
        MainViewModel(get())
    }
}

Inject into activity

class MainActivity: ComponentActivity() {
    private val viewModel: MainViewModel by inject()
}

Inject something not a viewmodel

In this example we inject MyApi just for representation.

class MainActivity: ComponentActivity() {
    //...
    private val api: MyApi by inject()
}

Start koin in the Application class

class BaseApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        startkoin {
            androidContext(this)
            modules(appModule)
        }
    }
}

Done !