-
-
Save m4xp1/9dcd98767bcb40ad386a4ddd7dea3a71 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
| /** Пример использования */ | |
| 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() | |
| } | |
| } |
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
| /** | |
| * Корневой компонент, который реализует зависимости всех фичей. | |
| * Находится в корневом модуле, который связывает зависимости (например :app) | |
| */ | |
| @Component( | |
| modules = [ | |
| FeatureDependenciesModule::class, | |
| FragmentFactoryModule::class | |
| ] | |
| ) | |
| @ActivityScope | |
| interface ActivityComponent : FeatureDependencies { | |
| fun inject(mainActivity: MainActivity) | |
| @Component.Factory | |
| interface Factory { | |
| fun create(): ActivityComponent | |
| } | |
| } |
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] | |
| ) | |
| @FragmentScope | |
| interface FeatureComponent { | |
| @Component.Factory | |
| interface Factory { | |
| fun create(dependencies: FeatureDependencies): FeatureComponent | |
| } | |
| } |
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
| /** | |
| * Рализация зависимостей одной конкретной фичи. | |
| * Находится в корневом модуле, который связывает зависимости (например :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 | |
| } |
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
| /** Фрагмент фичи. Находится в модуле фичи. */ | |
| class FeatureFragment @Inject constructor( | |
| private val dependencies: FeatureDependencies | |
| ) : Fragment() { | |
| private val component by nonConfigurationInstance { | |
| DaggerFeatureComponent.factory().create(dependencies) | |
| } | |
| } |
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
| /** Утилитарный класс. Может находиться в :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) | |
| } | |
| } |
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
| /** Утилитарный класс. Может находиться в :common:di */ | |
| @Module | |
| abstract class FragmentFactoryModule private constructor() { | |
| @Binds | |
| @ActivityScope | |
| abstract fun fragmentFactory(fragmentFactory: FragmentFactory): androidx.fragment.app.FragmentFactory | |
| } |
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
| /** Утилитарный класс. Может находиться в :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