Skip to content

Commit 37a1e60

Browse files
authored
Merge pull request #166 from NativeScript/tbozhikov/refactor-and-bump
refactor: extract some code into functions, bump version
2 parents d207292 + c0c19f8 commit 37a1e60

File tree

3 files changed

+119
-143
lines changed

3 files changed

+119
-143
lines changed

demo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
"typescript": "~2.7.2",
3333
"tslint": "~5.4.3"
3434
}
35-
}
35+
}

src/background-http.android.ts

+117-141
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ function onProgressReceiverProgress(context: Context, uploadInfo: UploadInfo) {
2020
task.setUpload(currentBytes);
2121
task.setStatus("uploading");
2222
task.notify(<common.ProgressEventData>{
23-
eventName: "progress",
24-
object: task,
25-
currentBytes: currentBytes,
26-
totalBytes: totalBytes
23+
eventName: "progress",
24+
object: task,
25+
currentBytes: currentBytes,
26+
totalBytes: totalBytes
2727
});
2828
}
2929

@@ -39,11 +39,11 @@ function onProgressReceiverError(context: Context, uploadInfo: UploadInfo, respo
3939
const task = Task.fromId(uploadId);
4040
task.setStatus("error");
4141
task.notify(<common.ErrorEventData>{
42-
eventName: "error",
43-
object: task,
44-
error,
45-
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
46-
response
42+
eventName: "error",
43+
object: task,
44+
error,
45+
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
46+
response
4747
});
4848
}
4949

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

6262
task.notify(<common.ProgressEventData>{
63-
eventName: "progress",
64-
object: task,
65-
currentBytes: totalUpload,
66-
totalBytes: totalUpload
63+
eventName: "progress",
64+
object: task,
65+
currentBytes: totalUpload,
66+
totalBytes: totalUpload
6767
});
6868
task.notify(<common.ResultEventData>{
69-
eventName: "responded",
70-
object: task,
71-
data: response.getBodyAsString(),
72-
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1
69+
eventName: "responded",
70+
object: task,
71+
data: response.getBodyAsString(),
72+
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1
7373
});
7474
task.notify(<common.CompleteEventData>{
75-
eventName: "complete",
76-
object: task,
77-
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
78-
response
75+
eventName: "complete",
76+
object: task,
77+
responseCode: response && typeof response.getHttpCode === 'function' ? response.getHttpCode() : -1,
78+
response
7979
});
8080
}
8181

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

9292
const temp: Partial<UploadServiceBroadcastReceiver> = {
93-
onProgress(context: Context, uploadInfo: UploadInfo) {
94-
zonedOnProgress(context, uploadInfo);
95-
},
93+
onProgress(context: Context, uploadInfo: UploadInfo) {
94+
zonedOnProgress(context, uploadInfo);
95+
},
9696

97-
onCancelled(context: Context, uploadInfo: UploadInfo) {
98-
zonedOnCancelled(context, uploadInfo);
99-
},
97+
onCancelled(context: Context, uploadInfo: UploadInfo) {
98+
zonedOnCancelled(context, uploadInfo);
99+
},
100100

101-
onError(context: Context, uploadInfo: UploadInfo, response: ServerResponse, error: java.lang.Exception) {
102-
zonedOnError(context, uploadInfo, response, error);
103-
},
101+
onError(context: Context, uploadInfo: UploadInfo, response: ServerResponse, error: java.lang.Exception) {
102+
zonedOnError(context, uploadInfo, response, error);
103+
},
104104

105-
onCompleted(context: Context, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
106-
zonedOnCompleted(context, uploadInfo, serverResponse);
107-
}
105+
onCompleted(context: Context, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
106+
zonedOnCompleted(context, uploadInfo, serverResponse);
107+
}
108108
};
109109

110110
const ProgressReceiverImpl = (<any>net.gotev.uploadservice.UploadServiceBroadcastReceiver).extend(temp);
@@ -177,50 +177,10 @@ class Task extends ObservableBase {
177177
static create(session: Session, file: string, options: common.Request): Task {
178178
ensureUploadServiceNamespace();
179179
ensureReceiver();
180-
const task = new Task();
181-
task._session = session;
182-
task._id = session.id + "{" + ++Task.taskCount + "}";
183-
184-
const context = application.android.context;
185-
186-
const request = new net.gotev.uploadservice.BinaryUploadRequest(context, task._id, options.url);
187-
188-
request.setFileToUpload(file);
189-
190-
const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
191-
if (displayNotificationProgress) {
192-
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
193-
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
194-
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
195-
request.setNotificationConfig(uploadNotificationConfig);
196-
}
197-
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
198-
if (autoDeleteAfterUpload) {
199-
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
200-
}
201-
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
202-
if (maxRetryCount) {
203-
request.setMaxRetries(maxRetryCount);
204-
}
205-
206-
const headers = options.headers;
207-
if (headers) {
208-
for (const header in headers) {
209-
const value = headers[header];
210-
if (value !== null && value !== void 0) {
211-
request.addHeader(header, value.toString());
212-
}
213-
}
214-
}
215-
216-
task.setDescription(options.description);
217-
218-
request.setMethod(options.method ? options.method : "GET");
219-
220-
task.setUpload(0);
221-
task.setTotalUpload(1);
222-
task.setStatus("pending");
180+
const taskId = session.id + "{" + ++Task.taskCount + "}";
181+
const request = getBinaryRequest(taskId, options, file);
223182

183+
const task = Task.initTask(taskId, session, options);
224184
request.startUpload();
225185

226186
Task.cache[task._id] = task;
@@ -231,79 +191,25 @@ class Task extends ObservableBase {
231191
static createMultiPart(session: Session, params: Array<any>, options: common.Request): Task {
232192
ensureUploadServiceNamespace();
233193
ensureReceiver();
234-
const task = new Task();
235-
task._session = session;
236-
task._id = session.id + "{" + (++Task.taskCount) + "}";
237-
238-
const context = application.android.context;
239-
240-
const request = new net.gotev.uploadservice.MultipartUploadRequest(context, task._id, options.url);
241-
242-
243-
for (let i = 0; i < params.length; i++) {
244-
const curParam = params[i];
245-
if (typeof curParam.name === 'undefined') {
246-
throw new Error("You must have a `name` value");
247-
}
248-
249-
if (curParam.filename) {
250-
let fileName = curParam.filename;
251-
if (fileName.startsWith("~/")) {
252-
fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/");
253-
}
194+
const taskId = session.id + "{" + ++Task.taskCount + "}";
195+
const request = getMultipartRequest(taskId, options, params);
196+
const task = Task.initTask(taskId, session, options);
254197

255-
const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length);
256-
request.addFileToUpload(fileName, curParam.name, destFileName, curParam.mimeType);
257-
} else {
258-
request.addParameter(params[i].name, params[i].value);
259-
260-
}
261-
}
262-
263-
const utf8 = options.utf8;
264-
265-
if (utf8) {
266-
request.setUtf8Charset();
267-
}
268-
269-
const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
270-
if (displayNotificationProgress) {
271-
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
272-
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
273-
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
274-
request.setNotificationConfig(uploadNotificationConfig);
275-
}
276-
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
277-
if (autoDeleteAfterUpload) {
278-
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
279-
}
280-
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
281-
if (maxRetryCount) {
282-
request.setMaxRetries(maxRetryCount);
283-
}
198+
request.startUpload();
199+
Task.cache[task._id] = task;
200+
return task;
201+
}
284202

285-
const headers = options.headers;
286-
if (headers) {
287-
for (const header in headers) {
288-
const value = headers[header];
289-
if (value !== null && value !== void 0) {
290-
request.addHeader(header, value.toString());
291-
}
292-
}
293-
}
203+
private static initTask(taskId: string, session: Session, options: common.Request) {
204+
const task = new Task();
205+
task._session = session;
206+
task._id = taskId;
294207

295208
task.setDescription(options.description);
296-
297-
request.setMethod(options.method ? options.method : "GET");
298-
299209
task.setUpload(0);
300210
task.setTotalUpload(1);
301211
task.setStatus("pending");
302212

303-
request.startUpload();
304-
305-
Task.cache[task._id] = task;
306-
307213
return task;
308214
}
309215

@@ -354,3 +260,73 @@ class Task extends ObservableBase {
354260
(<any>net).gotev.uploadservice.UploadService.stopUpload(this._id);
355261
}
356262
}
263+
264+
function getBinaryRequest(taskId: string, options: common.Request, file: string) {
265+
const request = new net.gotev.uploadservice.BinaryUploadRequest(application.android.context, taskId, options.url);
266+
267+
request.setFileToUpload(file);
268+
setRequestOptions(request, options);
269+
270+
return request;
271+
}
272+
273+
function getMultipartRequest(taskId: string, options: common.Request, params: any[]) {
274+
const request = new net.gotev.uploadservice.MultipartUploadRequest(application.android.context, taskId, options.url);
275+
276+
for (let i = 0; i < params.length; i++) {
277+
const curParam = params[i];
278+
if (typeof curParam.name === 'undefined') {
279+
throw new Error("You must have a `name` value");
280+
}
281+
if (curParam.filename) {
282+
let fileName = curParam.filename;
283+
if (fileName.startsWith("~/")) {
284+
fileName = fileName.replace("~/", fileSystemModule.knownFolders.currentApp().path + "/");
285+
}
286+
const destFileName = curParam.destFilename || fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length);
287+
request.addFileToUpload(fileName, curParam.name, destFileName, curParam.mimeType);
288+
}
289+
else {
290+
request.addParameter(params[i].name, params[i].value);
291+
}
292+
}
293+
294+
const utf8 = options.utf8;
295+
296+
if (utf8) {
297+
request.setUtf8Charset();
298+
}
299+
setRequestOptions(request, options);
300+
301+
return request;
302+
}
303+
304+
function setRequestOptions(request: any, options: common.Request) {
305+
const displayNotificationProgress = typeof options.androidDisplayNotificationProgress === "boolean" ? options.androidDisplayNotificationProgress : true;
306+
307+
if (displayNotificationProgress) {
308+
const uploadNotificationConfig = new net.gotev.uploadservice.UploadNotificationConfig();
309+
const notificationTitle = typeof options.androidNotificationTitle === "string" ? options.androidNotificationTitle : 'File Upload';
310+
uploadNotificationConfig.setTitleForAllStatuses(notificationTitle);
311+
request.setNotificationConfig(uploadNotificationConfig);
312+
}
313+
const autoDeleteAfterUpload = typeof options.androidAutoDeleteAfterUpload === "boolean" ? options.androidAutoDeleteAfterUpload : false;
314+
if (autoDeleteAfterUpload) {
315+
request.setAutoDeleteFilesAfterSuccessfulUpload(true);
316+
}
317+
const maxRetryCount = typeof options.androidMaxRetries === "number" ? options.androidMaxRetries : undefined;
318+
if (maxRetryCount) {
319+
request.setMaxRetries(maxRetryCount);
320+
}
321+
const headers = options.headers;
322+
if (headers) {
323+
for (const header in headers) {
324+
const value = headers[header];
325+
if (value !== null && value !== void 0) {
326+
request.addHeader(header, value.toString());
327+
}
328+
}
329+
}
330+
331+
request.setMethod(options.method ? options.method : "GET");
332+
}

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-background-http",
3-
"version": "3.2.7",
3+
"version": "3.3.0",
44
"main": "background-http",
55
"typings": "index.d.ts",
66
"nativescript": {

0 commit comments

Comments
 (0)