Move download completion logic to onComplete subscription

This commit is contained in:
mhutti1 2017-10-05 14:23:47 +01:00 committed by Isaac Hutt
parent d214ff9450
commit ebf995b3f7
2 changed files with 43 additions and 27 deletions

View File

@ -150,41 +150,44 @@ public class DownloadFragment extends Fragment {
return arg0; return arg0;
} }
public void complete(int notificationID) {
int position = Arrays.asList(mKeys).indexOf(notificationID);
ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition());
if (viewGroup == null) {
mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause);
pause.setEnabled(false);
String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position]));
{
Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG);
completeSnack.setAction(getResources().getString(R.string.open), new View.OnClickListener() {
@Override
public void onClick(View v) {
ZimFileSelectFragment.finishResult(fileName);
}
}).setActionTextColor(getResources().getColor(R.color.white)).show();
}
ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0);
zimFileSelectFragment.addBook(fileName);
mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
public void updateProgress(int progress, int notificationID) { public void updateProgress(int progress, int notificationID) {
if (isAdded()) { if (isAdded()) {
int position = Arrays.asList(mKeys).indexOf(notificationID); int position = Arrays.asList(mKeys).indexOf(notificationID);
ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition()); ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition());
if (viewGroup == null) { if (viewGroup == null) {
if (progress == 100) {
mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
return; return;
} }
ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress); ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress);
downloadProgress.setProgress(progress); downloadProgress.setProgress(progress);
if (progress == 100) {
ImageView pause = (ImageView) viewGroup.findViewById(R.id.pause);
pause.setEnabled(false);
String fileName = FileUtils.getFileName(mDownloadFiles.get(mKeys[position]));
{
Snackbar completeSnack = Snackbar.make(mainLayout, getResources().getString(R.string.download_complete_snackbar), Snackbar.LENGTH_LONG);
completeSnack.setAction(getResources().getString(R.string.open), new View.OnClickListener() {
@Override
public void onClick(View v) {
ZimFileSelectFragment.finishResult(fileName);
}
}).setActionTextColor(getResources().getColor(R.color.white)).show();
}
ZimFileSelectFragment zimFileSelectFragment = (ZimFileSelectFragment) zimManageActivity.mSectionsPagerAdapter.getItem(0);
zimFileSelectFragment.addBook(fileName);
mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining); TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining);
int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1); int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1);
if (secLeft != -1) if (secLeft != -1)

View File

@ -273,7 +273,7 @@ public class DownloadService extends Service {
.flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue())) .flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue()))
.flatMap(pair -> Observable.from(ChunkUtils.getChunks(pair.first, pair.second, notificationID))) .flatMap(pair -> Observable.from(ChunkUtils.getChunks(pair.first, pair.second, notificationID)))
.concatMap(this::downloadChunk) .concatMap(this::downloadChunk)
.distinctUntilChanged() .distinctUntilChanged().doOnCompleted(() -> {updateDownloadFragmentComplete(notificationID);})
.subscribe(progress -> { .subscribe(progress -> {
if (progress == 100) { if (progress == 100) {
notification.get(notificationID).setOngoing(false); notification.get(notificationID).setOngoing(false);
@ -319,6 +319,19 @@ public class DownloadService extends Service {
} }
} }
private void updateDownloadFragmentComplete(int notificationID) {
if (DownloadFragment.mDownloads != null && DownloadFragment.mDownloads.get(notificationID) != null) {
handler.post(new Runnable() {
@Override
public void run() {
if (DownloadFragment.mDownloads.get(notificationID) != null) {
DownloadFragment.downloadAdapter.complete(notificationID);
}
}
});
}
}
private void updateForeground() { private void updateForeground() {
// Allow notification to be dismissible while ensuring integrity of service if active downloads // Allow notification to be dismissible while ensuring integrity of service if active downloads
stopForeground(true); stopForeground(true);