Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- private MutableLiveData<Currency> currencyLiveData;
- String TAG = "MAIN";
- 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);
- }
- public class CurrencyRepository {
- private ApiInterface apiInterface;
- private String apiKey = ApiClient.KEY;
- public CurrencyRepository(Application application) {
- apiInterface = ApiClient.getClient();
- }
- public LiveData<Currency> getCurrency(String base, 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());
- }
- @Override
- public void onFailure(Call<Currency> call, Throwable t) {
- }
- });
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement