diff --git a/README.md b/README.md index abe853c..b57ad0c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ # Background Upload plugin for the NativeScript framework -[**How to use the plugin, see: source/README.md**](nativescript-background-http/) - ## iOS The iOS API is implemented in JavaScript. @@ -8,30 +6,19 @@ The iOS API is implemented in JavaScript. The minimum supported API level is 18 and the background file upload is handled by the [android-upload-service](https://github.com/alexbbb/android-upload-service) Open-Source library. ## Examples -To run the example open a terminal and run in the root of the repo: -``` -npm install -``` -This will start the server included in `examples/www`: +To run the demo open a terminal at the root of the repo and start the server included in `demo-server` with the command: ``` -npm start +npm run start-server ``` -Open a second terminal, again at the root of the repo. -This will create a link from the nativescript-background-http to the examples/SimpleBackgroundHttp's node_modules so you can build and run the plugin from source: +Then, open a second terminal, again at the root of the repo and execute (for Android): ``` -npm run link +npm run start-demo-android ``` - -To compile the TypeScript of the plugin: -``` -npm run tsc -``` - -Then to run the app: +OR (for iOS) ``` -tns run android --path examples/SimpleBackgroundHttp +npm run start-demo-ios ``` ## Usage diff --git a/demo/app/home-view-model.ts b/demo/app/home-view-model.ts index 03ee897..ab8ca2d 100644 --- a/demo/app/home-view-model.ts +++ b/demo/app/home-view-model.ts @@ -54,7 +54,6 @@ export class HomeViewModel extends Observable { "File-Name": name }, description: description, - androidDisplayNotificationProgress: false, androidAutoDeleteAfterUpload: false }; diff --git a/src/background-http.android.ts b/src/background-http.android.ts index 5200ade..114f48c 100644 --- a/src/background-http.android.ts +++ b/src/background-http.android.ts @@ -5,12 +5,14 @@ import * as fileSystemModule from "file-system"; import * as common from "./index"; declare const net: any; +net.gotev.uploadservice.UploadService.NAMESPACE = application.android.packageName; interface UploadInfo { getUploadId(): string; getTotalBytes(): number; getUploadedBytes(): number; } + interface ServerResponse { getBodyAsString(): string; } @@ -19,55 +21,55 @@ interface ServerResponse { let ProgressReceiver: any; function initializeProgressReceiver() { -if (ProgressReceiver) { - return; -} + if (ProgressReceiver) { + return; + } -const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({ - onProgress(uploadInfo: UploadInfo) { - const uploadId = uploadInfo.getUploadId(); - const task = Task.fromId(uploadId); - const totalBytes = uploadInfo.getTotalBytes(); - const currentBytes = uploadInfo.getUploadedBytes(); - task.setTotalUpload(totalBytes); - task.setUpload(currentBytes); - task.setStatus("uploading"); - task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes }); - }, - - onCancelled(uploadInfo: UploadInfo) { - const uploadId = uploadInfo.getUploadId(); - const task = Task.fromId(uploadId); - task.setStatus("cancelled"); - task.notify({ eventName: "cancelled", object: task }); - }, - - onError(uploadInfo: UploadInfo, error) { - const uploadId = uploadInfo.getUploadId(); - const task = Task.fromId(uploadId); - task.setStatus("error"); - task.notify({ eventName: "error", object: task, error: error }); - }, - - onCompleted(uploadInfo: UploadInfo, serverResponse: ServerResponse) { - const uploadId = uploadInfo.getUploadId(); - const task = Task.fromId(uploadId); - - let totalUpload = uploadInfo.getTotalBytes(); - if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) { - totalUpload = 1; - } - task.setUpload(totalUpload); - task.setTotalUpload(totalUpload); - task.setStatus("complete"); + const ProgressReceiverImpl = net.gotev.uploadservice.UploadServiceBroadcastReceiver.extend({ + onProgress(context: any, uploadInfo: UploadInfo) { + const uploadId = uploadInfo.getUploadId(); + const task = Task.fromId(uploadId); + const totalBytes = uploadInfo.getTotalBytes(); + const currentBytes = uploadInfo.getUploadedBytes(); + task.setTotalUpload(totalBytes); + task.setUpload(currentBytes); + task.setStatus("uploading"); + task.notify({ eventName: "progress", object: task, currentBytes: currentBytes, totalBytes: totalBytes }); + }, + + onCancelled(context: any, uploadInfo: UploadInfo) { + const uploadId = uploadInfo.getUploadId(); + const task = Task.fromId(uploadId); + task.setStatus("cancelled"); + task.notify({ eventName: "cancelled", object: task }); + }, + + onError(context: any, uploadInfo: UploadInfo, error) { + const uploadId = uploadInfo.getUploadId(); + const task = Task.fromId(uploadId); + task.setStatus("error"); + task.notify({ eventName: "error", object: task, error: error }); + }, + + onCompleted(context: any, uploadInfo: UploadInfo, serverResponse: ServerResponse) { + const uploadId = uploadInfo.getUploadId(); + const task = Task.fromId(uploadId); + + let totalUpload = uploadInfo.getTotalBytes(); + if (!totalUpload || !isFinite(totalUpload) || totalUpload <= 0) { + totalUpload = 1; + } + task.setUpload(totalUpload); + task.setTotalUpload(totalUpload); + task.setStatus("complete"); - task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload }); - task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() }); - task.notify({ eventName: "complete", object: task, response: serverResponse }); - } -}); + task.notify({ eventName: "progress", object: task, currentBytes: totalUpload, totalBytes: totalUpload }); + task.notify({ eventName: "responded", object: task, data: serverResponse.getBodyAsString() }); + task.notify({ eventName: "complete", object: task, response: serverResponse }); + } + }); -ProgressReceiver = ProgressReceiverImpl as any; + ProgressReceiver = ProgressReceiverImpl as any; } /* ProgressReceiver END */ @@ -140,7 +142,7 @@ class Task extends ObservableBase { request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig()); } const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false; - if(autoDeleteAfterUpload) { + if (autoDeleteAfterUpload) { request.setAutoDeleteFilesAfterSuccessfulUpload(true); } @@ -191,7 +193,7 @@ class Task extends ObservableBase { fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/"); } - const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/')+1, fileName.length); + const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length); request.addFileToUpload(fileName, curParam.name, destFileName, curParam.mimeType); } else { request.addParameter(params[i].name, params[i].value); @@ -207,7 +209,7 @@ class Task extends ObservableBase { const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true; if (displayNotificationProgress) { - request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig()); + request.setNotificationConfig(new net.gotev.uploadservice.UploadNotificationConfig()); } const headers = options.headers; @@ -281,7 +283,4 @@ class Task extends ObservableBase { cancel(): void { (net).gotev.uploadservice.UploadService.stopUpload(this._id); } -} - - - +} \ No newline at end of file diff --git a/src/platforms/android/include.gradle b/src/platforms/android/include.gradle index ca3f89e..2931fa0 100644 --- a/src/platforms/android/include.gradle +++ b/src/platforms/android/include.gradle @@ -9,5 +9,5 @@ android { //optional elements dependencies { - compile 'net.gotev:uploadservice:3.0.3' + compile 'net.gotev:uploadservice:3.4.2' } \ No newline at end of file