Skip to content

Commit

Permalink
Add component with dependencies sample. (#111)
Browse files Browse the repository at this point in the history
* Add component with dependencies sample.

Related #22

* Add binding from dependant component as entry point
  • Loading branch information
arunkumar9t2 committed Jul 21, 2022
1 parent a51dc11 commit cdad09b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Expand Up @@ -23,7 +23,14 @@ import javax.inject.Inject
import javax.inject.Named

class App : DaggerApplication() {
val appComponent by lazy { DaggerAppComponent.factory().build(this) }

val appComponent by lazy {
// Setup dependant component
val dependantComponent = DaggerDependantComponent.factory().create(this)
DaggerAppComponent
.factory()
.create(this, dependantComponent)
}

@Inject
lateinit var complexSingleton: ComplexSingleton
Expand Down
Expand Up @@ -38,16 +38,22 @@ import javax.inject.Singleton

// Activities
HomeActivity.Builder::class
]
],
dependencies = [DependantComponent::class]
)
interface AppComponent : AndroidInjector<App> {

fun inject(memberInjectionBinding: MemberInjectionBinding)

fun simpleSubcomponentFactory(): SimpleSubcomponent.Factory

fun helloWorld(): HelloWorld

@Component.Factory
interface Factory {
fun build(@BindsInstance application: Application): AppComponent
fun create(
@BindsInstance application: Application,
dependantComponent: DependantComponent
): AppComponent
}
}
Expand Up @@ -20,15 +20,13 @@ import android.app.Application
import dagger.BindsInstance
import dagger.Component
import javax.inject.Inject
import javax.inject.Singleton

class HelloWorld @Inject constructor(private val application: Application) {
fun say() {
println("Hello World")
}
}

@Singleton
@Component
interface DependantComponent {
fun helloWorld(): HelloWorld
Expand Down
Expand Up @@ -24,7 +24,6 @@ import dagger.android.support.DaggerAppCompatActivity
import dev.arunkumar.scabbard.R
import dev.arunkumar.scabbard.appComponent
import dev.arunkumar.scabbard.di.ComplexSingleton
import dev.arunkumar.scabbard.di.DaggerDependantComponent
import dev.arunkumar.scabbard.di.SimpleSingleton
import dev.arunkumar.scabbard.di.scope.ActivityScope
import dev.arunkumar.scabbard.home.fragment.HomeFragment
Expand Down Expand Up @@ -54,12 +53,6 @@ class HomeActivity : DaggerAppCompatActivity() {

// Setup simple subcomponent
application.appComponent.simpleSubcomponentFactory().create()

// Setup dependant component
DaggerDependantComponent.factory()
.create(application)
.helloWorld()
.say()
}

@Module
Expand Down

0 comments on commit cdad09b

Please sign in to comment.