I am learning MVVM and trying to get context in my repository and pass a value from the MainActivity to the repository class.
I know you can get context in the ViewModel through Application:
public MyViewModel(Application application) {
super(application);
...
}
myApi = RetrofitService.getClient(getApplication()).create(ApiInterface.class);
But how does it work for a repository class and how can I pass a String value (utc) from MainActivity to the repository class?
I have following example code for my repository class:
private String utc:
public RetroDataRepository() {
public MutableLiveData<TimezoneModel> getTimeZones() {
final MutableLiveData<TimezoneModel> timezones = new MutableLiveData<>();
apiInterface = RetrofitService.getClient(context????).create(ApiInterface.class);
Call<ResponseBody> call = apiInterface.getTimeZone(utc);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String time;
String utc;
String region;
String currentTime;
String timezoneTitle;
if(response.body()!=null) {
// store TimeZone data to a list
Document document = Jsoup.parse(response.body().toString());
....
}
isLoading.setValue(false);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "onFailure: ", t);
isLoading.setValue(false);
error.setValue(true);
}
});
return timezones;