KOINかわいいよ、KOIN

90ec81078e4fd2b905f8ef0776887afa?s=47 Ryo Sakaguchi
September 20, 2018

 KOINかわいいよ、KOIN

Kotlin愛好会 Vol.4 2018/09/20 #love_kotlin

90ec81078e4fd2b905f8ef0776887afa?s=128

Ryo Sakaguchi

September 20, 2018
Tweet

Transcript

  1. ©2018 Wantedly, Inc. ,0*/͔Θ͍͍Αɺ,0*/ ,PUMJOѪ޷ձWPM 3ZP4BLBHVDIJ !XBLXBL 2018.9.20 -

  2. ©2018 Wantedly, Inc. Ryo Sakaguchi • Wantedly People • Android

    • Kotlin/Sake/Beer/Guitar… • Twitter/GitHub: @wakwak3125
  3. ©2018 Wantedly, Inc. Koin 1.0.0 Page Title Page Subtitle

  4. ©2018 Wantedly, Inc. 1. Lightweight 2. No Proxy/No Code Generation/(No)Reflection

    3. Simple DSL 4. Multi Platform • Kotlin/Java Android(with AAC ViewModel support) • Standalone application • Spark • Ktor About Koin
  5. ©2018 Wantedly, Inc. How to use? Let’s convert from the

    DI declaration of google/dagger to Koin
  6. ©2018 Wantedly, Inc. Dagger

  7. ©2018 Wantedly, Inc. google/Dagger @Module class DaggerModule { @Provides fun

    provideBeer(): IBeer = Beer("BREWDOG, PUNK IPA") @Provides fun provideSake(): ISake = Sake("ُઘ") @Provides fun provideGuitar(): IGuitar = Guitar("Fender, Stratocaster") @Provides @Singleton fun provideWakWak(beer: IBeer, sake: ISake, guitar: IGuitar): WakWak = WakWak(beer, sake, guitar) } Module
  8. ©2018 Wantedly, Inc. google/Dagger @Component(modules = [DaggerModule::class]) interface AppComponent {

    fun inject(activity: MainActivity) } Component
  9. ©2018 Wantedly, Inc. google/Dagger class KoinDaggerApp : Application() { lateinit

    var component: AppComponent private set override fun onCreate() { super.onCreate() component = DaggerAppComponent.create() } } Your Application class
  10. ©2018 Wantedly, Inc. google/Dagger class MainActivity : AppCompatActivity() { @Inject

    lateinit var daggerWakWak: WakWak override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) (application as KoinDaggerApp).component.inject(this) text1.text = daggerWakWak.getMyFavorites() } } Your Activity or Fragment
  11. ©2018 Wantedly, Inc. KOIN

  12. ©2018 Wantedly, Inc. KOIN class KoinDaggerApp : Application() { override

    fun onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { factory { Guitar("Fender, Telecaster") as IGuitar } factory { Beer("αοϙϩࠇϥϕϧ") as IBeer } factory { Sake("ਲՆ") as ISake } single { WakWak(get(), get(), get()) } } } Your Application class
  13. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { override fun

    onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { factory { Guitar("Fender, Telecaster") as IGuitar } factory { Beer("αοϙϩࠇϥϕϧ") as IBeer } factory { Sake("ਲՆ") as ISake } single { WakWak(get(), get(), get()) } } } KOIN Your Application class
  14. ©2018 Wantedly, Inc. class MainActivity : AppCompatActivity() { private val

    koinWakWak by inject<WakWak>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) text2.text = koinWakWak.getMyFavorites() } } Your Activity or Fragment KOIN
  15. ©2018 Wantedly, Inc. More complex examples Sub module, Combined module,

    Namespace
  16. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { inner class

    Message(val message: String) override fun onCreate() { super.onCreate() startKoin(this, listOf(appModule)) } private val appModule = module { module("A") { // Submodule factory<IBeer> { Beer("αοϙϩࠇϥϕϧ") } factory<ISake> { Sake("ਲՆ") } factory<IGuitar> { Guitar("Fender, Telecaster") } module("user") { single { WakWak(get(), get(), get()) } } } } KOIN Submodule
  17. ©2018 Wantedly, Inc. class KoinDaggerApp : Application() { inner class

    Message(val message: String) override fun onCreate() { super.onCreate() startKoin(this, listOf(messageModule, appModule)) } private val messageModule = module { factory { Message("Hello :)") } } private val appModule = module { module("A") { ɹɹ //… module("user") { single { WakWak(get(), get(), get()) } } } } } KOIN Combined module
  18. ©2018 Wantedly, Inc. KOIN Namespace class MainActivity : AppCompatActivity() {

    private val aWakWak by inject<WakWak>(name = "A.user.WakWak") private val bWakWak by inject<WakWak>(name = "B.user.WakWak") private val hello by inject<KoinDaggerApp.Message>(name = "Hello.Message") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) text1.text = aWakWak.getMyFavorites() text2.text = bWakWak.getMyFavorites() • A.user.WakWak = ModuleName.ModuleName.ClassName • Hello.Message = ModuleName.ClassName
  19. ©2018 Wantedly, Inc. InsertKoinIO/koin

  20. ©2018 Wantedly, Inc. ͓ΘΓ