Intantiating a viewmodel using dagger2 in BaseFragment - java

I am sort of new to Dagger and still learning it. According to the tutorials and blogs I read, currently Android does not have a way of injecting dependencies into ViewModels hence we need to use a custom ViewModelProvider.Factory, that said, I managed to get my hands on this one
public class ViewModelProviderFactory implements ViewModelProvider.Factory {
private static final String TAG = ViewModelProviderFactory.class.getSimpleName();
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
#Inject
public ViewModelProviderFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
#Override
public <T extends ViewModel> T create(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);
}
}
}
It works, it has worked for many of my use cases until now. Originally I had to an instance of a ViewModel with something like this
public AFragment extends BaseFragment{
#Inject
ViewModelProviderFactory providerFactory;
private MyViewModel viewModel;
MyViewModel getViewModel(){
return ViewModelProviders.of(this, providerFactory).get(MyViewModel.class);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = getViewModel();
tokenAuthenticator.setAuthenticatorListener(this);
}
}
But as the project grew I realized this was not neat, I had to do this in all my fragments so I opted for a different approach, I wanted to instantiate my ViewModel in my BaseFragment instead and I did this
public abstract class BaseFragment<T extends BaseViewModel, E extends ViewDataBinding> extends DaggerFragment implements TokenAuthenticator.AuthenticatorListener {
private static final String TAG = BaseFragment.class.getSimpleName();
public E binding;
public final CompositeDisposable disposables = new CompositeDisposable();
public T viewModel;
#Inject
ViewModelProviderFactory providerFactory;
private int layoutId;
/**
* #return view model instance
*/
public T getViewModel() {
final Type[] types = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
return ViewModelProviders.of(this, providerFactory).get((Class<T>)types[0]);
}
}
This gives me compile error
A binding with matching key exists in component: xxx.xxx.core.base.dagger.builders.FragmentBuilderModule_ContributeDeliveryPlanFragment.DeliveryPlanFragmentSubcomponent
.
.
.
A binding with matching key exists in component: xxx.xxx.core.base.dagger.builders.FragmentBuilderModule_ContributePayWithMtmMobileMoneyFragment.PayWithMtmMobileMoneyFragmentSubcomponent
java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
xxx.xxx.core.base.ViewModelProviderFactory(creators)
xxx.xxx.core.base.ViewModelProviderFactory is injected at
mika.e.mikaexpressstore.core.base.BaseFragment.providerFactory
xxx.xxx.xxx.xxx.cashondelivery.CashOnDeliveryFragment is injected at
dagger.android.AndroidInjector.inject(T) [xxx.xxx.core.base.dagger.component.AppComponent → xxx.xxx.core.base.dagger.builders.FragmentBuilderModule_ContributeCashOnDeliveryFragment.CashOnDeliveryFragmentSubcomponent]
2 errors
From the error message I can tell Dagger is complaining the ViewModelProviderFactory is being injected in the base but used in the child, I need help, is there a way to make this work? surely I want to reduce on boilerplate and repetitive code.

I finally fixed it, not how I wanted but better than having to instantiate each viewmodel from the child class. After reading this answer I came to a realization that this was not possible, so instead I removed #Inject annotation from ViewModelProviderFactory in my BaseFragment and it looked like
public abstract class BaseFragment<T extends BaseViewModel, E extends ViewDataBinding> extends DaggerFragment implements TokenAuthenticator.AuthenticatorListener {
private static final String TAG = BaseFragment.class.getSimpleName();
public E binding;
public final CompositeDisposable disposables = new CompositeDisposable();
public T viewModel;
#Inject
private ViewModelProviderFactory providerFactory;
private int layoutId;
/**
* #return view model instance
*/
public T getViewModel() {
final Type[] types = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
return ViewModelProviders.of(this, providerFactory).get((Class<T>)types[0]);
}
#MainThread
protected final void setProviderFactory(ViewModelProviderFactory providerFactory) {
this.providerFactory = providerFactory;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = getViewModel();
}
}
And injected the provider from the child fragments instead then called the setter from the BaseFragment
public AFragment extends BaseFragment{
#Inject
ViewModelProviderFactory providerFactory;
private MyViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
setLayoutId(R.layout.layout_layout);
setProviderFactory(providerFactory);
super.onCreate(savedInstanceState);
}
}
Key here is to call setProviderFactory(provider) before super.onCreate(savedInstanceState) because when super onCreate is called the provider should not be null, it should be set and ready to create the ViewModel

Related

#Inject in BaseActivity

I am following coding with mitch's dagger 2 course from youtube.I am confused about something.Here is my classes:
AppComponent.class
#Singleton
#Component(
modules = {
AndroidSupportInjectionModule.class,
ActivityBuildersModule.class,
AppModule.class,
ViewModelFactoryModule.class,
}
)
public interface AppComponent extends AndroidInjector<BaseApplication> {
SessionManager sessionManager();
#Component.Builder
interface Builder{
#BindsInstance
Builder application(Application application);
AppComponent build();
}
}
ActivityBuildersModule
#Module
public abstract class ActivityBuildersModule {
#ContributesAndroidInjector(
modules = {AuthViewModelsModule.class,
AuthModule.class
}
)
abstract AuthActivity contributeAuthActivity();
#ContributesAndroidInjector
abstract MainActivity contributeMainActivity();
}
BaseActivity
public abstract class BaseActivity extends DaggerAppCompatActivity {
private static final String TAG = "BaseActivity";
#Inject
public SessionManager sessionManager;//confused here
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subscribeObservers();
}
private void subscribeObservers() {
sessionManager.getAuthUser().observe(this, userAuthResource -> {
if (userAuthResource != null) {
switch (userAuthResource.status) {
case LOADING: {
break;
}
case AUTHENTICATED: {
Log.d(TAG, "subsrcibeObservers: LOGIN SUCCESS:" + userAuthResource.data.getEmail());
break;
}
case ERROR: {
Toast.makeText(this, userAuthResource.message, Toast.LENGTH_SHORT).show();
break;
}
case NOT_AUTHENTICATED: {
navLoginScreen();
break;
}
}
}
});
}
private void navLoginScreen(){
Intent intent = new Intent(this, AuthActivity.class);
startActivity(intent);
finish();
}
}
MainActivity
public class MainActivity extends BaseActivity {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
SessionManager
#Singleton
public class SessionManager {
private static final String TAG = "SessionManager";
private MediatorLiveData<AuthResource<User>> cachedUser = new MediatorLiveData<>();
#Inject
public SessionManager() {
}
public void authenticaWithId(final LiveData<AuthResource<User>> source) {
if (cachedUser != null) {
cachedUser.setValue(AuthResource.loading(null));
cachedUser.addSource(source, userAuthResource -> {
cachedUser.setValue(userAuthResource);
cachedUser.removeSource(source);
});
}
}
public void logOut(){
Log.d(TAG, "logOut: logging out...");
cachedUser.setValue(AuthResource.logout());
}
public LiveData<AuthResource<User>> getAuthUser(){
return cachedUser;
}
}
BaseApplication
public class BaseApplication extends DaggerApplication {
#Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
}
My problem is #Inject SessionManager sessionManager that statement in the BaseActivity.In the ActivityBuildersModule class, we only annotated MainActivity as a subcomponent, not the BaseActivity.Since the MainActivity is a subcomponent it can access dependencies of the AppComponent.So how we get accessed that SessionManager in the Base Activity or we get accessed that object because MainActivity is derived from BaseActivity?
Dagger will inject any annotated fields and methods of the declared parameter type and its supertypes. You can also read about it on the JavaDoc:
Members-injection methods
Members-injection methods have a single parameter and inject dependencies into each of the Inject-annotated fields and methods of the passed instance.
[...]
A note about covariance
While a members-injection method for a type will accept instances of its subtypes, only Inject-annotated members of the parameter type and its supertypes will be injected;
So if you have a SubActivity < BaseActivity < AppCompatActivity then declaring the method as inject(activity: BaseActivity) would only inject fields of BaseActivity and any supertypes, wheras inject(activity: SubActivity) will inject SubActivity as well as any parent/supertypes (BaseActivity in this example, your observed behavior).

ViewModelProvider.Factory remains null on fragment using Dagger and Java on Android

Using the google sample GithubsampleBrowser I have become stuck on trying to inject the ViewModelProvider.Factory.
When comparing with the sample, I see it goes GithubViewModelFactory fine but mine never does and i'm not sure what I am missing. Hopefully it is something very simple and presumably is because I am using androidx components instead.
Main Activity:
public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector {
#Inject
DispatchingAndroidInjector<Fragment> dispatchingAndroidInjector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public DispatchingAndroidInjector<Fragment> supportFragmentInjector() {
return dispatchingAndroidInjector;
}
}
CategoryFragment:
public class CategoryFragment extends Fragment implements Injectable {
#Inject
ViewModelProvider.Factory viewModelFactory;// <-- remains null
AppInjector:
public class AppInjector {
private AppInjector() {}
public static void init(CrosscareApp crossCareApp) {
DaggerAppComponent.builder().application(crossCareApp)
.build().inject(crossCareApp);
crossCareApp
.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
........
}
private static void handleActivity(Activity activity) {
if (activity instanceof HasSupportFragmentInjector) {
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity) {
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
#Override
public void onFragmentCreated(FragmentManager fm, Fragment f,
Bundle savedInstanceState) {
if (f instanceof Injectable) {
AndroidSupportInjection.inject(f);
}
}
}, true);
}
}
}
AppComponent:
#Singleton
#Component(modules = {
AndroidSupportInjectionModule.class,
AppModule.class,
MainActivityModule.class
})
public interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(CrosscareApp crosscareApp);
}
AppModule:
#Module(includes = ViewModelModule.class)
class AppModule {
#Singleton #Provides
CrosscareService provideCrossCareService() {
return new Retrofit.Builder()
..........
}
ViewModelModule:
#Module
abstract class ViewModelModule {
#Binds
#IntoMap
#ViewModelKey(CategoryViewModel.class)
abstract ViewModel bindCategoryViewModel(CategoryViewModel categoryViewModel);
#Binds
abstract ViewModelProvider.Factory bindViewModelFactory(CrosscareViewModelFactory factory);
}
CrosscareViewModel:
#Singleton
public class CrosscareViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
#Inject
public CrosscareViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
Greatly appreciate any help.
Well after all that, I was missing one thing. In the manifest, I needed:
android:name=".CrosscareApp"
In the sample, it was
android:name=".GithubApp"
Presumably the file at the root of the directory.
And then it works.

How to include a dependency from #ModuleA, in #ModuleB using Dagger2 in Java

I am using Dagger2 for dependency injection in quite a large project.
There are 2 different sections of the app that each use multiple usecases.
For each section I have a module that provides the dependencies for that section. I have come across a scenario where I need to use a usecase from moduleA in ModuleB and I am not sure how to include it or add it to be used for that one scenario.I have looked at other answers on stackoverflow but have not found one that answers my question.
For example this answer here
does not help me because it's setup is different and I cannot deduce from this solution how to solve my problem.
and here
I am not certain that I should make any of these dependencies into Singletons.
for example I have ModuleA below that contains AddItem that I need in ModuleB PayForItem
#Module
public class ModuleA {
private final FragmentActivity mActivity;
public ModuleA(FragmentActivity fragmentActivity) {
mActivity = fragmentActivity;
}
...
#Provides
AddItem addItemPresenter(DateMapper dataMapper, ItemdetailUseCase itemDetailUsecase){
return new AddItem(dataMapper,itemDetailUsecase);
}
#Module
public class ModuleB {
private final FragmentActivity mActivity;
public ModuleB(FragmentActivity fragmentActivity) {
mActivity = fragmentActivity;
}
...
#Provides
PayForItem payForItemPresenter(AddItem addItem){
return new AddItem(addItem);
}
Each module is included in its own subcomponent eg:
#Subcomponent(modules = {ModuleA.class})
public interface ModuleAPresentationComponent {
void inject(Fragment testFragment;
}
#Subcomponent(modules = {ModuleB.class})
public interface ModuleBPresentationComponent {
void inject(Fragment testFragment;
}
Both are included in the main component eg:
#Singleton
#Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
public ModuleAPresentationComponent newPresentationComponent(ModuleAPresentationModule presentationModule);
public ModuleBPresentationComponent newPresentationComponent(ModuleBPresentationModule presentationModule);
}
I have a BaseFragment that I use to inject eg:
public abstract class BaseFragment extends Fragment {
private boolean mIsInjectorUsed;
protected static final String PARAM_USER_ID = "param_user_id";
private ApplicationComponent getApplicationComponent() {
return ((AndroidApplication) getActivity().getApplication()).getApplicationComponent();
}
#UiThread
protected ModuleAPresentationComponent getModuleAPresentationComponent() {
if (mIsInjectorUsed) {
throw new RuntimeException("there is no need to use injector more than once");
}
mIsInjectorUsed = true;
return getApplicationComponent()
.newPresentationComponent(new ModuleAPresentationModule(getActivity()));
}
#UiThread
protected ModuleBPresentationComponent getModuleBPresentationComponent() {
if (mIsInjectorUsed) {
throw new RuntimeException("there is no need to use injector more than once");
}
mIsInjectorUsed = true;
return getApplicationComponent()
.newPresentationComponent(new ModuleBPresentationModule(getActivity()));
}
So in my fragment it injects like this:
public class testFragment extends BaseFragment{
#Inject AddItem addItemPresenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getModuleAPresentationComponent().inject(this);
}
}
How would I use AddItem from ModuleA in PayForItem ModuleB without repeating those dependencies in ModuleB?

Mock creation of Object inside method

Problem Description
I'm trying to mock object creation inside of method.
I have LoginFragment which is creating LoginPresenterImpl inside of onCreate method, like shown below:
public class LoginFragment extends BaseFragment {
private LoginPresenter mPresenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = new LoginPresenterImpl(this); <<-- Should be mocked
}
}
I have some problems with combining RobolectricGradleTestRunner and PowerMockRunner in one test but after reading this post, I found way how to do that, so my test look like this:
BaseRobolectricTest.java
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, sdk = 21)
#PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
public abstract class BaseRobolectricTest {
}
Test.java
#PrepareForTest({LoginPresenterImpl.class})
public class Test extends BaseRobolectricTest {
private LoginPresenterImpl mPresenterMock;
#Rule
public PowerMockRule rule = new PowerMockRule();
#Before
public void setup() {
mockStatic(LoginPresenterImpl.class);
mPresenterMock = PowerMockito.mock(LoginPresenterImpl.class);
}
#Test
public void testing() throws Exception {
when(mPresenterMock.loadUsername(any(Context.class))).thenReturn(VALID_USERNAME);
when(mPresenterMock.loadPassword(any(Context.class))).thenReturn(VALID_PASSWORD);
when(mPresenterMock.canAutologin(VALID_USERNAME, VALID_PASSWORD)).thenReturn(true);
whenNew(LoginPresenterImpl.class).withAnyArguments().thenReturn(mPresenterMock);
FragmentTestUtil.startFragment(createLoginFragment());
}
private LoginFragment createLoginFragment() {
LoginFragment loginFragment = LoginFragment.newInstance();
return loginFragment;
}
}
This is just a bad code and opposite of what is "Dependency Injection" pattern.
If you used dependency injection framework like Dagger, such problem wouldn't happen as all used classed would be injected.
In test cases you would override your modules to provide mocks instead of real classes:
#Module
public class TestDataModule extends DataModule {
public TestDataModule(Application application) {
super(application);
}
#Override
public DatabaseManager provideDatabaseManager(DatabaseUtil databaseUtil) {
return mock(DatabaseManager.class);
}
}
And just mock their behavior.
SOLID rules are really important in mainaining testable and reusable code.
This might work, I have no way to test it though...
public class LoginFragment extends BaseFragment {
private LoginPresenter mPresenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = getLoginPresenter();
}
protected LoginPresenter getLoginPresenter() {
return new LoginPresenterImpl(this);
}
}
Then in your Test.java
private LoginFragment createLoginFragment() {
LoginFragment loginFragment = LoginFragmentTest.newInstance();
return loginFragment;
}
private static class LoginFragmentTest extends LoginFragment {
#Override
protected LoginPresenter getLoginPresenter() {
return mPresenterMock;
}
}
Firstly if you cannot change this source code my answer will not help and you have to return to heavy mocking tools.
From coding style and design perspective I would recommend to define a factory that creates an instance of LoginPresenter. You can ask for this factory in LoginFragment constructor. And then use this factory in onCreate method.
Then you can use your own implementation of this factory in unit tests that will create test implementation of LoginPresenter.
That is a POJO approach that makes your code testable.
For example
public class LoginFragment extends BaseFragment {
private LoginPresenter mPresenter;
private final LoginPresenterFactory presenterFactory;
public LoginFragment(LoginPresenterFactory presenterFactory) {
this.presenterFactory = presenterFactory;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = presenterFactory.create();
}
}
I assume that you can not change the production code.
so due to this bad design it is difficult achieve your requirement using a proper way.
But there is a dirty way to do this,
use reflection to assign value to the private field.
public class ReflectionUtility
{
public static void setValue(Object obj, String fieldName, Object value)
{
try
{
final Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
}
then in your test,
private LoginFragment createLoginFragment()
{
LoginFragment loginFragment = LoginFragment.newInstance();
ReflectionUtility.setValue(loginFragment, "mPresenter", mPresenterMock);
return loginFragment;
}

Android Dagger 2 Dependency not being injected

i'm trying to use Dagger 2 into my apps but i'm having some problems regarding the entities repository and i haven't figured it out what i'm missing.
Here is my Application Component:
#Singleton
#Component(
modules = {
AndroidModule.class,
RepositoryModule.class
}
)
public interface ApplicationComponent {
void inject(AndroidApplication app);
IDependenceyRepository dependencyRepository();
}
My modules:
#Module
public class RepositoryModule {
#Provides #Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
return new DependencyRepository(context);
}
}
#Module
public class AndroidModule {
private final AndroidApplication app;
public AndroidModule(AndroidApplication app) {
this.app = app;
}
#Provides #Singleton Context provideApplicationContext() {
return app;
}
}
My AndroidApplication:
public class AndroidApplication extends Application {
private ApplicationComponent component;
#Override
public void onCreate() {
super.onCreate();
setupGraph();
}
private void setupGraph() {
component = DaggerApplicationComponent.builder()
.androidModule(new AndroidModule(this))
.repositoryModule(new RepositoryModule())
.build();
component.inject(this);
}
public ApplicationComponent component() {
return component;
}
}
And here is my activity:
public class MainActivity extends Activity {
#Inject
IDependenceyRepository repository;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.testRepository();
}
private void testRepository() {
Dependency dep = new Dependency();
dep.setDescription("XXX");
dep.setName("AAAA");
try {
repository.save(dep);
Log.d("", repository.queryAll().get(0).getName());
} catch (SQLException e) {
e.printStackTrace();
}
}
What happens is that i'm getting a null pointer exception into the repository. He is not being injected.
If i add this line though:
repository = ((AndroidApplication) getApplication()).component().dependencyRepository();
It works, but the point of the DI it's not to have to worry about this, or am im wrong about that?
I've tried some other example and tutorials but haven't managed to solve my problem.
Thanks in advance,
david.mihola's comment is correct: in order to be able to have #Inject fields initialized, you need to have a method (typically void inject(MyClass)) in your component.
It's worth noting (not specifically for your question, but it could come up) that if you have a base class:
public class BaseActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((AndroidApplication) getApplication()).component().inject(this);
}
}
and in your component:
void inject(BaseActivity);
Injecting into a subclass that has it's own #Inject fields won't work:
public class ActualActivity extends BaseActivity {
#Inject IDependenceyRepository repo;
}
This is because Dagger needs to know the concrete classes that will be injected at compile time, not runtime (like Dagger 1 could do).

Categories