Fix chapter recognition. Improve initial requests to fetch chapters from source

This commit is contained in:
inorichi 2015-12-03 13:57:25 +01:00
parent b986309b81
commit 384acb2322
5 changed files with 48 additions and 15 deletions

View File

@ -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<ChaptersPresenter> implements
@ -84,7 +83,7 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> 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<ChaptersPresenter> 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<Chapter> 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();
}

View File

@ -41,6 +41,7 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
private boolean sortOrderAToZ = true;
private boolean onlyUnread = true;
private boolean onlyDownloaded;
private boolean hasRequested;
private PublishSubject<List<Chapter>> chaptersSubject;
@ -60,7 +61,8 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
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<ChaptersFragment> {
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().removeStickyEvent(ChapterCountEvent.class);
super.onDestroy();
}
@EventBusHook
@ -116,7 +118,8 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
.pullChaptersFromNetwork(manga.url)
.subscribeOn(Schedulers.io())
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
.observeOn(AndroidSchedulers.mainThread());
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(r -> hasRequested = true);
}
private Observable<List<Chapter>> getDbChaptersObs() {
@ -248,4 +251,12 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
return manga;
}
public List<Chapter> getChapters() {
return chapters;
}
public boolean hasRequested() {
return hasRequested;
}
}

View File

@ -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<Float> 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);
}

View File

@ -91,6 +91,7 @@
<string name="chapter_queued">Queued</string>
<string name="chapter_downloading">Downloading</string>
<string name="chapter_error">Error</string>
<string name="fetch_chapters_error">Error while fetching chapters</string>
<!-- Reader activity -->
<string name="downloading">Downloading…</string>

View File

@ -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));
}
}