-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBindsComponent.kt
57 lines (38 loc) · 1.16 KB
/
BindsComponent.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.b7_binds
import com.example.createSingleton
import dagger.Binds
import dagger.Component
import dagger.Module
import javax.inject.Inject
import javax.inject.Singleton
@Component(modules = [BinderModule::class])
interface BindsComponent {
fun getCharSequence(): CharSequence
}
@Module
interface BinderModule {
@Binds
fun bindCharSequence(impl: CharSequenceImpl): CharSequence
}
class CharSequenceImpl @Inject constructor() : CharSequence by "hello"
// After conversion
class KotlinBindsComponent : BindsComponent {
override fun getCharSequence(): CharSequence = CharSequenceImpl()
}
// Singleton version of binds
@Singleton
@Component(modules = [BinderSingletonModule::class])
interface BindsSingletonComponent {
fun getCharSequence(): CharSequence
}
@Module
interface BinderSingletonModule {
@Binds
@Singleton
fun bindCharSequence(hello: CharSequenceImpl): CharSequence
}
// After conversion
class KotlinBindsSingletonComponent : BindsSingletonComponent {
private val bindCharSequenceProvider = createSingleton { CharSequenceImpl() }
override fun getCharSequence(): CharSequence = bindCharSequenceProvider()
}