@DslMarker public annotation classViewModelFactoryDsl
public inline fun viewModelFactory( builder: InitializerViewModelFactoryBuilder.() -> Unit ): ViewModelProvider.Factory = InitializerViewModelFactoryBuilder().apply(builder).build()
publicabstractclassViewModel { // Can't use ConcurrentHashMap, because it can lose values on old apis (see b/37042460) @Nullable privatefinal Map<String, Object> mBagOfTags = newHashMap<>(); @Nullable privatefinal Set<Closeable> mCloseables = newLinkedHashSet<>(); privatevolatilebooleanmCleared=false;
@JvmOverloads constructor( private val store: ViewModelStore, private val factory: Factory, private val defaultCreationExtras: CreationExtras = CreationExtras.Empty, ) {
publicinterfaceFactory {
public fun <T : ViewModel> create(modelClass: Class<T>): T { throw UnsupportedOperationException( "Factory.create(String) is unsupported. This Factory requires " + "`CreationExtras` to be passed into `create` method." ) }
public fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T = create(modelClass)
@MainThread public open operator fun <T : ViewModel> get(modelClass: Class<T>): T { valcanonicalName= modelClass.canonicalName ?: throw IllegalArgumentException("Local and anonymous classes can not be ViewModels") return get("$DEFAULT_KEY:$canonicalName", modelClass) }
@Suppress("UNCHECKED_CAST") @MainThread public open operator fun <T : ViewModel> get(key: String, modelClass: Class<T>): T { valviewModel= store[key] if (modelClass.isInstance(viewModel)) { (factory as? OnRequeryFactory)?.onRequery(viewModel) return viewModel as T } else { @Suppress("ControlFlowWithEmptyBody") if (viewModel != null) { // TODO: log a warning. } } valextras= MutableCreationExtras(defaultCreationExtras) extras[VIEW_MODEL_KEY] = key // AGP has some desugaring issues associated with compileOnly dependencies so we need to // fall back to the other create method to keep from crashing. returntry { factory.create(modelClass, extras) } catch (e: AbstractMethodError) { factory.create(modelClass) }.also { store.put(key, it) } }
// actually there is getInstance() @Suppress("SingletonConstructor") public open classNewInstanceFactory : Factory { @Suppress("DocumentExceptions") override fun <T : ViewModel> create(modelClass: Class<T>): T { returntry { modelClass.newInstance() } catch (e: InstantiationException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } catch (e: IllegalAccessException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } }
public companion object { privatevar sInstance: NewInstanceFactory? = null @JvmStatic public val instance: NewInstanceFactory @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) get() { if (sInstance == null) { sInstance = NewInstanceFactory() } return sInstance!! }
private object ViewModelKeyImpl : Key<String>
@JvmField val VIEW_MODEL_KEY: Key<String> = ViewModelKeyImpl } }
public open classAndroidViewModelFactory privateconstructor( private val application: Application?, @Suppress("UNUSED_PARAMETER") unused: Int, ) : NewInstanceFactory() {
@Suppress("DocumentExceptions") override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T { returnif (application != null) { create(modelClass) } else { valapplication= extras[APPLICATION_KEY] if (application != null) { create(modelClass, application) } else { // For AndroidViewModels, CreationExtras must have an application set if (AndroidViewModel::class.java.isAssignableFrom(modelClass)) { throw IllegalArgumentException( "CreationExtras must have an application by `APPLICATION_KEY`" ) } super.create(modelClass) } } }
@Suppress("DocumentExceptions") override fun <T : ViewModel> create(modelClass: Class<T>): T { returnif (application == null) { throw UnsupportedOperationException( "AndroidViewModelFactory constructed " + "with empty constructor works only with " + "create(modelClass: Class<T>, extras: CreationExtras)." ) } else { create(modelClass, application) } }
@Suppress("DocumentExceptions") private fun <T : ViewModel> create(modelClass: Class<T>, app: Application): T { returnif (AndroidViewModel::class.java.isAssignableFrom(modelClass)) { try { modelClass.getConstructor(Application::class.java).newInstance(app) } catch (e: NoSuchMethodException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } catch (e: IllegalAccessException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } catch (e: InstantiationException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } catch (e: InvocationTargetException) { throw RuntimeException("Cannot create an instance of $modelClass", e) } } elsesuper.create(modelClass) }
public companion object { internal fun defaultFactory(owner: ViewModelStoreOwner): Factory = if (owner is HasDefaultViewModelProviderFactory) owner.defaultViewModelProviderFactory else instance