diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java index 30a1d40dc..e9cebd084 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersFragment.java @@ -35,7 +35,6 @@ import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.ToastUtil; import nucleus.factory.RequiresPresenter; import rx.Observable; -import timber.log.Timber; @RequiresPresenter(ChaptersPresenter.class) public class ChaptersFragment extends BaseRxFragment implements @@ -84,7 +83,7 @@ public class ChaptersFragment extends BaseRxFragment implemen setSortIcon(); // Init listeners - swipeRefresh.setOnRefreshListener(this::onFetchChapters); + swipeRefresh.setOnRefreshListener(this::fetchChapters); readCb.setOnCheckedChangeListener((arg, isChecked) -> getPresenter().setReadFilter(isChecked)); downloadedCb.setOnCheckedChangeListener((v, isChecked) -> @@ -127,31 +126,43 @@ public class ChaptersFragment extends BaseRxFragment implemen public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_refresh: - onFetchChapters(); + fetchChapters(); break; } return super.onOptionsItemSelected(item); } public void onNextChapters(List chapters) { - if (chapters.isEmpty() && isCatalogueManga()) { - swipeRefresh.setRefreshing(true); - getPresenter().fetchChaptersFromSource(); - } + // If the list is empty, fetch chapters from source if the conditions are met + // We use presenter chapters instead because they are always unfiltered + if (getPresenter().getChapters().isEmpty()) + initialFetchChapters(); closeActionMode(); adapter.setItems(chapters); } - public void onFetchChapters() { + private void initialFetchChapters() { + // Only fetch if this view is from the catalog and it hasn't requested previously + if (isCatalogueManga() && !getPresenter().hasRequested()) { + fetchChapters(); + } + } + + public void fetchChapters() { swipeRefresh.setRefreshing(true); getPresenter().fetchChaptersFromSource(); } - public void onFetchChaptersFinish() { + public void onFetchChaptersDone() { swipeRefresh.setRefreshing(false); } + public void onFetchChaptersError() { + swipeRefresh.setRefreshing(false); + ToastUtil.showShort(getContext(), R.string.fetch_chapters_error); + } + public boolean isCatalogueManga() { return ((MangaActivity) getActivity()).isCatalogueManga(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersPresenter.java index 2be7c048b..80568a813 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/chapter/ChaptersPresenter.java @@ -41,6 +41,7 @@ public class ChaptersPresenter extends BasePresenter { private boolean sortOrderAToZ = true; private boolean onlyUnread = true; private boolean onlyDownloaded; + private boolean hasRequested; private PublishSubject> chaptersSubject; @@ -60,7 +61,8 @@ public class ChaptersPresenter extends BasePresenter { restartableLatestCache(FETCH_CHAPTERS, this::getOnlineChaptersObs, - (view, result) -> view.onFetchChaptersFinish() + (view, result) -> view.onFetchChaptersDone(), + (view, error) -> view.onFetchChaptersError() ); } @@ -78,8 +80,8 @@ public class ChaptersPresenter extends BasePresenter { @Override protected void onDestroy() { - super.onDestroy(); EventBus.getDefault().removeStickyEvent(ChapterCountEvent.class); + super.onDestroy(); } @EventBusHook @@ -116,7 +118,8 @@ public class ChaptersPresenter extends BasePresenter { .pullChaptersFromNetwork(manga.url) .subscribeOn(Schedulers.io()) .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters)) - .observeOn(AndroidSchedulers.mainThread()); + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext(r -> hasRequested = true); } private Observable> getDbChaptersObs() { @@ -248,4 +251,12 @@ public class ChaptersPresenter extends BasePresenter { return manga; } + public List getChapters() { + return chapters; + } + + public boolean hasRequested() { + return hasRequested; + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java index 90ed609ee..c0552e6b2 100644 --- a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java +++ b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java @@ -10,7 +10,7 @@ import eu.kanade.mangafeed.data.database.models.Manga; public class ChapterRecognition { - private static Pattern p1 = Pattern.compile("ch.?(\\d+[\\.,]?\\d*)"); + private static Pattern p1 = Pattern.compile("Ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); private static Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); private static Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*:)"); @@ -19,7 +19,7 @@ public class ChapterRecognition { return; // Remove spaces and convert to lower case - String name = replaceIrrelevantCharacters(chapter.name); + String name = chapter.name; Matcher matcher; // Safest option, the chapter has a token prepended @@ -29,6 +29,8 @@ public class ChapterRecognition { return; } + name = replaceIrrelevantCharacters(name); + List occurences; // If there's only one number, use it @@ -71,7 +73,7 @@ public class ChapterRecognition { Matcher m = p2.matcher(text); if (m.find()) { try { - Float value = Float.parseFloat(m.group()); + Float value = Float.parseFloat(m.group(1)); if (!occurences.contains(value)) { occurences.add(value); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 360b62c3a..2d8b8bf3c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -91,6 +91,7 @@ Queued Downloading Error + Error while fetching chapters Downloading… diff --git a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java index 192d7bb50..e415ff5f7 100644 --- a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java +++ b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java @@ -105,4 +105,12 @@ public class ChapterRecognitionTest { assertThat(c.chapter_number, is(191f)); } + @Test + public void testWithKeywordChAtTheEndOfTheManga() { + // It should be 567, not 67 (ch is a keyword to get the chapter number) + Chapter c = createChapter("Bleach 567: Down With Snowwhite"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number, is(567f)); + } + }