user9897182
user9897182

Reputation: 265

Pass data from Repository to ViewModel

I need to pass the rate variable from my repository to the viewmodel. In viewmodel I have calculateRate method, there after button click I get the currencies to the retrofit question and then retrofit get data from the web. In rate variable I have exchange of two currencies and I need to pass it to viewmodel. How to do it?

Repository

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);
}

}

Upvotes: 3

Views: 3819

Answers (2)

Mostafa Osivand
Mostafa Osivand

Reputation: 1

using MutableLiveData is not a good idea for passing data from repository to viewmodel you can use callback

Upvotes: 0

Akram
Akram

Reputation: 2188

You can do it this way:

Define rate as a LiveData field instead of local variable:

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 
    ....

Then in your service call method you need to do this:

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;
}

Add a getter for rate in your repository:

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

And in your ViewModel you need a method to get rate from repository:

public class MainViewModel extends ViewModel {
    ...

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

And it's done!

You can now observe the getRate() method of your View Model and react based on the value of it in your Observer's onChange() method.

Upvotes: 3

Related Questions