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,22 +150,15 @@ public class DownloadFragment extends Fragment {
return arg0;
}
public void updateProgress(int progress, int notificationID) {
if (isAdded()) {
public void complete(int notificationID) {
int position = Arrays.asList(mKeys).indexOf(notificationID);
ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition());
if (viewGroup == null) {
if (progress == 100) {
mDownloads.remove(mKeys[position]);
mDownloadFiles.remove(mKeys[position]);
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
return;
}
ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress);
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]));
@ -185,6 +178,16 @@ public class DownloadFragment extends Fragment {
downloadAdapter.notifyDataSetChanged();
updateNoDownloads();
}
public void updateProgress(int progress, int notificationID) {
if (isAdded()) {
int position = Arrays.asList(mKeys).indexOf(notificationID);
ViewGroup viewGroup = (ViewGroup) listView.getChildAt(position - listView.getFirstVisiblePosition());
if (viewGroup == null) {
return;
}
ProgressBar downloadProgress = (ProgressBar) viewGroup.findViewById(R.id.downloadProgress);
downloadProgress.setProgress(progress);
TextView timeRemaining = (TextView) viewGroup.findViewById(R.id.time_remaining);
int secLeft = LibraryFragment.mService.timeRemaining.get(mKeys[position], -1);
if (secLeft != -1)

View File

@ -273,7 +273,7 @@ public class DownloadService extends Service {
.flatMap(metaLink -> getMetaLinkContentLength(metaLink.getRelevantUrl().getValue()))
.flatMap(pair -> Observable.from(ChunkUtils.getChunks(pair.first, pair.second, notificationID)))
.concatMap(this::downloadChunk)
.distinctUntilChanged()
.distinctUntilChanged().doOnCompleted(() -> {updateDownloadFragmentComplete(notificationID);})
.subscribe(progress -> {
if (progress == 100) {
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() {
// Allow notification to be dismissible while ensuring integrity of service if active downloads
stopForeground(true);