dagger 注入_使用Dagger Hilt注入ViewModel

dagger 注入

学习Android开发 (Learning Android Development)

ViewModel is one of the best things in Android Jetpack to enable logic moving away from Activity.

V iewModel是Android中的Jetpack最好的东西,以使一个逻辑从活动移开

However, one big fall back the ViewModel has it, there’s no easily inject dependencies into it while having it injected into the Activity. I explore many ways as per this article, and none of them are ideal.

但是,ViewModel的一大缺点是,将它注入到Activity中时,很难轻易地将依赖项注入到其中。 根据本文 ,我探索了许多方法,但都不是理想的方法。

Thankfully now with Dagger Hilt, this is made so much more possible.

幸运的是,现在有了Dagger Hilt ,这使这一切变得更加可能。

回顾ViewModel(无注入) (Recap on ViewModel (without injection))

Assuming I have to an Activity with its ViewModel. There are three things the Activity need to pass to the View Model

假设我必须要有一个带有ViewModel的Activity。 活动需要传递给视图模型的三件事

Image for post
  1. The Dependencies the ViewModel needs, i.e. the Repository as per the diagram above.

    ViewModel需要的依赖关系,即上图所示的存储库。
  2. The SavedStateRegistryOwner. This will be used to hold the saved stated so that ViewModel can preserve saved stated

    SavedStateRegistryOwner。 这将用于保存保存的声明,以便ViewModel可以保存保存的声明

  3. The Bundle Intent, where Activity can pass its argument intent received over to ViewModel.

    捆绑意图,其中Activity可以将其接收到的参数意图传递给ViewModel。

We cannot just use KTX viewModels() extension function since we need to pass these three parameters to ViewModel. We will need to provide a ViewModel Factory.

我们不能仅仅使用KTX viewModels()扩展函数,因为我们需要将这三个参数传递给ViewModel。 我们将需要提供一个ViewModel工厂。

private val viewModel: MyViewModel by viewModels{
MyViewModelFactory(this, Repository(),
intent.extras)}

With that, the Factory will need to be constructed as below.

这样,工厂将需要按以下方式建造。

class MyViewModelFactory(
owner: SavedStateRegistryOwner,
private val repository: Repository,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
override fun <T : ViewModel> create(
key: String, modelClass: Class<T>, handle: SavedStateHandle
): T {
return MyViewModel(
repository, handle
) as T
}
}

Wow! So much thing to setup!

哇! 要设置的东西太多了!

使用Dagger Hilt的@ViewModelInject (The @ViewModelInject with Dagger Hilt)

Dagger Hilt is a simplified Dagger for Android particularly. The setup is relatively straightforward. Check out the below for the initial setup.

Dagger Hilt特别是针对Android的简化Dagger。 设置相对简单。 请查看以下内容进行初始设置。

After you have Dagger Hilt setup for your Project, we can now work on ViewModel injection.

在为项目设置Dagger Hilt之后,我们现在可以进行ViewModel注入了。

附加库 (The additional libraries)

To use @ViewModelInject, you’ll need to add these 2 libraries on top of the Dagger Hilt Libraries added as shared in the article above.

要使用@ViewModelInject ,您需要在上述文章中共享的Dagger Hilt库的顶部添加这两个库。

implementation "androidx.hilt:hilt-lifecycle-viewmodel
:1.0.0-alpha02"
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02'

@ViewModelInject和@Assisted批注 (The @ViewModelInject and @Assisted annotation)

In your ViewModel, just add @ViewModelInject to the constructor, as well as @Assisted to the savedStateHandle

在您的ViewModel中,只需添加@ViewModelInject 到构造函数以及@Assisted savedStateHandle

class MyViewModel @ViewModelInject constructor(
private val repository: Repository,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel(), LifecycleObserver {

不再需要工厂! (No more Factory needed anymore!)

With this in place, you no longer need any ViewModelFactory! All is done for you! In your activity, you can now just use KTX viewModels() directly.

有了这个,您不再需要任何ViewModelFactory! 一切都为您完成! 在您的活动中,您现在可以直接使用KTX viewModels()

private val viewModel: MyViewModel by viewModels()

With these all in place, the entire thing will work without need Activity worries about how to inject any of the stuff into ViewModel.

所有这些都准备就绪后,整个事情将可以正常进行而无需Activity担心如何将任何东西注入ViewModel。

Image for post

比较方式 (Comparison)

I have created a sample working design here. Provided below for a quick simple comparison of

我在这里创建了一个示例工作设计。 以下提供了以下内容的快速简单比较

  1. ViewModel Creation with Factory

    使用工厂创建ViewModel
  2. ViewModel Creation wit @ViewModelInject and Dagger Hilt.

    使用@ViewModelInject和Dagger Hilt创建ViewModel。

Image for post

Hopes this helps your quest to move towards ViewModel better! Cheers.

希望这可以帮助您更好地向ViewModel迈进! 干杯。

If you find this helpful, the below article might be beneficial as well.

如果您觉得这有帮助,那么下面的文章也可能会有所帮助。

翻译自: https://medium.com/mobile-app-development-publication/injecting-viewmodel-with-dagger-hilt-54ca2e433865

dagger 注入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值