utilMap;
18 | private static UtilsManager mInstance;
19 |
20 | public static UtilsManager getInstance() {
21 | if (mInstance == null) {
22 | synchronized (UtilsManager.class) {
23 | if (mInstance == null) {
24 | mInstance = new UtilsManager();
25 | mInstance.utilMap = new HashMap<>(32);
26 | tryAutoInit();
27 | }
28 | }
29 | }
30 |
31 | return mInstance;
32 | }
33 |
34 | private static void tryAutoInit() {
35 | try {
36 | Class cls;
37 | cls = Class.forName("com.zzc.easycomponents.util.UtilAutoIniter");
38 | Method method;
39 | method = cls.getMethod("init");
40 | method.invoke(cls);
41 | } catch (ClassNotFoundException e) {
42 | e.printStackTrace();
43 | } catch (NoSuchMethodException e) {
44 | e.printStackTrace();
45 | } catch (IllegalAccessException e) {
46 | e.printStackTrace();
47 | } catch (InvocationTargetException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | public void init(IUtil util) {
53 | String name = util.name();
54 | int price = getUtilPrice(util);
55 |
56 | IUtil old = utilMap.get(name);
57 | if (old == null || getUtilPrice(old) < price) {
58 | utilMap.put(name, util);
59 | }
60 | }
61 |
62 | private int getUtilPrice(IUtil util) {
63 | EasyUtil easyUtil = util.getClass().getAnnotation(EasyUtil.class);
64 | if (easyUtil != null) {
65 | return easyUtil.price();
66 | }
67 | return 0;
68 | }
69 |
70 | public IUtil getUtil(String name) {
71 | IUtil util = utilMap.get(name);
72 | if (util == null) {
73 | throw new ComponentRuntimeException(name + " util has not initialized");
74 | }
75 | return util;
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/librarys/easycomponents/src/main/java/com/zzc/easycomponents/util/Views.java:
--------------------------------------------------------------------------------
1 | package com.zzc.easycomponents.util;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/11/13
6 | */
7 | public interface Views {
8 |
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/librarys/easycomponents/src/main/java/com/zzc/easycomponents/widget/Widgets.java:
--------------------------------------------------------------------------------
1 | package com.zzc.easycomponents.widget;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/11/9
6 | */
7 | public class Widgets {
8 | }
9 |
--------------------------------------------------------------------------------
/librarys/easycomponents/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | component_composite
3 |
4 |
--------------------------------------------------------------------------------
/librarys/easycomponents/src/test/java/com/zzc/component/composite/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zzc.component.composite;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/librarys/networklib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/librarys/networklib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 27
10 | targetSdkVersion 28
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation project(":librarys:baselib")
31 |
32 | api "io.reactivex.rxjava2:rxandroid:${rxandroidVersion}"
33 | api "com.squareup.retrofit2:retrofit:${retrofitVersion}"
34 | api "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
35 | api "com.squareup.retrofit2:adapter-rxjava2:${retrofitVersion}"
36 | api "com.squareup.okhttp3:okhttp:${okHttpVersion}"
37 |
38 | implementation project(":librarys:easycomponents")
39 | }
40 |
--------------------------------------------------------------------------------
/librarys/networklib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/librarys/networklib/src/androidTest/java/com/zzc/network/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.zzc.network.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/cache/CacheStrategy.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.cache;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/9/14
6 | */
7 | public class CacheStrategy {
8 |
9 | public static final String HEADER_KEY = "cachestg";
10 |
11 | protected static final String KEY_NETWORK = "network";
12 | protected static final String KEY_CACHE = "cache";
13 | protected static final String KEY_REFRESH = "refresh";
14 | protected static final String KEY_CACHE_AND_REFRESH = "getandrefresh";
15 | protected static final String KEY_ONLY_CACHE = "onlycache";
16 | protected static final String KEY_CACHE_1_HOUR = "cache1hour";
17 |
18 | public static final String NETWORK = HEADER_KEY + ":" + KEY_NETWORK;
19 | public static final String CACHE = HEADER_KEY + ":" + KEY_CACHE;
20 | public static final String REFRESH = HEADER_KEY + ":" + KEY_REFRESH;
21 |
22 | /**
23 | * # 此策略需要配合call和PriorityCacheResponseCallback类实现
24 | */
25 | public static final String CACHE_AND_REFRESH = HEADER_KEY + ":" + KEY_CACHE_AND_REFRESH;
26 | public static final String ONLY_CACHE = HEADER_KEY + ":" + KEY_ONLY_CACHE;
27 | public static final String CACHE_1_HOUR = HEADER_KEY + ":" + KEY_CACHE_1_HOUR;
28 | }
29 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/cache/CacheStrategyInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.cache;
2 |
3 |
4 | import com.zzc.baselib.util.NetworkUtils;
5 |
6 | import java.io.IOException;
7 |
8 | import okhttp3.CacheControl;
9 | import okhttp3.Interceptor;
10 | import okhttp3.Request;
11 | import okhttp3.Response;
12 |
13 | /**
14 | * @author Roye
15 | * @date 2018/9/10
16 | * 缓存拦截器,配合网络拦截器一起使用
17 | */
18 | public class CacheStrategyInterceptor implements Interceptor {
19 |
20 | @Override
21 | public Response intercept(Chain chain) throws IOException {
22 | Request oRequest = chain.request();
23 | boolean connected = NetworkUtils.isAvailable();
24 | //如果没有网络,则启用 FORCE_CACHE
25 | if (!connected) {
26 | oRequest = oRequest.newBuilder()
27 | .cacheControl(CacheControl.FORCE_CACHE)
28 | .build();
29 | Response originalResponse = chain.proceed(oRequest);
30 | return originalResponse.newBuilder()
31 | .removeHeader("Pragma")
32 | .removeHeader("Cache-Control")
33 | .header("Cache-Control", "public, only-if-cached, max-stale=2419200")
34 | .build();
35 | }
36 |
37 | Response tryCacheResponse = CacheStrategyUtil.doForCacheInterceptor(chain, oRequest);
38 | if (tryCacheResponse != null) {
39 | return tryCacheResponse;
40 | }
41 |
42 | return chain.proceed(oRequest);
43 | }
44 |
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/cache/NetworkInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.cache;
2 |
3 | import com.zzc.baselib.util.NetworkUtils;
4 |
5 | import java.io.IOException;
6 |
7 | import okhttp3.CacheControl;
8 | import okhttp3.Interceptor;
9 | import okhttp3.Request;
10 | import okhttp3.Response;
11 |
12 | /**
13 | * 1、修改response缓存设置,忽略服务端缓存控制
14 | * 2、无网络,直接请求缓存
15 | * 3、url缓存可设置, 以url+queryString哈希值为key,所以可以通过传递新querykey=timestamp来强刷新数据
16 | * 只读网络(Cache-Control: no-CacheStrategyUtil),
17 | * 无存储(Cache-Control: no-store),
18 | * 缓存时间(Cache-Control: max-age=640000),没有超出maxAge,不管怎么样都是返回缓存数据,超过了maxAge,发起新的请求获取数据更新,请求失败返回缓存数据。
19 | * 只读缓存(Cache-Control:public, only-if-cached, max-stale=2419200),没有超过maxStale,不管怎么样都返回缓存数据,超过了maxStale,发起请求获取更新数据,请求失败返回失败
20 | *
21 | * 这个拦截器决定要不要做统一缓存,Response header 最终决定缓存读写策略
22 | */
23 | public class NetworkInterceptor implements Interceptor {
24 |
25 | @Override
26 | public Response intercept(Chain chain) throws IOException {
27 | Request oRequest = chain.request();
28 | boolean connected = NetworkUtils.isAvailable();
29 | CacheControl cacheControl = oRequest.cacheControl();
30 |
31 | Request request = oRequest;
32 |
33 | if (connected) {
34 | Response tryCacheResponse = CacheStrategyUtil.doForNetworkInterceptor(chain, oRequest);
35 | if (tryCacheResponse != null) {
36 | return tryCacheResponse;
37 | } else {
38 | Response originalResponse = chain.proceed(request);
39 | return originalResponse.newBuilder()
40 | .removeHeader("Pragma")//清除响应体对Cache有影响的信息
41 | .removeHeader("Cache-Control")//清除响应体对Cache有影响的信息
42 | .header("Cache-Control", cacheControl.toString())
43 | .build();
44 | }
45 | } else {
46 | //如果没有网络,不做处理,直接返回
47 | return chain.proceed(oRequest);
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/BaseResponse.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * Created by Roye on 2016-2-22.
7 | */
8 | public class BaseResponse implements Serializable {
9 | public int code;
10 | public int result;
11 | public String msg;
12 | }
13 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/BaseResponseObserver.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.baselib.ui.listener.IProgressDialog;
4 | import com.zzc.baselib.ui.listener.IRxObserveDisposer;
5 |
6 | import java.lang.ref.WeakReference;
7 |
8 | import io.reactivex.Observer;
9 | import io.reactivex.disposables.Disposable;
10 |
11 | /**
12 | * Created by Roye on 2016-12-7.
13 | */
14 |
15 | public abstract class BaseResponseObserver implements Observer, SupportResponseLifecycle {
16 |
17 | private Disposable disposable;
18 | protected SupportProcedure procedure;
19 | WeakReference mRxObserveDisposer;
20 |
21 | public BaseResponseObserver(IRxObserveDisposer rxDisposer) {
22 | this(rxDisposer, new SupportProcedure());
23 | }
24 |
25 | public BaseResponseObserver(IRxObserveDisposer rxDisposer, SupportProcedure procedure) {
26 | this.procedure = procedure;
27 | this.procedure.setResponseLifecycle(this);
28 | if (rxDisposer != null) {
29 | this.mRxObserveDisposer = new WeakReference<>(rxDisposer);
30 | }
31 | }
32 |
33 | public void setProgressDialog(IProgressDialog progressDialog) {
34 | procedure.setProgressDialog(progressDialog);
35 | }
36 |
37 | public void setProgressDialog(IProgressDialog progressDialog, String loadingText) {
38 | procedure.setProgressDialog(progressDialog, loadingText);
39 | }
40 |
41 | @Override
42 | public void onSubscribe(Disposable d) {
43 | if (mRxObserveDisposer != null) {
44 | IRxObserveDisposer rxDisposer = mRxObserveDisposer.get();
45 | if (rxDisposer != null) {
46 | rxDisposer.addDisposable(d);
47 | }
48 | }
49 | this.disposable = d;
50 | onStart();
51 | }
52 |
53 | @Override
54 | public void onNext(Result result) {
55 | if (result == null) {
56 | onResponse(null);
57 | } else if (result instanceof IHttpResponse) {
58 | procedure.handleResponse((IHttpResponse) result);
59 | } else {
60 | onResponse((Data) result);
61 | }
62 | }
63 |
64 | @Override
65 | public void onComplete() {
66 | if (disposable != null && !disposable.isDisposed()) {
67 | disposable.dispose();
68 | }
69 | this.onFinish();
70 | }
71 |
72 | @Override
73 | public void onError(Throwable e) {
74 | procedure.handle(e);
75 | if (disposable != null) {
76 | disposable.dispose();
77 | }
78 | this.onFinish();
79 | }
80 |
81 | @Override
82 | public void onFinish() {
83 | procedure.hideLoading();
84 | }
85 |
86 | @Override
87 | public void onStart() {
88 | procedure.showLoading();
89 | }
90 |
91 | }
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/DefaultPriCResponseDataCallback.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/9/28
6 | */
7 | public abstract class DefaultPriCResponseDataCallback extends PriorityCacheResponseDataCallback, Data> {
8 |
9 | @Override
10 | public void onFailed(HttpResponse result) {
11 | super.onFailed(result);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/DefaultResponseCodeHandle.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.network.support.ClearTokenEvent;
4 | import com.zzc.network.support.NeedQuickLoginEvent;
5 |
6 | import org.greenrobot.eventbus.EventBus;
7 |
8 | /**
9 | * @author Roye
10 | * @date 2018/9/28
11 | */
12 | public class DefaultResponseCodeHandle {
13 |
14 | public static void handle(IHttpResponse response) {
15 | HttpResponse result = (HttpResponse) response;
16 | if (result.code == 1 || result.code == 1003) {
17 | return;
18 | }
19 | if (result.code == 1000 || result.code == 1001 || result.code == 1002) {
20 | EventBus.getDefault().post(new NeedQuickLoginEvent());
21 | return;
22 | }
23 | if (result.code == 6) {
24 | EventBus.getDefault().post(new ClearTokenEvent());
25 | return;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/HttpResponse.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | /**
4 | * Created by Roye on 2016/12/8.
5 | */
6 |
7 | public class HttpResponse extends BaseResponse implements IHttpResponse {
8 | public Data data;
9 |
10 | @Override
11 | public boolean isOk() {
12 | return code == 0;
13 | }
14 |
15 | @Override
16 | public String getMsg() {
17 | return msg;
18 | }
19 |
20 | @Override
21 | public Data getData() {
22 | return data;
23 | }
24 |
25 |
26 | }
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/IHttpResponse.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/9/26
6 | */
7 | public interface IHttpResponse {
8 |
9 | public boolean isOk();
10 | public String getMsg();
11 | public Data getData();
12 | }
13 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/IRxResponseProgress.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 |
4 | import com.zzc.baselib.ui.listener.IProgressDialog;
5 | import com.zzc.baselib.ui.listener.IRxObserveDisposer;
6 |
7 | /**
8 | * Created by Roye on 2018/5/18.
9 | */
10 | public interface IRxResponseProgress extends IRxObserveDisposer, IProgressDialog {
11 | }
12 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/PriorityCacheResponseCallback.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.baselib.ui.listener.IProgressDialog;
4 | import com.zzc.baselib.util.NetworkUtils;
5 | import com.zzc.network.cache.CacheStrategyUtil;
6 | import com.zzc.network.support.RequestKey;
7 |
8 | import io.reactivex.functions.Consumer;
9 | import retrofit2.Call;
10 | import retrofit2.Callback;
11 | import retrofit2.Response;
12 |
13 | /**
14 | * @author Roye
15 | * @date 2018/9/12
16 | */
17 | public abstract class PriorityCacheResponseCallback implements Callback, SupportResponseLifecycle {
18 |
19 | private SupportProcedure procedure;
20 | private int requestCount;
21 | private Result lastCacheResult;
22 |
23 | public PriorityCacheResponseCallback() {
24 | procedure = new SupportProcedure();
25 | procedure.setResponseLifecycle(this);
26 | }
27 |
28 | public void setProgressDialog(IProgressDialog progressDialog) {
29 | procedure.setProgressDialog(progressDialog);
30 | }
31 |
32 | public void setProgressDialog(IProgressDialog progressDialog, String loadingText) {
33 | procedure.setProgressDialog(progressDialog, loadingText);
34 | }
35 |
36 | @Override
37 | public void onStart() {
38 | procedure.showLoading();
39 | }
40 |
41 | @Override
42 | public void onResponse(Call call, final Response response) {
43 | if (response.code() >= 400 || response.body() == null) {
44 | onFinish();
45 | return;
46 | }
47 | okhttp3.Response networkResopnse = response.raw().networkResponse();
48 | if (networkResopnse != null) {
49 | if (lastCacheResult == null) {
50 | onResponse(response.body());
51 | onFinish();
52 | } else {
53 | CacheStrategyUtil.checkSame(lastCacheResult, response.body(), new Consumer() {
54 | @Override
55 | public void accept(String s) throws Exception {
56 | if ("n".equals(s)) {
57 | onResponse(response.body());
58 | }
59 | onFinish();
60 | }
61 | });
62 | }
63 | } else {
64 | lastCacheResult = response.body();
65 | onResponse(response.body());
66 | onFinish();
67 |
68 | if (requestCount == 0) {
69 | requestCount++;
70 | if (NetworkUtils.isAvailable()) {
71 | call.clone().enqueue(this);
72 | }
73 | }
74 | }
75 | }
76 |
77 | @Override
78 | public void onFinish() {
79 | procedure.hideLoading();
80 | }
81 |
82 | @Override
83 | public void onFailure(Call call, Throwable t) {
84 | onError(t);
85 | }
86 |
87 | public void onError(Throwable e) {
88 | procedure.handle(e);
89 | onFinish();
90 | }
91 |
92 | @Override
93 | public void onFailed(Result result) {
94 |
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/PriorityCacheResponseDataCallback.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.baselib.ui.listener.IProgressDialog;
4 | import com.zzc.baselib.util.NetworkUtils;
5 | import com.zzc.network.BuildConfig;
6 | import com.zzc.network.cache.CacheStrategyUtil;
7 |
8 | import io.reactivex.functions.Consumer;
9 | import retrofit2.Call;
10 | import retrofit2.Callback;
11 | import retrofit2.Response;
12 |
13 | /**
14 | * @author Roye
15 | * @date 2018/9/6
16 | */
17 | public abstract class PriorityCacheResponseDataCallback, Data> implements Callback, SupportResponseLifecycle {
18 |
19 | private SupportProcedure procedure;
20 | private int requestCount;
21 | private Result lastCacheResult;
22 |
23 | public PriorityCacheResponseDataCallback() {
24 | procedure = new SupportProcedure();
25 | procedure.setResponseLifecycle(this);
26 | }
27 |
28 | public void setProgressDialog(IProgressDialog progressDialog) {
29 | procedure.setProgressDialog(progressDialog);
30 | }
31 |
32 | public void setProgressDialog(IProgressDialog progressDialog, String loadingText) {
33 | procedure.setProgressDialog(progressDialog, loadingText);
34 | }
35 |
36 | @Override
37 | public void onStart() {
38 | procedure.showLoading();
39 | }
40 |
41 | @Override
42 | public void onResponse(Call call, final Response response) {
43 | if (response.code() >= 400 || response.body() == null) {
44 | onFinish();
45 | return;
46 | }
47 | okhttp3.Response networkResopnse = response.raw().networkResponse();
48 | if (networkResopnse != null) {
49 | if (lastCacheResult == null) {
50 | procedure.handleResponse(response.body());
51 | onFinish();
52 | } else {
53 | CacheStrategyUtil.checkSame(lastCacheResult, response.body(), new Consumer() {
54 | @Override
55 | public void accept(String s) throws Exception {
56 | if ("n".equals(s)) {
57 | procedure.handleResponse(response.body());
58 | }
59 | onFinish();
60 | }
61 | });
62 | }
63 | } else {
64 | lastCacheResult = response.body();
65 |
66 | procedure.handleResponse(response.body());
67 | onFinish();
68 |
69 | if (requestCount == 0) {
70 | requestCount++;
71 | if (NetworkUtils.isAvailable()) {
72 | call.clone().enqueue(this);
73 | }
74 | }
75 | }
76 | }
77 |
78 | @Override
79 | public void onFinish() {
80 | procedure.hideLoading();
81 | }
82 |
83 | @Override
84 | public void onFailed(Result result) {
85 | DefaultResponseCodeHandle.handle(result);
86 | if (result.getMsg() != null) {
87 | }
88 | }
89 |
90 | @Override
91 | public void onFailure(Call call, Throwable t) {
92 | onError(t);
93 | }
94 |
95 | public void onError(Throwable e) {
96 | procedure.handle(e);
97 | onFinish();
98 | }
99 | }
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/ResponseDataObserver.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.baselib.ui.listener.IRxObserveDisposer;
4 |
5 | /**
6 | * Created by Roye on 2016/12/8.
7 | */
8 |
9 | public abstract class ResponseDataObserver extends BaseResponseObserver, Data> {
10 |
11 | public ResponseDataObserver() {
12 | this(null);
13 | }
14 |
15 | /**
16 | * 非必传
17 | *
18 | * @param observeDisposer observeDisposer在fragment or activity实现,建议传此参数
19 | */
20 | public ResponseDataObserver(IRxObserveDisposer observeDisposer) {
21 | super(observeDisposer);
22 | }
23 |
24 | @Override
25 | public void onFailed(HttpResponse result) {
26 | DefaultResponseCodeHandle.handle(result);
27 | if (result.getMsg() != null) {
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/ResponseObserver.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import com.zzc.baselib.ui.listener.IRxObserveDisposer;
4 |
5 | public abstract class ResponseObserver extends BaseResponseObserver {
6 |
7 | public ResponseObserver() {
8 | this(null);
9 | }
10 |
11 | public ResponseObserver(IRxObserveDisposer rxDisposer) {
12 | super(rxDisposer);
13 | }
14 |
15 | @Override
16 | public void onFailed(Result t) {
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/SupportProcedure.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | import android.text.TextUtils;
4 | import android.util.Log;
5 |
6 | import com.zzc.baselib.support.rx.RxSimple;
7 | import com.zzc.baselib.ui.listener.IProgressDialog;
8 | import com.zzc.baselib.util.NetworkUtils;
9 |
10 | import java.io.IOException;
11 | import java.lang.ref.WeakReference;
12 |
13 | import retrofit2.HttpException;
14 |
15 | /**
16 | * @author Roye
17 | * @date 2018/9/11
18 | */
19 | public class SupportProcedure {
20 |
21 | WeakReference mProgressDialog;
22 | private String loadingText;
23 | private SupportResponseLifecycle responseLifecycle;
24 |
25 | public SupportProcedure() {
26 |
27 | }
28 |
29 | public void setResponseLifecycle(SupportResponseLifecycle responseLifecycle) {
30 | this.responseLifecycle = responseLifecycle;
31 | }
32 |
33 | public void setProgressDialog(IProgressDialog progressDialog) {
34 | setProgressDialog(progressDialog, "请求中...");
35 | }
36 |
37 | public void setProgressDialog(IProgressDialog progressDialog, String loadingText) {
38 | if (progressDialog != null) {
39 | this.mProgressDialog = new WeakReference<>(progressDialog);
40 | this.loadingText = loadingText;
41 | }
42 | }
43 |
44 | public void handle(Throwable e) {
45 | if (e != null) {
46 | String message;
47 | if (!NetworkUtils.isAvailable()) {
48 | message = "网络连接不上,请检查网络设置";
49 | } else {
50 | message = e.getMessage();
51 | if (e instanceof IOException) {
52 | message = "网络超时";
53 | } else if (e instanceof HttpException) {
54 | message = "连接服务器出错:" + ((HttpException) e).code() + ", 请稍候重试";
55 | }
56 | if (TextUtils.isEmpty(message)) {
57 | message = "网络连接错误";
58 | }
59 | }
60 | final String showMessage = message;
61 | //ToastUtils.showToast(showMessage);
62 | }
63 | Log.e("okhttp", "error", e);
64 | }
65 |
66 | public void handleResponse(Result result) {
67 | if (result != null) {
68 | if (result.isOk())
69 | responseLifecycle.onResponse((Data) result.getData());
70 | else
71 | responseLifecycle.onFailed(result);
72 | } else {
73 | responseLifecycle.onFailed(null);
74 | }
75 | }
76 |
77 | public void showLoading() {
78 | if (!TextUtils.isEmpty(loadingText) && mProgressDialog != null) {
79 | RxSimple.runOnUIThread(new Runnable() {
80 | @Override
81 | public void run() {
82 | IProgressDialog progress = mProgressDialog.get();
83 | if (progress != null) {
84 | progress.showLoading(loadingText);
85 | }
86 | }
87 | });
88 | }
89 | }
90 |
91 | public void hideLoading() {
92 | if (!TextUtils.isEmpty(loadingText) && mProgressDialog != null) {
93 | RxSimple.runOnUIThread(new Runnable() {
94 | @Override
95 | public void run() {
96 | IProgressDialog progress = mProgressDialog.get();
97 | if (progress != null) {
98 | progress.hideLoading();
99 | }
100 | }
101 | });
102 | }
103 | }
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/response/SupportResponseLifecycle.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.response;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/9/11
6 | */
7 | public interface SupportResponseLifecycle {
8 |
9 | public void onStart();
10 |
11 | public void onResponse(Data result);
12 |
13 | public void onFailed(Result result);
14 |
15 | public void onFinish();
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/support/ClearTokenEvent.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.support;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Roye
7 | * @date 2018/8/29
8 | */
9 | public class ClearTokenEvent implements Serializable {
10 | }
11 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/support/GlobalRequestAdapter.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.support;
2 |
3 | import okhttp3.HttpUrl;
4 | import okhttp3.Request;
5 |
6 | /**
7 | * @author Roye
8 | * @date 2018/8/6
9 | */
10 | public interface GlobalRequestAdapter {
11 |
12 | void addHeader(Request.Builder builder);
13 |
14 | void addQueryParams(HttpUrl.Builder httpUrlBuilder);
15 | }
16 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/support/NeedQuickLoginEvent.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.support;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Roye
7 | * @date 2018/8/29
8 | */
9 | public class NeedQuickLoginEvent implements Serializable {
10 | }
11 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/support/ProgressResponseBody.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.support;
2 |
3 | import java.io.IOException;
4 |
5 | import okhttp3.MediaType;
6 | import okhttp3.ResponseBody;
7 | import okio.Buffer;
8 | import okio.BufferedSource;
9 | import okio.ForwardingSource;
10 | import okio.Okio;
11 | import okio.Source;
12 |
13 | /**
14 | * Created by liqi on 2016-2-23.
15 | */
16 | public class ProgressResponseBody extends ResponseBody {
17 | private final ResponseBody responseBody;
18 | private final ProgressListener progressListener;
19 | private BufferedSource bufferedSource;
20 |
21 | public static interface ProgressListener {
22 | void update(long bytesRead, long contentLength, boolean done);
23 | }
24 |
25 | public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
26 | this.responseBody = responseBody;
27 | this.progressListener = progressListener;
28 | }
29 |
30 | @Override
31 | public MediaType contentType() {
32 | return responseBody.contentType();
33 | }
34 |
35 | @Override
36 | public long contentLength() {
37 | return responseBody.contentLength();
38 | }
39 |
40 | @Override
41 | public BufferedSource source() {
42 | if (bufferedSource == null) {
43 | bufferedSource = Okio.buffer(source(responseBody.source()));
44 | }
45 | return bufferedSource;
46 | }
47 |
48 | private Source source(Source source) {
49 | return new ForwardingSource(source) {
50 | long totalBytesRead = 0L;
51 |
52 | @Override
53 | public long read(Buffer sink, long byteCount) throws IOException {
54 | long bytesRead = super.read(sink, byteCount);
55 | // read() returns the number of bytes read, or -1 if this source is exhausted.
56 | totalBytesRead += bytesRead != -1 ? bytesRead : 0;
57 | progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
58 | return bytesRead;
59 | }
60 | };
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/java/com/zzc/network/support/RequestKey.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network.support;
2 |
3 | /**
4 | * @author Roye
5 | * @date 2018/10/31
6 | */
7 | public class RequestKey {
8 |
9 | private String key;
10 |
11 | public RequestKey() {
12 | reset();
13 | }
14 |
15 | public void reset() {
16 | key = String.valueOf(System.currentTimeMillis());
17 | }
18 |
19 | public String requestKey() {
20 | return key;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/librarys/networklib/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | networklib
3 |
4 |
--------------------------------------------------------------------------------
/librarys/networklib/src/test/java/com/zzc/network/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.zzc.network;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ":librarys",
2 | ':librarys:baselib',
3 | ':librarys:easycomponents',
4 | ':librarys:networklib',
5 | ':librarys:design_style',
6 | ':librarys:easycompiler',
7 | ':librarys:easycommon'
--------------------------------------------------------------------------------