Advertisement
Guest User

repo

a guest
Dec 31st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. public class MainViewModel extends AndroidViewModel {
  2.  
  3. private CurrencyRepository currencyRepository;
  4. public final ObservableField<String> from = new ObservableField<>();
  5. public final ObservableField<String> to = new ObservableField<>();
  6. public final ObservableFloat value = new ObservableFloat();
  7. private MutableLiveData<Currency> currencyLiveData;
  8. String TAG = "MAIN";
  9.  
  10. public MainViewModel(Application application) {
  11.    super(application);
  12.    currencyRepository = new CurrencyRepository(application);
  13. }
  14.  
  15. public void calculateRate() {
  16.     currencyRepository.getCurrency(String.valueOf(from.get()), String.valueOf(to.get()), ApiClient.KEY);
  17. }
  18.  
  19.  
  20. public class CurrencyRepository {
  21.  
  22. private ApiInterface apiInterface;
  23. private  String apiKey = ApiClient.KEY;
  24.  
  25. public CurrencyRepository(Application application) {
  26.    apiInterface = ApiClient.getClient();
  27. }
  28.  
  29. public LiveData<Currency> getCurrency(String base, String target, String apiKey) {
  30.  
  31.     final MutableLiveData<Currency> data = new MutableLiveData<>();
  32.     apiInterface.getCurrentCurrency(base, target, apiKey).enqueue(new Callback<Currency>() {
  33.         @Override
  34.         public void onResponse(Call<Currency> call, Response<Currency> response) {
  35.             data.setValue(response.body());
  36.  
  37.         }
  38.  
  39.         @Override
  40.         public void onFailure(Call<Currency> call, Throwable t) {
  41.  
  42.         }
  43.     });
  44.     return data;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement