From 03ddf6d376b3e8bf22e964d9daf9efdcfca197d7 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 5 Dec 2015 18:22:29 +0100 Subject: [PATCH] Add an observable that notifies of every download progress --- .../data/download/DownloadManager.java | 2 +- .../data/download/model/DownloadQueue.java | 30 +++++++++++++++++++ .../ui/manga/info/MangaInfoFragment.java | 1 - 3 files changed, 31 insertions(+), 2 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 47009bd0e3..5b1825084b 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 @@ -219,8 +219,8 @@ public class DownloadManager { .doOnNext(p -> { page.setImagePath(imagePath.getAbsolutePath()); page.setProgress(100); - page.setStatus(Page.READY); download.downloadedImages++; + page.setStatus(Page.READY); }) // If the download fails, mark this page as error .doOnError(e -> page.setStatus(Page.ERROR)) 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 02a5754ba7..33d6090b88 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 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import eu.kanade.mangafeed.data.database.models.Chapter; +import eu.kanade.mangafeed.data.source.model.Page; import rx.Observable; import rx.subjects.PublishSubject; @@ -58,4 +59,33 @@ public class DownloadQueue { return statusSubject; } + public Observable getProgressObservable() { + return statusSubject + .startWith(getActiveDownloads()) + .flatMap(download -> { + if (download.getStatus() == Download.DOWNLOADING) { + PublishSubject pageStatusSubject = PublishSubject.create(); + setPagesSubject(download.pages, pageStatusSubject); + return pageStatusSubject + .filter(status -> status == Page.READY) + .flatMap(status -> Observable.just(download)); + + } else if (download.getStatus() == Download.DOWNLOADED || + download.getStatus() == Download.ERROR) { + + setPagesSubject(download.pages, null); + } + return Observable.just(download); + }) + .filter(download -> download.getStatus() == Download.DOWNLOADING); + } + + private void setPagesSubject(List pages, PublishSubject subject) { + if (pages != null) { + for (Page page : pages) { + page.setStatusSubject(subject); + } + } + } + } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/manga/info/MangaInfoFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/manga/info/MangaInfoFragment.java index 3ed89af97d..b456ecb08c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/manga/info/MangaInfoFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/manga/info/MangaInfoFragment.java @@ -86,7 +86,6 @@ public class MangaInfoFragment extends BaseRxFragment { } else { coverCache.loadFromNetwork(cover, manga.thumbnail_url, headers); } - cover.setTag(manga.thumbnail_url); } }