18

我首先尝试 androidViewModelHiltDI

正如我从下面的链接中了解的那样,要使用运行时值初始化 ViewModel,我应该使用ViewModelFactory

使用 ViewModelFactory

//ViewModel
class ScoreViewModel(finalScore: Int) : ViewModel() {
   // The final score
   var score = finalScore
   init {
       Log.i("ScoreViewModel", "Final score is $finalScore")
   }
}

//ViewModelFactory
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
   if (modelClass.isAssignableFrom(ScoreViewModel::class.java)) {
       return ScoreViewModel(finalScore) as T
   }
   throw IllegalArgumentException("Unknown ViewModel class")
}


//Fragment
viewModelFactory = ScoreViewModelFactory(ScoreFragmentArgs.fromBundle(arguments!!).score)

并且要使用带有刀柄的 ViewModel,我应该@ViewModelInject按照以下链接中的说明使用

Hilt 和 Jetpack 集成

//ViewModel
class ExampleViewModel @ViewModelInject constructor(
  private val repository: ExampleRepository,
  @Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
  ...
}

//Activity / Fragment
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {
  private val exampleViewModel: ExampleViewModel by viewModels()
  ...
}

但是怎么HiltViewModelFactory呢?

似乎答案在@Assisted但我不知道如何

如何告诉 hilt 我喜欢将存储库接口注入 ViewModel,同时仍然允许 ViewModelFactory 在运行时使用参数初始化 ViewModel?

4

2 回答 2

24

由@Elye 提供,下一篇文章很有帮助。我推荐阅读。

通过注入将 Activity Intent 数据传递给 ViewModel

使用 Dagger Hilt 注入 ViewModel

似乎不需要大多数工厂,因为大多数viewmodel初始参数都是从以前的片段中获取的,并且可以通过SavedStateHandle标记为@Assisted 时自动注入来访问

为了设置刀柄,我使用了以下代码实验室教程

在您的 Android 应用程序中使用 Hilt

然后,viewModel仅使用下一个代码自动完成注入

请注意,正如 fabioCollini在这里所指出的,它似乎savedStateHandle也可以通过简单地将参数名称作为键来从安全参数中获取值。事实上,这就是我在以下示例中所做的。ps:为了使安全参数更加“安全”,我确实尝试将其替换为SavedStateHandle希望ItemsFragmentArgs它可以工作但应用程序没有编译。我确实希望它会在未来实施(如果已经实施,请告诉我)

//ItemFragment file
@AndroidEntryPoint
class ItemsFragment : Fragment() {

    private val viewModel: ItemsViewModel by viewModels()

    //use viewModel as you would. No need to initialize.
}

//Module file - if you have any repository, remember to bind it 
//or provide the exact implementation as noted in code-labs
@InstallIn(ApplicationComponent::class)
@Module
abstract class DatabaseModuleBinder {

    @Binds
    abstract fun bindGlistRepository(impl: FirestoreGlistRepository): GlistRepository

}


//ItemsViewModel file - lastly, anotate as follows and take your arguments 
//from savedStateHandle (for safe args, use variable name as key)
class ItemsViewModel @ViewModelInject constructor(private val glistRepo: GlistRepository,
                     @Assisted private val savedStateHandle: SavedStateHandle) : ViewModel() {

    private val glistLiveDate = glistRepo.getGlistLiveData(
        savedStateHandle.get<String>("listId")!!
    )

..
}

希望它可以帮助任何人,如果有任何错误,请告诉我

于 2020-07-02T21:06:34.277 回答
3

将您的 ScoreViewModelFactory 传递到 viewModel 的内置 ktx-extension 中。您还可以通过将 SavedStateHandle 本身与 defaultViewModelProviderFactory 一起使用来使用 Activity/Fragment 参数。

/*
Gradle Dependencies
def lifecycle_version = "2.2.0"
def hiltLifeVersion = "1.0.0-alpha01"
def hiltVersion = "2.28.1-alpha"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "com.google.dagger:hilt-android:$hiltVersion"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01"
implementation "androidx.hilt:hilt-work:$hiltLifeVersion"
implementation "androidx.hilt:hilt-common:1.0.0-alpha01"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
kapt "androidx.hilt:hilt-compiler:$hiltLifeVersion"
*/

import androidx.fragment.app.viewModels

@AndroidEntryPoint
class ExampleFragment : Fragment(R.layout.example_fragment) {

    //internally using defaultViewModelProviderFactory 
    private val viewModel : ExampleViewModel by viewModels()

    //or you own viewmodal factory instance --> scoreViewModelFactory
    private val viewModel : ExampleViewModel by viewModels { scoreViewModelFactory }

}

class ExampleViewModel @ViewModelInject constructor(
    private val repository: ExampleRepository,
    @Assisted override val savedStateHandle: SavedStateHandle
) : ViewModel() {

    //bundle args -> String, Int, Parcelable etc.. 
    private val arg1LiveData: MutableLiveData<String> = 
                         savedStateHandle.getLiveData("arg1", "")

}

在 Fragment viewmodel 的内置 ktx-extension

@MainThread
inline fun <reified VM : ViewModel> Fragment.viewModels(
    noinline ownerProducer: () -> ViewModelStoreOwner = { this },
    noinline factoryProducer: (() -> Factory)? = null
) = createViewModelLazy(VM::class, { ownerProducer().viewModelStore }, factoryProducer)
于 2020-07-02T08:00:32.223 回答