diff --git a/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java b/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java index de66204eed..b555a6c754 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/download/model/Download.java @@ -3,12 +3,10 @@ package eu.kanade.mangafeed.data.download.model; import java.io.File; import java.util.List; -import de.greenrobot.event.EventBus; import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.source.base.Source; import eu.kanade.mangafeed.data.source.model.Page; -import eu.kanade.mangafeed.event.DownloadStatusEvent; import rx.subjects.PublishSubject; public class Download { @@ -53,6 +51,5 @@ public class Download { private void notifyStatus() { if (statusSubject != null) statusSubject.onNext(this); - EventBus.getDefault().post(new DownloadStatusEvent(chapter, status)); } } \ No newline at end of file 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 fe0490cbf1..31ffcfdeed 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 @@ -56,8 +56,7 @@ public class DownloadQueue { } public Observable getStatusObservable() { - return statusSubject - .startWith(getActiveDownloads()); + return statusSubject; } } diff --git a/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java b/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java deleted file mode 100644 index c3f288b80c..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/event/DownloadStatusEvent.java +++ /dev/null @@ -1,23 +0,0 @@ -package eu.kanade.mangafeed.event; - -import eu.kanade.mangafeed.data.database.models.Chapter; - -public class DownloadStatusEvent { - - private Chapter chapter; - private int status; - - public DownloadStatusEvent(Chapter chapter, int status) { - this.chapter = chapter; - this.status = status; - } - - public Chapter getChapter() { - return chapter; - } - - public int getStatus() { - return status; - } - -} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/download/DownloadPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/download/DownloadPresenter.java index 7eb6cfe30c..28958c11f5 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/download/DownloadPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/download/DownloadPresenter.java @@ -52,7 +52,7 @@ public class DownloadPresenter extends BasePresenter { super.onTakeView(view); add(statusSubscription = downloadQueue.getStatusObservable() - .subscribeOn(Schedulers.io()) + .startWith(downloadQueue.getActiveDownloads()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(download -> { processStatus(download, view); 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 e9cebd0848..7cd422b6ef 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 @@ -23,15 +23,13 @@ import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.database.models.Chapter; -import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.download.DownloadService; -import eu.kanade.mangafeed.event.DownloadStatusEvent; +import eu.kanade.mangafeed.data.download.model.Download; import eu.kanade.mangafeed.ui.base.activity.BaseActivity; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration; import eu.kanade.mangafeed.ui.manga.MangaActivity; import eu.kanade.mangafeed.ui.reader.ReaderActivity; -import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.ToastUtil; import nucleus.factory.RequiresPresenter; import rx.Observable; @@ -107,12 +105,10 @@ public class ChaptersFragment extends BaseRxFragment implemen @Override public void onResume() { super.onResume(); - registerForEvents(); } @Override public void onPause() { - unregisterForEvents(); super.onPause(); } @@ -173,20 +169,12 @@ public class ChaptersFragment extends BaseRxFragment implemen startActivity(intent); } - @EventBusHook - 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.equals(manga.id)) - return; - - getPresenter().updateChapterStatus(event); - + public void onChapterStatusChange(Download download) { Chapter chapter; for (int i = linearLayout.findFirstVisibleItemPosition(); i < linearLayout.findLastVisibleItemPosition(); i++) { int pos = recyclerView.getChildAdapterPosition(linearLayout.findViewByPosition(i)); chapter = adapter.getItem(pos); - if (event.getChapter().id.equals(chapter.id)) { + if (chapter != null && download.chapter.id.equals(chapter.id)) { 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 80568a8135..d09ce6aff2 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 @@ -17,7 +17,6 @@ import eu.kanade.mangafeed.data.source.SourceManager; import eu.kanade.mangafeed.data.source.base.Source; import eu.kanade.mangafeed.event.ChapterCountEvent; import eu.kanade.mangafeed.event.DownloadChaptersEvent; -import eu.kanade.mangafeed.event.DownloadStatusEvent; import eu.kanade.mangafeed.event.ReaderEvent; import eu.kanade.mangafeed.ui.base.presenter.BasePresenter; import eu.kanade.mangafeed.util.EventBusHook; @@ -47,6 +46,7 @@ public class ChaptersPresenter extends BasePresenter { private static final int DB_CHAPTERS = 1; private static final int FETCH_CHAPTERS = 2; + private static final int CHAPTER_STATUS_CHANGES = 3; @Override protected void onCreate(Bundle savedState) { @@ -64,6 +64,12 @@ public class ChaptersPresenter extends BasePresenter { (view, result) -> view.onFetchChaptersDone(), (view, error) -> view.onFetchChaptersError() ); + + restartableLatestCache(CHAPTER_STATUS_CHANGES, + this::getChapterStatusObs, + (view, download) -> view.onChapterStatusChange(download), + (view, error) -> Timber.e(error.getCause(), error.getMessage()) + ); } @Override @@ -96,16 +102,19 @@ public class ChaptersPresenter extends BasePresenter { add(db.getChapters(manga).createObservable() .subscribeOn(Schedulers.io()) .doOnNext(chapters -> { + stop(CHAPTER_STATUS_CHANGES); this.chapters = chapters; EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); for (Chapter chapter : chapters) { setChapterStatus(chapter); } + start(CHAPTER_STATUS_CHANGES); }) .subscribe(chaptersSubject::onNext)); } public void fetchChaptersFromSource() { + hasRequested = true; start(FETCH_CHAPTERS); } @@ -118,8 +127,7 @@ public class ChaptersPresenter extends BasePresenter { .pullChaptersFromNetwork(manga.url) .subscribeOn(Schedulers.io()) .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters)) - .observeOn(AndroidSchedulers.mainThread()) - .doOnNext(r -> hasRequested = true); + .observeOn(AndroidSchedulers.mainThread()); } private Observable> getDbChaptersObs() { @@ -137,13 +145,9 @@ public class ChaptersPresenter extends BasePresenter { 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); - } - }); + return observable.toSortedList((chapter, chapter2) -> sortOrderAToZ ? + Float.compare(chapter.chapter_number, chapter2.chapter_number) : + Float.compare(chapter2.chapter_number, chapter.chapter_number)); } private void setChapterStatus(Chapter chapter) { @@ -161,14 +165,21 @@ public class ChaptersPresenter extends BasePresenter { } } - public void updateChapterStatus(DownloadStatusEvent event) { + private Observable getChapterStatusObs() { + return downloadManager.getQueue().getStatusObservable() + .observeOn(AndroidSchedulers.mainThread()) + .filter(download -> download.manga.id.equals(manga.id)) + .doOnNext(this::updateChapterStatus); + } + + public void updateChapterStatus(Download download) { for (Chapter chapter : chapters) { - if (event.getChapter().id.equals(chapter.id)) { - chapter.status = event.getStatus(); + if (download.chapter.id.equals(chapter.id)) { + chapter.status = download.getStatus(); break; } } - if (onlyDownloaded && event.getStatus() == Download.DOWNLOADED) + if (onlyDownloaded && download.getStatus() == Download.DOWNLOADED) refreshChapters(); }