Skip to content

Commit

Permalink
fix(android): progress support for uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Jan 20, 2022
1 parent e776b25 commit 67817ac
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);

}
}
10 changes: 10 additions & 0 deletions src/https.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions src/typings/android.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand Down

0 comments on commit 67817ac

Please sign in to comment.