• 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.scopes;
18 
19 import java.lang.annotation.Retention;
20 import java.lang.annotation.RetentionPolicy;
21 import javax.inject.Scope;
22 
23 /**
24  * Scope annotation for bindings that should exist for the life of a a single {@link
25  * androidx.lifecycle.ViewModel}.
26  *
27  * <p>Use this scope annotation when you want to define a dependency in the {@link
28  * dagger.hilt.android.components.ViewModelComponent} for which a single instance will be provided
29  * across all other dependencies for a single {@link
30  * dagger.hilt.android.lifecycle.HiltViewModel}-annotated {@code ViewModel}. Other {@code
31  * ViewModel}s that request the scoped dependency will receive a different instance. For sharing the
32  * same instance of a dependency across all {@code ViewModel}s use a scope from one of the parent
33  * components of {@code dagger.hilt.android.components.ViewModelComponent}, such as {@link
34  * javax.inject.Singleton} or {@link dagger.hilt.android.scopes.ActivityRetainedScoped}.
35  *
36  * <p>For example:
37  *
38  * <pre>
39  * &#64;Module
40  * &#64;InstallIn(ViewModelComponent.class)
41  * public final class ViewModelMovieModule {
42  *     &#64;Provides
43  *     &#64;ViewModelScoped
44  *     public static MovieRepository provideRepo(SavedStateHandle handle) {
45  *         return new MovieRepository(handle.getString("movie-id"));
46  *     }
47  * }
48  *
49  * public final class MovieDetailFetcher {
50  *     &#64;Inject MovieDetailFetcher(MovieRepository movieRepo) {
51  *         // ...
52  *     }
53  * }
54  *
55  * public final class MoviePosterFetcher {
56  *     &#64;Inject MoviePosterFetcher(MovieRepository movieRepo) {
57  *         // ...
58  *     }
59  * }
60  *
61  * &#64;HiltViewModel
62  * public class MovieViewModel extends ViewModel {
63  *     &#64;Inject
64  *     public MovieViewModel(MovieDetailFetcher detailFetcher, MoviePosterFetcher posterFetcher) {
65  *         // Both detailFetcher and posterFetcher will contain the same instance of
66  *         // the MovieRepository.
67  *     }
68  * }
69  * </pre>
70  *
71  * @see dagger.hilt.android.lifecycle.HiltViewModel
72  * @see dagger.hilt.android.components.ViewModelComponent
73  */
74 @Scope
75 @Retention(RetentionPolicy.CLASS)
76 public @interface ViewModelScoped {}
77