Update download progress in chapters fragment

This commit is contained in:
inorichi 2015-12-05 23:58:03 +01:00
parent 03ddf6d376
commit a304ccaaea
7 changed files with 67 additions and 23 deletions

View File

@ -192,7 +192,7 @@ public class DownloadManager {
// Do after download completes // Do after download completes
.doOnCompleted(() -> onDownloadCompleted(download)) .doOnCompleted(() -> onDownloadCompleted(download))
.toList() .toList()
.flatMap(pages -> Observable.just(download)) .map(pages -> download)
// If the page list threw, it will resume here // If the page list threw, it will resume here
.onErrorResumeNext(error -> { .onErrorResumeNext(error -> {
download.setStatus(Download.ERROR); download.setStatus(Download.ERROR);
@ -222,10 +222,12 @@ public class DownloadManager {
download.downloadedImages++; download.downloadedImages++;
page.setStatus(Page.READY); page.setStatus(Page.READY);
}) })
// If the download fails, mark this page as error // Mark this page as error and allow to download the remaining
.doOnError(e -> page.setStatus(Page.ERROR)) .onErrorResumeNext(e -> {
// Allow to download the remaining images page.setProgress(0);
.onErrorResumeNext(e -> Observable.just(page)); page.setStatus(Page.ERROR);
return Observable.just(page);
});
} }
// Save image on disk // Save image on disk
@ -242,7 +244,7 @@ public class DownloadManager {
}); });
} }
// Public method to get the image from the filesystem. It does NOT provide any way to download the iamge // Public method to get the image from the filesystem. It does NOT provide any way to download the image
public Observable<Page> getDownloadedImage(final Page page, File chapterDir) { public Observable<Page> getDownloadedImage(final Page page, File chapterDir) {
if (page.getImageUrl() == null) { if (page.getImageUrl() == null) {
page.setStatus(Page.ERROR); page.setStatus(Page.ERROR);

View File

@ -68,7 +68,7 @@ public class DownloadQueue {
setPagesSubject(download.pages, pageStatusSubject); setPagesSubject(download.pages, pageStatusSubject);
return pageStatusSubject return pageStatusSubject
.filter(status -> status == Page.READY) .filter(status -> status == Page.READY)
.flatMap(status -> Observable.just(download)); .map(status -> download);
} else if (download.getStatus() == Download.DOWNLOADED || } else if (download.getStatus() == Download.DOWNLOADED ||
download.getStatus() == Download.ERROR) { download.getStatus() == Download.ERROR) {

View File

@ -21,6 +21,7 @@ public class ChaptersAdapter extends FlexibleAdapter<ChaptersHolder, Chapter> {
this.fragment = fragment; this.fragment = fragment;
mItems = new ArrayList<>(); mItems = new ArrayList<>();
clickListener = (OnItemClickListener) fragment; clickListener = (OnItemClickListener) fragment;
setHasStableIds(true);
} }
@Override @Override
@ -38,6 +39,11 @@ public class ChaptersAdapter extends FlexibleAdapter<ChaptersHolder, Chapter> {
holder.onSetValues(fragment.getActivity(), chapter); holder.onSetValues(fragment.getActivity(), chapter);
} }
@Override
public long getItemId(int position) {
return mItems.get(position).id;
}
public void setItems(List<Chapter> chapters) { public void setItems(List<Chapter> chapters) {
mItems = chapters; mItems = chapters;
notifyDataSetChanged(); notifyDataSetChanged();

View File

@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.manga.chapter;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.view.ActionMode; import android.support.v7.view.ActionMode;
@ -33,6 +34,7 @@ import eu.kanade.mangafeed.ui.reader.ReaderActivity;
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;
import rx.Subscription;
@RequiresPresenter(ChaptersPresenter.class) @RequiresPresenter(ChaptersPresenter.class)
public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implements public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implements
@ -51,6 +53,8 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
private LinearLayoutManager linearLayout; private LinearLayoutManager linearLayout;
private ActionMode actionMode; private ActionMode actionMode;
private Subscription downloadProgressSubscription;
public static ChaptersFragment newInstance() { public static ChaptersFragment newInstance() {
return new ChaptersFragment(); return new ChaptersFragment();
} }
@ -105,10 +109,12 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
observeChapterDownloadProgress();
} }
@Override @Override
public void onPause() { public void onPause() {
unsubscribeChapterDownloadProgress();
super.onPause(); super.onPause();
} }
@ -169,16 +175,31 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
startActivity(intent); startActivity(intent);
} }
private void observeChapterDownloadProgress() {
downloadProgressSubscription = getPresenter().getDownloadProgressObs()
.subscribe(this::onDownloadProgressChange);
}
private void unsubscribeChapterDownloadProgress() {
if (downloadProgressSubscription != null)
downloadProgressSubscription.unsubscribe();
}
private void onDownloadProgressChange(Download download) {
ChaptersHolder holder = getHolder(download.chapter);
if (holder != null)
holder.onProgressChange(getContext(), download.downloadedImages, download.pages.size());
}
public void onChapterStatusChange(Download download) { public void onChapterStatusChange(Download download) {
Chapter chapter; ChaptersHolder holder = getHolder(download.chapter);
for (int i = linearLayout.findFirstVisibleItemPosition(); i < linearLayout.findLastVisibleItemPosition(); i++) { if (holder != null)
int pos = recyclerView.getChildAdapterPosition(linearLayout.findViewByPosition(i)); holder.onStatusChange(download.getStatus());
chapter = adapter.getItem(pos); }
if (chapter != null && download.chapter.id.equals(chapter.id)) {
adapter.notifyItemChanged(i); @Nullable
break; private ChaptersHolder getHolder(Chapter chapter) {
} return (ChaptersHolder) recyclerView.findViewHolderForItemId(chapter.id);
}
} }
@Override @Override
@ -280,7 +301,6 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
adapter.toggleSelection(position, false); adapter.toggleSelection(position, false);
int count = adapter.getSelectedItemCount(); int count = adapter.getSelectedItemCount();
if (count == 0) { if (count == 0) {
actionMode.finish(); actionMode.finish();
} else { } else {

View File

@ -62,7 +62,18 @@ public class ChaptersHolder extends RecyclerView.ViewHolder implements
pages.setText(""); pages.setText("");
} }
switch (chapter.status) { onStatusChange(chapter.status);
date.setText(sdf.format(new Date(chapter.date_upload)));
toggleActivation();
}
private void toggleActivation() {
itemView.setActivated(adapter.isSelected(getAdapterPosition()));
}
public void onStatusChange(int status) {
switch (status) {
case Download.QUEUE: case Download.QUEUE:
downloadText.setText(R.string.chapter_queued); break; downloadText.setText(R.string.chapter_queued); break;
case Download.DOWNLOADING: case Download.DOWNLOADING:
@ -74,13 +85,11 @@ public class ChaptersHolder extends RecyclerView.ViewHolder implements
default: default:
downloadText.setText(""); break; downloadText.setText(""); break;
} }
date.setText(sdf.format(new Date(chapter.date_upload)));
toggleActivation();
} }
private void toggleActivation() { public void onProgressChange(Context context, int downloaded, int total) {
itemView.setActivated(adapter.isSelected(getAdapterPosition())); downloadText.setText(context.getString(
R.string.chapter_downloading_progress, downloaded, total));
} }
@Override @Override

View File

@ -170,6 +170,12 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
refreshChapters(); refreshChapters();
} }
public Observable<Download> getDownloadProgressObs() {
return downloadManager.getQueue().getProgressObservable()
.filter(download -> download.manga.id.equals(manga.id))
.observeOn(AndroidSchedulers.mainThread());
}
public void onOpenChapter(Chapter chapter) { public void onOpenChapter(Chapter chapter) {
EventBus.getDefault().postSticky(new ReaderEvent(source, manga, chapter)); EventBus.getDefault().postSticky(new ReaderEvent(source, manga, chapter));
} }

View File

@ -90,6 +90,7 @@
<string name="chapter_downloaded">Downloaded</string> <string name="chapter_downloaded">Downloaded</string>
<string name="chapter_queued">Queued</string> <string name="chapter_queued">Queued</string>
<string name="chapter_downloading">Downloading</string> <string name="chapter_downloading">Downloading</string>
<string name="chapter_downloading_progress">Downloading (%1$d/%2$d)</string>
<string name="chapter_error">Error</string> <string name="chapter_error">Error</string>
<string name="fetch_chapters_error">Error while fetching chapters</string> <string name="fetch_chapters_error">Error while fetching chapters</string>