SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Activity
  2. import androidx.fragment.app.Fragment
  3. import android.app.Application
  4. import android.app.Service
  5. import android.view.View
  6. import android.view.ViewParent
  7. import com.bluelinelabs.conductor.Controller
  8. import dagger.MapKey
  9. import dagger.Module
  10. import dagger.multibindings.Multibinds
  11. import kotlin.reflect.KClass
  12.  
  13. interface ComponentDependencies
  14.  
  15. inline fun <reified T : ComponentDependencies> Application.findComponentDependencies() = (this as HasComponentDependencies).dependencies[T::class.java] as? T
  16.         ?: depsNotFound<T>(this)
  17.  
  18. inline fun <reified T : ComponentDependencies> Activity.findComponentDependencies() = dependenciesProviderHolder.get<T>()
  19.  
  20. inline fun <reified T : ComponentDependencies> Service.findComponentDependencies() = dependenciesProviderHolder.get<T>()
  21.  
  22. inline fun <reified T : ComponentDependencies> Controller.findComponentDependencies() = dependenciesProviderHolder.get<T>()
  23.  
  24. inline fun <reified T : ComponentDependencies> View.findComponentDependencies() = dependenciesProviderHolder.get<T>()
  25.  
  26. inline fun <reified T : ComponentDependencies> Fragment.findComponentDependencies() = dependenciesProviderHolder.get<T>()
  27.  
  28. inline fun <reified T : ComponentDependencies> HasComponentDependencies.get(): T = dependencies[T::class.java] as? T ?: depsNotFound<T>(this)
  29.  
  30. inline fun <reified T : ComponentDependencies> depsNotFound(dependenciesHolder: Any): Nothing = throw IllegalStateException("Dependencies ${T::class.java.name} not found in $dependenciesHolder")
  31.  
  32. val Activity.dependenciesProviderHolder
  33.     get() = application as HasComponentDependencies
  34.  
  35. val Service.dependenciesProviderHolder
  36.     get() = application as HasComponentDependencies
  37.  
  38. val Controller.dependenciesProviderHolder: HasComponentDependencies
  39.     get() {
  40.  
  41.         var ancestor: Controller? = parentController
  42.         while (ancestor !is HasComponentDependencies?) {
  43.             ancestor = ancestor?.parentController
  44.         }
  45.  
  46.         return ancestor
  47.                 ?: activity as? HasComponentDependencies
  48.                 ?: activity?.application as? HasComponentDependencies
  49.                 ?: throw IllegalStateException("Can not find suitable dagger provider for $this")
  50.     }
  51.  
  52. val View.dependenciesProviderHolder: HasComponentDependencies
  53.     get() {
  54.         var ancestor: ViewParent? = parent
  55.         while (ancestor !is HasComponentDependencies?) {
  56.             ancestor = ancestor?.parent
  57.         }
  58.  
  59.         return ancestor
  60.                 ?: context as? HasComponentDependencies
  61.                 ?: context.applicationContext as? HasComponentDependencies
  62.                 ?: throw IllegalStateException("Can not find suitable dagger provider for $this")
  63.     }
  64.  
  65. val Fragment.dependenciesProviderHolder: HasComponentDependencies
  66.     get() {
  67.         var ancestor: Fragment? = parentFragment
  68.         while (ancestor !is HasComponentDependencies?) {
  69.             ancestor = ancestor?.parentFragment
  70.         }
  71.  
  72.         return ancestor
  73.                 ?: activity as? HasComponentDependencies
  74.                 ?: activity?.application as? HasComponentDependencies
  75.                 ?: throw IllegalStateException("Can not find suitable dagger provider for $this")
  76.     }
  77.  
  78. typealias ComponentDependenciesProvider = Map<Class<out ComponentDependencies>, @JvmSuppressWildcards ComponentDependencies>
  79.  
  80. interface HasComponentDependencies {
  81.     val dependencies: ComponentDependenciesProvider
  82. }
  83.  
  84. @MapKey
  85. @Target(AnnotationTarget.FUNCTION)
  86. annotation class ComponentDependenciesKey(val value: KClass<out ComponentDependencies>)
  87.  
  88. @Module
  89. abstract class ComponentDependenciesModule private constructor() {
  90.     @Multibinds
  91.     abstract fun componentDependencies(): ComponentDependenciesProvider
  92. }
  93.  
  94. /*
  95. The commented code below is an example of a dagger module which provides gradle module's component dependencies from main application
  96.  
  97. internal typealias Deps = /*? extends */ComponentDependencies
  98. internal typealias Component = dagger.Component
  99.  
  100. @dagger.Module
  101. internal interface BindingModule {
  102.     @Binds
  103.     @IntoMap
  104.     @ComponentDependenciesKey(Deps::class)
  105.     fun providePanoramaDependencies(component: Component): ComponentDependencies
  106. }
  107. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top