-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify injection of ViewModelProvider.Factory instances by using Mu…
…ltibinding. Adding a ViewModel now only requires adding one method in ViewModelModule and no changes in GithubViewModelFactory.
- Loading branch information
1 parent
bf256af
commit 619ef78
Showing
5 changed files
with
65 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
GithubBrowserSample/app/src/main/java/com/android/example/github/di/ViewModelKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.android.example.github.di; | ||
|
||
import android.arch.lifecycle.ViewModel; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import dagger.MapKey; | ||
|
||
@Documented | ||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MapKey | ||
@interface ViewModelKey { | ||
Class<? extends ViewModel> value(); | ||
} |
34 changes: 34 additions & 0 deletions
34
GithubBrowserSample/app/src/main/java/com/android/example/github/di/ViewModelModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.android.example.github.di; | ||
|
||
import android.arch.lifecycle.ViewModel; | ||
import android.arch.lifecycle.ViewModelProvider; | ||
|
||
import com.android.example.github.ui.repo.RepoViewModel; | ||
import com.android.example.github.ui.search.SearchViewModel; | ||
import com.android.example.github.ui.user.UserViewModel; | ||
import com.android.example.github.viewmodel.GithubViewModelFactory; | ||
|
||
import dagger.Binds; | ||
import dagger.Module; | ||
import dagger.multibindings.IntoMap; | ||
|
||
@Module | ||
abstract class ViewModelModule { | ||
@Binds | ||
@IntoMap | ||
@ViewModelKey(UserViewModel.class) | ||
abstract ViewModel bindUserViewModel(UserViewModel userViewModel); | ||
|
||
@Binds | ||
@IntoMap | ||
@ViewModelKey(SearchViewModel.class) | ||
abstract ViewModel bindSearchViewModel(SearchViewModel searchViewModel); | ||
|
||
@Binds | ||
@IntoMap | ||
@ViewModelKey(RepoViewModel.class) | ||
abstract ViewModel bindRepoViewModel(RepoViewModel repoViewModel); | ||
|
||
@Binds | ||
abstract ViewModelProvider.Factory bindViewModelFactory(GithubViewModelFactory factory); | ||
} |
39 changes: 0 additions & 39 deletions
39
...bBrowserSample/app/src/main/java/com/android/example/github/di/ViewModelSubComponent.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters