Last active
July 2, 2018 10:42
-
-
Save Alezhka/b36930f4b8a4d75aba6d4ab777cced77 to your computer and use it in GitHub Desktop.
Dagger2 viewmodel mapping.
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
@Singleton | |
@Component(modules = { | |
AndroidSupportInjectionModule.class, | |
AppModule.class, | |
BuildersModule.class}) | |
public interface AppComponent { | |
void inject(Application app); | |
@Component.Builder | |
interface Builder { | |
@BindsInstance | |
Builder application(Application app); | |
AppComponent build(); | |
} | |
} |
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
@Module(includes = {AndroidInjectionModule.class, ViewModelModule.class}) | |
public class AppModule { | |
@Provides | |
Context provideContext(Application application) { | |
return application.getApplicationContext(); | |
} | |
@Provides | |
android.app.Application provideAndroidApplication(Application application) { | |
return application; | |
} | |
} |
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
@Singleton | |
public class SampleViewModelFactory extends ViewModelProvider.NewInstanceFactory { | |
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators; | |
@Inject | |
public SampleViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { | |
this.creators = creators; | |
} | |
@NonNull | |
@SuppressWarnings("unchecked") | |
@Override | |
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | |
Provider<? extends ViewModel> creator = creators.get(modelClass); | |
if (creator == null) { | |
for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) { | |
if (modelClass.isAssignableFrom(entry.getKey())) { | |
creator = entry.getValue(); | |
break; | |
} | |
} | |
} | |
if (creator == null) { | |
throw new IllegalArgumentException("unknown model class " + modelClass); | |
} | |
try { | |
return (T) creator.get(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
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
@Module | |
abstract class BuildersModule { | |
@ContributesAndroidInjector(modules = {SampleActivityModule.class}) | |
abstract SampleActivity bindSampleActivity(); | |
} |
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
public class SampleActivity extends AppCompatActivity { | |
@Inject | |
SampleViewModelFactory viewModelFactory; | |
private SampleViewModel viewModel; | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
AndroidInjection.inject(this); | |
super.onCreate(savedInstanceState); | |
viewModel = ViewModelProviders.of(this, viewModelFactory).get(SampleViewModel.class); | |
} | |
} |
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
@Module(includes = {InterviewModule.class}) | |
public class SampleActivityModule { | |
} |
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
public class SampleViewModel extends AndroidViewModel { | |
@Inject | |
public InterviewViewModel(@NonNull final Application application) { | |
super(application); | |
} | |
} |
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
@Documented | |
@Target({ElementType.METHOD}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@MapKey | |
public @interface ViewModelKey { | |
Class<? extends ViewModel> value(); | |
} |
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
@Module | |
public abstract class ViewModelModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey(SampleViewModel.class) | |
abstract ViewModel bindSampleViewModel(SampleViewModel viewModel); | |
@Binds | |
abstract ViewModelProvider.Factory bindViewModelFactory(AppViewModelFactory factory); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment