/**
* Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
* an activity), associated with this `ViewModelProvider`.
*
*
* The created ViewModel is associated with the given scope and will be retained
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* @param modelClass The class of the ViewModel to create an instance of it if it is not
* present.
* @return A ViewModel that is an instance of the given type `T`.
* @throws IllegalArgumentException if the given [modelClass] is local or anonymous class.
*/@MainThreadpublicopenoperatorfun<T:ViewModel>get(modelClass:Class<T>):T{valcanonicalName=modelClass.canonicalName?:throwIllegalArgumentException("Local and anonymous classes can not be ViewModels")returnget("$DEFAULT_KEY:$canonicalName",modelClass)}/**
* Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or
* an activity), associated with this `ViewModelProvider`.
*
* The created ViewModel is associated with the given scope and will be retained
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* @param key The key to use to identify the ViewModel.
* @param modelClass The class of the ViewModel to create an instance of it if it is not
* present.
* @return A ViewModel that is an instance of the given type `T`.
*/@Suppress("UNCHECKED_CAST")@MainThreadpublicopenoperatorfun<T:ViewModel>get(key:String,modelClass:Class<T>):T{valviewModel=store[key]if(modelClass.isInstance(viewModel)){(factoryas?OnRequeryFactory)?.onRequery(viewModel!!)returnviewModelasT}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)}}