Commit 70670522355b965086fc90c89003afe93c2a0aea

Authored by 朱显松
1 parent f199d5ab

1.MVP increases ViewState data persistence logic code

2.The network requests the SSL authentication to update the outdated method
3.Network loading optimization error handling logic
4.LogUtil can not get Debug status problem code adjustment
app/src/main/AndroidManifest.xml
@@ -24,6 +24,8 @@ @@ -24,6 +24,8 @@
24 <category android:name="android.intent.category.LAUNCHER" /> 24 <category android:name="android.intent.category.LAUNCHER" />
25 </intent-filter> 25 </intent-filter>
26 </activity> 26 </activity>
  27 +
  28 +
27 </application> 29 </application>
28 30
29 </manifest> 31 </manifest>
30 \ No newline at end of file 32 \ No newline at end of file
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/ILaunchView.java
1 package com.cnlive.libs.demo.mvptest.frame; 1 package com.cnlive.libs.demo.mvptest.frame;
2 2
3 -import com.hannesdorfmann.mosby3.mvp.MvpView; 3 +import com.cnlive.libs.base.frame.view.MvpStateView;
  4 +import com.cnlive.libs.demo.data.model.AppInfo;
  5 +import com.cnlive.libs.demo.mvptest.model.LaunchPage;
4 6
5 7
6 -public interface ILaunchView extends MvpView { 8 +public interface ILaunchView extends MvpStateView<LaunchPage> {
7 /** 9 /**
8 * 开始加载页面数据 10 * 开始加载页面数据
9 */ 11 */
@@ -11,13 +13,15 @@ public interface ILaunchView extends MvpView { @@ -11,13 +13,15 @@ public interface ILaunchView extends MvpView {
11 13
12 /** 14 /**
13 * 完成数据加载 15 * 完成数据加载
  16 + *
14 * @param data 页面数据 17 * @param data 页面数据
15 */ 18 */
16 - void onLoadDataComplete(Object data); 19 + void onLoadDataComplete(AppInfo data);
17 20
18 /** 21 /**
19 * 错误的加载 22 * 错误的加载
20 - * @param code 页面错误码 23 + *
  24 + * @param code 页面错误码
21 * @param message 页面错误信息 25 * @param message 页面错误信息
22 */ 26 */
23 void onLoadDataError(String code, String message); 27 void onLoadDataError(String code, String message);
@@ -28,13 +32,10 @@ public interface ILaunchView extends MvpView { @@ -28,13 +32,10 @@ public interface ILaunchView extends MvpView {
28 void onTimerFinish(); 32 void onTimerFinish();
29 33
30 /** 34 /**
31 - * 计时器剩余时间通知 35 + * 计时器剩余时间通知
  36 + *
32 * @param time 剩余时间 37 * @param time 剩余时间
33 */ 38 */
34 void onTimerInterval(long time); 39 void onTimerInterval(long time);
35 40
36 - /**  
37 - * 启动主程序  
38 - */  
39 - void startMainPage();  
40 } 41 }
41 \ No newline at end of file 42 \ No newline at end of file
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/presenter/LaunchPresenter.java
1 package com.cnlive.libs.demo.mvptest.frame.presenter; 1 package com.cnlive.libs.demo.mvptest.frame.presenter;
2 2
  3 +import android.app.Activity;
3 import android.content.Context; 4 import android.content.Context;
  5 +import android.content.Intent;
4 6
5 import com.cnlive.libs.base.data.network.Subscriber; 7 import com.cnlive.libs.base.data.network.Subscriber;
6 import com.cnlive.libs.base.util.LogUtil; 8 import com.cnlive.libs.base.util.LogUtil;
7 import com.cnlive.libs.base.util.TimerUtil; 9 import com.cnlive.libs.base.util.TimerUtil;
8 import com.cnlive.libs.demo.Config; 10 import com.cnlive.libs.demo.Config;
  11 +import com.cnlive.libs.demo.MainActivity;
9 import com.cnlive.libs.demo.data.model.AppInfo; 12 import com.cnlive.libs.demo.data.model.AppInfo;
10 import com.cnlive.libs.demo.data.network.testapi.ApiRequest; 13 import com.cnlive.libs.demo.data.network.testapi.ApiRequest;
11 import com.cnlive.libs.demo.data.network.testapi.ApiService; 14 import com.cnlive.libs.demo.data.network.testapi.ApiService;
@@ -34,7 +37,7 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; { @@ -34,7 +37,7 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; {
34 @Override 37 @Override
35 public void onCompleted(String code, String message, AppInfo data) { 38 public void onCompleted(String code, String message, AppInfo data) {
36 ILaunchView view = getView(); 39 ILaunchView view = getView();
37 - if (view != null) view.onLoadDataComplete(null); 40 + if (view != null) view.onLoadDataComplete(data);
38 } 41 }
39 42
40 @Override 43 @Override
@@ -63,7 +66,6 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; { @@ -63,7 +66,6 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; {
63 timerUtil.setTotalTime(totalTime);//设置毫秒数 66 timerUtil.setTotalTime(totalTime);//设置毫秒数
64 timerUtil.setIntervalTime(1000);//设置间隔数 67 timerUtil.setIntervalTime(1000);//设置间隔数
65 timerUtil.start(); 68 timerUtil.start();
66 -  
67 timerUtil.setTimerLiener(new TimerUtil.TimeListener() { 69 timerUtil.setTimerLiener(new TimerUtil.TimeListener() {
68 @Override 70 @Override
69 public void onFinish() { 71 public void onFinish() {
@@ -80,11 +82,11 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; { @@ -80,11 +82,11 @@ public class LaunchPresenter extends MvpBasePresenter&lt;LaunchView&gt; {
80 }); 82 });
81 } 83 }
82 84
83 - public void startMainPage() {  
84 - if (getView() == null) return; 85 + public void startMainPage(Activity activity) {
85 if (timerUtil != null) timerUtil.cancel(); 86 if (timerUtil != null) timerUtil.cancel();
86 if (subscription != null) subscription.dispose(); 87 if (subscription != null) subscription.dispose();
87 - getView().startMainPage(); 88 + activity.startActivity(new Intent(activity, MainActivity.class));
  89 + activity.finish();
88 } 90 }
89 91
90 /** 92 /**
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/view/LaunchView.java
1 package com.cnlive.libs.demo.mvptest.frame.view; 1 package com.cnlive.libs.demo.mvptest.frame.view;
2 2
3 -import android.content.Intent;  
4 import android.view.View; 3 import android.view.View;
5 4
6 -import com.cnlive.libs.demo.MainActivity; 5 +import com.cnlive.libs.demo.data.model.AppInfo;
7 import com.cnlive.libs.demo.mvptest.frame.ILaunchView; 6 import com.cnlive.libs.demo.mvptest.frame.ILaunchView;
  7 +import com.cnlive.libs.demo.mvptest.model.LaunchPage;
8 import com.cnlive.libs.demo.mvptest.ui.activity.LaunchActivity; 8 import com.cnlive.libs.demo.mvptest.ui.activity.LaunchActivity;
9 9
10 10
@@ -23,11 +23,16 @@ public class LaunchView implements ILaunchView { @@ -23,11 +23,16 @@ public class LaunchView implements ILaunchView {
23 } 23 }
24 24
25 @Override 25 @Override
26 - public void onLoadDataComplete(Object data) { 26 + public void onLoadDataComplete(AppInfo data) {
27 loadEnd(); 27 loadEnd();
28 } 28 }
29 29
30 @Override 30 @Override
  31 + public void onRestoreDataComplete(LaunchPage data) {
  32 + activity.binding.setData(data);
  33 + }
  34 +
  35 + @Override
31 public void onLoadDataError(String code, String message) { 36 public void onLoadDataError(String code, String message) {
32 loadEnd(); 37 loadEnd();
33 } 38 }
@@ -40,13 +45,7 @@ public class LaunchView implements ILaunchView { @@ -40,13 +45,7 @@ public class LaunchView implements ILaunchView {
40 @Override 45 @Override
41 public void onTimerFinish() { 46 public void onTimerFinish() {
42 activity.binding.getData().setTime(0); 47 activity.binding.getData().setTime(0);
43 - startMainPage();  
44 - }  
45 -  
46 - @Override  
47 - public void startMainPage() {  
48 - activity.startActivity(new Intent(activity, MainActivity.class));  
49 - activity.finish(); 48 + activity.getPresenter().startMainPage(activity);
50 } 49 }
51 50
52 @Override 51 @Override
app/src/main/java/com/cnlive/libs/demo/mvptest/model/LaunchPage.java
@@ -2,12 +2,13 @@ package com.cnlive.libs.demo.mvptest.model; @@ -2,12 +2,13 @@ package com.cnlive.libs.demo.mvptest.model;
2 2
3 import android.databinding.BaseObservable; 3 import android.databinding.BaseObservable;
4 import android.databinding.Bindable; 4 import android.databinding.Bindable;
  5 +import android.os.Parcel;
  6 +import android.os.Parcelable;
5 import android.view.View; 7 import android.view.View;
6 8
7 import com.cnlive.libs.demo.BR; 9 import com.cnlive.libs.demo.BR;
8 10
9 -  
10 -public class LaunchPage extends BaseObservable { 11 +public class LaunchPage extends BaseObservable implements Parcelable {
11 private long time = -1; 12 private long time = -1;
12 private int timing = View.GONE; 13 private int timing = View.GONE;
13 private int loading = View.GONE; 14 private int loading = View.GONE;
@@ -42,4 +43,33 @@ public class LaunchPage extends BaseObservable { @@ -42,4 +43,33 @@ public class LaunchPage extends BaseObservable {
42 this.loading = loading; 43 this.loading = loading;
43 notifyPropertyChanged(BR.loading); 44 notifyPropertyChanged(BR.loading);
44 } 45 }
  46 +
  47 + @Override
  48 + public int describeContents() {
  49 + return 0;
  50 + }
  51 +
  52 + @Override
  53 + public void writeToParcel(Parcel parcel, int i) {
  54 + parcel.writeLong(this.time);
  55 + parcel.writeInt(this.timing);
  56 + parcel.writeInt(this.loading);
  57 + }
  58 +
  59 +
  60 + public static final Creator<LaunchPage> CREATOR = new Creator<LaunchPage>() {
  61 + @Override
  62 + public LaunchPage createFromParcel(Parcel in) {
  63 + LaunchPage target = new LaunchPage();
  64 + target.time = in.readLong();
  65 + target.timing = in.readInt();
  66 + target.loading = in.readInt();
  67 + return target;
  68 + }
  69 +
  70 + @Override
  71 + public LaunchPage[] newArray(int size) {
  72 + return new LaunchPage[size];
  73 + }
  74 + };
45 } 75 }
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/LaunchActivity.java
@@ -5,14 +5,14 @@ import android.content.Intent; @@ -5,14 +5,14 @@ import android.content.Intent;
5 import android.os.Bundle; 5 import android.os.Bundle;
6 import android.view.View; 6 import android.view.View;
7 7
8 -import com.cnlive.libs.base.frame.activity.MvpBindingActivity; 8 +import com.cnlive.libs.base.frame.activity.MvpViewStateBindingActivity;
9 import com.cnlive.libs.demo.R; 9 import com.cnlive.libs.demo.R;
10 import com.cnlive.libs.demo.databinding.ActivityLaunchBinding; 10 import com.cnlive.libs.demo.databinding.ActivityLaunchBinding;
11 import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter; 11 import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter;
12 import com.cnlive.libs.demo.mvptest.frame.view.LaunchView; 12 import com.cnlive.libs.demo.mvptest.frame.view.LaunchView;
13 import com.cnlive.libs.demo.mvptest.model.LaunchPage; 13 import com.cnlive.libs.demo.mvptest.model.LaunchPage;
14 14
15 -public class LaunchActivity extends MvpBindingActivity<LaunchView, LaunchPresenter, LaunchPage, ActivityLaunchBinding> { 15 +public class LaunchActivity extends MvpViewStateBindingActivity<LaunchView, LaunchPresenter, LaunchPage, ActivityLaunchBinding> {
16 16
17 @Override 17 @Override
18 protected int getLayoutId() { 18 protected int getLayoutId() {
@@ -22,13 +22,11 @@ public class LaunchActivity extends MvpBindingActivity&lt;LaunchView, LaunchPresent @@ -22,13 +22,11 @@ public class LaunchActivity extends MvpBindingActivity&lt;LaunchView, LaunchPresent
22 @Override 22 @Override
23 protected void onCreate(Bundle savedInstanceState) { 23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState); 24 super.onCreate(savedInstanceState);
25 - initData();  
26 } 25 }
27 26
28 @Override 27 @Override
29 protected void onNewIntent(Intent intent) { 28 protected void onNewIntent(Intent intent) {
30 super.onNewIntent(intent); 29 super.onNewIntent(intent);
31 - initData();  
32 } 30 }
33 31
34 @Override 32 @Override
@@ -37,16 +35,17 @@ public class LaunchActivity extends MvpBindingActivity&lt;LaunchView, LaunchPresent @@ -37,16 +35,17 @@ public class LaunchActivity extends MvpBindingActivity&lt;LaunchView, LaunchPresent
37 getPresenter().destroy(); 35 getPresenter().destroy();
38 } 36 }
39 37
40 - private void initData() {  
41 - getPresenter().initData(this);  
42 - }  
43 -  
44 public void onClick(View view){ 38 public void onClick(View view){
45 switch (view.getId()){ 39 switch (view.getId()){
46 case R.id.click: 40 case R.id.click:
47 - getPresenter().startMainPage(); 41 + getViewData().setTime(2);
  42 + startActivity(new Intent(this,LaunchActivity.class));
48 break; 43 break;
49 } 44 }
50 } 45 }
51 46
  47 + @Override
  48 + public void onNewViewStateInstance() {
  49 + getPresenter().initData(this);
  50 + }
52 } 51 }
cnlive/build.gradle
@@ -3,7 +3,7 @@ apply plugin: &#39;maven&#39; @@ -3,7 +3,7 @@ apply plugin: &#39;maven&#39;
3 3
4 android { 4 android {
5 compileSdkVersion 26 5 compileSdkVersion 26
6 - buildToolsVersion "26.0.0" 6 + buildToolsVersion "26.0.1"
7 7
8 defaultConfig { 8 defaultConfig {
9 minSdkVersion 16 9 minSdkVersion 16
cnlive/src/main/java/com/cnlive/libs/base/data/network/RetrofitUtil.java
@@ -3,13 +3,17 @@ package com.cnlive.libs.base.data.network; @@ -3,13 +3,17 @@ package com.cnlive.libs.base.data.network;
3 3
4 import android.content.Context; 4 import android.content.Context;
5 5
6 -  
7 import com.cnlive.libs.base.util.LogUtil; 6 import com.cnlive.libs.base.util.LogUtil;
8 7
  8 +import java.security.KeyStore;
  9 +import java.util.Arrays;
9 import java.util.concurrent.TimeUnit; 10 import java.util.concurrent.TimeUnit;
10 11
11 import javax.net.ssl.SSLContext; 12 import javax.net.ssl.SSLContext;
12 import javax.net.ssl.SSLSocketFactory; 13 import javax.net.ssl.SSLSocketFactory;
  14 +import javax.net.ssl.TrustManager;
  15 +import javax.net.ssl.TrustManagerFactory;
  16 +import javax.net.ssl.X509TrustManager;
13 17
14 import okhttp3.Cache; 18 import okhttp3.Cache;
15 import okhttp3.OkHttpClient; 19 import okhttp3.OkHttpClient;
@@ -42,10 +46,19 @@ public class RetrofitUtil { @@ -42,10 +46,19 @@ public class RetrofitUtil {
42 OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder(); 46 OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder();
43 47
44 try { 48 try {
  49 + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
  50 + TrustManagerFactory.getDefaultAlgorithm());
  51 + trustManagerFactory.init((KeyStore) null);
  52 + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
  53 + if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager))
  54 + throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
  55 + X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
  56 +
45 SSLContext sslContext = SSLContext.getInstance("TLS"); 57 SSLContext sslContext = SSLContext.getInstance("TLS");
46 - sslContext.init(null, null, null); 58 + sslContext.init(null, new TrustManager[]{trustManager}, null);
47 SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory()); 59 SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory());
48 - okHttpClientBuilder.sslSocketFactory(socketFactory); 60 +
  61 + okHttpClientBuilder.sslSocketFactory(socketFactory, trustManager);
49 } catch (Exception e) { 62 } catch (Exception e) {
50 LogUtil.e(TAG, "RetrofitUtil", e); 63 LogUtil.e(TAG, "RetrofitUtil", e);
51 } 64 }
cnlive/src/main/java/com/cnlive/libs/base/data/network/Subscriber.java
@@ -22,7 +22,10 @@ import retrofit2.adapter.rxjava2.Result; @@ -22,7 +22,10 @@ import retrofit2.adapter.rxjava2.Result;
22 22
23 public abstract class Subscriber<T extends IBaseInfo> { 23 public abstract class Subscriber<T extends IBaseInfo> {
24 24
  25 + private static final String TAG = "NetLoader";
  26 +
25 private Response<T> response; 27 private Response<T> response;
  28 + private Throwable response_error;
26 private Context context; 29 private Context context;
27 private Config config; 30 private Config config;
28 31
@@ -47,7 +50,7 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; { @@ -47,7 +50,7 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; {
47 config.load_failure_message = reloadString(config.load_failure_message, R.string.load_failure_message); 50 config.load_failure_message = reloadString(config.load_failure_message, R.string.load_failure_message);
48 config.load_error_message = reloadString(config.load_error_message, R.string.load_error_message); 51 config.load_error_message = reloadString(config.load_error_message, R.string.load_error_message);
49 config.no_network_message = reloadString(config.no_network_message, R.string.no_network_message); 52 config.no_network_message = reloadString(config.no_network_message, R.string.no_network_message);
50 - config.unknown_error_message = reloadString(config.no_network_message, R.string.no_network_message); 53 + config.unknown_error_message = reloadString(config.no_network_message, R.string.unknown_error_message);
51 } 54 }
52 55
53 public static Config newConfig() { 56 public static Config newConfig() {
@@ -57,18 +60,21 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; { @@ -57,18 +60,21 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; {
57 public Consumer<Result<T>> next = new Consumer<Result<T>>() { 60 public Consumer<Result<T>> next = new Consumer<Result<T>>() {
58 @Override 61 @Override
59 public void accept(Result<T> data) throws Exception { 62 public void accept(Result<T> data) throws Exception {
60 - response = data == null ? null : data.response(); 63 + if (data == null) return;
  64 + if (data.isError()) response_error = data.error();
  65 + else response = data.response();
61 } 66 }
62 }; 67 };
63 68
64 public Consumer<Throwable> error = new Consumer<Throwable>() { 69 public Consumer<Throwable> error = new Consumer<Throwable>() {
65 @Override 70 @Override
66 public void accept(Throwable e) throws Exception { 71 public void accept(Throwable e) throws Exception {
67 - LogUtil.d("NetLoader", "onError ", e); 72 + LogUtil.d(TAG, "onError ", e);
68 if (context != null && !NetworkUtil.isConnectInternet(context)) 73 if (context != null && !NetworkUtil.isConnectInternet(context))
69 onNetworkNoConnected(); 74 onNetworkNoConnected();
70 - else 75 + else {
71 onError(config.load_error_code, e != null ? e.getMessage() : config.load_error_message); 76 onError(config.load_error_code, e != null ? e.getMessage() : config.load_error_message);
  77 + }
72 } 78 }
73 }; 79 };
74 80
@@ -77,7 +83,10 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; { @@ -77,7 +83,10 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; {
77 public void run() throws Exception { 83 public void run() throws Exception {
78 if (response == null) { 84 if (response == null) {
79 if (!NetworkUtil.isConnectInternet(context)) onNetworkNoConnected(); 85 if (!NetworkUtil.isConnectInternet(context)) onNetworkNoConnected();
80 - else onError(config.unknown_error_code, config.unknown_error_message); 86 + else if (response_error != null) {
  87 + onError(config.load_error_code, config.load_error_message);
  88 + LogUtil.d(TAG, "onError ", response_error);
  89 + } else onError(config.unknown_error_code, config.unknown_error_message);
81 } else { 90 } else {
82 T pageData = response.body(); 91 T pageData = response.body();
83 92
@@ -87,7 +96,8 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; { @@ -87,7 +96,8 @@ public abstract class Subscriber&lt;T extends IBaseInfo&gt; {
87 if (config.customLoadResponse != null && config.custom_map.containsKey(pageData.getCode())) 96 if (config.customLoadResponse != null && config.custom_map.containsKey(pageData.getCode()))
88 config.customLoadResponse.onCustomEvent(pageData.getCode(), pageData.getMessage(), pageData); 97 config.customLoadResponse.onCustomEvent(pageData.getCode(), pageData.getMessage(), pageData);
89 else onError(pageData.getCode(), pageData.getMessage()); 98 else onError(pageData.getCode(), pageData.getMessage());
90 - else if (context != null && !NetworkUtil.isConnectInternet(context)) onNetworkNoConnected(); 99 + else if (context != null && !NetworkUtil.isConnectInternet(context))
  100 + onNetworkNoConnected();
91 else onError(config.load_failure_code, config.load_failure_message); 101 else onError(config.load_failure_code, config.load_failure_message);
92 } 102 }
93 } 103 }
cnlive/src/main/java/com/cnlive/libs/base/frame/activity/MvpViewStateActivity.java
1 package com.cnlive.libs.base.frame.activity; 1 package com.cnlive.libs.base.frame.activity;
2 2
3 import android.os.Bundle; 3 import android.os.Bundle;
  4 +import android.os.Parcelable;
4 import android.support.annotation.NonNull; 5 import android.support.annotation.NonNull;
5 6
6 import com.cnlive.libs.base.frame.ReflectUtil; 7 import com.cnlive.libs.base.frame.ReflectUtil;
  8 +import com.cnlive.libs.base.frame.state.MvpViewState;
  9 +import com.cnlive.libs.base.frame.view.MvpStateView;
7 import com.hannesdorfmann.mosby3.mvp.MvpPresenter; 10 import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
8 -import com.hannesdorfmann.mosby3.mvp.MvpView;  
9 -import com.hannesdorfmann.mosby3.mvp.viewstate.ViewState;  
10 11
11 /** 12 /**
12 * Created by xiansong on 2017/8/25. 13 * Created by xiansong on 2017/8/25.
13 */ 14 */
14 15
15 -public abstract class MvpViewStateActivity<V extends MvpView, P extends MvpPresenter<V>, VS extends ViewState<V>> extends com.hannesdorfmann.mosby3.mvp.viewstate.MvpViewStateActivity<V, P, VS> { 16 +public abstract class MvpViewStateActivity<V extends MvpStateView<D>, P extends MvpPresenter<V>, D extends Parcelable> extends com.hannesdorfmann.mosby3.mvp.viewstate.MvpViewStateActivity<V, P, MvpViewState<V, D>> {
16 17
17 public V view; 18 public V view;
  19 + public MvpViewState<V, D> viewState;
18 20
19 protected abstract int getLayoutId(); 21 protected abstract int getLayoutId();
20 22
@@ -35,10 +37,24 @@ public abstract class MvpViewStateActivity&lt;V extends MvpView, P extends MvpPrese @@ -35,10 +37,24 @@ public abstract class MvpViewStateActivity&lt;V extends MvpView, P extends MvpPrese
35 return ReflectUtil.instance(1, this); 37 return ReflectUtil.instance(1, this);
36 } 38 }
37 39
  40 + protected D initLayoutData() {
  41 + return ReflectUtil.instance(2, this);
  42 + }
  43 +
  44 + protected MvpViewState<V, D> initViewState() {
  45 + return new MvpViewState<>(initLayoutData());
  46 + }
  47 +
38 @NonNull 48 @NonNull
39 @Override 49 @Override
40 - public VS createViewState() {  
41 - return ReflectUtil.instance(2, this); 50 + public MvpViewState<V, D> createViewState() {
  51 + if (viewState == null) viewState = initViewState();
  52 + return viewState;
  53 + }
  54 +
  55 + @NonNull
  56 + public D getViewData() {
  57 + return getViewState().getViewData();
42 } 58 }
43 59
44 @NonNull 60 @NonNull
cnlive/src/main/java/com/cnlive/libs/base/frame/activity/MvpViewStateBindingActivity.java
@@ -3,35 +3,33 @@ package com.cnlive.libs.base.frame.activity; @@ -3,35 +3,33 @@ package com.cnlive.libs.base.frame.activity;
3 import android.databinding.DataBindingUtil; 3 import android.databinding.DataBindingUtil;
4 import android.databinding.ViewDataBinding; 4 import android.databinding.ViewDataBinding;
5 import android.os.Bundle; 5 import android.os.Bundle;
  6 +import android.os.Parcelable;
6 import android.support.annotation.NonNull; 7 import android.support.annotation.NonNull;
7 8
8 import com.cnlive.libs.base.frame.ReflectUtil; 9 import com.cnlive.libs.base.frame.ReflectUtil;
  10 +import com.cnlive.libs.base.frame.state.MvpViewState;
  11 +import com.cnlive.libs.base.frame.view.MvpStateView;
9 import com.hannesdorfmann.mosby3.mvp.MvpPresenter; 12 import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
10 -import com.hannesdorfmann.mosby3.mvp.MvpView;  
11 -import com.hannesdorfmann.mosby3.mvp.viewstate.ViewState;  
12 13
13 /** 14 /**
14 * Created by xiansong on 2017/8/25. 15 * Created by xiansong on 2017/8/25.
15 */ 16 */
16 17
17 -public abstract class MvpViewStateBindingActivity<V extends MvpView, P extends MvpPresenter<V>, VS extends ViewState<V>, D, T extends ViewDataBinding> extends MvpViewStateActivity<V, P, VS> { 18 +public abstract class MvpViewStateBindingActivity<V extends MvpStateView<D>, P extends MvpPresenter<V>, D extends Parcelable, T extends ViewDataBinding> extends MvpViewStateActivity<V, P, D> {
  19 +
18 public T binding; 20 public T binding;
19 - public D data;  
20 21
21 @Override 22 @Override
22 protected void onCreate(Bundle savedInstanceState) { 23 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState); 24 super.onCreate(savedInstanceState);
24 binding = DataBindingUtil.setContentView(this, getLayoutId()); 25 binding = DataBindingUtil.setContentView(this, getLayoutId());
25 - ReflectUtil.invoke(binding, "setData", getViewData());  
26 - }  
27 -  
28 - protected D initLayoutData() {  
29 - return ReflectUtil.instance(2, this);  
30 } 26 }
31 27
32 @NonNull 28 @NonNull
33 - public D getViewData() {  
34 - if(data == null) data = initLayoutData();  
35 - return data; 29 + @Override
  30 + public MvpViewState<V, D> createViewState() {
  31 + super.createViewState();
  32 + ReflectUtil.invoke(binding, "setData", viewState.getViewData());
  33 + return viewState;
36 } 34 }
37 } 35 }
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpBindingFragment.java
@@ -15,6 +15,7 @@ import com.hannesdorfmann.mosby3.mvp.MvpView; @@ -15,6 +15,7 @@ import com.hannesdorfmann.mosby3.mvp.MvpView;
15 15
16 /** 16 /**
17 * MVP Databinding Base Fragment 17 * MVP Databinding Base Fragment
  18 + *
18 * @param <V> MVP View {@link MvpView} 19 * @param <V> MVP View {@link MvpView}
19 * @param <P> MVP Presenter {@link MvpPresenter} 20 * @param <P> MVP Presenter {@link MvpPresenter}
20 * @param <D> Databind Page Data 21 * @param <D> Databind Page Data
@@ -39,7 +40,7 @@ public abstract class MvpBindingFragment&lt;V extends MvpView, P extends MvpPresent @@ -39,7 +40,7 @@ public abstract class MvpBindingFragment&lt;V extends MvpView, P extends MvpPresent
39 40
40 @NonNull 41 @NonNull
41 public D getViewData() { 42 public D getViewData() {
42 - if(data == null) data = initLayoutData(); 43 + if (data == null) data = initLayoutData();
43 return data; 44 return data;
44 } 45 }
45 } 46 }
46 \ No newline at end of file 47 \ No newline at end of file
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpFragment.java
@@ -29,7 +29,6 @@ public abstract class MvpFragment&lt;V extends MvpView, P extends MvpPresenter&lt;V&gt;&gt; @@ -29,7 +29,6 @@ public abstract class MvpFragment&lt;V extends MvpView, P extends MvpPresenter&lt;V&gt;&gt;
29 29
30 protected abstract int getLayoutId(); 30 protected abstract int getLayoutId();
31 31
32 -  
33 @NonNull 32 @NonNull
34 public V createView() { 33 public V createView() {
35 return ReflectUtil.instance(0, this.getClass(), this); 34 return ReflectUtil.instance(0, this.getClass(), this);
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpViewStateBindingFragment.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.fragment;
  2 +
  3 +import android.databinding.DataBindingUtil;
  4 +import android.databinding.ViewDataBinding;
  5 +import android.os.Bundle;
  6 +import android.os.Parcelable;
  7 +import android.support.annotation.NonNull;
  8 +import android.support.annotation.Nullable;
  9 +import android.view.LayoutInflater;
  10 +import android.view.View;
  11 +import android.view.ViewGroup;
  12 +
  13 +import com.cnlive.libs.base.frame.ReflectUtil;
  14 +import com.cnlive.libs.base.frame.state.MvpViewState;
  15 +import com.cnlive.libs.base.frame.view.MvpStateView;
  16 +import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
  17 +
  18 +/**
  19 + * Created by xiansong on 2017/8/29.
  20 + */
  21 +
  22 +public abstract class MvpViewStateBindingFragment<V extends MvpStateView<D>, P extends MvpPresenter<V>, D extends Parcelable, T extends ViewDataBinding> extends MvpViewStateFragment<V, P, D> {
  23 + public T binding;
  24 +
  25 + @Nullable
  26 + @Override
  27 + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  28 + binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
  29 + ReflectUtil.invoke(binding, "setData", getViewData());
  30 + return binding.getRoot();
  31 + }
  32 +
  33 + @NonNull
  34 + @Override
  35 + public MvpViewState<V, D> createViewState() {
  36 + super.createViewState();
  37 + ReflectUtil.invoke(binding, "setData", viewState.getViewData());
  38 + return viewState;
  39 + }
  40 +}
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpViewStateFragment.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.fragment;
  2 +
  3 +import android.os.Bundle;
  4 +import android.os.Parcelable;
  5 +import android.support.annotation.NonNull;
  6 +import android.support.annotation.Nullable;
  7 +import android.view.LayoutInflater;
  8 +import android.view.View;
  9 +import android.view.ViewGroup;
  10 +
  11 +import com.cnlive.libs.base.frame.ReflectUtil;
  12 +import com.cnlive.libs.base.frame.state.MvpViewState;
  13 +import com.cnlive.libs.base.frame.view.MvpStateView;
  14 +import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
  15 +
  16 +/**
  17 + * Created by xiansong on 2017/8/29.
  18 + */
  19 +
  20 +public abstract class MvpViewStateFragment<V extends MvpStateView<D>, P extends MvpPresenter<V>, D extends Parcelable> extends com.hannesdorfmann.mosby3.mvp.viewstate.MvpViewStateFragment<V, P, MvpViewState<V, D>> {
  21 +
  22 + public V view;
  23 + public MvpViewState<V, D> viewState;
  24 +
  25 + protected abstract int getLayoutId();
  26 +
  27 + @Nullable
  28 + @Override
  29 + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  30 + return inflater.inflate(getLayoutId(), container, false);
  31 + }
  32 +
  33 + @NonNull
  34 + public V createView() {
  35 + return ReflectUtil.instance(0, this.getClass(), this);
  36 + }
  37 +
  38 + @NonNull
  39 + @Override
  40 + public P createPresenter() {
  41 + return ReflectUtil.instance(1, this);
  42 + }
  43 +
  44 + protected D initLayoutData() {
  45 + return ReflectUtil.instance(2, this);
  46 + }
  47 +
  48 + protected MvpViewState<V, D> initViewState() {
  49 + return new MvpViewState<>(initLayoutData());
  50 + }
  51 +
  52 + @NonNull
  53 + @Override
  54 + public MvpViewState<V, D> createViewState() {
  55 + if (viewState == null) viewState = initViewState();
  56 + return viewState;
  57 + }
  58 +
  59 + @NonNull
  60 + public D getViewData() {
  61 + return getViewState().getViewData();
  62 + }
  63 +
  64 + @NonNull
  65 + @Override
  66 + public V getMvpView() {
  67 + if (view == null) view = createView();
  68 + return view;
  69 + }
  70 +}
0 \ No newline at end of file 71 \ No newline at end of file
cnlive/src/main/java/com/cnlive/libs/base/frame/state/MvpViewState.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.state;
  2 +
  3 +import android.os.Bundle;
  4 +import android.os.Parcelable;
  5 +import android.support.annotation.NonNull;
  6 +
  7 +import com.cnlive.libs.base.frame.view.MvpStateView;
  8 +import com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState;
  9 +
  10 +/**
  11 + * Created by xiansong on 2017/8/28.
  12 + */
  13 +
  14 +public class MvpViewState<V extends MvpStateView<D>, D extends Parcelable> implements RestorableViewState<V> {
  15 +
  16 + private static final String key_data = "state-data";
  17 +
  18 + protected D data;
  19 +
  20 + public MvpViewState(D data) {
  21 + this.data = data;
  22 + }
  23 +
  24 + @NonNull
  25 + public D getViewData() {
  26 + return data;
  27 + }
  28 +
  29 + @Override
  30 + public void saveInstanceState(@NonNull Bundle out) {
  31 + out.putParcelable(key_data, data);
  32 + }
  33 +
  34 + @Override
  35 + public RestorableViewState<V> restoreInstanceState(Bundle in) {
  36 + if (in == null) return null;
  37 + data = in.getParcelable(key_data);
  38 + return this;
  39 + }
  40 +
  41 + @Override
  42 + public void apply(V view, boolean retained) {
  43 + view.onRestoreDataComplete(data);
  44 + }
  45 +
  46 +
  47 +}
cnlive/src/main/java/com/cnlive/libs/base/frame/view/MvpStateView.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.view;
  2 +
  3 +import com.hannesdorfmann.mosby3.mvp.MvpView;
  4 +
  5 +/**
  6 + * Created by xiansong on 2017/8/28.
  7 + */
  8 +
  9 +public interface MvpStateView<D> extends MvpView {
  10 + void onRestoreDataComplete(D data);
  11 +}
cnlive/src/main/java/com/cnlive/libs/base/util/LogUtil.java
@@ -2,26 +2,30 @@ package com.cnlive.libs.base.util; @@ -2,26 +2,30 @@ package com.cnlive.libs.base.util;
2 2
3 import android.util.Log; 3 import android.util.Log;
4 4
5 -import com.cnlive.libs.base.BuildConfig;  
6 -  
7 /** 5 /**
8 * Log output tool 6 * Log output tool
9 */ 7 */
10 public class LogUtil { 8 public class LogUtil {
11 9
  10 + private static boolean ENABLED = true;
  11 +
  12 + public static void setDebug(boolean debug){
  13 + LogUtil.ENABLED = debug;
  14 + }
  15 +
12 public static void e(String tag, String msg) { 16 public static void e(String tag, String msg) {
13 - if (BuildConfig.DEBUG) Log.e(tag, msg); 17 + if (ENABLED) Log.e(tag, msg);
14 } 18 }
15 19
16 public static void e(String tag, String msg, Throwable tr) { 20 public static void e(String tag, String msg, Throwable tr) {
17 - if (BuildConfig.DEBUG) Log.e(tag, msg, tr); 21 + if (ENABLED) Log.e(tag, msg, tr);
18 } 22 }
19 23
20 public static void d(String tag, String msg) { 24 public static void d(String tag, String msg) {
21 - if (BuildConfig.DEBUG) Log.d(tag, msg); 25 + if (ENABLED) Log.d(tag, msg);
22 } 26 }
23 27
24 public static void d(String tag, String msg, Throwable tr) { 28 public static void d(String tag, String msg, Throwable tr) {
25 - if (BuildConfig.DEBUG) Log.d(tag, msg, tr); 29 + if (ENABLED) Log.d(tag, msg, tr);
26 } 30 }
27 } 31 }