From ad97d03f1d87b760a504b49a377876e3a8ee2048 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 20 Jan 2016 19:21:17 +0100 Subject: [PATCH] Change toolbar color (Fix #43). Allow to also remove from library (Fix #44). Rewrite RxPager. --- .../ui/catalogue/CatalogueFragment.java | 10 ++-- .../ui/catalogue/CataloguePresenter.java | 47 +++++++++++-------- .../eu/kanade/tachiyomi/util/RxPager.java | 32 +++++-------- app/src/main/res/values/colors.xml | 4 +- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java index 7059d63bb..8b2460023 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.java @@ -109,6 +109,7 @@ public class CatalogueFragment extends BaseRxFragment } else { selectedIndex = position; showProgressBar(); + recycler.setAdapter(adapter); getPresenter().startRequesting(source); } } @@ -211,7 +212,7 @@ public class CatalogueFragment extends BaseRxFragment public void onAddPage(int page, List mangas) { hideProgressBar(); - if (page == 1) { + if (page == 0) { adapter.clear(); scrollListener.resetScroll(); } @@ -260,13 +261,16 @@ public class CatalogueFragment extends BaseRxFragment @Override public void onListItemLongClick(int position) { final Manga selectedManga = adapter.getItem(position); + final Manga dbManga = getPresenter().getDbManga(selectedManga.id); + + int textRes = dbManga.favorite ? R.string.remove_from_library : R.string.add_to_library; new MaterialDialog.Builder(getActivity()) - .items(getString(R.string.add_to_library)) + .items(getString(textRes)) .itemsCallback((dialog, itemView, which, text) -> { switch (which) { case 0: - getPresenter().addMangaToLibrary(selectedManga); + getPresenter().changeMangaFavorite(selectedManga); break; } }) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java index 0db1795f3..bc30ba760 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CataloguePresenter.java @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.catalogue; import android.os.Bundle; import android.text.TextUtils; -import android.util.Pair; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; @@ -38,14 +37,14 @@ public class CataloguePresenter extends BasePresenter { private String query; - private int currentPage; - private RxPager pager; + private RxPager pager; private MangasPage lastMangasPage; private PublishSubject> mangaDetailSubject; private static final int GET_MANGA_LIST = 1; private static final int GET_MANGA_DETAIL = 2; + private static final int GET_MANGA_PAGE = 3; @Override protected void onCreate(Bundle savedState) { @@ -57,13 +56,16 @@ public class CataloguePresenter extends BasePresenter { mangaDetailSubject = PublishSubject.create(); + pager = new RxPager<>(); + restartableReplay(GET_MANGA_LIST, - () -> pager.pages().concatMap(page -> getMangasPageObservable(page + 1)), - (view, pair) -> view.onAddPage(pair.first, pair.second), - (view, error) -> { - view.onAddPageError(); - Timber.e(error.getMessage()); - }); + pager::results, + (view, pair) -> view.onAddPage(pair.first, pair.second)); + + restartableFirst(GET_MANGA_PAGE, + () -> pager.request(page -> getMangasPageObservable(page + 1)), + (view, next) -> {}, + (view, error) -> view.onAddPageError()); restartableLatestCache(GET_MANGA_DETAIL, () -> mangaDetailSubject @@ -82,6 +84,7 @@ public class CataloguePresenter extends BasePresenter { source = sourceManager.get(sourceId); stop(GET_MANGA_LIST); stop(GET_MANGA_DETAIL); + stop(GET_MANGA_PAGE); } public void startRequesting(Source source) { @@ -92,20 +95,21 @@ public class CataloguePresenter extends BasePresenter { public void restartRequest(String query) { this.query = query; - stop(GET_MANGA_LIST); - currentPage = 1; - pager = new RxPager(); + stop(GET_MANGA_PAGE); + lastMangasPage = null; start(GET_MANGA_DETAIL); start(GET_MANGA_LIST); + start(GET_MANGA_PAGE); } public void requestNext() { - if (hasNextPage()) - pager.requestNext(++currentPage); + if (hasNextPage()) { + start(GET_MANGA_PAGE); + } } - private Observable>> getMangasPageObservable(int page) { + private Observable> getMangasPageObservable(int page) { MangasPage nextMangasPage = new MangasPage(page); if (page != 1) { nextMangasPage.url = lastMangasPage.nextPageUrl; @@ -120,10 +124,9 @@ public class CataloguePresenter extends BasePresenter { .flatMap(mangasPage -> Observable.from(mangasPage.mangas)) .map(this::networkToLocalManga) .toList() - .map(mangas -> Pair.create(page, mangas)) - .doOnNext(pair -> { + .doOnNext(mangas -> { if (mangaDetailSubject != null) - mangaDetailSubject.onNext(pair.second); + mangaDetailSubject.onNext(mangas); }) .observeOn(AndroidSchedulers.mainThread()); } @@ -149,6 +152,10 @@ public class CataloguePresenter extends BasePresenter { .onErrorResumeNext(error -> Observable.just(manga)); } + public Manga getDbManga(long id) { + return db.getManga(id).executeAsBlocking(); + } + public Source getSource() { return source; } @@ -170,8 +177,8 @@ public class CataloguePresenter extends BasePresenter { return sourceManager.getSources(); } - public void addMangaToLibrary(Manga manga) { - manga.favorite = true; + public void changeMangaFavorite(Manga manga) { + manga.favorite = !manga.favorite; db.insertManga(manga).executeAsBlocking(); } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/RxPager.java b/app/src/main/java/eu/kanade/tachiyomi/util/RxPager.java index 2fe26e3e9..a553b0657 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/RxPager.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/RxPager.java @@ -1,32 +1,26 @@ package eu.kanade.tachiyomi.util; +import android.util.Pair; + +import java.util.List; + import rx.Observable; +import rx.functions.Func1; import rx.subjects.PublishSubject; -public class RxPager { +public class RxPager { - private final int initialPageCount; - private final PublishSubject requests = PublishSubject.create(); + private final PublishSubject> results = PublishSubject.create(); private int requestedCount; - public RxPager() { - this(1); + public Observable>> results() { + requestedCount = 0; + return results.map(list -> Pair.create(requestedCount++, list)); } - public RxPager(int initialPageCount) { - this.initialPageCount = initialPageCount; + public Observable> request(Func1>> networkObservable) { + return networkObservable.call(requestedCount).doOnNext(results::onNext); } - public void requestNext(int page) { - requests.onNext(page); - } +} - public Observable pages() { - return requests - .concatMap(targetPage -> targetPage <= requestedCount ? - Observable.empty() : - Observable.range(requestedCount, targetPage - requestedCount)) - .startWith(Observable.range(0, initialPageCount)) - .doOnNext(it -> requestedCount = it + 1); - } -} \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 746a25f81..785ed6aea 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,8 +1,8 @@ @color/md_blue_A400 - @color/md_blue_grey_500 - @color/md_blue_grey_700 + #54759e + #435e7e @color/md_blue_grey_900 @color/md_blue_grey_100