From b986309b816379e684199e70856ec956e7319f9d Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 3 Dec 2015 12:48:30 +0100 Subject: [PATCH] Improve the refresh of chapter filters --- .../data/download/DownloadManager.java | 1 - .../ui/manga/chapter/ChaptersFragment.java | 28 +++++--- .../ui/manga/chapter/ChaptersPresenter.java | 70 ++++++++++++------- 3 files changed, 61 insertions(+), 38 deletions(-) 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 67280e9511..e8c7092536 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 @@ -340,7 +340,6 @@ public class DownloadManager { public void deleteChapter(Source source, Manga manga, Chapter chapter) { File path = getAbsoluteChapterDirectory(source, manga, chapter); DiskUtils.deleteFiles(path); - queue.remove(chapter); } public DownloadQueue getQueue() { 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 3369d0eccc..30a1d40dc2 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,12 +35,13 @@ 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 ActionMode.Callback, ChaptersAdapter.OnItemClickListener { - @Bind(R.id.chapter_list) RecyclerView chapters; + @Bind(R.id.chapter_list) RecyclerView recyclerView; @Bind(R.id.swipe_refresh) SwipeRefreshLayout swipeRefresh; @Bind(R.id.toolbar_bottom) Toolbar toolbarBottom; @@ -50,7 +51,7 @@ public class ChaptersFragment extends BaseRxFragment implemen @Bind(R.id.action_show_downloaded) CheckBox downloadedCb; private ChaptersAdapter adapter; - + private LinearLayoutManager linearLayout; private ActionMode actionMode; public static ChaptersFragment newInstance() { @@ -61,7 +62,6 @@ public class ChaptersFragment extends BaseRxFragment implemen public void onCreate(Bundle savedState) { super.onCreate(savedState); setHasOptionsMenu(true); - getPresenter().setIsCatalogueManga(isCatalogueManga()); } @Override @@ -72,10 +72,11 @@ public class ChaptersFragment extends BaseRxFragment implemen ButterKnife.bind(this, view); // Init RecyclerView and adapter - chapters.setLayoutManager(new LinearLayoutManager(getActivity())); - chapters.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(this.getContext(), R.drawable.line_divider))); + linearLayout = new LinearLayoutManager(getActivity()); + recyclerView.setLayoutManager(linearLayout); + recyclerView.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(getContext(), R.drawable.line_divider))); adapter = new ChaptersAdapter(this); - chapters.setAdapter(adapter); + recyclerView.setAdapter(adapter); // Set initial values setReadFilter(); @@ -133,13 +134,18 @@ public class ChaptersFragment extends BaseRxFragment implemen } public void onNextChapters(List chapters) { + if (chapters.isEmpty() && isCatalogueManga()) { + swipeRefresh.setRefreshing(true); + getPresenter().fetchChaptersFromSource(); + } + closeActionMode(); adapter.setItems(chapters); } public void onFetchChapters() { swipeRefresh.setRefreshing(true); - getPresenter().fetchChapters(); + getPresenter().fetchChaptersFromSource(); } public void onFetchChaptersFinish() { @@ -163,11 +169,13 @@ public class ChaptersFragment extends BaseRxFragment implemen if (manga != null && !event.getChapter().manga_id.equals(manga.id)) return; + getPresenter().updateChapterStatus(event); + Chapter chapter; - for (int i = 0; i < adapter.getItemCount(); i++) { - chapter = adapter.getItem(i); + 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)) { - 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 8968405c96..2be7c048b4 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,6 +17,7 @@ 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; @@ -25,6 +26,7 @@ import rx.Observable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import rx.subjects.PublishSubject; +import timber.log.Timber; public class ChaptersPresenter extends BasePresenter { @@ -36,7 +38,6 @@ 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; @@ -83,30 +84,33 @@ public class ChaptersPresenter extends BasePresenter { @EventBusHook public void onEventMainThread(Manga manga) { - if (this.manga == null) { - this.manga = manga; - source = sourceManager.get(manga.source); - start(DB_CHAPTERS); + if (this.manga != null) + return; - add(db.getChapters(manga).createObservable() - .subscribeOn(Schedulers.io()) - .doOnNext(chapters -> { - this.chapters = chapters; - EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); - }) - .subscribe(chaptersSubject::onNext)); + this.manga = manga; + source = sourceManager.get(manga.source); + start(DB_CHAPTERS); - // Get chapters if it's an online source - if (isCatalogueManga) { - fetchChapters(); - } - } + add(db.getChapters(manga).createObservable() + .subscribeOn(Schedulers.io()) + .doOnNext(chapters -> { + this.chapters = chapters; + EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); + for (Chapter chapter : chapters) { + setChapterStatus(chapter); + } + }) + .subscribe(chaptersSubject::onNext)); } - public void fetchChapters() { + public void fetchChaptersFromSource() { start(FETCH_CHAPTERS); } + private void refreshChapters() { + chaptersSubject.onNext(chapters); + } + private Observable getOnlineChaptersObs() { return source .pullChaptersFromNetwork(manga.url) @@ -127,8 +131,6 @@ public class ChaptersPresenter extends BasePresenter { if (onlyUnread) { observable = observable.filter(chapter -> !chapter.read); } - - observable = observable.doOnNext(this::setChapterStatus); if (onlyDownloaded) { observable = observable.filter(chapter -> chapter.status == Download.DOWNLOADED); } @@ -156,6 +158,17 @@ public class ChaptersPresenter extends BasePresenter { } } + public void updateChapterStatus(DownloadStatusEvent event) { + for (Chapter chapter : chapters) { + if (event.getChapter().id.equals(chapter.id)) { + chapter.status = event.getStatus(); + break; + } + } + if (onlyDownloaded && event.getStatus() == Download.DOWNLOADED) + refreshChapters(); + } + public void onOpenChapter(Chapter chapter) { EventBus.getDefault().postSticky(new ReaderEvent(source, manga, chapter)); } @@ -190,30 +203,33 @@ public class ChaptersPresenter extends BasePresenter { public void deleteChapters(Observable selectedChapters) { add(selectedChapters .subscribe(chapter -> { + // Somehow I can't delete files on Schedulers.io() downloadManager.deleteChapter(source, manga, chapter); + downloadManager.getQueue().remove(chapter); chapter.status = Download.NOT_DOWNLOADED; + }, error -> { + Timber.e(error.getMessage()); + }, () -> { + if (onlyDownloaded) + refreshChapters(); })); } public void revertSortOrder() { //TODO manga.chapter_order sortOrderAToZ = !sortOrderAToZ; - chaptersSubject.onNext(chapters); + refreshChapters(); } public void setReadFilter(boolean onlyUnread) { //TODO do we need save filter for manga? this.onlyUnread = onlyUnread; - chaptersSubject.onNext(chapters); + refreshChapters(); } public void setDownloadedFilter(boolean onlyDownloaded) { this.onlyDownloaded = onlyDownloaded; - chaptersSubject.onNext(chapters); - } - - public void setIsCatalogueManga(boolean value) { - isCatalogueManga = value; + refreshChapters(); } public boolean getSortOrder() {