android getting NULL error on inject DI into class - android

I can make DI from some class to use in application such as Retrofit, Picasso.
they can work fine when i use them on Activity but when i try to use some DI on other class i get NULL, for exmple this code work fine
public class ActivityRegister extends BaseActivities {
#Inject
GithubService githubService;
#Inject
JobManager jobManager;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
...
repositoryCall = githubService.getAllRepositories();
...
}
private void getRepositories() {
repositoryCall.enqueue(new Callback<List<GithubRepo>>() {
#Override
public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
List<GithubRepo> repoList = new ArrayList<>();
repoList.addAll(response.body());
Log.e("JOB ", "OK");
}
#Override
public void onFailure(Call<List<GithubRepo>> call, Throwable t) {
Log.e("JOB ", "NO!!");
}
});
}
for GithubService i get created instance successfull, not i'm trying to use that into GetLatestRepositories, but i get NULL, how can i define correctly that to injecting into class?
public class GetLatestRepositories extends Job {
#Inject
GithubService githubService;
private Call<List<GithubRepo>> repositoryCall;
public GetLatestRepositories() {
super(new Params(Priority.MID).requireNetwork().persist());
repositoryCall = githubService.getAllRepositories();
}
#Override
public void onAdded() {
}
#Override
public void onRun() throws Throwable {
repositoryCall.enqueue(new Callback<List<GithubRepo>>() {
#Override
public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
List<GithubRepo> repoList = new ArrayList<>();
repoList.addAll(response.body());
Log.e("JOB ", "OK");
}
#Override
public void onFailure(Call<List<GithubRepo>> call, Throwable t) {
Log.e("JOB ", "NO!!");
}
});
}
#Override
protected void onCancel(int cancelReason, #Nullable Throwable throwable) {
}
#Override
protected RetryConstraint shouldReRunOnThrowable(#NonNull Throwable throwable, int runCount, int maxRunCount) {
return null;
}
}
ActivityRegisterComponent component class:
#ActivityRegisterScope
#Component(modules = ActivityRegisterModule.class, dependencies = GithubApplicationComponent.class)
public interface ActivityRegisterComponent {
void injectActivityRegister(ActivityRegister homeActivity);
}
GithubApplicationComponent:
#GithubApplicationScope
#Component(modules = {GithubServiceModule.class, PicassoModule.class, JobManagerModule.class, ActivityModule.class})
public interface GithubApplicationComponent {
Picasso getPicasso();
GithubService getGithubService();
JobManager getJobManager();
}
Application class:
public class Alachiq extends Application {
...
public static Alachiq alachiq;
public static String packageName;
public static Resources resources;
private static Context context;
private GithubService githubService;
private Picasso picasso;
private GithubApplicationComponent component;
private JobManager jobManager;
private static Alachiq instance;
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
}
#Override
public void onCreate() {
super.onCreate();
//#formatter:off
resources = this.getResources();
context = getApplicationContext();
packageName = getPackageName();
//#formatter:on
Timber.plant(new Timber.DebugTree());
component = DaggerGithubApplicationComponent.builder()
.contextModule(new ContextModule(this))
.build();
githubService = component.getGithubService();
picasso = component.getPicasso();
jobManager = component.getJobManager();
}
public GithubApplicationComponent component() {
return component;
}
public static Alachiq get(Activity activity) {
return (Alachiq) activity.getApplication();
}
public static Alachiq getInstance() {
return instance;
}
public static Context getContext() {
return context;
}
}
and ActivityRegister onCreate:
ApplicationComponent component = DaggerApplicationComponent.builder()
.githubApplicationComponent(Alachiq.get(this).component())
.build();
GithubService class:
public interface GithubService {
#GET("users/{username}/repos")
Call<List<GithubRepo>> getReposForUser(#Path("username") String username);
#GET("repositories")
Call<List<GithubRepo>> getAllRepositories();
#GET("users/{username}")
Call<GithubUser> getUser(#Path("username") String username);
}

Before you can even use the object from GithubService class you need to do the following:
myComponent= DaggerMyComponent.builder().computerModule(new ComputerModule()).build();//replace accordingly.
In your case you will have to do something like:
githubService = DaggerGithubApplicationComponent.builder().nameofYourModule(new NameOfTheClass()).build);
Now you can use the githubService object:
repositoryCall = githubService.getAllRepositories();
This is a type of design pattern called creational.

Related

How to create UseCase for MVVM Clean architecture in android

RepositoryClass
public Observable<Weather> requestWeather (String location,String unit,String appId){
return weatherAPI.requestWeather(location, unit,appId);
}
GetUseCase
public interface GetUseCase {
void execute(String location,String unit,String appId);
}
GetUseCaseImpl
public class GetUseCaseImpl implements GetUseCase {
private WeatherRepositorty weatherRepositorty;
private CompositeDisposable disposable = new CompositeDisposable();
private MutableLiveData<WeatherViewStated> mutableLiveData = new MutableLiveData<>();
public GetUseCaseImpl() {
weatherRepositorty = WeatherRepositorty.getInstance();
}
#Override
public void execute(String location, String unit, String appId) {
disposable.add(weatherRepositorty.requestWeather(location, unit, appId).subscribeOn(Schedulers.io()).doOnSubscribe((newsList) -> onLoading())
.subscribe(this::onSuccess,
this::onError));
}
private void onSuccess(Weather weather) {
WeatherViewStated.SUCCESS_STATE.setData(weather);
mutableLiveData.postValue(WeatherViewStated.SUCCESS_STATE);
}
private void onError(Throwable error) {
WeatherViewStated.ERROR_STATE.setError(error);
mutableLiveData.postValue(WeatherViewStated.ERROR_STATE);
}
private void onLoading() {
mutableLiveData.postValue(WeatherViewStated.LOADING_STATE);
}
}
WeatherViewStated
public class WeatherViewStated extends WeatherViewState<Weather> {
private WeatherViewStated(Weather data, int currentState, Throwable error) {
this.data = data;
this.error = error;
this.currentState = currentState;
}
public static WeatherViewStated ERROR_STATE = new WeatherViewStated(null, WeatherViewState.State.FAILED.value, new Throwable());
public static WeatherViewStated LOADING_STATE = new WeatherViewStated(null, State.LOADING.value, null);
public static WeatherViewStated SUCCESS_STATE = new WeatherViewStated(new Weather(), State.SUCCESS.value, null);
}
WeatherViewModel
public class WeatherViewModel extends ViewModel {
private MutableLiveData<Weather> mutableWeatherLiveData;
private WeatherRepositorty weatherRepositorty;
public void init(String location,String unit,String appID) {
}
}
This is my code i am trying to write code using mvvm clean architecture but i am unable to add code in GetUseCase and GetUseCaseImpl so that i can get MutableLiveDta success and failure state in viewmodel class so that i can use it in MainActvitiy please suggest me how to call and how to get data in View Model .
Change UseCase
public interface GetUseCase {
void execute(String location,String unit,String appId);
}
To
public interface GetUseCase {
Observable<Weather> execute(String location,String unit,String appId);
}
GetUseCaseImpl To
public class GetUseCaseImpl implements GetUseCase {
private WeatherRepositorty weatherRepositorty;
public GetUseCaseImpl() {
weatherRepositorty = WeatherRepositorty.getInstance();
}
#Override
public Observable<Weather> execute(String location, String unit, String appId) {
return weatherRepositorty.requestWeather(location, unit, appId)
}
}
ViewModel
public class WeatherViewModel extends ViewModel {
private CompositeDisposable disposable = new CompositeDisposable();
private MutableLiveData<WeatherViewStated> mutableLiveData = new
MutableLiveData<>();
private GetUseCaseImpl useCaseImpl = new GetUseCaseImpl()
public void init(String location,String unit,String appID) {
disposable.add(useCaseImpl.execute(location, unit, appId).subscribeOn(Schedulers.io()).doOnSubscribe((newsList) -> onLoading())
.subscribe(this::onSuccess,
this::onError));
}
private void onSuccess(Weather weather) {
WeatherViewStated.SUCCESS_STATE.setData(weather);
mutableLiveData.postValue(WeatherViewStated.SUCCESS_STATE);
}
private void onError(Throwable error) {
WeatherViewStated.ERROR_STATE.setError(error);
mutableLiveData.postValue(WeatherViewStated.ERROR_STATE);
}
private void onLoading() {
mutableLiveData.postValue(WeatherViewStated.LOADING_STATE);
}
override fun onCleared() {
disposable.clear()
}
}

java.lang.RuntimeException: Cannot create an instance of ViewModel class

I am trying to instantiate UserViewModel in my activity however it keeps giving me a java.lang.RuntimeException: Cannot create an instance of viewmodel class kindly assist.
This is how my ViewModel looks like
public class UserViewModel extends AndroidViewModel {
private NodeAuthService api;
private SharedPreferences pref;
private static MutableLiveData<List<User>> userDetails = new MutableLiveData<>();
public UserViewModel(#NonNull Application application) {
super(application);
api = AuthRetrofitClient.getInstance().create(NodeAuthService.class);
}
private String email = pref.getString("email", "");
public void loadUser(){
Call<List<User>> call;
call = api.getUser(email);
call.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
userDetails.postValue(response.body());
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
Log.d("USER",t.getMessage());
}
});
}
public MutableLiveData<List<User>>getUserDetails(){
return userDetails;
}
}
This is how my activity is setup
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
String nameVm;
userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
userViewModel.loadUser();
userViewModel.getUserDetails().observe(this, new Observer<List<User>>() {
#Override
public void onChanged(List<User> users) {
if (users != null){
for (int i = 0; i<users.size(); i++){
nameVm = String.valueOf(users.get(0));
}
}
}
});
}
Create ViewModelFactory class
public class MyViewModelFactory implements ViewModelProvider.Factory {
private Application mApplication;
public MyViewModelFactory(Application application) {
mApplication = application;
}
#Override
public <T extends ViewModel> T create(Class<T> modelClass) {
// Replace UserViewModel → with whatever or however you create your ViewModel
return (T) new UserViewModel(mApplication);
}
}
and init ViewModel like
UserViewModel myViewModel = ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication())).get(UserViewModel.class);

How to relay retrofit response from data repository to view model using mutable live data in android?

I am able to make a network request and get back a response inside my data repository but not able to get that inside my view model.
Data repository:
public class DataRepository {
private APIService apiService;
private static DataRepository INSTANCE = null;
public MutableLiveData<ResponseEntity> loginUser(UserEntity userEntity){
final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();
apiService.loginUser(userEntity)
.enqueue(new Callback<ResponseEntity>() {
#Override
public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
Log.d(Constants.LOGGER, "from data repository " + response.body());
responseEntity.setValue(response.body());
}
#Override
public void onFailure(Call<ResponseEntity> call, Throwable t) {
Log.d(Constants.LOGGER, "from data repository: there was an error");
responseEntity.setValue(null);
}
});
return responseEntity;
}
}
View model:
public class LoginViewModel extends AndroidViewModel {
private MutableLiveData<ResponseEntity> networkResponse;
public void sendLoginNetworkRequest(UserEntity userEntity){
networkResponse = mRepository.loginUser(userEntity);
}
public MutableLiveData<ResponseEntity> getResponse(){
return networkResponse;
}
Activity:
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
loginViewModel.getResponse()
.observe(this, new Observer<ResponseEntity>() {
#Override
public void onChanged(#Nullable ResponseEntity responseEntity) {
Log.d(Constants.LOGGER, "response entity changed " + responseEntity);
}
});
}
public void loginClicked(View view) {
loginViewModel.sendLoginNetworkRequest(userEntity);
}
}
The log from the data repository shows up but the one from the activity doesn't. What am I doing wrong?
I found the answer!
I had to make the responseEntity MutableLiveData variable in my DataRepository class into a class variable and create a function which returns that and now it works!
Repository:
public class DataRepository {
private APIService apiService;
private static DataRepository INSTANCE = null;
final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();
public void loginUser(UserEntity userEntity){
apiService.loginUser(userEntity)
.enqueue(new Callback<ResponseEntity>() {
#Override
public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
Log.d(Constants.LOGGER, "from data repository " + response.body());
responseEntity.setValue(response.body());
}
#Override
public void onFailure(Call<ResponseEntity> call, Throwable t) {
Log.d(Constants.LOGGER, "from data repository: there was an error");
responseEntity.setValue(null);
}
});
}
public MutableLiveData<ResponseEntity> getLiveResponses(){
return responseEntity;
}
}
Viewmodel:
public class LoginViewModel extends AndroidViewModel {
public void sendLoginNetworkRequest(UserEntity userEntity){
mRepository.loginUser(userEntity);
}
public MutableLiveData<ResponseEntity> getResponse(){
return mRepository.getLiveResponse;
}
}

Android - How to use viewmodel for handling http call in OnCreate which doesn't get called on screen rotation?

. Get stuck with a basic scenario of loading the data in oncreate of an activity. So I am trying to load the data as soon as i open my activity but when i change the screen orientation it gets called again.
below is my rest client for retrofit
public class MyRestApiClient {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).connectTimeout(30,TimeUnit.SECONDS).readTimeout(30,TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:8080/rest/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
return retrofit;
}
}
below is my resturl interface for loading the data
public interface MyRestUrlInterface {
#GET("user/{user_id}")
Call<Object> getData(#Path("user_id") String user_id);
}
below is my viewmodel class:
public class MyViewModelObserver extends ViewModel {
private MutableLiveData<Object> httpCallBackObserver;
public MutableLiveData<Object> getHttpCallBackObserver() {
if (httpCallBackObserver == null) {
httpCallBackObserver = new MutableLiveData<Object>();
}
return httpCallBackObserver;
}
}
below is my Activity code :
public class MyActivity extends AppCompatActivity {
private static final String TAG = "MyActivity" ;
MyRestUrlInterface restUrlInterface;
public MyViewModelObserver myViewModelObserver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
restUrlInterface = MyRestApiClient.getClient().create(MyRestUrlInterface.class);
myViewModelObserver = ViewModelProviders.of(this).get(MyViewModelObserver.class);
myViewModelObserver.getHttpCallBackObserver().observe(this, getData());
//load data via http
Call<Object> call = restUrlInterface.getData("123");
call.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
myViewModelObserver.getHttpCallBackObserver().setValue(response.body());
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
}
private Observer<Object> getData(){
return new Observer<Object>() {
#Override
public void onChanged(#Nullable final Object responseString) {
Log.d(TAG,"***** Loaded Data --- "+responseString);
}
};
}
}
How to use view model so that it wont make http call again in screen orientation change
suggested answer:
public class MyViewModelObserver extends ViewModel {
private MutableLiveData<Object> httpCallBackObserver;
public MutableLiveData<Object> getHttpCallBackObserver() {
if (httpCallBackObserver == null) {
httpCallBackObserver = new MutableLiveData<Object>();
loadData();
}
return httpCallBackObserver;
}
private void loadData(){
Call<Object> call = restUrlInterface.getData("123");
call.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
myViewModelObserver.getHttpCallBackObserver().setValue(response.body());
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
}
}

Dependency Injection with Dagger2, how to get context

I'm learning dependency injection with Dagger2.
I created a HttpRequester class that has a get method and returns some data from a server.
This is my code:
MainActivity
public class MainActivity extends AppCompatActivity {
#Inject
HttpRequester httpRequester;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getHttpRequester();
httpRequester.get("http://url.to.get.the.data", new HttpRequester.Listener() {
#Override
public void onDataRetrieved(String result) {
Log.d("App", "Result: " + result);
}
});
}
#Override
protected void onStop () {
super.onStop();
if (httpRequester != null) {
httpRequester.cancelAll();
}
}
void getHttpRequester() {
HttpRequesterComponent httpRequesterComponent = DaggerHttpRequesterComponent.builder().httpRequesterModule(new HttpRequesterModule()).build();
httpRequester = httpRequesterComponent.provideHttpRequester();
}
}
HttpRequesterModule
#Module
public class HttpRequesterModule {
#Provides #Singleton
HttpRequester provideHttpRequester(Context context){
return new HttpRequester(context);
}
}
HttpRequesterComponent
#Singleton
#Component(modules = {HttpRequesterModule.class})
public interface HttpRequesterComponent {
HttpRequester provideHttpRequester();
}
HttpRequester
public class HttpRequester {
Context context;
public HttpRequester(Context context) {
this.context = context;
}
public interface Listener {
void onDataRetrieved(String result);
}
private RequestQueue queue = Volley.newRequestQueue(context);
public static final String TAG = "TAG";
public void get(String url, final Listener listener){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String res) {
listener.onDataRetrieved(res);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
stringRequest.setTag(TAG);
queue.add(stringRequest);
}
public void cancelAll() {
queue.cancelAll(TAG);
}
}
The problem is that when I call httpRequester.get() I get a NullPointerException since the context is null. I want to know how to inject the context. Thanks for your help!
You have to notice that you never initialize the context attribute con HttpRequester. Receive a Context in your constructor so it has a value:
public class HttpRequester {
Context context;
public HttpRequester(Context context) {
this.context = context
}
...
}
After that, you just have to provide it in your HttpRequesterModule:
#Module
public class HttpRequesterModule {
private final Context context;
public HttpRequesterModule(Context context) {
this.context = context;
}
#Provides #Singleton
HttpRequester provideHttpRequester(Context context){
return new HttpRequester(context);
}
#Provides
Context provideContext() {
return context;
}
}
Now you just have to pass a Context to your module

Categories

Resources