Last active
December 20, 2016 07:34
-
-
Save ianomad/12aca2f970f40a6b8b8895eb4aac483c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SearchNewsFeedPresenter extends NewsFeedPresenter { | |
private SearchNewsFragment view; | |
private Subscription pendingSubscription; | |
private final static int DEFAULT_PAGE_SIZE = 20; | |
public SearchNewsFeedPresenter(Bundle savedState, SearchNewsFragment view) { | |
ReporterApplication application = (ReporterApplication) view.getActivity().getApplication(); | |
application.getAppComponent().inject(this); | |
Icepick.restoreInstanceState(this, savedState); | |
Timber.tag(SearchNewsFeedPresenter.class.getSimpleName()); | |
this.view = view; | |
if (null == newsList || newsList.isEmpty()) { | |
fetchNews(true); | |
} else { | |
view.showNews(newsList, false); | |
} | |
} | |
@Override | |
public void fetchNews(boolean firstLoad) { | |
if (view.getSearchKey().isEmpty()) { | |
return; | |
} | |
if (firstLoad) { | |
newsList = new ArrayList<>(); | |
view.showLoading(); | |
} | |
pendingSubscription = reporterApiClient.getNewsBySearchKey(page, view.getSearchKey()) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.delaySubscription(1, TimeUnit.SECONDS) | |
.subscribe(response -> { | |
view.hideLoading(); | |
if (!response.isSuccessful()) { | |
Timber.e(response.message()); | |
view.showErrorPanel(); | |
return; | |
} | |
boolean shouldAnimate = newsList.isEmpty(); | |
newsList.addAll(response.body()); | |
view.showNews(newsList, shouldAnimate); | |
Timber.i("Total news loaded: " + newsList.size()); | |
}, throwable -> { | |
Timber.e(throwable); | |
view.showErrorPanel(); | |
}); | |
} | |
@Override | |
public void destroy() { | |
Timber.i("Destroying pending subscription"); | |
if (null != pendingSubscription) { | |
pendingSubscription.unsubscribe(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment