MVVM,在存储库中获取上下文,传递值

时间:2019-06-10 12:38:57

标签: android mvvm

我正在学习MVVM,并尝试在存储库中获取上下文,并从MainActivity 传递值到存储库类。

我知道您可以通过Application在ViewModel中获取上下文:

public MyViewModel(Application application) {
        super(application);

        ...
    }

myApi = RetrofitService.getClient(getApplication()).create(ApiInterface.class);

但是它对存储库类如何工作?如何将String值(utc)从MainActivity传递给存储库类?

我的存储库类有以下示例代码:

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;

0 个答案:

没有答案