From 67817ac284f8da424829b0c400b5c2c788ccf9dc Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Thu, 20 Jan 2022 14:34:13 +0100 Subject: [PATCH 1/2] fix(android): progress support for uploading --- .../https/ProgressRequestWrapper.java | 73 +++++++++++++++++++ src/https.android.ts | 10 +++ src/typings/android.d.ts | 9 +++ 3 files changed, 92 insertions(+) create mode 100644 plugin/platforms/android/java/com/nativescript/https/ProgressRequestWrapper.java diff --git a/plugin/platforms/android/java/com/nativescript/https/ProgressRequestWrapper.java b/plugin/platforms/android/java/com/nativescript/https/ProgressRequestWrapper.java new file mode 100644 index 0000000..86a7f3c --- /dev/null +++ b/plugin/platforms/android/java/com/nativescript/https/ProgressRequestWrapper.java @@ -0,0 +1,73 @@ +package com.nativescript.https; + +import okhttp3.RequestBody; +import okhttp3.MediaType; + +import java.io.IOException; + +import okio.Buffer; +import okio.BufferedSink; +import okio.ForwardingSink; +import okio.Okio; +import okio.Sink; + +public class ProgressRequestWrapper extends RequestBody { + + protected RequestBody delegate; + protected ProgressListener listener; + + protected CountingSink countingSink; + + public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) { + this.delegate = delegate; + this.listener = listener; + } + + @Override + public MediaType contentType() { + return delegate.contentType(); + } + + @Override + public long contentLength() throws IOException { + return delegate.contentLength(); + } + + @Override + public void writeTo(BufferedSink sink) throws IOException { + + BufferedSink bufferedSink; + + countingSink = new CountingSink(sink); + bufferedSink = Okio.buffer(countingSink); + + delegate.writeTo(bufferedSink); + + bufferedSink.flush(); + } + + protected final class CountingSink extends ForwardingSink { + + private long bytesWritten = 0; + + public CountingSink(Sink delegate) { + super(delegate); + } + + @Override + public void write(Buffer source, long byteCount) throws IOException { + + super.write(source, byteCount); + + bytesWritten += byteCount; + listener.onRequestProgress(bytesWritten, contentLength()); + } + + } + + public interface ProgressListener { + + void onRequestProgress(long bytesWritten, long contentLength); + + } +} \ No newline at end of file diff --git a/src/https.android.ts b/src/https.android.ts index 8f32c3d..a48db24 100644 --- a/src/https.android.ts +++ b/src/https.android.ts @@ -443,6 +443,16 @@ export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolea } }); okHttpBody = builder.build(); + if (opts.onProgress) { + okHttpBody = new com.nativescript.https.ProgressRequestWrapper( + okHttpBody, + new com.nativescript.https.ProgressRequestWrapper.ProgressListener({ + onRequestProgress(bytesWritten: number, contentLength: number) { + opts.onProgress(bytesWritten, contentLength); + }, + }) + ); + } } else if (type === 'application/x-www-form-urlencoded') { const builder = new okhttp3.FormBody.Builder(); Object.keys(opts.body).forEach((key) => { diff --git a/src/typings/android.d.ts b/src/typings/android.d.ts index e4d67ee..393ac16 100644 --- a/src/typings/android.d.ts +++ b/src/typings/android.d.ts @@ -22,6 +22,15 @@ declare namespace com { toFile(); toFileAsync(filePath: string, callback: OkHttpResponse.OkHttpResponseAsyncCallback); } + export class ProgressRequestWrapper extends okhttp3.RequestBody { + constructor(body: okhttp3.RequestBody, listener: ProgressRequestWrapper.ProgressListener); + } + export namespace ProgressRequestWrapper { + export class ProgressListener { + constructor(impl: { onRequestProgress(current: number, total: number) }); + onRequestProgress(current: number, total: number); + } + } export namespace OkHttpResponse { export class OkHttpResponseProgressCallback { constructor(impl: { onProgress(current: number, total: number) }); From 873ca64dc8f4197d6d9e40f58db37c388932db74 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Thu, 20 Jan 2022 14:34:45 +0100 Subject: [PATCH 2/2] v3.3.12 --- CHANGELOG.md | 11 +++++++++++ lerna.json | 2 +- plugin/CHANGELOG.md | 11 +++++++++++ plugin/package.json | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07780c4..cf1c70b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.12](https://github.com/nativescript-community/https/compare/v3.3.11...v3.3.12) (2022-01-20) + + +### Bug Fixes + +* **android:** progress support for uploading ([67817ac](https://github.com/nativescript-community/https/commit/67817ac284f8da424829b0c400b5c2c788ccf9dc)) + + + + + ## [3.3.11](https://github.com/nativescript-community/https/compare/v3.3.10...v3.3.11) (2022-01-20) diff --git a/lerna.json b/lerna.json index 621df6a..5ec116b 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "plugin" ], - "version": "3.3.11", + "version": "3.3.12", "command": { "publish": { "conventionalCommits": true diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index 0d86f1d..499a46b 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.12](https://github.com/nativescript-community/https/compare/v3.3.11...v3.3.12) (2022-01-20) + + +### Bug Fixes + +* **android:** progress support for uploading ([67817ac](https://github.com/nativescript-community/https/commit/67817ac284f8da424829b0c400b5c2c788ccf9dc)) + + + + + ## [3.3.11](https://github.com/nativescript-community/https/compare/v3.3.10...v3.3.11) (2022-01-20) diff --git a/plugin/package.json b/plugin/package.json index dd9ee01..e8fa593 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript-community/https", - "version": "3.3.11", + "version": "3.3.12", "description": "Nativescript plugin for gestures", "main": "https", "sideEffects": false,