/*
 * Copyright (C) 2020 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.hilt.android.internal.lifecycle;

import androidx.lifecycle.AbstractSavedStateViewModelFactory;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.savedstate.SavedStateRegistryOwner;
import dagger.Module;
import dagger.hilt.EntryPoint;
import dagger.hilt.EntryPoints;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ViewModelComponent;
import dagger.hilt.android.internal.builders.ViewModelComponentBuilder;
import dagger.multibindings.Multibinds;
import java.util.Map;
import java.util.Set;
import javax.inject.Provider;

/**
 * View Model Provider Factory for the Hilt Extension.
 *
 * <p>A provider for this factory will be installed in the {@link
 * dagger.hilt.android.components.ActivityComponent} and {@link
 * dagger.hilt.android.components.FragmentComponent}. An instance of this factory will also be the
 * default factory by activities and fragments annotated with {@link
 * dagger.hilt.android.AndroidEntryPoint}.
 */
public final class HiltViewModelFactory implements ViewModelProvider.Factory {

  @EntryPoint
  @InstallIn(ViewModelComponent.class)
  interface ViewModelFactoriesEntryPoint {
    @HiltViewModelMap
    Map<String, Provider<ViewModel>> getHiltViewModelMap();
  }

  /** Hilt module for providing the empty multi-binding map of ViewModels. */
  @Module
  @InstallIn(ViewModelComponent.class)
  interface ViewModelModule {
    @Multibinds
    @HiltViewModelMap
    Map<String, ViewModel> hiltViewModelMap();
  }

  private final Set<String> viewModelInjectKeys;
  private final ViewModelProvider.Factory delegateFactory;
  private final AbstractSavedStateViewModelFactory viewModelInjectFactory;

  public HiltViewModelFactory(
      @NonNull SavedStateRegistryOwner owner,
      @Nullable Bundle defaultArgs,
      @NonNull Set<String> viewModelInjectKeys,
      @NonNull ViewModelProvider.Factory delegateFactory,
      @NonNull ViewModelComponentBuilder viewModelComponentBuilder) {
    this.viewModelInjectKeys = viewModelInjectKeys;
    this.delegateFactory = delegateFactory;
    this.viewModelInjectFactory =
        new AbstractSavedStateViewModelFactory(owner, defaultArgs) {
          @NonNull
          @Override
          @SuppressWarnings("unchecked")
          protected <T extends ViewModel> T create(
              @NonNull String key, @NonNull Class<T> modelClass, @NonNull SavedStateHandle handle) {
            ViewModelComponent component =
                viewModelComponentBuilder.savedStateHandle(handle).build();
            Provider<? extends ViewModel> provider =
                EntryPoints.get(component, ViewModelFactoriesEntryPoint.class)
                    .getHiltViewModelMap()
                    .get(modelClass.getName());
            if (provider == null) {
              throw new IllegalStateException(
                  "Expected the @ViewModelInject-annotated class '"
                      + modelClass.getName()
                      + "' to be available in the multi-binding of "
                      + "@ViewModelInjectMap but none was found.");
            }
            return (T) provider.get();
          }
        };
  }

  @NonNull
  @Override
  public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    if (viewModelInjectKeys.contains(modelClass.getName())) {
      return viewModelInjectFactory.create(modelClass);
    } else {
      return delegateFactory.create(modelClass);
    }
  }
}

@@ -2286,33 +2286,37 @@
 Set%3CString%3E 
-v
+hiltV
 iewModel
 InjectKeys;%0A
@@ -2295,38 +2295,32 @@
 g%3E hiltViewModel
-Inject
 Keys;%0A  private 
@@ -2419,25 +2419,29 @@
 Factory 
-v
+hiltV
 iewModel
 InjectFa
@@ -2428,30 +2428,24 @@
 iltViewModel
-Inject
 Factory;%0A%0A  
@@ -2582,25 +2582,29 @@
 String%3E 
-v
+hiltV
 iewModel
 InjectKe
@@ -2591,30 +2591,24 @@
 iltViewModel
-Inject
 Keys,%0A      
@@ -2734,33 +2734,37 @@
  %7B%0A    this.
-v
+hiltV
 iewModel
 InjectKeys =
@@ -2755,30 +2755,28 @@
 odel
-Inject
 Keys = 
-v
+hiltV
 iewModel
 Inje
@@ -2767,30 +2767,24 @@
 iltViewModel
-Inject
 Keys;%0A    th
@@ -2830,25 +2830,29 @@
    this.
-v
+hiltV
 iewModel
 InjectFa
@@ -2839,30 +2839,24 @@
 iltViewModel
-Inject
 Factory =%0A  
@@ -3613,16 +3613,20 @@
 ed the @
+Hilt
 ViewMode
@@ -3626,22 +3626,16 @@
 iewModel
-Inject
 -annotat
@@ -3786,16 +3786,20 @@
     + %22@
+Hilt
 ViewMode
@@ -3799,22 +3799,16 @@
 iewModel
-Inject
 Map but 
@@ -4010,25 +4010,29 @@
     if (
-v
+hiltV
 iewModel
 InjectKe
@@ -4023,22 +4023,16 @@
 iewModel
-Inject
 Keys.con
@@ -4075,25 +4075,29 @@
  return 
-v
+hiltV
 iewModel
 InjectFa
@@ -4092,14 +4092,8 @@
 odel
-Inject
 Fact

Rename some usages of ViewModelInject to HiltViewModel