Skip to content

Instantly share code, notes, and snippets.

@m4xp1
Forked from be1ski/Activity.kt
Created July 7, 2020 12:39
Show Gist options
  • Select an option

  • Save m4xp1/9dcd98767bcb40ad386a4ddd7dea3a71 to your computer and use it in GitHub Desktop.

Select an option

Save m4xp1/9dcd98767bcb40ad386a4ddd7dea3a71 to your computer and use it in GitHub Desktop.
/** Пример использования */
class MainActivity : AppCompatActivity(R.layout.activity_main) {
@Inject lateinit var fragmentFactory: FragmentFactory
override fun onCreate(savedInstanceState: Bundle?) {
DaggerActivityComponent
.factory()
.create()
.inject(this)
supportFragmentManager.fragmentFactory = fragmentFactory
super.onCreate(savedInstanceState)
supportFragmentManager
.beginTransaction()
.replace(containerId, FeatureFragment::class.java, null)
.commit()
}
}
/**
* Корневой компонент, который реализует зависимости всех фичей.
* Находится в корневом модуле, который связывает зависимости (например :app)
*/
@Component(
modules = [
FeatureDependenciesModule::class,
FragmentFactoryModule::class
]
)
@ActivityScope
interface ActivityComponent : FeatureDependencies {
fun inject(mainActivity: MainActivity)
@Component.Factory
interface Factory {
fun create(): ActivityComponent
}
}
/** Компонент фичи. Находится в модуле фичи. */
@Component(
dependencies = [FeatureDependencies::class]
)
@FragmentScope
interface FeatureComponent {
@Component.Factory
interface Factory {
fun create(dependencies: FeatureDependencies): FeatureComponent
}
}
/**
* Рализация зависимостей одной конкретной фичи.
* Находится в корневом модуле, который связывает зависимости (например :app)
*/
@Module
abstract class FeatureDependenciesModule private constructor() {
@Binds
@ActivityScope
abstract fun provideDependencies(component: ActivityComponent): FeatureDependencies
@Binds
@IntoMap
@FragmentKey(FeatureFragment::class)
abstract fun provideFragment(fragment: FeatureFragment): Fragment
}
/** Фрагмент фичи. Находится в модуле фичи. */
class FeatureFragment @Inject constructor(
private val dependencies: FeatureDependencies
) : Fragment() {
private val component by nonConfigurationInstance {
DaggerFeatureComponent.factory().create(dependencies)
}
}
/** Утилитарный класс. Может находиться в :common:di */
class FragmentFactory @Inject constructor(
providers: Map<Class<out Fragment>, @JvmSuppressWildcards Provider<Fragment>>
) : androidx.fragment.app.FragmentFactory() {
private val providersByClassName = providers.mapKeys { (fragmentClass, _) -> fragmentClass.name }
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return providersByClassName[className]?.get() ?: super.instantiate(classLoader, className)
}
}
/** Утилитарный класс. Может находиться в :common:di */
@Module
abstract class FragmentFactoryModule private constructor() {
@Binds
@ActivityScope
abstract fun fragmentFactory(fragmentFactory: FragmentFactory): androidx.fragment.app.FragmentFactory
}
/** Утилитарный класс. Может находиться в :common:di */
@MapKey
@Target(FUNCTION, PROPERTY_GETTER)
@Retention(AnnotationRetention.RUNTIME)
annotation class FragmentKey(val value: KClass<out Fragment>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment