This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DaggerSavableViewModel.kt
32 lines (26 loc) · 1.59 KB
/
DaggerSavableViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ufkoku.archcomponents
import android.os.Bundle
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.savedstate.SavedStateRegistryOwner
import dagger.android.HasAndroidInjector
abstract class DaggerSavableViewModel(hasAndroidInjector: HasAndroidInjector,
protected val savedStateHandle: SavedStateHandle) : DaggerViewModel(hasAndroidInjector) {
class Factory(private val hasAndroidInjector: HasAndroidInjector,
savedRegistryOwner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null) : AbstractSavedStateViewModelFactory(savedRegistryOwner, defaultArgs) {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T = modelClass.constructors.let {
if (it.size == 1) {
val ctr = it[0]
if (ctr.parameterTypes.size == 2
&& ctr.parameterTypes[0].isAssignableFrom(HasAndroidInjector::class.java)
&& ctr.parameterTypes[1].isAssignableFrom(SavedStateHandle::class.java)) {
return@let ctr.newInstance(hasAndroidInjector, handle) as T
}
}
return@let null
} ?: throw IllegalArgumentException("Provided ${modelClass.canonicalName} must contain exactly 1 constructor with 2 arguments which are ${HasAndroidInjector::class.java.canonicalName} and ${SavedStateHandle::class.java.canonicalName}")
}
}