Last active
June 13, 2018 07:26
-
-
Save Tagakov/a4645b11a8677747f8c7cdb799b8d1b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ComponentDependencies | |
inline fun <reified T : ComponentDependencies> Controller.findComponentDependencies(): T { | |
var currentController: Controller? = parentController | |
while (currentController !is HasComponentDependencies?) { | |
currentController = currentController?.parentController | |
} | |
val hasDaggerProviders = currentController ?: when { | |
activity is HasComponentDependencies -> activity as HasComponentDependencies | |
activity?.application is HasComponentDependencies -> activity?.application as HasComponentDependencies | |
else -> throw IllegalStateException("Can not find suitable dagger provider for $this") | |
} | |
return hasDaggerProviders.dependencies[T::class.java] as T | |
} | |
typealias ComponentDependenciesProvider = Map<Class<out ComponentDependencies>, @JvmSuppressWildcards ComponentDependencies> | |
interface HasComponentDependencies { | |
val dependencies: ComponentDependenciesProvider | |
} | |
@MapKey | |
@Target(AnnotationTarget.FUNCTION) | |
annotation class ComponentDependenciesKey(val value: KClass<out ComponentDependencies>) | |
@Module | |
abstract class ComponentDependenciesModule private constructor() { | |
@Multibinds | |
abstract fun componentDependencies(): ComponentDependenciesProvider | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(modules = [DependenciesModule::class]) | |
interface ActivityComponent : FeatureDependencies | |
@Module | |
abstract class DependenciesModule private constructor() { | |
@Binds | |
@IntoMap | |
@ComponentDependenciesKey(FeatureDependencies::class) | |
abstract fun featureDependencies(component: ActivityComponent): ComponentDependencies | |
} | |
class SomeActivity: Activity, HasComponentDependencies { | |
@Inject | |
protected lateinit var dependencies: ComponentDependenciesProvider | |
//... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component(dependencies = [FeatureDependencies::class]) | |
interface FeatureComponent { | |
fun inject(featureController: FeatureController) | |
} | |
interface FeatureDependencies : ComponentDependencies | |
class FeatureController : Controller {//activity, controller, fragment, whatever | |
fun onCreate(state: Bundle?) { | |
super.onCreate(state) | |
DaggerFeatureComponent.builder() | |
.featureDependencies(findComponentDependencies()) | |
.build() | |
.inject(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment