Stop download functionality #45

Merged
akinwale merged 4 commits from stop-downloading into master 2018-03-30 13:11:53 +02:00
4 changed files with 63 additions and 2 deletions

View file

@ -9,7 +9,7 @@ import {
selectRewardContentClaimIds, selectRewardContentClaimIds,
makeSelectCostInfoForUri makeSelectCostInfoForUri
} from 'lbry-redux'; } from 'lbry-redux';
import { doDeleteFile } from '../../redux/actions/file'; import { doDeleteFile, doStopDownloadingFile } from '../../redux/actions/file';
import FilePage from './view'; import FilePage from './view';
const select = (state, props) => { const select = (state, props) => {
@ -29,6 +29,7 @@ const select = (state, props) => {
const perform = dispatch => ({ const perform = dispatch => ({
fetchFileInfo: uri => dispatch(doFetchFileInfo(uri)), fetchFileInfo: uri => dispatch(doFetchFileInfo(uri)),
fetchCostInfo: uri => dispatch(doFetchCostInfoForUri(uri)), fetchCostInfo: uri => dispatch(doFetchCostInfoForUri(uri)),
stopDownload: (uri, fileInfo) => dispatch(doStopDownloadingFile(uri, fileInfo)),
deleteFile: (fileInfo, deleteFromDevice, abandonClaim) => { deleteFile: (fileInfo, deleteFromDevice, abandonClaim) => {
dispatch(doDeleteFile(fileInfo, deleteFromDevice, abandonClaim)); dispatch(doDeleteFile(fileInfo, deleteFromDevice, abandonClaim));
}, },

View file

@ -76,6 +76,20 @@ class FilePage extends React.PureComponent {
); );
} }
onStopDownloadPressed = () => {
const { deleteFile, stopDownload, fileInfo, navigation } = this.props;
Alert.alert(
'Stop download',
'Are you sure you want to stop downloading this file?',
[
{ text: 'No' },
{ text: 'Yes', onPress: () => { stopDownload(navigation.state.params.uri, fileInfo); } }
],
{ cancelable: true }
);
}
componentWillUnmount() { componentWillUnmount() {
StatusBar.setHidden(false); StatusBar.setHidden(false);
if (NativeModules.ScreenOrientation) { if (NativeModules.ScreenOrientation) {
@ -128,7 +142,7 @@ class FilePage extends React.PureComponent {
{ showActions && { showActions &&
<View style={filePageStyle.actions}> <View style={filePageStyle.actions}>
{completed && <Button color="red" title="Delete" onPress={this.onDeletePressed} />} {completed && <Button color="red" title="Delete" onPress={this.onDeletePressed} />}
{fileInfo && !fileInfo.stopped && fileInfo.written_bytes < fileInfo.total_bytes && {!completed && fileInfo && !fileInfo.stopped && fileInfo.written_bytes < fileInfo.total_bytes &&
<Button color="red" title="Stop Download" onPress={this.onStopDownloadPressed} /> <Button color="red" title="Stop Download" onPress={this.onStopDownloadPressed} />
} }
</View>} </View>}

View file

@ -97,6 +97,30 @@ export function doStartDownload(uri, outpoint) {
}; };
} }
export function doStopDownloadingFile(uri, fileInfo) {
return dispatch => {
let params = { status: 'stop' };
if (fileInfo.sd_hash) {
params.sd_hash = fileInfo.sd_hash;
}
if (fileInfo.stream_hash) {
params.stream_hash = fileInfo.stream_hash;
}
Lbry.file_set_status(params).then(() => {
dispatch({
type: ACTIONS.DOWNLOADING_CANCELED,
data: {}
});
});
NativeModules.LbryDownloadManager.stopDownload(uri, fileInfo.file_name);
// Should also delete the file after the user stops downloading
dispatch(doDeleteFile(fileInfo.outpoint, uri));
neb-b commented 2018-03-30 00:27:58 +02:00 (Migrated from github.com)
Review

Can we keep the file half downloaded? I guess I'm not sure if this it would be valuable, but I might want to pause a large download and come back to it (if I leave wifi)

Can we keep the file half downloaded? I guess I'm not sure if this it would be valuable, but I might want to pause a large download and come back to it (if I leave wifi)
akinwale commented 2018-03-30 06:44:28 +02:00 (Migrated from github.com)
Review

Yes, I believe we can. In this case, I assume it would make sense to add a pause download button specifically for this.

Yes, I believe we can. In this case, I assume it would make sense to add a pause download button specifically for this.
};
}
export function doDownloadFile(uri, streamInfo) { export function doDownloadFile(uri, streamInfo) {
return dispatch => { return dispatch => {
dispatch(doStartDownload(uri, streamInfo.outpoint)); dispatch(doStartDownload(uri, streamInfo.outpoint));

View file

@ -75,15 +75,37 @@ public class DownloadManagerModule extends ReactContextBaseJavaModule {
NotificationCompat.Builder builder = builders.get(notificationId); NotificationCompat.Builder builder = builders.get(notificationId);
builder.setProgress(MAX_PROGRESS, new Double(progress).intValue(), false); builder.setProgress(MAX_PROGRESS, new Double(progress).intValue(), false);
builder.setContentText(String.format("%.0f%% (%s / %s)", progress, formatBytes(writtenBytes), formatBytes(totalBytes))); builder.setContentText(String.format("%.0f%% (%s / %s)", progress, formatBytes(writtenBytes), formatBytes(totalBytes)));
builder.setOngoing(true);
notificationManager.notify(notificationId, builder.build()); notificationManager.notify(notificationId, builder.build());
if (progress == MAX_PROGRESS) { if (progress == MAX_PROGRESS) {
builder.setContentTitle(String.format("Downloaded %s.", fileName)); builder.setContentTitle(String.format("Downloaded %s.", fileName));
builder.setOngoing(false);
downloadIdNotificationIdMap.remove(id); downloadIdNotificationIdMap.remove(id);
builders.remove(notificationId); builders.remove(notificationId);
} }
} }
@ReactMethod
public void stopDownload(String id, String filename) {
if (!downloadIdNotificationIdMap.containsKey(id)) {
return;
}
int notificationId = downloadIdNotificationIdMap.get(id);
if (!builders.containsKey(notificationId)) {
return;
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationCompat.Builder builder = builders.get(notificationId);
builder.setOngoing(false);
notificationManager.cancel(notificationId);
downloadIdNotificationIdMap.remove(id);
builders.remove(notificationId);
}
private String formatBytes(double bytes) private String formatBytes(double bytes)
{ {
if (bytes < 1048576) { // < 1MB if (bytes < 1048576) { // < 1MB