Use download queue subject for updating chapter status

This commit is contained in:
inorichi 2015-12-03 17:12:43 +01:00
parent 384acb2322
commit 976f010d64
6 changed files with 30 additions and 58 deletions

View file

@ -3,12 +3,10 @@ package eu.kanade.mangafeed.data.download.model;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import de.greenrobot.event.EventBus;
import eu.kanade.mangafeed.data.database.models.Chapter; import eu.kanade.mangafeed.data.database.models.Chapter;
import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.source.base.Source; import eu.kanade.mangafeed.data.source.base.Source;
import eu.kanade.mangafeed.data.source.model.Page; import eu.kanade.mangafeed.data.source.model.Page;
import eu.kanade.mangafeed.event.DownloadStatusEvent;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
public class Download { public class Download {
@ -53,6 +51,5 @@ public class Download {
private void notifyStatus() { private void notifyStatus() {
if (statusSubject != null) if (statusSubject != null)
statusSubject.onNext(this); statusSubject.onNext(this);
EventBus.getDefault().post(new DownloadStatusEvent(chapter, status));
} }
} }

View file

@ -56,8 +56,7 @@ public class DownloadQueue {
} }
public Observable<Download> getStatusObservable() { public Observable<Download> getStatusObservable() {
return statusSubject return statusSubject;
.startWith(getActiveDownloads());
} }
} }

View file

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

View file

@ -52,7 +52,7 @@ public class DownloadPresenter extends BasePresenter<DownloadFragment> {
super.onTakeView(view); super.onTakeView(view);
add(statusSubscription = downloadQueue.getStatusObservable() add(statusSubscription = downloadQueue.getStatusObservable()
.subscribeOn(Schedulers.io()) .startWith(downloadQueue.getActiveDownloads())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(download -> { .subscribe(download -> {
processStatus(download, view); processStatus(download, view);

View file

@ -23,15 +23,13 @@ import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Chapter; 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.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.activity.BaseActivity;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment; import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration; import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration;
import eu.kanade.mangafeed.ui.manga.MangaActivity; import eu.kanade.mangafeed.ui.manga.MangaActivity;
import eu.kanade.mangafeed.ui.reader.ReaderActivity; import eu.kanade.mangafeed.ui.reader.ReaderActivity;
import eu.kanade.mangafeed.util.EventBusHook;
import eu.kanade.mangafeed.util.ToastUtil; import eu.kanade.mangafeed.util.ToastUtil;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
import rx.Observable; import rx.Observable;
@ -107,12 +105,10 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
registerForEvents();
} }
@Override @Override
public void onPause() { public void onPause() {
unregisterForEvents();
super.onPause(); super.onPause();
} }
@ -173,20 +169,12 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
startActivity(intent); startActivity(intent);
} }
@EventBusHook public void onChapterStatusChange(Download download) {
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);
Chapter chapter; Chapter chapter;
for (int i = linearLayout.findFirstVisibleItemPosition(); i < linearLayout.findLastVisibleItemPosition(); i++) { for (int i = linearLayout.findFirstVisibleItemPosition(); i < linearLayout.findLastVisibleItemPosition(); i++) {
int pos = recyclerView.getChildAdapterPosition(linearLayout.findViewByPosition(i)); int pos = recyclerView.getChildAdapterPosition(linearLayout.findViewByPosition(i));
chapter = adapter.getItem(pos); chapter = adapter.getItem(pos);
if (event.getChapter().id.equals(chapter.id)) { if (chapter != null && download.chapter.id.equals(chapter.id)) {
adapter.notifyItemChanged(i); adapter.notifyItemChanged(i);
break; break;
} }

View file

@ -17,7 +17,6 @@ import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.data.source.base.Source; import eu.kanade.mangafeed.data.source.base.Source;
import eu.kanade.mangafeed.event.ChapterCountEvent; import eu.kanade.mangafeed.event.ChapterCountEvent;
import eu.kanade.mangafeed.event.DownloadChaptersEvent; import eu.kanade.mangafeed.event.DownloadChaptersEvent;
import eu.kanade.mangafeed.event.DownloadStatusEvent;
import eu.kanade.mangafeed.event.ReaderEvent; import eu.kanade.mangafeed.event.ReaderEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter; import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import eu.kanade.mangafeed.util.EventBusHook; import eu.kanade.mangafeed.util.EventBusHook;
@ -47,6 +46,7 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
private static final int DB_CHAPTERS = 1; private static final int DB_CHAPTERS = 1;
private static final int FETCH_CHAPTERS = 2; private static final int FETCH_CHAPTERS = 2;
private static final int CHAPTER_STATUS_CHANGES = 3;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -64,6 +64,12 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
(view, result) -> view.onFetchChaptersDone(), (view, result) -> view.onFetchChaptersDone(),
(view, error) -> view.onFetchChaptersError() (view, error) -> view.onFetchChaptersError()
); );
restartableLatestCache(CHAPTER_STATUS_CHANGES,
this::getChapterStatusObs,
(view, download) -> view.onChapterStatusChange(download),
(view, error) -> Timber.e(error.getCause(), error.getMessage())
);
} }
@Override @Override
@ -96,16 +102,19 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
add(db.getChapters(manga).createObservable() add(db.getChapters(manga).createObservable()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.doOnNext(chapters -> { .doOnNext(chapters -> {
stop(CHAPTER_STATUS_CHANGES);
this.chapters = chapters; this.chapters = chapters;
EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size())); EventBus.getDefault().postSticky(new ChapterCountEvent(chapters.size()));
for (Chapter chapter : chapters) { for (Chapter chapter : chapters) {
setChapterStatus(chapter); setChapterStatus(chapter);
} }
start(CHAPTER_STATUS_CHANGES);
}) })
.subscribe(chaptersSubject::onNext)); .subscribe(chaptersSubject::onNext));
} }
public void fetchChaptersFromSource() { public void fetchChaptersFromSource() {
hasRequested = true;
start(FETCH_CHAPTERS); start(FETCH_CHAPTERS);
} }
@ -118,8 +127,7 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
.pullChaptersFromNetwork(manga.url) .pullChaptersFromNetwork(manga.url)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters)) .flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread());
.doOnNext(r -> hasRequested = true);
} }
private Observable<List<Chapter>> getDbChaptersObs() { private Observable<List<Chapter>> getDbChaptersObs() {
@ -137,13 +145,9 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
if (onlyDownloaded) { if (onlyDownloaded) {
observable = observable.filter(chapter -> chapter.status == Download.DOWNLOADED); observable = observable.filter(chapter -> chapter.status == Download.DOWNLOADED);
} }
return observable.toSortedList((chapter, chapter2) -> { return observable.toSortedList((chapter, chapter2) -> sortOrderAToZ ?
if (sortOrderAToZ) { Float.compare(chapter.chapter_number, chapter2.chapter_number) :
return Float.compare(chapter.chapter_number, chapter2.chapter_number); Float.compare(chapter2.chapter_number, chapter.chapter_number));
} else {
return Float.compare(chapter2.chapter_number, chapter.chapter_number);
}
});
} }
private void setChapterStatus(Chapter chapter) { private void setChapterStatus(Chapter chapter) {
@ -161,14 +165,21 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
} }
} }
public void updateChapterStatus(DownloadStatusEvent event) { private Observable<Download> 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) { for (Chapter chapter : chapters) {
if (event.getChapter().id.equals(chapter.id)) { if (download.chapter.id.equals(chapter.id)) {
chapter.status = event.getStatus(); chapter.status = download.getStatus();
break; break;
} }
} }
if (onlyDownloaded && event.getStatus() == Download.DOWNLOADED) if (onlyDownloaded && download.getStatus() == Download.DOWNLOADED)
refreshChapters(); refreshChapters();
} }