Change toolbar color (Fix #43). Allow to also remove from library (Fix #44). Rewrite RxPager.

This commit is contained in:
inorichi 2016-01-20 19:21:17 +01:00
parent 7fc23d526b
commit ad97d03f1d
4 changed files with 49 additions and 44 deletions

View File

@ -109,6 +109,7 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
} else { } else {
selectedIndex = position; selectedIndex = position;
showProgressBar(); showProgressBar();
recycler.setAdapter(adapter);
getPresenter().startRequesting(source); getPresenter().startRequesting(source);
} }
} }
@ -211,7 +212,7 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
public void onAddPage(int page, List<Manga> mangas) { public void onAddPage(int page, List<Manga> mangas) {
hideProgressBar(); hideProgressBar();
if (page == 1) { if (page == 0) {
adapter.clear(); adapter.clear();
scrollListener.resetScroll(); scrollListener.resetScroll();
} }
@ -260,13 +261,16 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
@Override @Override
public void onListItemLongClick(int position) { public void onListItemLongClick(int position) {
final Manga selectedManga = adapter.getItem(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()) new MaterialDialog.Builder(getActivity())
.items(getString(R.string.add_to_library)) .items(getString(textRes))
.itemsCallback((dialog, itemView, which, text) -> { .itemsCallback((dialog, itemView, which, text) -> {
switch (which) { switch (which) {
case 0: case 0:
getPresenter().addMangaToLibrary(selectedManga); getPresenter().changeMangaFavorite(selectedManga);
break; break;
} }
}) })

View File

@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.catalogue;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair;
import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
@ -38,14 +37,14 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
private String query; private String query;
private int currentPage; private RxPager<Manga> pager;
private RxPager pager;
private MangasPage lastMangasPage; private MangasPage lastMangasPage;
private PublishSubject<List<Manga>> mangaDetailSubject; private PublishSubject<List<Manga>> mangaDetailSubject;
private static final int GET_MANGA_LIST = 1; private static final int GET_MANGA_LIST = 1;
private static final int GET_MANGA_DETAIL = 2; private static final int GET_MANGA_DETAIL = 2;
private static final int GET_MANGA_PAGE = 3;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -57,13 +56,16 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
mangaDetailSubject = PublishSubject.create(); mangaDetailSubject = PublishSubject.create();
pager = new RxPager<>();
restartableReplay(GET_MANGA_LIST, restartableReplay(GET_MANGA_LIST,
() -> pager.pages().concatMap(page -> getMangasPageObservable(page + 1)), pager::results,
(view, pair) -> view.onAddPage(pair.first, pair.second), (view, pair) -> view.onAddPage(pair.first, pair.second));
(view, error) -> {
view.onAddPageError(); restartableFirst(GET_MANGA_PAGE,
Timber.e(error.getMessage()); () -> pager.request(page -> getMangasPageObservable(page + 1)),
}); (view, next) -> {},
(view, error) -> view.onAddPageError());
restartableLatestCache(GET_MANGA_DETAIL, restartableLatestCache(GET_MANGA_DETAIL,
() -> mangaDetailSubject () -> mangaDetailSubject
@ -82,6 +84,7 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
source = sourceManager.get(sourceId); source = sourceManager.get(sourceId);
stop(GET_MANGA_LIST); stop(GET_MANGA_LIST);
stop(GET_MANGA_DETAIL); stop(GET_MANGA_DETAIL);
stop(GET_MANGA_PAGE);
} }
public void startRequesting(Source source) { public void startRequesting(Source source) {
@ -92,20 +95,21 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
public void restartRequest(String query) { public void restartRequest(String query) {
this.query = query; this.query = query;
stop(GET_MANGA_LIST); stop(GET_MANGA_PAGE);
currentPage = 1; lastMangasPage = null;
pager = new RxPager();
start(GET_MANGA_DETAIL); start(GET_MANGA_DETAIL);
start(GET_MANGA_LIST); start(GET_MANGA_LIST);
start(GET_MANGA_PAGE);
} }
public void requestNext() { public void requestNext() {
if (hasNextPage()) if (hasNextPage()) {
pager.requestNext(++currentPage); start(GET_MANGA_PAGE);
}
} }
private Observable<Pair<Integer, List<Manga>>> getMangasPageObservable(int page) { private Observable<List<Manga>> getMangasPageObservable(int page) {
MangasPage nextMangasPage = new MangasPage(page); MangasPage nextMangasPage = new MangasPage(page);
if (page != 1) { if (page != 1) {
nextMangasPage.url = lastMangasPage.nextPageUrl; nextMangasPage.url = lastMangasPage.nextPageUrl;
@ -120,10 +124,9 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
.flatMap(mangasPage -> Observable.from(mangasPage.mangas)) .flatMap(mangasPage -> Observable.from(mangasPage.mangas))
.map(this::networkToLocalManga) .map(this::networkToLocalManga)
.toList() .toList()
.map(mangas -> Pair.create(page, mangas)) .doOnNext(mangas -> {
.doOnNext(pair -> {
if (mangaDetailSubject != null) if (mangaDetailSubject != null)
mangaDetailSubject.onNext(pair.second); mangaDetailSubject.onNext(mangas);
}) })
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
} }
@ -149,6 +152,10 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
.onErrorResumeNext(error -> Observable.just(manga)); .onErrorResumeNext(error -> Observable.just(manga));
} }
public Manga getDbManga(long id) {
return db.getManga(id).executeAsBlocking();
}
public Source getSource() { public Source getSource() {
return source; return source;
} }
@ -170,8 +177,8 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
return sourceManager.getSources(); return sourceManager.getSources();
} }
public void addMangaToLibrary(Manga manga) { public void changeMangaFavorite(Manga manga) {
manga.favorite = true; manga.favorite = !manga.favorite;
db.insertManga(manga).executeAsBlocking(); db.insertManga(manga).executeAsBlocking();
} }
} }

View File

@ -1,32 +1,26 @@
package eu.kanade.tachiyomi.util; package eu.kanade.tachiyomi.util;
import android.util.Pair;
import java.util.List;
import rx.Observable; import rx.Observable;
import rx.functions.Func1;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
public class RxPager { public class RxPager<T> {
private final int initialPageCount; private final PublishSubject<List<T>> results = PublishSubject.create();
private final PublishSubject<Integer> requests = PublishSubject.create();
private int requestedCount; private int requestedCount;
public RxPager() { public Observable<Pair<Integer, List<T>>> results() {
this(1); requestedCount = 0;
return results.map(list -> Pair.create(requestedCount++, list));
} }
public RxPager(int initialPageCount) { public Observable<List<T>> request(Func1<Integer, Observable<List<T>>> networkObservable) {
this.initialPageCount = initialPageCount; return networkObservable.call(requestedCount).doOnNext(results::onNext);
} }
public void requestNext(int page) { }
requests.onNext(page);
}
public Observable<Integer> pages() {
return requests
.concatMap(targetPage -> targetPage <= requestedCount ?
Observable.<Integer>empty() :
Observable.range(requestedCount, targetPage - requestedCount))
.startWith(Observable.range(0, initialPageCount))
.doOnNext(it -> requestedCount = it + 1);
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="colorAccent">@color/md_blue_A400</color> <color name="colorAccent">@color/md_blue_A400</color>
<color name="colorPrimary">@color/md_blue_grey_500</color> <color name="colorPrimary">#54759e</color>
<color name="colorPrimaryDark">@color/md_blue_grey_700</color> <color name="colorPrimaryDark">#435e7e</color>
<color name="colorPrimarySuperDark">@color/md_blue_grey_900</color> <color name="colorPrimarySuperDark">@color/md_blue_grey_900</color>
<color name="colorPrimaryLight">@color/md_blue_grey_100</color> <color name="colorPrimaryLight">@color/md_blue_grey_100</color>