Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class TodoViewModelFactory @Inject constructor(
  2. private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>
  3. ) : ViewModelProvider.Factory {
  4. override fun <T : ViewModel?> create(modelClass: Class<T>): T {
  5. var creator: Provider<out ViewModel>? = creators[modelClass]
  6. if (creator == null) {
  7. for ((key, value) in creators) {
  8. if (modelClass.isAssignableFrom(key)) {
  9. creator = value
  10. break
  11. }
  12. }
  13. }
  14. if (creator == null) {
  15. throw IllegalArgumentException("Unknown model class: $modelClass")
  16. }
  17. try {
  18. @Suppress("UNCHECKED_CAST")
  19. return creator.get() as T
  20. } catch (e: Exception) {
  21. throw RuntimeException(e)
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement