• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.hilt.android.lifecycle;
18 
19 import dagger.hilt.GeneratesRootInput;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import java.lang.annotation.Target;
24 
25 /**
26  * Identifies a {@link androidx.lifecycle.ViewModel} for construction injection.
27  *
28  * <p>The {@code ViewModel} annotated with {@link HiltViewModel} will be available for creation by
29  * the {@link dagger.hilt.android.lifecycle.HiltViewModelFactory} and can be retrieved by default in
30  * an {@code Activity} or {@code Fragment} annotated with {@link
31  * dagger.hilt.android.AndroidEntryPoint}. The {@code HiltViewModel} containing a constructor
32  * annotated with {@link javax.inject.Inject} will have its dependencies defined in the constructor
33  * parameters injected by Dagger's Hilt.
34  *
35  * <p>Example:
36  *
37  * <pre>
38  * &#64;HiltViewModel
39  * public class DonutViewModel extends ViewModel {
40  *     &#64;Inject
41  *     public DonutViewModel(SavedStateHandle handle, RecipeRepository repository) {
42  *         // ...
43  *     }
44  * }
45  * </pre>
46  *
47  * <pre>
48  * &#64;AndroidEntryPoint
49  * public class CookingActivity extends AppCompatActivity {
50  *     public void onCreate(Bundle savedInstanceState) {
51  *         DonutViewModel vm = new ViewModelProvider(this).get(DonutViewModel.class);
52  *     }
53  * }
54  * </pre>
55  *
56  * <p>{@code ViewModel}s annotated with {@link HiltViewModel} can also be used with assisted
57  * injection:
58  *
59  * <pre>
60  * &#64;HiltViewModel(assistedFactory = DonutViewModel.Factory.class)
61  * public class DonutViewModel extends ViewModel {
62  *     &#64;AssistedInject
63  *     public DonutViewModel(
64  *         SavedStateHandle handle,
65  *         RecipeRepository repository,
66  *         $#64;Assisted int donutId
67  *     ) {
68  *         // ...
69  *     }
70  *
71  *     &#64;AssistedFactory
72  *     public interface Factory {
73  *         DonutViewModel create(int donutId);
74  *     }
75  * }
76  * </pre>
77  *
78  * <pre>
79  * &#64;AndroidEntryPoint
80  * public class CookingActivity extends AppCompatActivity {
81  *     public void onCreate(Bundle savedInstanceState) {
82  *         DonutViewModel vm = new ViewModelProvider(
83  *             getViewModelStore(),
84  *             getDefaultViewModelProviderFactory(),
85  *             HiltViewModelExtensions.withCreationCallback(
86  *                 getDefaultViewModelCreationExtras(),
87  *                 (DonutViewModel.Factory factory) -> factory.create(1)
88  *             )
89  *         ).get(DonutViewModel.class);
90  *     }
91  * }
92  * </pre>
93  *
94  * <p>Exactly one constructor in the {@code ViewModel} must be annotated with {@code Inject} or
95  * {@code AssistedInject}.
96  *
97  * <p>Only dependencies available in the {@link dagger.hilt.android.components.ViewModelComponent}
98  * can be injected into the {@code ViewModel}.
99  *
100  * <p>
101  *
102  * @see dagger.hilt.android.components.ViewModelComponent
103  */
104 @Target(ElementType.TYPE)
105 @Retention(RetentionPolicy.CLASS)
106 @GeneratesRootInput
107 public @interface HiltViewModel {
108   /**
109    * Returns a factory class that can be used to create this ViewModel with assisted injection. The
110    * default value `Object.class` denotes that no factory is specified and the ViewModel is not
111    * assisted injected.
112    */
assistedFactory()113   Class<?> assistedFactory() default Object.class;
114 }
115