如何使用Dagger2将ViewModel注入BottomSheetDialogFragment中?

时间:2018-11-28 11:09:05

标签: android dagger-2 bottom-sheet

我以AndroidInjection.inject(this)的首选方式在“活动和片段”中注入依赖项。我有推荐的ViewmodelFactory注入viewmodel。我的注射工作在“活动和片段”中。但是我遇到了BottomSheetDialogFragment的问题,因为不允许我将BottomSheetDialogFragment指定为this。因此我的@Inject lateinit var viewModelFactory: ViewModelFactory未初始化。我相信注入应该是可能的,因为BottomSheetDialogFragment应该是Fragment类的子类,但看起来并非如此。我使用的android.x相信也会引起问题。可能是Dagger不支持的吗?

我应该怎样实现ViewModelFactory注入?

更新:当我尝试用AndroidInjection.inject(this)注入片段时,androidx.fragment.app.Fragmentandroid.app.Fragement都不可能。我用DaggerFragment扩展了片段,它们按预期工作。

2 个答案:

答案 0 :(得分:2)

这是我向BottomSheetDialogFragment()提供ViewModel的方式。 首先在MyApp类中设置Dagger。

class MyApp : Application(), HasActivityInjector, HasSupportFragmentInjector {

@Inject
lateinit var activityInjector: DispatchingAndroidInjector<Activity>
@Inject
lateinit var supportFragmentInjector: DispatchingAndroidInjector<Fragment>

override fun onCreate() {
    super.onCreate()

    initDagger()
}

override fun activityInjector(): AndroidInjector<Activity> = activityInjector

override fun supportFragmentInjector(): AndroidInjector<Fragment> = supportFragmentInjector

private fun initDagger(){
    DaggerAppComponent
        .builder()
        .application(this)
        .build()
        .injectApp(this)
}

然后是AppComponent类

@Singleton
@Component(
modules = [
    AppModule::class,
    UiModule::class,
    AndroidSupportInjectionModule::class,
    AndroidInjectionModule::class
])interface AppComponent : AndroidInjector<MyApp> {

   @Component.Builder
   interface Builder {
       @BindsInstance
       fun application(application: MyApp): Builder

       fun build(): AppComponent
   }

    fun injectApp(app: MyApp)
}

我们在这里对UiModule :: class

感兴趣
@Module
abstract class UiModule {

    @Binds
    abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory

    @PerFragment
    @ContributesAndroidInjector(modules = [(FilterModule::class)])
    abstract fun contributeFilterFragment(): FilterFragment
}

您已经拥有ViewModelFactory,所以我不粘贴此代码。 然后FilterModule

@Module
abstract class FilterModule {

   @Binds
   @IntoMap
   @PerFragment
   @ViewModelKey(FilterViewModel::class)
   abstract fun bindViewModel(viewModel: FilterViewModel): ViewModel
}

最后是FilterFragment和FilterViewModel

class FilterFragment : BottomSheetDialogFragment() {

   @Inject
   lateinit var factory: ViewModelProvider.Factory

   private lateinit var binding: FragmentFilterBinding
   private lateinit var viewModel: FilterViewModel

   override fun onAttach(context: Context?) {
       AndroidSupportInjection.inject(this)
       super.onAttach(context)
   }

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       binding = getDataBinding(inflater, R.layout.fragment_filter, container)
       viewModel = getViewModel(factory)
       binding.viewModel = viewModel
       return binding.root
   }
}

class FilterViewModel @Inject constructor(private val testUseCase:TestUseCase) : BaseViewModel() {
     //do something
}

fragment_filter布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">

<data>

    <variable
        name="viewModel"
        type="com.myapp.presentation.screen.filter.FilterViewModel" />
</data>
   .......

getDataBinding()和getViewModel()是扩展函数

fun <T : ViewDataBinding> Fragment.getDataBinding(inflater: LayoutInflater, @LayoutRes layoutId: Int, container: ViewGroup?): T =
    DataBindingUtil.inflate(inflater, layoutId, container, false)

inline fun <reified T : BaseViewModel> Fragment.getViewModel(factory: ViewModelProvider.Factory = ViewModelProviders.DefaultFactory(activity!!.application)): T =
    ViewModelProviders.of(this, factory).get(T::class.java)

答案 1 :(得分:-1)

尝试扩展可注射

class BottomSheet : BootomSheetDialogFragment(), Injectable 

我认为这将解决问题