将数据从存储库传递到ViewModel

时间:2019-01-08 11:16:29

标签: android retrofit2 android-livedata

我需要将rate变量从我的存储库传递到viewmodel。 在viewmodel中,我有calculateRate方法,单击按钮后,我获得了货币到改造问题,然后从网络获取改造数据。在rate变量中,我有两种货币的兑换,需要将其传递给viewmodel。怎么做?

存储库

public class CurrencyRepository {

private ApiInterface apiInterface;
private  String apiKey = ApiClient.KEY;
private String TAG = "REPOSITORY ";

public CurrencyRepository(Application application) {
   apiInterface = ApiClient.getClient();
}

public LiveData<Currency> getCurrency(String base, final String target, String apiKey) {

    final MutableLiveData<Currency> data = new MutableLiveData<>();
    apiInterface.getCurrentCurrency(base, target, apiKey).enqueue(new Callback<Currency>() {
        @Override
        public void onResponse(Call<Currency> call, Response<Currency> response) {
            data.setValue(response.body());

            String get = data.getValue().getTarget();
            double rate = 0;
            DecimalFormat df = new DecimalFormat("0.00"); //#
            switch (get) {
                case "EUR":
                    rate = data.getValue().getRates().getEUR();
                    break;
                case "PLN":
                    rate = data.getValue().getRates().getPLN();
                    break;
                case "USD":
                    rate = data.getValue().getRates().getUSD();
                    break;
                case "CHF":
                    rate = data.getValue().getRates().getCHF();
                    break;
            }
            Log.d(TAG, String.valueOf(df.format(rate)));
        }

        @Override
        public void onFailure(Call<Currency> call, Throwable t) {

        }
    });
    return data;
}

}

Viewmodel

public class MainViewModel extends AndroidViewModel {

private CurrencyRepository currencyRepository;
public final ObservableField<String> from = new ObservableField<>();
public final ObservableField<String> to = new ObservableField<>();
public final ObservableFloat value = new ObservableFloat();
public final ObservableField<String> result = new ObservableField<>();


public MainViewModel(Application application) {
   super(application);
   currencyRepository = new CurrencyRepository(application);
}

public void calculateRate() {
    currencyRepository.getCurrency(String.valueOf(from.get()), String.valueOf(to.get()), ApiClient.KEY);
}

}

1 个答案:

答案 0 :(得分:1)

您可以这样做:

rate定义为LiveData字段而不是局部变量:

public class CurrencyRepository {

    private ApiInterface apiInterface;
    private String apiKey = ApiClient.KEY;
    private String TAG = "REPOSITORY ";
    private MutableLiveData<Double> rate = new MutableLiveData(); //or new it in constructor 
    ....

然后在服务调用方法中,您需要执行以下操作:

public MutableLiveData<Currency> getCurrency(String base, final String target, String apiKey) {

final MutableLiveData<Currency> data = new MutableLiveData<>();
apiInterface.getCurrentCurrency(base, target, apiKey).enqueue(new Callback<Currency>() {
    @Override
    public void onResponse(Call<Currency> call, Response<Currency> response) {
        data.setValue(response.body());

        String get = data.getValue().getTarget();

        DecimalFormat df = new DecimalFormat("0.00"); //#
        switch (get) {
            case "EUR":
                rate.postValue(data.getValue().getRates().getEUR());
                break;
            case "PLN":
                rate.postValue(data.getValue().getRates().getPLN());
                break;
            case "USD":
                rate.postValue(data.getValue().getRates().getUSD());
                break;
            case "CHF":
                rate.postValue(data.getValue().getRates().getCHF());
                break;
        }
        Log.d(TAG, String.valueOf(df.format(rate)));
    }

    @Override
    public void onFailure(Call<Currency> call, Throwable t) {

    }
  });
  return data;
}

在存储库中添加getter作为费率:

public LiveData<Double> getRate() {
    return rate;
}

在您的ViewModel中,您需要一种从存储库中获取rate的方法:

public class MainViewModel extends AndroidViewModel {
    ...

    public LiveData<Double> getRate(){
         return currencyRepository.getRate();
    }
}

就完成了!

您现在可以观察getRate()的{​​{1}}方法,并根据观察者的View Model方法的值进行反应。