From 75236559caa20bbc2ab1027de7d42198b1fb6fbe Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 16 Nov 2015 18:09:31 +0100 Subject: [PATCH] Minor changes --- app/build.gradle | 2 +- .../data/database/DatabaseHelper.java | 10 +- .../ui/catalogue/CatalogueFragment.java | 13 +- .../ui/catalogue/CataloguePresenter.java | 117 +++++++++--------- .../mangafeed/ui/main/MainActivity.java | 7 +- 5 files changed, 80 insertions(+), 69 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 00d7b39f55..044ee56b58 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,7 +94,7 @@ dependencies { apt "com.pushtorefresh.storio:sqlite-annotations-processor:$STORIO_VERSION" provided 'org.glassfish:javax.annotation:10.0-b28' - compile('com.mikepenz:materialdrawer:4.3.0@aar') { + compile('com.mikepenz:materialdrawer:4.4.8@aar') { transitive = true } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java index d1f48c6849..518cb3d68e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java @@ -173,8 +173,9 @@ public class DatabaseHelper { .withQuery(Query.builder() .table(ChaptersTable.TABLE) .where(ChaptersTable.COLUMN_MANGA_ID + "=? AND " + - ChaptersTable.COLUMN_CHAPTER_NUMBER + ">?") - .whereArgs(chapter.manga_id, chapter.chapter_number) + ChaptersTable.COLUMN_CHAPTER_NUMBER + ">? AND " + + ChaptersTable.COLUMN_CHAPTER_NUMBER + "<=?") + .whereArgs(chapter.manga_id, chapter.chapter_number, chapter.chapter_number + 1) .orderBy(ChaptersTable.COLUMN_CHAPTER_NUMBER) .limit(1) .build()) @@ -187,8 +188,9 @@ public class DatabaseHelper { .withQuery(Query.builder() .table(ChaptersTable.TABLE) .where(ChaptersTable.COLUMN_MANGA_ID + "=? AND " + - ChaptersTable.COLUMN_CHAPTER_NUMBER + "=?") + .whereArgs(chapter.manga_id, chapter.chapter_number, chapter.chapter_number - 1) .orderBy(ChaptersTable.COLUMN_CHAPTER_NUMBER + " DESC") .limit(1) .build()) diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java index 79d43c91cd..7259a05d27 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CatalogueFragment.java @@ -129,10 +129,15 @@ public class CatalogueFragment extends BaseRxFragment { } public void initializeScrollListener() { - scroll_listener = new EndlessScrollListener(getPresenter()::requestNext); + scroll_listener = new EndlessScrollListener(this::requestNext); manga_list.setOnScrollListener(scroll_listener); } + public void requestNext() { + if (getPresenter().requestNext()) + showGridProgressBar(); + } + public void showProgressBar() { progress.setVisibility(ProgressBar.VISIBLE); } @@ -147,6 +152,7 @@ public class CatalogueFragment extends BaseRxFragment { } public void onAddPage(PageBundle> page) { + hideProgressBar(); if (page.page == 0) { adapter.getItems().clear(); scroll_listener.resetScroll(); @@ -155,7 +161,7 @@ public class CatalogueFragment extends BaseRxFragment { } private int getMangaIndex(Manga manga) { - for (int i = 0; i < adapter.getCount(); i++) { + for (int i = adapter.getCount() - 1; i >= 0; i--) { if (manga.id == adapter.getItem(i).id) { return i; } @@ -164,6 +170,9 @@ public class CatalogueFragment extends BaseRxFragment { } private ImageView getImageView(int position) { + if (position == -1) + return null; + View v = manga_list.getChildAt(position - manga_list.getFirstVisiblePosition()); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java index 52b3940f06..a3f601e508 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/catalogue/CataloguePresenter.java @@ -23,6 +23,7 @@ import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import rx.subjects.PublishSubject; +import timber.log.Timber; public class CataloguePresenter extends BasePresenter { @@ -31,40 +32,55 @@ public class CataloguePresenter extends BasePresenter { private Source selectedSource; - @State protected String mSearchName; - @State protected boolean mSearchMode; + @State protected String searchName; + @State protected boolean searchMode; private final int SEARCH_TIMEOUT = 1000; - private int mCurrentPage; + private int currentPage; private RxPager pager; private MangasPage lastMangasPage; - private Subscription mQueryDebouncerSubscription; - private Subscription mMangaDetailFetchSubscription; - private PublishSubject mQueryDebouncerSubject; - private PublishSubject> mMangaDetailPublishSubject; + private Subscription queryDebouncerSubscription; + private PublishSubject queryDebouncerSubject; + private PublishSubject> mangaDetailSubject; private static final int GET_MANGA_LIST = 1; + private static final int GET_MANGA_DETAIL = 2; @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); + mangaDetailSubject = PublishSubject.create(); + restartableReplay(GET_MANGA_LIST, () -> pager.pages().>>concatMap( page -> getMangaObs(page + 1) .map(mangas -> new PageBundle<>(page, mangas)) - .observeOn(AndroidSchedulers.mainThread()) - ), + .observeOn(AndroidSchedulers.mainThread())), (view, page) -> { - view.hideProgressBar(); view.onAddPage(page); - if (mMangaDetailPublishSubject != null) - mMangaDetailPublishSubject.onNext(page.data); - }); + if (mangaDetailSubject != null) + mangaDetailSubject.onNext(page.data); + }, + (view, error) -> Timber.e(error.fillInStackTrace(), error.getMessage())); + + restartableLatestCache(GET_MANGA_DETAIL, + () -> mangaDetailSubject + .subscribeOn(Schedulers.io()) + .flatMap(Observable::from) + .filter(manga -> !manga.initialized) + .window(3) + .concatMap(pack -> pack.concatMap(this::getMangaDetails)) + .filter(manga -> manga.initialized) + .onBackpressureBuffer() + .observeOn(AndroidSchedulers.mainThread()), + (view, manga) -> { + view.updateImage(manga); + }, + (view, error) -> Timber.e(error.fillInStackTrace(), error.getMessage())); initializeSearch(); - initializeMangaDetailsLoader(); } @Override @@ -73,8 +89,8 @@ public class CataloguePresenter extends BasePresenter { view.setToolbarTitle(selectedSource.getName()); - if (mSearchMode) - view.restoreSearch(mSearchName); + if (searchMode) + view.restoreSearch(searchName); } public void startRequesting(int sourceId) { @@ -84,31 +100,32 @@ public class CataloguePresenter extends BasePresenter { private void restartRequest() { stop(GET_MANGA_LIST); - mCurrentPage = 1; + currentPage = 1; pager = new RxPager(); if (getView() != null) getView().showProgressBar(); + start(GET_MANGA_DETAIL); start(GET_MANGA_LIST); } - public void requestNext() { - pager.requestNext(++mCurrentPage); + public boolean requestNext() { + if (lastMangasPage.nextPageUrl == null) + return false; + + pager.requestNext(++currentPage); + return true; } private Observable> getMangaObs(int page) { MangasPage nextMangasPage = new MangasPage(page); if (page != 1) { - if (lastMangasPage.nextPageUrl == null) - return Observable.empty(); nextMangasPage.url = lastMangasPage.nextPageUrl; } - if (getView() != null) - getView().showGridProgressBar(); Observable obs; - if (mSearchMode) - obs = selectedSource.searchMangasFromNetwork(nextMangasPage, mSearchName); + if (searchMode) + obs = selectedSource.searchMangasFromNetwork(nextMangasPage, searchName); else obs = selectedSource.pullPopularMangasFromNetwork(nextMangasPage); @@ -131,41 +148,20 @@ public class CataloguePresenter extends BasePresenter { } private void initializeSearch() { - if (mQueryDebouncerSubscription != null) + if (queryDebouncerSubscription != null) return; - mSearchName = ""; - mSearchMode = false; - mQueryDebouncerSubject = PublishSubject.create(); + searchName = ""; + searchMode = false; + queryDebouncerSubject = PublishSubject.create(); - add(mQueryDebouncerSubscription = mQueryDebouncerSubject + add(queryDebouncerSubscription = queryDebouncerSubject .debounce(SEARCH_TIMEOUT, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(this::queryFromSearch)); } - private void initializeMangaDetailsLoader() { - if (mMangaDetailFetchSubscription != null) - return; - - mMangaDetailPublishSubject = PublishSubject.create(); - - add(mMangaDetailFetchSubscription = mMangaDetailPublishSubject - .subscribeOn(Schedulers.io()) - .flatMap(Observable::from) - .filter(manga -> !manga.initialized) - .window(3) - .concatMap(pack -> pack.concatMap(this::getMangaDetails)) - .filter(manga -> manga.initialized) - .onBackpressureBuffer() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(manga -> { - if (getView() != null) - getView().updateImage(manga); - })); - } - private Observable getMangaDetails(final Manga manga) { return selectedSource.pullMangaFromNetwork(manga.url) .subscribeOn(Schedulers.io()) @@ -173,6 +169,9 @@ public class CataloguePresenter extends BasePresenter { Manga.copyFromNetwork(manga, networkManga); db.insertManga(manga).executeAsBlocking(); return Observable.just(manga); + }) + .onErrorResumeNext(error -> { + return Observable.just(manga); }); } @@ -180,25 +179,25 @@ public class CataloguePresenter extends BasePresenter { // If the query is empty or not debounced, resolve it instantly if (now || query.equals("")) queryFromSearch(query); - else if (mQueryDebouncerSubject != null) - mQueryDebouncerSubject.onNext(query); + else if (queryDebouncerSubject != null) + queryDebouncerSubject.onNext(query); } private void queryFromSearch(String query) { // If text didn't change, do nothing - if (mSearchName.equals(query)) { + if (searchName.equals(query)) { return; } // If going to search mode - else if (mSearchName.equals("") && !query.equals("")) { - mSearchMode = true; + else if (searchName.equals("") && !query.equals("")) { + searchMode = true; } // If going to normal mode - else if (!mSearchName.equals("") && query.equals("")) { - mSearchMode = false; + else if (!searchName.equals("") && query.equals("")) { + searchMode = false; } - mSearchName = query; + searchName = query; restartRequest(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java index 1344bc90bb..08c053a03f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java @@ -14,11 +14,11 @@ import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; -import eu.kanade.mangafeed.ui.setting.SettingsActivity; import eu.kanade.mangafeed.ui.base.activity.BaseActivity; +import eu.kanade.mangafeed.ui.catalogue.SourceFragment; import eu.kanade.mangafeed.ui.download.DownloadFragment; import eu.kanade.mangafeed.ui.library.LibraryFragment; -import eu.kanade.mangafeed.ui.catalogue.SourceFragment; +import eu.kanade.mangafeed.ui.setting.SettingsActivity; public class MainActivity extends BaseActivity { @@ -88,7 +88,8 @@ public class MainActivity extends BaseActivity { ) .build(); - drawer.setSelection(R.id.nav_drawer_library); + if (savedInstanceState == null) + drawer.setSelection(R.id.nav_drawer_library); } public void setFragment(Fragment fragment) {