Skip to content

Commit

Permalink
Merge pull request #166 from NativeScript/tbozhikov/refactor-and-bump
Browse files Browse the repository at this point in the history
refactor: extract some code into functions, bump version
  • Loading branch information
tbozhikov authored Jul 26, 2018
2 parents d207292 + c0c19f8 commit 37a1e60
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 143 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
"typescript": "~2.7.2",
"tslint": "~5.4.3"
}
}
}
258 changes: 117 additions & 141 deletions src/background-http.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function onProgressReceiverProgress(context: Context, uploadInfo: UploadInfo) {
task.setUpload(currentBytes);
task.setStatus("uploading");
task.notify(<common.ProgressEventData>{
eventName: "progress",
object: task,
currentBytes: currentBytes,
totalBytes: totalBytes
eventName: "progress",
object: task,
currentBytes: currentBytes,
totalBytes: totalBytes
});
}

Expand All @@ -39,11 +39,11 @@ function onProgressReceiverError(context: Context, uploadInfo: UploadInfo, respo
const task = Task.fromId(uploadId);
task.setStatus("error");
task.notify(<common.ErrorEventData>{
eventName: "error",
object: task,
error,
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
response
eventName: "error",
object: task,
error,
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
response
});
}

Expand All @@ -60,22 +60,22 @@ function onProgressReceiverCompleted(context: Context, uploadInfo: UploadInfo, r
task.setStatus("complete");

task.notify(<common.ProgressEventData>{
eventName: "progress",
object: task,
currentBytes: totalUpload,
totalBytes: totalUpload
eventName: "progress",
object: task,
currentBytes: totalUpload,
totalBytes: totalUpload
});
task.notify(<common.ResultEventData>{
eventName: "responded",
object: task,
data: response.getBodyAsString(),
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1
eventName: "responded",
object: task,
data: response.getBodyAsString(),
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1
});
task.notify(<common.CompleteEventData>{
eventName: "complete",
object: task,
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
response
eventName: "complete",
object: task,
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
response
});
}

Expand All @@ -90,21 +90,21 @@ function initializeProgressReceiver() {
const zonedOnCompleted = global.zonedCallback(onProgressReceiverCompleted);

const temp: Partial<UploadServiceBroadcastReceiver> = {
onProgress(context: Context, uploadInfo: UploadInfo) {
zonedOnProgress(context, uploadInfo);
},
onProgress(context: Context, uploadInfo: UploadInfo) {
zonedOnProgress(context, uploadInfo);
},

onCancelled(context: Context, uploadInfo: UploadInfo) {
zonedOnCancelled(context, uploadInfo);
},
onCancelled(context: Context, uploadInfo: UploadInfo) {
zonedOnCancelled(context, uploadInfo);
},

onError(context: Context, uploadInfo: UploadInfo, response: ServerResponse, error: java.lang.Exception) {
zonedOnError(context, uploadInfo, response, error);
},
onError(context: Context, uploadInfo: UploadInfo, response: ServerResponse, error: java.lang.Exception) {
zonedOnError(context, uploadInfo, response, error);
},

onCompleted(context: Context, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
zonedOnCompleted(context, uploadInfo, serverResponse);
}
onCompleted(context: Context, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
zonedOnCompleted(context, uploadInfo, serverResponse);
}
};

const ProgressReceiverImpl = (<any>net.gotev.uploadservice.UploadServiceBroadcastReceiver).extend(temp);
Expand Down Expand Up @@ -177,50 +177,10 @@ class Task extends ObservableBase {
static create(session: Session, file: string, options: common.Request): Task {
ensureUploadServiceNamespace();
ensureReceiver();
const task = new Task();
task._session = session;
task._id = session.id + "{" + ++Task.taskCount + "}";

const context = application.android.context;

const request = new net.gotev.uploadservice.BinaryUploadRequest(context, task._id, options.url);

request.setFileToUpload(file);

const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
if (displayNotificationProgress) {
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
request.setNotificationConfig(uploadNotificationConfig);
}
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
if (autoDeleteAfterUpload) {
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
}
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
if (maxRetryCount) {
request.setMaxRetries(maxRetryCount);
}

const headers = options.headers;
if (headers) {
for (const header in headers) {
const value = headers[header];
if (value !== null && value !== void 0) {
request.addHeader(header, value.toString());
}
}
}

task.setDescription(options.description);

request.setMethod(options.method ? options.method : "GET");

task.setUpload(0);
task.setTotalUpload(1);
task.setStatus("pending");
const taskId = session.id + "{" + ++Task.taskCount + "}";
const request = getBinaryRequest(taskId, options, file);

const task = Task.initTask(taskId, session, options);
request.startUpload();

Task.cache[task._id] = task;
Expand All @@ -231,79 +191,25 @@ class Task extends ObservableBase {
static createMultiPart(session: Session, params: Array<any>, options: common.Request): Task {
ensureUploadServiceNamespace();
ensureReceiver();
const task = new Task();
task._session = session;
task._id = session.id + "{" + (++Task.taskCount) + "}";

const context = application.android.context;

const request = new net.gotev.uploadservice.MultipartUploadRequest(context, task._id, options.url);


for (let i = 0; i < params.length; i++) {
const curParam = params[i];
if (typeof curParam.name === 'undefined') {
throw new Error("You must have a `name` value");
}

if (curParam.filename) {
let fileName = curParam.filename;
if (fileName.startsWith("~/")) {
fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/");
}
const taskId = session.id + "{" + ++Task.taskCount + "}";
const request = getMultipartRequest(taskId, options, params);
const task = Task.initTask(taskId, session, options);

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);

}
}

const utf8 = options.utf8;

if (utf8) {
request.setUtf8Charset();
}

const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
if (displayNotificationProgress) {
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
request.setNotificationConfig(uploadNotificationConfig);
}
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
if (autoDeleteAfterUpload) {
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
}
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
if (maxRetryCount) {
request.setMaxRetries(maxRetryCount);
}
request.startUpload();
Task.cache[task._id] = task;
return task;
}

const headers = options.headers;
if (headers) {
for (const header in headers) {
const value = headers[header];
if (value !== null && value !== void 0) {
request.addHeader(header, value.toString());
}
}
}
private static initTask(taskId: string, session: Session, options: common.Request) {
const task = new Task();
task._session = session;
task._id = taskId;

task.setDescription(options.description);

request.setMethod(options.method ? options.method : "GET");

task.setUpload(0);
task.setTotalUpload(1);
task.setStatus("pending");

request.startUpload();

Task.cache[task._id] = task;

return task;
}

Expand Down Expand Up @@ -354,3 +260,73 @@ class Task extends ObservableBase {
(<any>net).gotev.uploadservice.UploadService.stopUpload(this._id);
}
}

function getBinaryRequest(taskId: string, options: common.Request, file: string) {
const request = new net.gotev.uploadservice.BinaryUploadRequest(application.android.context, taskId, options.url);

request.setFileToUpload(file);
setRequestOptions(request, options);

return request;
}

function getMultipartRequest(taskId: string, options: common.Request, params: any[]) {
const request = new net.gotev.uploadservice.MultipartUploadRequest(application.android.context, taskId, options.url);

for (let i = 0; i < params.length; i++) {
const curParam = params[i];
if (typeof curParam.name === 'undefined') {
throw new Error("You must have a `name` value");
}
if (curParam.filename) {
let fileName = curParam.filename;
if (fileName.startsWith("~/")) {
fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/");
}
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);
}
}

const utf8 = options.utf8;

if (utf8) {
request.setUtf8Charset();
}
setRequestOptions(request, options);

return request;
}

function setRequestOptions(request: any, options: common.Request) {
const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;

if (displayNotificationProgress) {
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
request.setNotificationConfig(uploadNotificationConfig);
}
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
if (autoDeleteAfterUpload) {
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
}
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
if (maxRetryCount) {
request.setMaxRetries(maxRetryCount);
}
const headers = options.headers;
if (headers) {
for (const header in headers) {
const value = headers[header];
if (value !== null && value !== void 0) {
request.addHeader(header, value.toString());
}
}
}

request.setMethod(options.method ? options.method : "GET");
}
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-background-http",
"version": "3.2.7",
"version": "3.3.0",
"main": "background-http",
"typings": "index.d.ts",
"nativescript": {
Expand Down

0 comments on commit 37a1e60

Please sign in to comment.