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
.doOnCompleted(() -> onDownloadCompleted(download))
.toList()
.flatMap(pages -> Observable.just(download))
.map(pages -> download)
// If the page list threw, it will resume here
.onErrorResumeNext(error -> {
download.setStatus(Download.ERROR);
@ -222,10 +222,12 @@ public class DownloadManager {
download.downloadedImages++;
page.setStatus(Page.READY);
})
// If the download fails, mark this page as error
.doOnError(e -> page.setStatus(Page.ERROR))
// Allow to download the remaining images
.onErrorResumeNext(e -> Observable.just(page));
// Mark this page as error and allow to download the remaining
.onErrorResumeNext(e -> {
page.setProgress(0);
page.setStatus(Page.ERROR);
return Observable.just(page);
});
}
// 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) {
if (page.getImageUrl() == null) {
page.setStatus(Page.ERROR);

View File

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

View File

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

View File

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

View File

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

View File

@ -170,6 +170,12 @@ public class ChaptersPresenter extends BasePresenter<ChaptersFragment> {
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) {
EventBus.getDefault().postSticky(new ReaderEvent(source, manga, chapter));
}

View File

@ -90,6 +90,7 @@
<string name="chapter_downloaded">Downloaded</string>
<string name="chapter_queued">Queued</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="fetch_chapters_error">Error while fetching chapters</string>