Skip to content

Instantly share code, notes, and snippets.

View aasitnikov's full-sized avatar

Alexander Sitnikov aasitnikov

View GitHub Profile
package com.example;
import com.android.build.api.transform.DirectoryInput;
import com.android.build.api.transform.Format;
import com.android.build.api.transform.QualifiedContent;
import com.android.build.api.transform.Status;
import com.android.build.api.transform.Transform;
import com.android.build.api.transform.TransformException;
import com.android.build.api.transform.TransformInput;
import com.android.build.api.transform.TransformInvocation;
fun <T> createSingleton(provider: () -> T): () -> T {
val lazy by kotlin.lazy(provider)
return { lazy }
}
fun <T> createLazy(provider: () -> T): dagger.Lazy<T> {
val lazy by kotlin.lazy(provider)
return dagger.Lazy { lazy }
}
class KotlinParentComponent : ParentComponent {
override fun getString(): String = SubcomponentModule.getStringFromSubcomponent(ChildComponentFactory())
private inner class ChildComponentFactory : ChildComponent.Factory {
override fun create(string: String): ChildComponent = ChildComponentImpl(string)
}
private inner class ChildComponentImpl(
private val childString: String
@Qualifier
annotation class DefinedInChildComponent
@Module(subcomponents = [ChildComponent::class])
object SubcomponentModule {
@Provides
fun getStringFromSubcomponent(
subcomponent: ChildComponent.Factory
): String {
class KotlinParentComponent(
private val string: String
) : ParentComponent {
override fun createChildComponent(): ChildComponent = ChildComponentImpl()
private inner class ChildComponentImpl : ChildComponent {
override fun getString(): String = this@KotlinParentComponent.string
}
}
@Component
interface ParentComponent {
fun createChildComponent(): ChildComponent
@Component.Factory
interface Factory {
fun create(
@BindsInstance string: String
): ParentComponent
class SimplifiedKotlinCircularComponent : CircularComponent {
private lateinit var apiServiceProviderDelegate: () -> ApiService
private val apiServiceProvider = { apiServiceProviderDelegate() }
private val authenticatorProvider = { Authenticator(createLazy(apiServiceProvider)) }
private val httpClientProvider = { HttpClient(authenticatorProvider()) }
init {
apiServiceProviderDelegate = createSingleton { ApiService(httpClientProvider()) }
}
class KotlinCircularComponent : CircularComponent {
private val apiServiceProvider = DelegateFactory<ApiService>()
private val authenticatorProvider = { Authenticator(createLazy(apiServiceProvider)) }
private val httpClientProvider = { HttpClient(authenticatorProvider()) }
init {
val delegate = createSingleton { ApiService(httpClientProvider()) }
DelegateFactory.setDelegate(apiServiceProvider, delegate)
}
class HttpClient @Inject constructor(
val authenticator: Authenticator
)
class Authenticator @Inject constructor(
val apiService: Lazy<ApiService>
) {
fun authenticate() {
apiService.get().doAuthCall() // Use apiService to authenticate
}
class HttpClient @Inject constructor(
val authenticator: Authenticator
)
class Authenticator @Inject constructor(
val apiService: ApiService
) {
fun authenticate() {
apiService.get().doAuthCall() // Use apiService to authenticate
}