A basic project implementation of Chasing Coins API with implemented Android Architecture Components.
- Create your ViewModel class composed of a getData() method and a MutableLiveData of your model:
public class CoinsListViewModel extends BaseViewModel {
private MutableLiveData<List<String>> coinsList;
public LiveData<List<String>> getCoinsList() {
if (coinsList == null) {
coinsList = new MutableLiveData<>();
fetchData();
}
return coinsList;
}
public void fetchData() {
disposable.add(apiClient.fetchCoinsList()
.subscribeOn(Schedulers.newThread())
.unsubscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<List<String>>() {
@Override
public void onNext(List<String> value) {
coinsList.postValue(value);
}
@Override
public void onError(Throwable e) {
coinsList.postValue(new ArrayList<String>());
}
@Override
public void onComplete() {
}
}));
}
}
- Implement it in your Activity or Fragment by retrieving the ViewModel’s instance from the ViewModelsProvider and to be used for the current scope (until Activity/Fragment is destroyed completely).
private CoinsListViewModel coinsListViewModel;
- Upon the creation of your Activity/Fragment (onCreate method), initialize the ViewModel, optionally you can assign an Observer for the ViewModel to detect changes and update the views with the changes in the data.
coinsListViewModel = ViewModelProviders.of(this).get(CoinsListViewModel.class);
coinsListViewModel.getCoinsList().observeForever(coinsListObserver);
- The observer used is this:
private Observer<List<String>> coinsListObserver = new Observer<List<String>>() {
@Override
public void onChanged(@Nullable List<String> strings) {
coinsList.addAll(strings);
coinsListAdapter.notifyDataSetChanged();
}
};
Whenever there are changes in the data for the list of coins, the RecyclerView adapter will be updated and the list will also be updated.
The API client uses Retrofit 2 for API calls