diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java index 2bf5013d0..146e91870 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Chapter.java @@ -10,10 +10,10 @@ import eu.kanade.mangafeed.util.UrlUtil; public class Chapter { @StorIOSQLiteColumn(name = ChapterTable.COLUMN_ID, key = true) - public long id; + public Long id; @StorIOSQLiteColumn(name = ChapterTable.COLUMN_MANGA_ID) - public long manga_id; + public Long manga_id; @StorIOSQLiteColumn(name = ChapterTable.COLUMN_URL) public String url; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java index c3dfc122f..047a21fc8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Manga.java @@ -10,7 +10,7 @@ import eu.kanade.mangafeed.util.UrlUtil; public class Manga { @StorIOSQLiteColumn(name = MangaTable.COLUMN_ID, key = true) - public long id; + public Long id; @StorIOSQLiteColumn(name = MangaTable.COLUMN_SOURCE) public int source; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java b/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java index 3b1d37f49..67280e951 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/download/DownloadManager.java @@ -133,7 +133,7 @@ public class DownloadManager { private boolean prepareDownload(Download download) { // If the chapter is already queued, don't add it again for (Download queuedDownload : queue.get()) { - if (download.chapter.id == queuedDownload.chapter.id) + if (download.chapter.id.equals(queuedDownload.chapter.id)) return true; } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/download/model/DownloadQueue.java b/app/src/main/java/eu/kanade/mangafeed/data/download/model/DownloadQueue.java index f4254093b..fe0490cbf 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/download/model/DownloadQueue.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/download/model/DownloadQueue.java @@ -31,7 +31,7 @@ public class DownloadQueue { public void remove(Chapter chapter) { for (Download download : queue) { - if (download.chapter.id == chapter.id) { + if (download.chapter.id.equals(chapter.id)) { remove(download); break; } 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 5c6950288..83f4f0667 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 @@ -155,7 +155,7 @@ public class CatalogueFragment extends BaseRxFragment { private int getMangaIndex(Manga manga) { for (int i = adapter.getCount() - 1; i >= 0; i--) { - if (manga.id == adapter.getItem(i).id) { + if (manga.id.equals(adapter.getItem(i).id)) { return i; } } 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 7d912a961..3369d0ecc 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 @@ -160,13 +160,13 @@ public class ChaptersFragment extends BaseRxFragment implemen public void onEventMainThread(DownloadStatusEvent event) { Manga manga = getPresenter().getManga(); // If the download status is from another manga, don't bother - if (manga != null && event.getChapter().manga_id != manga.id) + if (manga != null && !event.getChapter().manga_id.equals(manga.id)) return; Chapter chapter; for (int i = 0; i < adapter.getItemCount(); i++) { chapter = adapter.getItem(i); - if (event.getChapter().id == chapter.id) { + if (event.getChapter().id.equals(chapter.id)) { chapter.status = event.getStatus(); adapter.notifyItemChanged(i); break; 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 36febe954..8968405c9 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 @@ -24,6 +24,7 @@ import eu.kanade.mangafeed.util.PostResult; import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; +import rx.subjects.PublishSubject; public class ChaptersPresenter extends BasePresenter { @@ -34,11 +35,14 @@ public class ChaptersPresenter extends BasePresenter { private Manga manga; private Source source; + private List chapters; private boolean isCatalogueManga; private boolean sortOrderAToZ = true; private boolean onlyUnread = true; private boolean onlyDownloaded; + private PublishSubject> chaptersSubject; + private static final int DB_CHAPTERS = 1; private static final int FETCH_CHAPTERS = 2; @@ -46,12 +50,11 @@ public class ChaptersPresenter extends BasePresenter { protected void onCreate(Bundle savedState) { super.onCreate(savedState); + chaptersSubject = PublishSubject.create(); + restartableLatestCache(DB_CHAPTERS, this::getDbChaptersObs, - (view, chapters) -> { - view.onNextChapters(chapters); - EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); - } + ChaptersFragment::onNextChapters ); restartableLatestCache(FETCH_CHAPTERS, @@ -85,6 +88,14 @@ public class ChaptersPresenter extends BasePresenter { source = sourceManager.get(manga.source); start(DB_CHAPTERS); + add(db.getChapters(manga).createObservable() + .subscribeOn(Schedulers.io()) + .doOnNext(chapters -> { + this.chapters = chapters; + EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); + }) + .subscribe(chaptersSubject::onNext)); + // Get chapters if it's an online source if (isCatalogueManga) { fetchChapters(); @@ -96,14 +107,6 @@ public class ChaptersPresenter extends BasePresenter { start(FETCH_CHAPTERS); } - private Observable> getDbChaptersObs() { - return db.getChapters(manga.id, sortOrderAToZ, onlyUnread).createObservable() - .doOnNext(this::checkChaptersStatus) - .flatMap(this::applyDownloadedFilter) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()); - } - private Observable getOnlineChaptersObs() { return source .pullChaptersFromNetwork(manga.url) @@ -112,16 +115,54 @@ public class ChaptersPresenter extends BasePresenter { .observeOn(AndroidSchedulers.mainThread()); } + private Observable> getDbChaptersObs() { + return chaptersSubject + .observeOn(Schedulers.io()) + .flatMap(this::applyChapterFilters) + .observeOn(AndroidSchedulers.mainThread()); + } + + private Observable> applyChapterFilters(List chapters) { + Observable observable = Observable.from(chapters); + if (onlyUnread) { + observable = observable.filter(chapter -> !chapter.read); + } + + observable = observable.doOnNext(this::setChapterStatus); + if (onlyDownloaded) { + observable = observable.filter(chapter -> chapter.status == Download.DOWNLOADED); + } + return observable.toSortedList((chapter, chapter2) -> { + if (sortOrderAToZ) { + return Float.compare(chapter.chapter_number, chapter2.chapter_number); + } else { + return Float.compare(chapter2.chapter_number, chapter.chapter_number); + } + }); + } + + private void setChapterStatus(Chapter chapter) { + for (Download download : downloadManager.getQueue().get()) { + if (chapter.id.equals(download.chapter.id)) { + chapter.status = download.getStatus(); + return; + } + } + + if (downloadManager.isChapterDownloaded(source, manga, chapter)) { + chapter.status = Download.DOWNLOADED; + } else { + chapter.status = Download.NOT_DOWNLOADED; + } + } + public void onOpenChapter(Chapter chapter) { EventBus.getDefault().postSticky(new ReaderEvent(source, manga, chapter)); } public Chapter getNextUnreadChapter() { List chapters = db.getNextUnreadChapter(manga).executeAsBlocking(); - if (chapters.isEmpty()) { - return null; - } - return chapters.get(0); + return !chapters.isEmpty() ? chapters.get(0) : null; } public void markChaptersRead(Observable selectedChapters, boolean read) { @@ -154,51 +195,21 @@ public class ChaptersPresenter extends BasePresenter { })); } - private void checkChaptersStatus(List chapters) { - for (Chapter chapter : chapters) { - checkIsChapterDownloaded(chapter); - } - } - - private void checkIsChapterDownloaded(Chapter chapter) { - for (Download download : downloadManager.getQueue().get()) { - if (chapter.id == download.chapter.id) { - chapter.status = download.getStatus(); - return; - } - } - - if (downloadManager.isChapterDownloaded(source, manga, chapter)) { - chapter.status = Download.DOWNLOADED; - } else { - chapter.status = Download.NOT_DOWNLOADED; - } - } - - private Observable> applyDownloadedFilter(List chapters) { - if (onlyDownloaded) - return Observable.from(chapters) - .filter(chapter -> chapter.status == Download.DOWNLOADED) - .toList(); - - return Observable.just(chapters); - } - public void revertSortOrder() { //TODO manga.chapter_order sortOrderAToZ = !sortOrderAToZ; - start(DB_CHAPTERS); + chaptersSubject.onNext(chapters); } public void setReadFilter(boolean onlyUnread) { //TODO do we need save filter for manga? this.onlyUnread = onlyUnread; - start(DB_CHAPTERS); + chaptersSubject.onNext(chapters); } public void setDownloadedFilter(boolean onlyDownloaded) { this.onlyDownloaded = onlyDownloaded; - start(DB_CHAPTERS); + chaptersSubject.onNext(chapters); } public void setIsCatalogueManga(boolean value) { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java index e764bf918..851c7613e 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/reader/ReaderPresenter.java @@ -215,10 +215,7 @@ public class ReaderPresenter extends BasePresenter { // Check whether the given chapter is downloaded public boolean isChapterDownloaded(Chapter chapter) { - File dir = downloadManager.getAbsoluteChapterDirectory(source, manga, chapter); - List pageList = downloadManager.getSavedPageList(source, manga, chapter); - - return pageList != null && pageList.size() + 1 == dir.listFiles().length; + return downloadManager.isChapterDownloaded(source, manga, chapter); } // Called before loading another chapter or leaving the reader. It allows to do operations