Remove redundant code

This commit is contained in:
inorichi 2015-10-14 11:54:22 +02:00
parent 8dc7c550ad
commit 991e4d40d0

View File

@ -96,7 +96,7 @@ public class CatalogueListPresenter extends BasePresenter {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.flatMap(Observable::from) .flatMap(Observable::from)
.filter(manga -> !manga.initialized) .filter(manga -> !manga.initialized)
.buffer(5) .buffer(3)
.concatMap(localMangas -> { .concatMap(localMangas -> {
List<Observable<Manga>> mangaObservables = new ArrayList<>(); List<Observable<Manga>> mangaObservables = new ArrayList<>();
for (Manga manga : localMangas) { for (Manga manga : localMangas) {
@ -131,16 +131,8 @@ public class CatalogueListPresenter extends BasePresenter {
public void getMangasFromSource(int page) { public void getMangasFromSource(int page) {
subscriptions.remove(mMangaFetchSubscription); subscriptions.remove(mMangaFetchSubscription);
mMangaFetchSubscription = selectedSource.pullPopularMangasFromNetwork(page) mMangaFetchSubscription = getMangasSubscriber(
.subscribeOn(Schedulers.io()) selectedSource.pullPopularMangasFromNetwork(page));
.observeOn(AndroidSchedulers.mainThread())
.flatMap(Observable::from)
.map(this::networkToLocalManga)
.toList()
.subscribe(newMangas -> {
adapter.addItems(newMangas);
mMangaDetailPublishSubject.onNext(Observable.just(newMangas));
});
subscriptions.add(mMangaFetchSubscription); subscriptions.add(mMangaFetchSubscription);
} }
@ -148,7 +140,14 @@ public class CatalogueListPresenter extends BasePresenter {
public void getMangasFromSearch(int page) { public void getMangasFromSearch(int page) {
subscriptions.remove(mMangaSearchSubscription); subscriptions.remove(mMangaSearchSubscription);
mMangaSearchSubscription = selectedSource.searchMangasFromNetwork(mSearchName, page) mMangaSearchSubscription = getMangasSubscriber(
selectedSource.searchMangasFromNetwork(mSearchName, page));
subscriptions.add(mMangaSearchSubscription);
}
private Subscription getMangasSubscriber(Observable<List<Manga>> mangas) {
return mangas
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.flatMap(Observable::from) .flatMap(Observable::from)
@ -156,10 +155,9 @@ public class CatalogueListPresenter extends BasePresenter {
.toList() .toList()
.subscribe(newMangas -> { .subscribe(newMangas -> {
adapter.addItems(newMangas); adapter.addItems(newMangas);
if (mMangaDetailPublishSubject != null)
mMangaDetailPublishSubject.onNext(Observable.just(newMangas)); mMangaDetailPublishSubject.onNext(Observable.just(newMangas));
}); });
subscriptions.add(mMangaSearchSubscription);
} }
private Manga networkToLocalManga(Manga networkManga) { private Manga networkToLocalManga(Manga networkManga) {
@ -206,8 +204,7 @@ public class CatalogueListPresenter extends BasePresenter {
} }
private int getMangaIndex(Manga manga) { private int getMangaIndex(Manga manga) {
int i; for (int i = 0; i < adapter.getCount(); i++) {
for (i = 0; i < adapter.getCount(); i++) {
if (manga.id == adapter.getItem(i).id) { if (manga.id == adapter.getItem(i).id) {
return i; return i;
} }