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

View file

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

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);
add(statusSubscription = downloadQueue.getStatusObservable()
.subscribeOn(Schedulers.io())
.startWith(downloadQueue.getActiveDownloads())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(download -> {
processStatus(download, view);

View file

@ -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<ChaptersPresenter> implemen
@Override
public void onResume() {
super.onResume();
registerForEvents();
}
@Override
public void onPause() {
unregisterForEvents();
super.onPause();
}
@ -173,20 +169,12 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> 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;
}

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.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<ChaptersFragment> {
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<ChaptersFragment> {
(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<ChaptersFragment> {
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<ChaptersFragment> {
.pullChaptersFromNetwork(manga.url)
.subscribeOn(Schedulers.io())
.flatMap(chapters -> db.insertOrRemoveChapters(manga, chapters))
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(r -> hasRequested = true);
.observeOn(AndroidSchedulers.mainThread());
}
private Observable<List<Chapter>> getDbChaptersObs() {
@ -137,13 +145,9 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
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<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) {
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();
}