Skip to content

Commit

Permalink
fix progresss upload
Browse files Browse the repository at this point in the history
  • Loading branch information
andhikayuana committed Dec 17, 2018
1 parent ae8e24a commit ab86b09
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,10 @@ public interface ProgressListener {

private static class CountingFileRequestBody extends RequestBody {
private static final int SEGMENT_SIZE = 2048;
private static final int IGNORE_FIRST_NUMBER_OF_WRITE_TO_CALL = 0;
private final File file;
private final ProgressListener progressListener;
private int numWriteToCall = -1;

private CountingFileRequestBody(File file, ProgressListener progressListener) {
this.file = file;
Expand All @@ -798,6 +800,8 @@ public long contentLength() throws IOException {

@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
numWriteToCall++;

Source source = null;
try {
source = Okio.source(file);
Expand All @@ -807,13 +811,21 @@ public void writeTo(@NonNull BufferedSink sink) throws IOException {
while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
total += read;
sink.flush();
progressListener.onProgress(total);

/**
* When we use HttpLoggingInterceptor,
* we have issue with progress update not valid.
* So we must check, first call is to HttpLoggingInterceptor
* second call is to request
*/
if (numWriteToCall > IGNORE_FIRST_NUMBER_OF_WRITE_TO_CALL) {
progressListener.onProgress(total);
}

}
} finally {
Util.closeQuietly(source);
}
}

}
}

0 comments on commit ab86b09

Please sign in to comment.