Initial download queue fragment. Update progress working

This commit is contained in:
inorichi
2015-11-04 18:54:07 +01:00
parent 3b9f4cb6f1
commit 999cc0df6e
15 changed files with 374 additions and 27 deletions

View File

@@ -12,11 +12,11 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import eu.kanade.mangafeed.data.models.Chapter;
import eu.kanade.mangafeed.data.models.Download;
import eu.kanade.mangafeed.data.models.DownloadQueue;
import eu.kanade.mangafeed.data.models.Manga;
import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.events.DownloadChapterEvent;
@@ -37,7 +37,9 @@ public class DownloadManager {
private PreferencesHelper preferences;
private Gson gson;
private List<Download> queue;
private DownloadQueue queue;
public static final String PAGE_LIST_FILE = "index.json";
public DownloadManager(Context context, SourceManager sourceManager, PreferencesHelper preferences) {
this.context = context;
@@ -45,7 +47,7 @@ public class DownloadManager {
this.preferences = preferences;
this.gson = new Gson();
queue = new ArrayList<>();
queue = new DownloadQueue();
initializeDownloadSubscription();
}
@@ -78,7 +80,7 @@ public class DownloadManager {
final Source source = sourceManager.get(event.getManga().source);
// If the chapter is already queued, don't add it again
for (Download download : queue) {
for (Download download : queue.get()) {
if (download.chapter.id == event.getChapter().id)
return true;
}
@@ -119,7 +121,10 @@ public class DownloadManager {
.pullPageListFromNetwork(download.chapter.url)
.subscribeOn(Schedulers.io())
// Add resulting pages to download object
.doOnNext(pages -> download.pages = pages)
.doOnNext(pages -> {
download.pages = pages;
download.setStatus(Download.DOWNLOADING);
})
// Get all the URLs to the source images, fetch pages if necessary
.flatMap(pageList -> Observable.merge(
Observable.from(pageList).filter(page -> page.getImageUrl() != null),
@@ -127,7 +132,7 @@ public class DownloadManager {
// Start downloading images, consider we can have downloaded images already
.concatMap(page -> getDownloadedImage(page, download.source, download.directory))
// Remove from the queue
.doOnCompleted(() -> removeFromQueue(download));
.doOnCompleted(() -> onChapterDownloaded(download));
}
// Get downloaded image if exists, otherwise download it with the method below
@@ -179,15 +184,15 @@ public class DownloadManager {
return imagePath.exists() && !imagePath.isDirectory();
}
private void removeFromQueue(final Download download) {
private void onChapterDownloaded(final Download download) {
download.setStatus(Download.DOWNLOADED);
savePageList(download.source, download.manga, download.chapter, download.pages);
queue.remove(download);
}
// Return the page list from the chapter's directory if it exists, null otherwise
public List<Page> getSavedPageList(Source source, Manga manga, Chapter chapter) {
File chapterDir = getAbsoluteChapterDirectory(source, manga, chapter);
File pagesFile = new File(chapterDir, "index.json");
File pagesFile = new File(chapterDir, PAGE_LIST_FILE);
try {
JsonReader reader = new JsonReader(new FileReader(pagesFile.getAbsolutePath()));
@@ -202,7 +207,7 @@ public class DownloadManager {
// Save the page list to the chapter's directory
public void savePageList(Source source, Manga manga, Chapter chapter, List<Page> pages) {
File chapterDir = getAbsoluteChapterDirectory(source, manga, chapter);
File pagesFile = new File(chapterDir, "index.json");
File pagesFile = new File(chapterDir, PAGE_LIST_FILE);
FileOutputStream out;
try {
@@ -230,4 +235,8 @@ public class DownloadManager {
File path = getAbsoluteChapterDirectory(source, manga, chapter);
DiskUtils.deleteFiles(path);
}
public DownloadQueue getQueue() {
return queue;
}
}

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.util.List;
import eu.kanade.mangafeed.sources.base.Source;
import rx.subjects.PublishSubject;
public class Download {
public Source source;
@@ -12,9 +13,38 @@ public class Download {
public List<Page> pages;
public File directory;
public transient volatile int totalProgress;
private transient volatile int status;
private transient PublishSubject<Download> statusSubject;
public static final int QUEUE = 0;
public static final int DOWNLOADING = 1;
public static final int DOWNLOADED = 2;
public static final int ERROR = 3;
public Download(Source source, Manga manga, Chapter chapter) {
this.source = source;
this.manga = manga;
this.chapter = chapter;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
notifyStatus();
}
public void setStatusSubject(PublishSubject<Download> subject) {
this.statusSubject = subject;
}
private void notifyStatus() {
if (statusSubject != null)
statusSubject.onNext(this);
}
}

View File

@@ -0,0 +1,43 @@
package eu.kanade.mangafeed.data.models;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.subjects.PublishSubject;
public class DownloadQueue {
private List<Download> queue;
private PublishSubject<Download> statusSubject;
public DownloadQueue() {
queue = new ArrayList<>();
statusSubject = PublishSubject.create();
}
public void add(Download download) {
download.setStatusSubject(statusSubject);
queue.add(download);
}
public void remove(Download download) {
queue.remove(download);
download.setStatusSubject(null);
}
public List<Download> get() {
return queue;
}
public Observable<Download> getActiveDownloads() {
return Observable.from(queue)
.filter(download -> download.getStatus() == Download.DOWNLOADING);
}
public Observable<Download> getStatusObservable() {
return statusSubject
.startWith(getActiveDownloads());
}
}

View File

@@ -9,8 +9,8 @@ public class Page implements NetworkHelper.ProgressListener {
private String url;
private String imageUrl;
private String imagePath;
private transient int status;
private transient int progress;
private transient volatile int status;
private transient volatile int progress;
private transient BehaviorSubject<Integer> statusSubject;