Skip to content

Commit

Permalink
add progress listener for net
Browse files Browse the repository at this point in the history
  • Loading branch information
limedroid committed Sep 10, 2017
1 parent 63d742b commit 25a20e4
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mvp/src/main/java/cn/droidlover/xdroidmvp/kit/Kits.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
* Created by wanglei on 2016/11/28.
Expand Down Expand Up @@ -1114,6 +1116,14 @@ public static boolean check(String str) {
return str == null || "".equals(str);
}

public static boolean check(Map map) {
return map == null || map.isEmpty();
}

public static boolean check(Set set) {
return set == null || set.isEmpty();
}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface NetProvider {
boolean configLogEnable();

boolean handleError(NetError error);

boolean dispatchProgressEnable();
}
5 changes: 5 additions & 0 deletions mvp/src/main/java/cn/droidlover/xdroidmvp/net/XApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.net.progress.ProgressHelper;
import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -125,6 +126,10 @@ private OkHttpClient getClient(String baseUrl, NetProvider provider) {
builder.addInterceptor(new XInterceptor(handler));
}

if (provider.dispatchProgressEnable()) {
builder.addInterceptor(ProgressHelper.get().getInterceptor());
}

Interceptor[] interceptors = provider.configInterceptors();
if (!Kits.Empty.check(interceptors)) {
for (Interceptor interceptor : interceptors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cn.droidlover.xdroidmvp.net.progress;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Set;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;

/**
* Created by wanglei on 2017/9/10.
*/

public class ProRequestBody extends RequestBody {
private RequestBody delegate;
private BufferedSink bufferedSink;
private Set<WeakReference<ProgressListener>> listeners;

public ProRequestBody(RequestBody delegate, Set<WeakReference<ProgressListener>> listeners) {
this.delegate = delegate;
this.listeners = listeners;
}

@Override
public MediaType contentType() {
return delegate.contentType();
}

@Override
public long contentLength() {
try {
return delegate.contentLength();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}


@Override
public void writeTo(BufferedSink sink) throws IOException {
if (bufferedSink == null) {
bufferedSink = Okio.buffer(new ProgressSink(sink));
}
try {
delegate.writeTo(bufferedSink);
bufferedSink.flush();
} catch (IOException e) {
e.printStackTrace();
ProgressHelper.dispatchErrorEvent(listeners, e);
throw e;
}
}

final class ProgressSink extends ForwardingSink {
private long soFarBytes = 0;
private long totalBytes = -1;

public ProgressSink(okio.Sink delegate) {
super(delegate);
}

@Override
public void write(Buffer source, long byteCount) {
try {
super.write(source, byteCount);
} catch (Exception e) {
ProgressHelper.dispatchErrorEvent(listeners, e);
}

if (totalBytes < 0) {
totalBytes = contentLength();
}
soFarBytes += byteCount;

ProgressHelper.dispatchProgressEvent(listeners, soFarBytes, totalBytes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cn.droidlover.xdroidmvp.net.progress;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Set;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;

/**
* Created by wanglei on 2017/9/10.
*/

public class ProResponseBody extends ResponseBody {
private ResponseBody delegate;
private BufferedSource bufferedSource;
private Set<WeakReference<ProgressListener>> listeners;

public ProResponseBody(ResponseBody delegate, Set<WeakReference<ProgressListener>> listeners) {
this.delegate = delegate;
this.listeners = listeners;
}

@Override
public MediaType contentType() {
return delegate.contentType();
}

@Override
public long contentLength() {
return delegate.contentLength();
}

@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(new ProgressSource(delegate.source()));
}
return bufferedSource;
}


final class ProgressSource extends ForwardingSource {
private long soFarBytes = 0;
private long totalBytes = -1;

public ProgressSource(Source delegate) {
super(delegate);
}

@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = 0L;
try {
bytesRead = super.read(sink, byteCount);

if (totalBytes < 0) {
totalBytes = contentLength();
}
soFarBytes += (bytesRead != -1 ? bytesRead : 0);

ProgressHelper.dispatchProgressEvent(listeners, soFarBytes, totalBytes);
} catch (IOException e) {
ProgressHelper.dispatchErrorEvent(listeners, e);
throw e;
}

return bytesRead;
}
}
}
Loading

0 comments on commit 25a20e4

Please sign in to comment.