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 {
selectedIndex = position;
showProgressBar();
recycler.setAdapter(adapter);
getPresenter().startRequesting(source);
}
}
@ -211,7 +212,7 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
public void onAddPage(int page, List<Manga> mangas) {
hideProgressBar();
if (page == 1) {
if (page == 0) {
adapter.clear();
scrollListener.resetScroll();
}
@ -260,13 +261,16 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
@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;
}
})

View File

@ -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<CatalogueFragment> {
private String query;
private int currentPage;
private RxPager pager;
private RxPager<Manga> pager;
private MangasPage lastMangasPage;
private PublishSubject<List<Manga>> 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<CatalogueFragment> {
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<CatalogueFragment> {
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<CatalogueFragment> {
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<Pair<Integer, List<Manga>>> getMangasPageObservable(int page) {
private Observable<List<Manga>> getMangasPageObservable(int page) {
MangasPage nextMangasPage = new MangasPage(page);
if (page != 1) {
nextMangasPage.url = lastMangasPage.nextPageUrl;
@ -120,10 +124,9 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
.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<CatalogueFragment> {
.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<CatalogueFragment> {
return sourceManager.getSources();
}
public void addMangaToLibrary(Manga manga) {
manga.favorite = true;
public void changeMangaFavorite(Manga manga) {
manga.favorite = !manga.favorite;
db.insertManga(manga).executeAsBlocking();
}
}

View File

@ -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<T> {
private final int initialPageCount;
private final PublishSubject<Integer> requests = PublishSubject.create();
private final PublishSubject<List<T>> results = PublishSubject.create();
private int requestedCount;
public RxPager() {
this(1);
public Observable<Pair<Integer, List<T>>> results() {
requestedCount = 0;
return results.map(list -> Pair.create(requestedCount++, list));
}
public RxPager(int initialPageCount) {
this.initialPageCount = initialPageCount;
public Observable<List<T>> request(Func1<Integer, Observable<List<T>>> networkObservable) {
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"?>
<resources>
<color name="colorAccent">@color/md_blue_A400</color>
<color name="colorPrimary">@color/md_blue_grey_500</color>
<color name="colorPrimaryDark">@color/md_blue_grey_700</color>
<color name="colorPrimary">#54759e</color>
<color name="colorPrimaryDark">#435e7e</color>
<color name="colorPrimarySuperDark">@color/md_blue_grey_900</color>
<color name="colorPrimaryLight">@color/md_blue_grey_100</color>