Skip to content

Instantly share code, notes, and snippets.

@yemyatthu1990
Last active February 9, 2017 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemyatthu1990/cecacbe442c773137c3aab4abfb32801 to your computer and use it in GitHub Desktop.
Save yemyatthu1990/cecacbe442c773137c3aab4abfb32801 to your computer and use it in GitHub Desktop.
public class TownshipListPresenter implements Presenter{
private TownshipListView townshipListView;
private final GetTownshipList getTownshipList;
private final TownshipModelMapper townshipModelMapper;
public TownshipListPresenter(GetTownshipList getTownshipList,TownshipModelMapper townshipModelMapper){
this.getTownshipList = getTownshipList;
this.townshipModelMapper = townshipModelMapper;
}
public void setTownshipListView(TownshipListView townshipListView){
this.townshipListView = townshipListView;
}
@Override
public void resume() {
}
@Override
public void pause() {
}
@Override
public void destroy() {
this.getTownshipList.dispose();
this.townshipListView = null;
}
public void initialize(){
this.loadTownshipList();
}
private void loadTownshipList() {
this.showViewLoading();
this.getTownshipList();
}
private void showViewLoading() {
this.townshipListView.showLoading();
}
private void hideViewLoading() {
this.townshipListView.hideLoading();
}
private void showErrorMessage(String errorMessage) {
this.townshipListView.showError(errorMessage);
}
private void showTownshipListInView(List<Township> townships) {
final List<TownshipModel> townshipModelList =
this.townshipModelMapper.transformList(townships);
this.townshipListView.renderTownshipList(townshipModelList);
}
private void getTownshipList() {
this.getTownshipList.execute(new TownshipListObserver(), null);
}
private final class TownshipListObserver extends DefaultObserver<List<Township>> {
@Override public void onComplete() {
TownshipListPresenter.this.hideViewLoading();
}
@Override public void onError(Throwable e) {
e.printStackTrace();
TownshipListPresenter.this.hideViewLoading();
TownshipListPresenter.this.showErrorMessage(e.getLocalizedMessage());
}
@Override public void onNext(List<Township> townships) {
TownshipListPresenter.this.showTownshipListInView(townships);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment