I have a Retrofit Service like this
public interface BrandsService {
@GET("listBrand")
Call<List<Brand>> getBrands();
}
Then I have a Repository to get data from an api like this
public class BrandsRepository {
public static final String TAG = "BrandsRepository";
MutableLiveData<List<Brand>> mutableLiveData;
Retrofit retrofit;
@Inject
public BrandsRepository(Retrofit retrofit) {
this.retrofit = retrofit;
}
public LiveData<List<Brand>> getListOfBrands() {
// Retrofit retrofit = ApiManager.getAdapter();
final BrandsService brandsService = retrofit.create(BrandsService.class);
Log.d(TAG, "getListOfBrands: 00000000000 "+retrofit);
mutableLiveData = new MutableLiveData<>();
Call<List<Brand>> retrofitCall = brandsService.getBrands();
retrofitCall.enqueue(new Callback<List<Brand>>() {
@Override
public void onResponse(Call<List<Brand>> call, Response<List<Brand>> response) {
mutableLiveData.setValue(response.body());
}
@Override
public void onFailure(Call<List<Brand>> call, Throwable t) {
t.printStackTrace();
}
});
return mutableLiveData;
}
}
I am using constructor injection of Dagger2 by injecting Retrofit like that. Then I have a ViewModel like this
public class BrandsViewModel extends ViewModel{
BrandsRepository brandsRepository;
LiveData<List<Brand>> brandsLiveData;
@Inject
public BrandsViewModel(BrandsRepository brandsRepository) {
this.brandsRepository = brandsRepository;
}
public void callService(){
brandsLiveData = brandsRepository.getListOfBrands();
}
public LiveData<List<Brand>> getBrandsLiveData() {
return brandsLiveData;
}
}
To inject Retrofit in BrandsRepository, I have to inject BrandsRepository like that. Then I have MainActivity like this
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Inject
BrandsViewModel brandsViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MainApplication)getApplication()).getNetComponent().inject(this);
// BrandsViewModel brandsViewModel = ViewModelProviders.of(this).get(BrandsViewModel.class);
brandsViewModel.callService();
LiveData<List<Brand>> brandsLiveData = brandsViewModel.getBrandsLiveData();
brandsLiveData.observe(this, new Observer<List<Brand>>() {
@Override
public void onChanged(@Nullable List<Brand> brands) {
Log.d(TAG, "onCreate: "+brands.get(0).getName());
}
});
}
}
The BrandsViewModel is injected using Dagger2 rather than ViewModelProviders. This is working fine but when I try to use ViewModelProviders by uncommenting it, dagger gives me the error which is obvious. The proper way of getting a ViewModel is by using ViewModelProviders but how will I accomplish this while injecting retrofit like that.
The answer is based on android-architecture-components.
You can use Map multibinding in Dagger.
First, to declare map key like this.
Second, to create map.
Next, to create the factory file to handle the map which uses key to choose ViewModel.
Finally, to inject factory in your activity or fragment.
By this way, you can inject repository in your ViewModel.
The answer can be simpler than Mumi's approach, which is that you expose the ViewModel on your component:
And now you can access this method on the component inside the ViewModelFactory:
All this can be simplified and hidden with Kotlin:
which now lets you do
Where now
BrandsViewModel
can receive its parameters from Dagger: