Skip to content

Commit a6e8abe

Browse files
committed
downloader: add missing resolve when iOS cancels picker copy
code cleanup
1 parent 0ec1b5b commit a6e8abe

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

packages/nativescript-downloader/index.android.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export class Downloader extends DownloaderCommon {
5656
// android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
5757
req.setNotificationVisibility(notification ? android.app.DownloadManager.Request.VISIBILITY_VISIBLE : android.app.DownloadManager.Request.VISIBILITY_HIDDEN);
5858

59-
// req.allowScanningByMediaScanner();//deprecated, files saved outside app directory will be scanned automatically
60-
6159
//if we don't set the title, the server may provide a valid filename, or it may provide the url with whatever querystring it has
6260
req.setDescription(destinationFilename);
6361
req.setTitle(destinationFilename);
@@ -117,7 +115,7 @@ export class Downloader extends DownloaderCommon {
117115
if (fdelete.delete()) {
118116
// console.log('file Deleted :' + uri.getPath());
119117
} else {
120-
// console.log('file not Deleted :' + uri.getPath());
118+
// console.warn('file not Deleted :' + uri.getPath());
121119
}
122120
}
123121

@@ -215,6 +213,7 @@ export class Downloader extends DownloaderCommon {
215213
outputStream.close();
216214
}
217215
}
216+
218217
//return the user-accessible downloaded file path to user
219218
const downloadedFile = File.fromPath(outputpath);
220219
emit(DownloaderCommon.DOWNLOAD_COMPLETE, { filepath: outputpath });
@@ -335,7 +334,7 @@ function getActivity(): android.app.Activity {
335334
return Application.android.foregroundActivity || Application.android.startActivity;
336335
}
337336

338-
// Not sure why DocumentsContact is not defined in Android types yet?
337+
// Not sure why DocumentsContact is not defined in NS Android types yet?
339338
type ProviderWithDocumentsContact = typeof android.provider & {
340339
DocumentsContract: any;
341340
};

packages/nativescript-downloader/index.ios.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ export class Downloader extends DownloaderCommon {
8989
if (!this.handle.seekToEndReturningOffsetError(written)) {
9090
emit(DownloaderCommon.DOWNLOAD_ERROR, { error: 'Error seeking end of file' });
9191
return reject();
92-
// throw new Error('Error seeking end of file');
9392
}
9493
if (!this.handle.writeDataError(data)) {
95-
// throw new Error('Error writing data');
9694
emit(DownloaderCommon.DOWNLOAD_ERROR, { error: 'Error writing data' });
9795
return reject();
9896
}
@@ -219,14 +217,13 @@ export class Downloader extends DownloaderCommon {
219217
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(iosurl);
220218
} else if (isImage) {
221219
PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(iosurl);
222-
}
223-
// else console.log('neither a video or image, not saving to gallery');
220+
} else console.warn('not a recognized video or image file extension, not saving to gallery');
224221
},
225222
(success, err) => {
226223
if (success) {
227224
// console.log('success');
228225
} else {
229-
// console.log('failed');
226+
console.warn('Unable to copy into Photos gallery', err);
230227
}
231228
resolve(downloadedFile);
232229
}
@@ -298,7 +295,7 @@ class UIDocumentPickerDelegateImpl extends NSObject implements UIDocumentPickerD
298295
return this._owner.get();
299296
}
300297

301-
//this shouldn't be called, but we'll have the same handler code just in case
298+
//for a directory picker, this should never be called, but we'll include the code anyway
302299
documentPickerDidPickDocumentAtURL(controller: UIDocumentPickerViewController, url: NSURL): void {
303300
const access = url.startAccessingSecurityScopedResource();
304301
let copypath = url.path + '/' + this._downloadFilename;
@@ -314,20 +311,16 @@ class UIDocumentPickerDelegateImpl extends NSObject implements UIDocumentPickerD
314311
if (!NSFileManager.defaultManager.fileExistsAtPath(copypath)) break;
315312
}
316313
}
317-
const suc = NSFileManager.defaultManager.copyItemAtPathToPathError(this._downloadPath, copypath);
314+
NSFileManager.defaultManager.copyItemAtPathToPathError(this._downloadPath, copypath);
318315
if (access) url.stopAccessingSecurityScopedResource();
319316
const downloadedFile = File.fromPath(this._downloadPath);
320317
this._resolve(downloadedFile);
321318
controller.dismissViewControllerAnimatedCompletion(true, null);
322319
this.deRegisterFromGlobal();
323320
}
324321

325-
//if multiple selections allowed:
326322
documentPickerDidPickDocumentsAtURLs(controller: UIDocumentPickerViewController, urls: NSArray<NSURL>): void {
327-
const files: File[] = [];
328-
//This view can't display an UIActivityIndicatorView inside it using the usual ios spinner approach,
329-
// but picker shows a small spinner on the "Open" button while processing
330-
//Process picker results
323+
//we should only get one folder, but will loop through just in case and just return the first one as destination
331324
for (let i = 0; i < urls.count; i++) {
332325
const url = urls.objectAtIndex(i); //urls[0];
333326
const access = url.startAccessingSecurityScopedResource();
@@ -337,15 +330,14 @@ class UIDocumentPickerDelegateImpl extends NSObject implements UIDocumentPickerD
337330
const fileName = fileParts[fileParts.length - 1];
338331
const filePrefix = fileName.split('.', 2).length > 0 ? fileName.split('.', 2)[0] : null;
339332
const fileSuffix = fileName.split('.', 2).length > 0 ? '.' + fileName.split('.', 2)[1] : null;
340-
// let tempFileName = 'dl-' + generateId() + fileSuffix;
341333
let tempFileName;
342334
for (let i = 1; i < 999999999; i++) {
343335
tempFileName = filePrefix + '-' + i + fileSuffix;
344336
copypath = copypath.replace(/\/[^/]+$/, `/${tempFileName}`);
345337
if (!NSFileManager.defaultManager.fileExistsAtPath(copypath)) break;
346338
}
347339
}
348-
const suc = NSFileManager.defaultManager.copyItemAtPathToPathError(this._downloadPath, copypath);
340+
NSFileManager.defaultManager.copyItemAtPathToPathError(this._downloadPath, copypath);
349341
if (access) url.stopAccessingSecurityScopedResource();
350342
const downloadedFile = File.fromPath(this._downloadPath);
351343
this._resolve(downloadedFile);
@@ -357,5 +349,8 @@ class UIDocumentPickerDelegateImpl extends NSObject implements UIDocumentPickerD
357349
documentPickerWasCancelled(controller: UIDocumentPickerViewController): void {
358350
controller.dismissViewControllerAnimatedCompletion(true, null);
359351
this.deRegisterFromGlobal();
352+
//user canceled the extra copy, just resolve the downloaded file
353+
const downloadedFile = File.fromPath(this._downloadPath);
354+
this._resolve(downloadedFile);
360355
}
361356
}

0 commit comments

Comments
 (0)