Skip to content

Commit 67817ac

Browse files
committed
fix(android): progress support for uploading
1 parent e776b25 commit 67817ac

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.nativescript.https;
2+
3+
import okhttp3.RequestBody;
4+
import okhttp3.MediaType;
5+
6+
import java.io.IOException;
7+
8+
import okio.Buffer;
9+
import okio.BufferedSink;
10+
import okio.ForwardingSink;
11+
import okio.Okio;
12+
import okio.Sink;
13+
14+
public class ProgressRequestWrapper extends RequestBody {
15+
16+
protected RequestBody delegate;
17+
protected ProgressListener listener;
18+
19+
protected CountingSink countingSink;
20+
21+
public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) {
22+
this.delegate = delegate;
23+
this.listener = listener;
24+
}
25+
26+
@Override
27+
public MediaType contentType() {
28+
return delegate.contentType();
29+
}
30+
31+
@Override
32+
public long contentLength() throws IOException {
33+
return delegate.contentLength();
34+
}
35+
36+
@Override
37+
public void writeTo(BufferedSink sink) throws IOException {
38+
39+
BufferedSink bufferedSink;
40+
41+
countingSink = new CountingSink(sink);
42+
bufferedSink = Okio.buffer(countingSink);
43+
44+
delegate.writeTo(bufferedSink);
45+
46+
bufferedSink.flush();
47+
}
48+
49+
protected final class CountingSink extends ForwardingSink {
50+
51+
private long bytesWritten = 0;
52+
53+
public CountingSink(Sink delegate) {
54+
super(delegate);
55+
}
56+
57+
@Override
58+
public void write(Buffer source, long byteCount) throws IOException {
59+
60+
super.write(source, byteCount);
61+
62+
bytesWritten += byteCount;
63+
listener.onRequestProgress(bytesWritten, contentLength());
64+
}
65+
66+
}
67+
68+
public interface ProgressListener {
69+
70+
void onRequestProgress(long bytesWritten, long contentLength);
71+
72+
}
73+
}

src/https.android.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolea
443443
}
444444
});
445445
okHttpBody = builder.build();
446+
if (opts.onProgress) {
447+
okHttpBody = new com.nativescript.https.ProgressRequestWrapper(
448+
okHttpBody,
449+
new com.nativescript.https.ProgressRequestWrapper.ProgressListener({
450+
onRequestProgress(bytesWritten: number, contentLength: number) {
451+
opts.onProgress(bytesWritten, contentLength);
452+
},
453+
})
454+
);
455+
}
446456
} else if (type === 'application/x-www-form-urlencoded') {
447457
const builder = new okhttp3.FormBody.Builder();
448458
Object.keys(opts.body).forEach((key) => {

src/typings/android.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ declare namespace com {
2222
toFile();
2323
toFileAsync(filePath: string, callback: OkHttpResponse.OkHttpResponseAsyncCallback);
2424
}
25+
export class ProgressRequestWrapper extends okhttp3.RequestBody {
26+
constructor(body: okhttp3.RequestBody, listener: ProgressRequestWrapper.ProgressListener);
27+
}
28+
export namespace ProgressRequestWrapper {
29+
export class ProgressListener {
30+
constructor(impl: { onRequestProgress(current: number, total: number) });
31+
onRequestProgress(current: number, total: number);
32+
}
33+
}
2534
export namespace OkHttpResponse {
2635
export class OkHttpResponseProgressCallback {
2736
constructor(impl: { onProgress(current: number, total: number) });

0 commit comments

Comments
 (0)