Slide 1

Slide 1 text

@rbusarow GETTING BIG PAYOUTS WITH KOIN Rick Busarow [email protected]

Slide 2

Slide 2 text

@rbusarow What’s Koin?

Slide 3

Slide 3 text

What’s Koin? @rbusarow

Slide 4

Slide 4 text

What’s Koin? @rbusarow

Slide 5

Slide 5 text

What’s Koin? @rbusarow

Slide 6

Slide 6 text

@rbusarow Screen ViewModel UserId Repository Dao Room Database App Context Service Retrofit OkHttpClient Base Url

Slide 7

Slide 7 text

@rbusarow Normal Control Flow

Slide 8

Slide 8 text

@rbusarow class UserRepository { internal val service: UserService = retrofit.create() internal val dao: UserDao = database.userDao } Normal Control Flow

Slide 9

Slide 9 text

@rbusarow class UserRepository { internal val okHttpClient: OkHttpClient = OkHttpClient() internal val retrofit: Retrofit = Retrofit.Builder() .baseUrl("https://example.dev") .client(okHttpClient) .build() internal val database: Database = Database.init(APP_CONTEXT) internal val service: UserService = retrofit.create() internal val dao: UserDao = database.userDao } Normal Control Flow

Slide 10

Slide 10 text

@rbusarow class UserRepository { internal val okHttpClient: OkHttpClient = OkHttpClient() internal val retrofit: Retrofit = Retrofit.Builder() .baseUrl("https://example.dev") .client(okHttpClient) .build() internal val database: Database = Database.init(APP_CONTEXT) internal val service: UserService = retrofit.create() internal val dao: UserDao = database.userDao } Normal Control Flow

Slide 11

Slide 11 text

@rbusarow Inversion of Control

Slide 12

Slide 12 text

@rbusarow Service Location Dependency Injection Inversion of Control

Slide 13

Slide 13 text

@rbusarow Service Location Dependency Injection class UserViewModel { private val repository = Locator.createRepository() } Inversion of Control

Slide 14

Slide 14 text

@rbusarow Service Location Dependency Injection class UserViewModel { private val repository = Locator.createRepository() } class UserViewModel(val repository: UserRepository) { lateinit var otherDependency : OtherDependency } Inversion of Control

Slide 15

Slide 15 text

@rbusarow Field Injection

Slide 16

Slide 16 text

@rbusarow class UserScreen : Fragment() { lateinit var viewModel: UserViewModel } Field Injection

Slide 17

Slide 17 text

class UserScreen : Fragment() { lateinit var viewModel: UserViewModel override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } object Injector { fun inject(screen: UserScreen) { screen.viewModel = TODO() } } @rbusarow Field Injection

Slide 18

Slide 18 text

class UserScreen : Fragment() { lateinit var viewModel: UserViewModel override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } object Injector { fun inject(screen: UserScreen) { screen.viewModel = TODO() } } @rbusarow Field Injection

Slide 19

Slide 19 text

@rbusarow class UserScreen : Fragment() { lateinit var viewModel: UserViewModel override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } object Injector { fun inject(screen: UserScreen) { screen.viewModel = TODO() } } Field Injection public mutable late init

Slide 20

Slide 20 text

@rbusarow Constructor Injection

Slide 21

Slide 21 text

@rbusarow class UserRepository( private val service: UserService, private val dao: UserDao ) { … } Constructor Injection

Slide 22

Slide 22 text

@rbusarow class UserViewModel { val repository = UserRepository(service = TODO(), dao = TODO()) } Constructor Injection

Slide 23

Slide 23 text

@rbusarow class UserViewModel { val client = OkHttpClient() val retrofit = Retrofit.Builder() .baseUrl("https://example.dev") .client(client) .build() val service = retrofit.create() val database = Database.init(APP_CONTEXT) val dao = database.userDao val repository = UserRepository(service = service, dao = dao) } Constructor Injection

Slide 24

Slide 24 text

@rbusarow class UserViewModel { val client = OkHttpClient() val retrofit = Retrofit.Builder() .baseUrl("https://example.dev") .client(client) .build() val service = retrofit.create() val database = Database.init(APP_CONTEXT) val dao = database.userDao val repository = UserRepository(service = service, dao = dao) } Constructor Injection

Slide 25

Slide 25 text

@rbusarow class UserViewModel(service: UserService, dao: UserDao) { val repository = UserRepository(service = service, dao = dao) } Constructor Injection

Slide 26

Slide 26 text

@rbusarow class UserViewModel( private val repository: UserRepository ) : ViewModel() Constructor Injection

Slide 27

Slide 27 text

@rbusarow class UserScreen : Fragment() { val viewModel = UserViewModel(repository = TODO()) } Constructor Injection

Slide 28

Slide 28 text

@rbusarow class UserScreen : Fragment() { lateinit var viewModel : UserViewModel override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Field Injection…

Slide 29

Slide 29 text

@rbusarow class UserScreen : Fragment() { lateinit var viewModel : UserViewModel override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Field Injection…

Slide 30

Slide 30 text

@rbusarow Service Location

Slide 31

Slide 31 text

@rbusarow class UserScreen : Fragment() { private val viewModel by lazy { ViewModelProviders .of(this) .get(UserViewModel::class.java) } override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Service Location

Slide 32

Slide 32 text

@rbusarow class UserScreen : Fragment() { private val viewModel by lazy { Locator.get() } override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Service Location

Slide 33

Slide 33 text

@rbusarow class UserScreen : Fragment() { private val viewModel by lazy { Locator.get() } override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Service Location object Locator { inline fun get(): T = … }

Slide 34

Slide 34 text

@rbusarow class UserScreen : Fragment() { private val presenter by lazy { Locator.get() } private val viewModel by lazy { Locator.get() } override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } Service Location object Locator { inline fun get(): T = … }

Slide 35

Slide 35 text

@rbusarow class UserScreen : Fragment() { private val presenter by lazy { Locator.instance.get() } private val viewModel by lazy { Locator.instance.get() } override fun onAttach(context: Context) { Injector.inject(this) super.onAttach(context) } } interface Locator { fun get(): T = … companion object { lateinit var instance: Locator } } Service Location

Slide 36

Slide 36 text

@rbusarow Koin KoinApplication

Slide 37

Slide 37 text

class Koin { val graph: Graph fun inject(): Lazy fun get(): T fun bind(s: S): S fun createScope(scopeId: ScopeID, qualifier: Qualifier): Scope } class KoinApplication { val koin = Koin() fun modules(modules: List): KoinApplication fun logger(logger: Logger): KoinApplication fun unloadModules(modules: List) fun close() } @rbusarow Koin & KoinApplication

Slide 38

Slide 38 text

class Koin { val graph: Graph fun inject(): Lazy fun get(): T fun bind(s: S): S fun createScope(scopeId: ScopeID, qualifier: Qualifier): Scope } class KoinApplication { val koin = Koin() fun modules(modules: List): KoinApplication fun logger(logger: Logger): KoinApplication fun unloadModules(modules: List) fun close() } @rbusarow Koin & KoinApplication

Slide 39

Slide 39 text

class Koin { val graph: Graph fun inject(): Lazy fun get(): T fun bind(s: S): S fun createScope(scopeId: ScopeID, qualifier: Qualifier): Scope } class KoinApplication { val koin = Koin() fun modules(modules: List): KoinApplication fun logger(logger: Logger): KoinApplication fun unloadModules(modules: List) fun close() } @rbusarow Koin & KoinApplication Not an Android Application

Slide 40

Slide 40 text

@rbusarow GlobalContext

Slide 41

Slide 41 text

@rbusarow object GlobalContext { internal var app: KoinApplication? = null fun get(): KoinApplication fun getOrNull(): KoinApplication? fun start(koinApplication: KoinApplication) fun stop() = synchronized(this) { app?.close() app = null } } GlobalContext

Slide 42

Slide 42 text

@rbusarow startKoin( ) koinApplication( )

Slide 43

Slide 43 text

@rbusarow fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() GlobalContext.start(koinApplication) appDeclaration(koinApplication) koinApplication.createEagerInstances() return koinApplication } fun koinApplication(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() appDeclaration(koinApplication) return koinApplication } startKoin & koinApplication

Slide 44

Slide 44 text

@rbusarow fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() GlobalContext.start(koinApplication) appDeclaration(koinApplication) koinApplication.createEagerInstances() return koinApplication } fun koinApplication(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() appDeclaration(koinApplication) return koinApplication } startKoin & koinApplication

Slide 45

Slide 45 text

@rbusarow fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() GlobalContext.start(koinApplication) appDeclaration(koinApplication) koinApplication.createEagerInstances() return koinApplication } fun koinApplication(appDeclaration: KoinAppDeclaration): KoinApplication { val koinApplication = KoinApplication.create() appDeclaration(koinApplication) return koinApplication } startKoin & koinApplication

Slide 46

Slide 46 text

@rbusarow class MyApplication : Application() { override fun onCreate() { super.onCreate() } } MyApplication.kt

Slide 47

Slide 47 text

@rbusarow class MyApplication : Application() { override fun onCreate() { super.onCreate() startKoin { // route all Koin logs through android.util.Log androidLogger() } } } MyApplication.kt

Slide 48

Slide 48 text

@rbusarow private class TimberLogger(level: Level = Level.INFO) : Logger(level) { override fun log(level: Level, msg: MESSAGE) { if (this.level <= level) { when (this.level) { Level.DEBUG -> Timber.tag(KOIN_TAG).d(msg) Level.INFO -> Timber.tag(KOIN_TAG).i(msg) Level.ERROR -> Timber.tag(KOIN_TAG).e(msg) } } } } fun KoinApplication.timberLogger(level: Level = Level.INFO): KoinApplication { KoinApplication.logger = TimberLogger(level) return this } TimberLogger.kt

Slide 49

Slide 49 text

@rbusarow class MyApplication : Application() { override fun onCreate() { super.onCreate() Timber.plant(DebugTree()) startKoin { // route all Koin logs through Timber timberLogger() } } } MyApplication.kt

Slide 50

Slide 50 text

@rbusarow class MyApplication : Application() { override fun onCreate() { super.onCreate() Timber.plant(DebugTree()) startKoin { // route all Koin logs through Timber timberLogger() // provide Application context to the graph androidContext(this@MyApplication) } } } MyApplication.kt

Slide 51

Slide 51 text

@rbusarow class MyApplication : Application() { override fun onCreate() { super.onCreate() Timber.plant(DebugTree()) startKoin { // route all Koin logs through Timber timberLogger() // provide Application context to the graph androidContext(this@MyApplication) modules(koinDemoModules) } } } MyApplication.kt

Slide 52

Slide 52 text

@rbusarow Modules

Slide 53

Slide 53 text

@rbusarow val dataModule = module { } Modules

Slide 54

Slide 54 text

@rbusarow val dataModule = module { factory { } } Modules

Slide 55

Slide 55 text

@rbusarow val dataModule = module { factory { UserRepository() } } Modules

Slide 56

Slide 56 text

@rbusarow val dataModule = module { factory { Timber.v("Hello everyone!") UserRepository().also { Timber.v("Goodbye!") } } } Modules

Slide 57

Slide 57 text

@rbusarow val dataModule = module { factory { UserRepository() } } Modules

Slide 58

Slide 58 text

@rbusarow val dataModule = module { factory { UserRepository() } } Modules BeanDefinition Component

Slide 59

Slide 59 text

@rbusarow Connecting the Graph

Slide 60

Slide 60 text

class MyApplication : Application() { override fun onCreate() { super.onCreate() Timber.plant(DebugTree()) startKoin { // route all Koin logs through Timber timberLogger() // provide Application context to the graph androidContext(this@MyApplication) modules(koinDemoModules) } } } @rbusarow Connecting the Graph

Slide 61

Slide 61 text

class MyApplication : Application() { override fun onCreate() { super.onCreate() Timber.plant(DebugTree()) startKoin { // route all Koin logs through Timber timberLogger() // provide Application context to the graph androidContext(this@MyApplication) modules(koinDemoModules) } } } @rbusarow Connecting the Graph

Slide 62

Slide 62 text

@rbusarow val koinDemoModules: List get() = emptyListOf() val dataModule = module { factory { UserRepository() } } Connecting the Graph

Slide 63

Slide 63 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule) val dataModule = module { factory { UserRepository() } } Connecting the Graph

Slide 64

Slide 64 text

@rbusarow class UserRepository { internal val database: Database = Database.init(APP_CONTEXT) internal val okHttpClient: OkHttpClient = OkHttpClient() internal val retrofit: Retrofit = Retrofit.Builder() .baseUrl("https://example.dev") .client(okHttpClient) .build() internal val service: UserService = retrofit.create() internal val dao: UserDao = database.userDao } Connecting the Graph

Slide 65

Slide 65 text

@rbusarow class UserRepository( private val service: UserService, private val dao: UserDao ) { … } Modules

Slide 66

Slide 66 text

@rbusarow val dataModule = module { factory { UserRepository() } } Connecting the Graph

Slide 67

Slide 67 text

@rbusarow val dataModule = module { factory { Repository(service = get(), dao = get()) } } Modules

Slide 68

Slide 68 text

@rbusarow val dataModule = module { factory { TODO() } factory { TODO() } factory { UserRepository(service = get(), dao = get()) } } Connecting the Graph

Slide 69

Slide 69 text

val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { TODO() } factory { UserRepository(service = get(), dao = get()) } } @rbusarow Connecting the Graph

Slide 70

Slide 70 text

val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { TODO() } factory { UserRepository(service = get(), dao = get()) } } @rbusarow Connecting the Graph

Slide 71

Slide 71 text

val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } } @rbusarow Connecting the Graph

Slide 72

Slide 72 text

val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } } @rbusarow Connecting the Graph

Slide 73

Slide 73 text

@rbusarow Connecting the Graph val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } }

Slide 74

Slide 74 text

@rbusarow Modules val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } }

Slide 75

Slide 75 text

@rbusarow Modules val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } }

Slide 76

Slide 76 text

val dataModule = module { factory { OkHttpClient() } factory { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } factory { get().create() } factory { Database.init(androidContext()) } factory { get().userDao } factory { UserRepository(service = get(), dao = get()) } } @rbusarow Connecting the Graph

Slide 77

Slide 77 text

val dataModule = module { single { OkHttpClient() } single { Retrofit.Builder() .baseUrl("https://example.dev") .client(get()) .build() } single { get().create() } single { Database.init(androidContext()) } factory { get().userDao } single { UserRepository(service = get(), dao = get()) } } @rbusarow Modules

Slide 78

Slide 78 text

@rbusarow Accessing the Graph

Slide 79

Slide 79 text

@rbusarow class UserScreen { val repository: UserRepository = TODO() } Accessing the Graph

Slide 80

Slide 80 text

@rbusarow class UserScreen { val repository = GlobalContext.get().koin.get() } Accessing the Graph

Slide 81

Slide 81 text

@rbusarow class UserScreen : KoinComponent { val repository = get() } Accessing the Graph

Slide 82

Slide 82 text

@rbusarow class UserScreen : Fragment() { val repository = get() } Accessing the Graph

Slide 83

Slide 83 text

@rbusarow class UserScreen : Fragment() { val repository = get() } class UserViewModel(private val repository: UserRepository) : ViewModel() Accessing the Graph

Slide 84

Slide 84 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, viewModelModule) val viewModelModule = module { viewModel { UserViewModel(repository = get()) } } Accessing the Graph

Slide 85

Slide 85 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, viewModelModule) val viewModelModule = module { viewModel { ViewModel(repository = get()) } } Accessing the Graph

Slide 86

Slide 86 text

@rbusarow class UserScreen : Fragment() { val viewModel = get() } class UserViewModel(private val repository: UserRepository) : ViewModel() Accessing the Graph

Slide 87

Slide 87 text

@rbusarow class UserScreen : Fragment() { val viewModel by viewModel() } class UserViewModel(private val repository: UserRepository) : ViewModel() Accessing the Graph

Slide 88

Slide 88 text

@rbusarow class UserScreen : Fragment() { val repository by inject() val viewModel by viewModel() } class UserViewModel(private val repository: UserRepository) : ViewModel() Accessing the Graph

Slide 89

Slide 89 text

@rbusarow Parameters

Slide 90

Slide 90 text

@rbusarow class UserScreen : Fragment() { val repository by inject() val viewModel by viewModel() } class UserViewModel(private val repository: UserViewModel) : ViewModel() Parameters

Slide 91

Slide 91 text

@rbusarow class UserScreen : Fragment() { val repository by inject() val viewModel by viewModel() } class UserViewModel( private val userId: String, private val repository: UserRepository ) : ViewModel() Parameters

Slide 92

Slide 92 text

@rbusarow class UserScreen : Fragment() { val repository by inject() val viewModel by viewModel() } class UserViewModel( private val userId: String, private val repository: UserRepository ) : ViewModel() val viewModelModule = module { viewModel { (userId: String) -> ViewModel(userId = userId, repository = get()) } } Parameters

Slide 93

Slide 93 text

@rbusarow class UserScreen : Fragment() { val repository by inject() val viewModel by viewModel { parametersOf(“50 Cent”) } } class UserViewModel( private val userId: String, private val repository: UserRepository ) : ViewModel() val viewModelModule = module { viewModel { (userId: String) -> UserViewModel(userId = userId, repository = get()) } } Parameters

Slide 94

Slide 94 text

@rbusarow Scope

Slide 95

Slide 95 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, scopedModule, viewModelModule) val scopedModule = module { scoped { Preferences() } } Scope

Slide 96

Slide 96 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, scopedModule, viewModelModule) val scopedModule = module { scoped { Preferences() } } object Scopes val Scopes.login: StringQualifier get() = named("login") Scope

Slide 97

Slide 97 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, scopedModule, viewModelModule) val scopedModule = module { scope(scopeName = Scopes.login) { scoped { Preferences() } } } object Scopes val Scopes.login: StringQualifier get() = named("login") Scope

Slide 98

Slide 98 text

@rbusarow val koinDemoModules: List get() = listOf(dataModule, scopedModule, viewModelModule) val scopedModule = module { scope(scopeName = Scopes.login) { scoped { Preferences() } } } class Anywhere : KoinComponent { val loginScope = getKoin().createScope(Scopes.login, named("login")) val preferences by loginScope.inject() } Scope

Slide 99

Slide 99 text

@rbusarow class Screen : Fragment() { val presenter by currentScope.inject() val viewModel by viewModel() } val scopedModule = module { scope(named()) { scoped { Presenter() } } scope(scopeName = Scopes.login) { scoped { Preferences() } } } Parameters

Slide 100

Slide 100 text

@rbusarow Testing

Slide 101

Slide 101 text

@rbusarow Overriding Modules

Slide 102

Slide 102 text

@rbusarow internal class UserRepositoryTest { val service = mockk() val dao = mockk() lateinit var repo: UserRepository @BeforeEach fun beforeEach() { repo = UserRepository(service, dao) } } Testing

Slide 103

Slide 103 text

@rbusarow internal class UserRepositoryTest : KoinTest { val service = mockk() val dao = mockk() lateinit var repo: UserRepository @BeforeEach fun beforeEach() { repo = UserRepository(service, dao) } } Testing

Slide 104

Slide 104 text

@rbusarow internal class UserRepositoryTest : KoinTest { … @BeforeEach fun beforeEach() { startKoin { modules(koinDemoModules) } repo = get() } @AfterEach fun afterEach() { getKoin().close() } } Testing

Slide 105

Slide 105 text

@rbusarow internal class UserRepositoryTest : KoinTest { … val mocks = module(override = true) { single { service } single { dao } } @BeforeEach fun beforeEach() { startKoin { modules(koinDemoModules) } repo = get() } @AfterEach fun afterEach() { getKoin().close() } } Testing

Slide 106

Slide 106 text

@rbusarow internal class UserRepositoryTest : KoinTest { … val mocks = module(override = true) { single { service } single { dao } } @BeforeEach fun beforeEach() { startKoin { modules(koinDemoModules) modules(mocks) } repo = get() } … } Testing

Slide 107

Slide 107 text

@rbusarow Checking the Graph

Slide 108

Slide 108 text

@rbusarow internal class ModuleTest : KoinTest { @BeforeEach fun beforeEach() { startKoin { modules(koinDemoModules) } } @AfterEach fun afterEach() { getKoin().close() } @Test fun `check modules`() { getKoin().checkModules() } } Testing

Slide 109

Slide 109 text

@rbusarow Try it yourself!

Slide 110

Slide 110 text

@rbusarow Further Reading http://bit.ly/322XZr5 http://bit.ly/2L2IfO4 Koin Android documentation: Koin Core documentation: Koin Github: http://bit.ly/2ZkTk2d

Slide 111

Slide 111 text

rbusarow rbusarow rbusarow THANKS! Rick Busarow [email protected]