gpt4 book ai didi

android - 如何使用 Dagger2 将 ViewModel 注入(inject) BottomSheetDialogFragment?

转载 作者:行者123 更新时间:2023-11-29 15:34:38 24 4
gpt4 key购买 nike

我使用 AndroidInjection.inject(this) 以首选方式将我的依赖项注入(inject) Activity 和 Fragments。我有推荐的 ViewmodelFactory 来注入(inject) View 模型。我的注入(inject)在 Activities 和 Fragments 中起作用。但是我遇到了 BottomSheetDialogFragment 的问题,因为不允许我将 BottomSheetDialogFragment 指定为 this。所以我的 @Inject lateinit var viewModelFactory: ViewModelFactory 没有初始化。我相信注入(inject)应该是可能的,因为 BottomSheetDialogFragment 应该是 Fragment 类的子类,但看起来不是。我使用 android.x,我相信这也会导致问题。难不成 Dagger 还不支持它?

我应该用什么方式来实现我的 ViewModelFactory 注入(inject)?

更新:当我尝试使用 AndroidInjection.inject(this) 注入(inject) fragment 时,仅使用 androidx.fragment.app.Fragment 也不可能android.app.Fragement。我用 DaggerFragment 扩展了我的 fragment ,它们按预期工作。

最佳答案

下面是我如何将 ViewModel 提供给 BottomSheetDialogFragment()。首先在 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 过滤器布局

<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)

关于android - 如何使用 Dagger2 将 ViewModel 注入(inject) BottomSheetDialogFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com