如何在动态功能模块中将SavedStateHandle注入ViewModel?

时间:2020-09-25 17:01:35

标签: android android-savedstate dagger-hilt dynamic-feature-module viewmodel-savedstate

使用带有@Assistedby viewModels()的注释,可以在不是动态特征模块的模块中将SavedStateHandle对象注入到ViewModel中,而这些模块的匕首刀柄为

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    private val viewModel: MainActivityViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

class MainActivityViewModel @ViewModelInject constructor(
    @Assisted savedStateHandle: SavedStateHandle
) : ViewModel() {

    val stringData = savedStateHandle.getLiveData<String>("hello_world")
}

,但是动态功能模块无法做到这一点。动态要素模块ViewModels如何完成?

1 个答案:

答案 0 :(得分:2)

我的ViewModel是

class DashboardViewModel @ViewModelInject constructor(
    @Assisted private val savedStateHandle: SavedStateHandle,
    private val coroutineScope: CoroutineScope,
    private val dashboardStatsUseCase: GetDashboardStatsUseCase,
    private val setPropertyStatsUseCase: SetPropertyStatsUseCase
) : ViewModel() {

}

使用以下方式为SavedStateHandle创建通用FragmentFactory

interface ViewModelFactory<out V : ViewModel> {
    fun create(handle: SavedStateHandle): V
}

class GenericSavedStateViewModelFactory<out V : ViewModel>(
    private val viewModelFactory: ViewModelFactory<V>,
    owner: SavedStateRegistryOwner,
    defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(
        key: String,
        modelClass: Class<T>,
        handle: SavedStateHandle
    ): T {
        return viewModelFactory.create(handle) as T
    }
}

/**
 * Convenience function to use with `by viewModels` that creates an instance of
 * [AbstractSavedStateViewModelFactory] that enables us to pass [SavedStateHandle]
 * to the [ViewModel]'s constructor.
 *
 * @param factory instance of [ViewModelFactory] that will be used to construct the [ViewModel]
 * @param owner instance of Fragment or Activity that owns the [ViewModel]
 * @param defaultArgs Bundle with default values to populate the [SavedStateHandle]
 *
 * @see ViewModelFactory
 */
@MainThread
inline fun <reified VM : ViewModel> SavedStateRegistryOwner.withFactory(
    factory: ViewModelFactory<VM>,
    defaultArgs: Bundle? = null
) = GenericSavedStateViewModelFactory(factory, this, defaultArgs)

ViewModel的ViewModel工厂

class DashboardViewModelFactory @Inject constructor(
    private val coroutineScope: CoroutineScope,
    private val dashboardStatsUseCase: GetDashboardStatsUseCase,
    private val setPropertyStatsUseCase: SetPropertyStatsUseCase
) : ViewModelFactory<DashboardViewModel> {

    override fun create(handle: SavedStateHandle): DashboardViewModel {
        return DashboardViewModel(
            handle,
            coroutineScope,
            dashboardStatsUseCase,
            setPropertyStatsUseCase
        )
    }
}

并使用Fragment中的DashBoardViewModelFactory创建ViewModel

@Inject
lateinit var dashboardViewModelFactory: DashboardViewModelFactory

private val viewModel: DashboardViewModel
by viewModels { withFactory(dashboardViewModelFactory) }

Here,您可以看到完整的实施方案。我找不到用于实现此解决方案的资源,如果您可以评论链接,我想感谢作者。