Tim Malseed
02/11/2020, 11:51 PM@AssistedInject
with Dagger 2, though I’ve used it with success in other projects..
It seems as though the `@AssistedInject.Factory Factory`is not being provided its non-assisted properties. I don’t quite have the terminology to describe this problem, so I’ll show rather than tell:Tim Malseed
02/11/2020, 11:53 PMMissingBinding (BridgeSelectionController), FragmentManager cannot be provided without @Provides method. A binding with matching key exists in component FragmentComponent
javax.inject.Provider<androidx.fragment.app.FragmentManager> is injected at
<http://au.com|au.com>.smartsnugg.ui.screens.bridge_onboarding.bridgeselection.BridgeSelectionNavigator_AssistedFactory.<init>(fragmentManager)
The thing is, fragmentManager
is provided via `NavigatorModule`:
class BridgeSelectionController { ...
@Inject
lateinit var navigatorFactory: BridgeSelectionNavigator.Factory
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(context!!.applicationContext as SmartSnuggApplication).appComponent
.plus(ActivityModule(activity!!))
.plus(NavigatorModule(fragmentManager!!))
.inject(this)
Tim Malseed
02/11/2020, 11:53 PMBridgeSelectionNavigator
looks like so:
class BridgeSelectionNavigator @AssistedInject constructor(
fragmentManager: FragmentManager,
@Assisted private val subject: Subject
) : BaseNavigator(fragmentManager), BridgeSelectionContract.Navigator {
@AssistedInject.Factory
interface Factory {
fun create(subject: Subject): BridgeSelectionNavigator
}
Tim Malseed
02/11/2020, 11:55 PMBridgeSelectionController
uses plus NavigatorModule
to construct a component that can provide an instance of FragmentManager.
Then, BridgeSelectionNavigator.Factory
is injected, and the required property FragmentManager
should be supplied accordingly.Tim Malseed
02/11/2020, 11:58 PMfragmentManager
an @Assisted
property in the BridgeSelectionNavigator
Factory, and then used @Inject fragmentManager
in BridgeSelectionController
, I’d be able to pass the fragmentManager
to the Factory.create()
method. But Dagger should do this for me - I should only have to provide @Assisted
properties myself, Dagger should be able to provide the rest.Barco
02/11/2020, 11:59 PMTim Malseed
02/12/2020, 12:00 AM@FragmentScope
@Subcomponent(modules = [NavigatorModule::class, PresenterModule::class])
interface FragmentComponent {
...
fun inject(target: BridgeSelectionController)
...
}
Tim Malseed
02/12/2020, 12:02 AMNavigatorModule
(I apologise for this mess):
@Module(includes = [NavigatorModule.AbsNavigatorModule::class])
class NavigatorModule(
private val fragmentManager: FragmentManager,
@Named(CHILD_FRAGMENT_MANAGER) private val childFragmentManager: FragmentManager? = null,
@Named(PARENT_FRAGMENT_MANAGER) private val parentFragmentManager: FragmentManager? = null
) {
@Provides
fun provideFragmentManager(): FragmentManager {
return fragmentManager
}
@Named(CHILD_FRAGMENT_MANAGER)
@Provides
fun provideChildFragmentManager(): FragmentManager? {
return childFragmentManager
}
@Named(PARENT_FRAGMENT_MANAGER)
@Provides
fun provideParentFragmentManager(): FragmentManager? {
return parentFragmentManager
}
@Provides
fun provideNavigator(): Navigator {
return BaseNavigator(fragmentManager)
}
@Provides
@Named(PARENT_NAVIGATOR)
fun provideParentNavigator(): Navigator? {
return parentFragmentManager?.let { BaseNavigator(parentFragmentManager) }
}
...
Tim Malseed
02/12/2020, 12:03 AMNavigatorModule
, IMO is just:
@Provides
fun provideFragmentManager(): FragmentManager {
return fragmentManager
}
(It does indeed provide a FragmentManager)Barco
02/12/2020, 1:25 AMBarco
02/12/2020, 1:25 AMTim Malseed
02/12/2020, 2:11 AMAppAssistedModule
?Tim Malseed
02/12/2020, 2:11 AMAppComponent:
@Singleton
@Component(modules = [AppModule::class, AppAssistedModule::class, NetworkingModule::class, EventModule::class])
interface AppComponent {
...
Barco
02/12/2020, 3:12 AMAssistedInject_YourModule
Barco
02/12/2020, 3:16 AM@AssistedModule
, which will indicate to Dagger to generate a module called AssistedInject_YourModule
, which in turn will satisfy the dependency on your FactoryBarco
02/12/2020, 3:17 AMincludes
parameter in the @Module
annotation.Tim Malseed
02/12/2020, 3:47 AMTim Malseed
02/12/2020, 3:47 AM@AssistedModule
@Module(includes = [AssistedInject_AppAssistedModule::class])
abstract class AppAssistedModule
Tim Malseed
02/12/2020, 3:48 AMAhmed Ibrahim
02/12/2020, 9:50 AMFragmentManager
somewhere in your graph. If I were you I would check `ActivityModule`otherwise I would check the parent component of the FragmentComponent
since it is a @Subcomponent
Tim Malseed
02/12/2020, 10:05 AM