Commit 70670522355b965086fc90c89003afe93c2a0aea
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
Showing
18 changed files
with
312 additions
and
70 deletions
app/src/main/AndroidManifest.xml
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/ILaunchView.java
| 1 | 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 | |
| 12 | 14 | /** |
| 13 | 15 | * 完成数据加载 |
| 16 | + * | |
| 14 | 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 | 25 | * @param message 页面错误信息 |
| 22 | 26 | */ |
| 23 | 27 | void onLoadDataError(String code, String message); |
| ... | ... | @@ -28,13 +32,10 @@ public interface ILaunchView extends MvpView { |
| 28 | 32 | void onTimerFinish(); |
| 29 | 33 | |
| 30 | 34 | /** |
| 31 | - * 计时器剩余时间通知 | |
| 35 | + * 计时器剩余时间通知 | |
| 36 | + * | |
| 32 | 37 | * @param time 剩余时间 |
| 33 | 38 | */ |
| 34 | 39 | void onTimerInterval(long time); |
| 35 | 40 | |
| 36 | - /** | |
| 37 | - * 启动主程序 | |
| 38 | - */ | |
| 39 | - void startMainPage(); | |
| 40 | 41 | } |
| 41 | 42 | \ No newline at end of file | ... | ... |
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/presenter/LaunchPresenter.java
| 1 | 1 | package com.cnlive.libs.demo.mvptest.frame.presenter; |
| 2 | 2 | |
| 3 | +import android.app.Activity; | |
| 3 | 4 | import android.content.Context; |
| 5 | +import android.content.Intent; | |
| 4 | 6 | |
| 5 | 7 | import com.cnlive.libs.base.data.network.Subscriber; |
| 6 | 8 | import com.cnlive.libs.base.util.LogUtil; |
| 7 | 9 | import com.cnlive.libs.base.util.TimerUtil; |
| 8 | 10 | import com.cnlive.libs.demo.Config; |
| 11 | +import com.cnlive.libs.demo.MainActivity; | |
| 9 | 12 | import com.cnlive.libs.demo.data.model.AppInfo; |
| 10 | 13 | import com.cnlive.libs.demo.data.network.testapi.ApiRequest; |
| 11 | 14 | import com.cnlive.libs.demo.data.network.testapi.ApiService; |
| ... | ... | @@ -34,7 +37,7 @@ public class LaunchPresenter extends MvpBasePresenter<LaunchView> { |
| 34 | 37 | @Override |
| 35 | 38 | public void onCompleted(String code, String message, AppInfo data) { |
| 36 | 39 | ILaunchView view = getView(); |
| 37 | - if (view != null) view.onLoadDataComplete(null); | |
| 40 | + if (view != null) view.onLoadDataComplete(data); | |
| 38 | 41 | } |
| 39 | 42 | |
| 40 | 43 | @Override |
| ... | ... | @@ -63,7 +66,6 @@ public class LaunchPresenter extends MvpBasePresenter<LaunchView> { |
| 63 | 66 | timerUtil.setTotalTime(totalTime);//设置毫秒数 |
| 64 | 67 | timerUtil.setIntervalTime(1000);//设置间隔数 |
| 65 | 68 | timerUtil.start(); |
| 66 | - | |
| 67 | 69 | timerUtil.setTimerLiener(new TimerUtil.TimeListener() { |
| 68 | 70 | @Override |
| 69 | 71 | public void onFinish() { |
| ... | ... | @@ -80,11 +82,11 @@ public class LaunchPresenter extends MvpBasePresenter<LaunchView> { |
| 80 | 82 | }); |
| 81 | 83 | } |
| 82 | 84 | |
| 83 | - public void startMainPage() { | |
| 84 | - if (getView() == null) return; | |
| 85 | + public void startMainPage(Activity activity) { | |
| 85 | 86 | if (timerUtil != null) timerUtil.cancel(); |
| 86 | 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 | 1 | package com.cnlive.libs.demo.mvptest.frame.view; |
| 2 | 2 | |
| 3 | -import android.content.Intent; | |
| 4 | 3 | import android.view.View; |
| 5 | 4 | |
| 6 | -import com.cnlive.libs.demo.MainActivity; | |
| 5 | +import com.cnlive.libs.demo.data.model.AppInfo; | |
| 7 | 6 | import com.cnlive.libs.demo.mvptest.frame.ILaunchView; |
| 7 | +import com.cnlive.libs.demo.mvptest.model.LaunchPage; | |
| 8 | 8 | import com.cnlive.libs.demo.mvptest.ui.activity.LaunchActivity; |
| 9 | 9 | |
| 10 | 10 | |
| ... | ... | @@ -23,11 +23,16 @@ public class LaunchView implements ILaunchView { |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | @Override |
| 26 | - public void onLoadDataComplete(Object data) { | |
| 26 | + public void onLoadDataComplete(AppInfo data) { | |
| 27 | 27 | loadEnd(); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | @Override |
| 31 | + public void onRestoreDataComplete(LaunchPage data) { | |
| 32 | + activity.binding.setData(data); | |
| 33 | + } | |
| 34 | + | |
| 35 | + @Override | |
| 31 | 36 | public void onLoadDataError(String code, String message) { |
| 32 | 37 | loadEnd(); |
| 33 | 38 | } |
| ... | ... | @@ -40,13 +45,7 @@ public class LaunchView implements ILaunchView { |
| 40 | 45 | @Override |
| 41 | 46 | public void onTimerFinish() { |
| 42 | 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 | 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 | 2 | |
| 3 | 3 | import android.databinding.BaseObservable; |
| 4 | 4 | import android.databinding.Bindable; |
| 5 | +import android.os.Parcel; | |
| 6 | +import android.os.Parcelable; | |
| 5 | 7 | import android.view.View; |
| 6 | 8 | |
| 7 | 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 | 12 | private long time = -1; |
| 12 | 13 | private int timing = View.GONE; |
| 13 | 14 | private int loading = View.GONE; |
| ... | ... | @@ -42,4 +43,33 @@ public class LaunchPage extends BaseObservable { |
| 42 | 43 | this.loading = loading; |
| 43 | 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 | 5 | import android.os.Bundle; |
| 6 | 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 | 9 | import com.cnlive.libs.demo.R; |
| 10 | 10 | import com.cnlive.libs.demo.databinding.ActivityLaunchBinding; |
| 11 | 11 | import com.cnlive.libs.demo.mvptest.frame.presenter.LaunchPresenter; |
| 12 | 12 | import com.cnlive.libs.demo.mvptest.frame.view.LaunchView; |
| 13 | 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 | 17 | @Override |
| 18 | 18 | protected int getLayoutId() { |
| ... | ... | @@ -22,13 +22,11 @@ public class LaunchActivity extends MvpBindingActivity<LaunchView, LaunchPresent |
| 22 | 22 | @Override |
| 23 | 23 | protected void onCreate(Bundle savedInstanceState) { |
| 24 | 24 | super.onCreate(savedInstanceState); |
| 25 | - initData(); | |
| 26 | 25 | } |
| 27 | 26 | |
| 28 | 27 | @Override |
| 29 | 28 | protected void onNewIntent(Intent intent) { |
| 30 | 29 | super.onNewIntent(intent); |
| 31 | - initData(); | |
| 32 | 30 | } |
| 33 | 31 | |
| 34 | 32 | @Override |
| ... | ... | @@ -37,16 +35,17 @@ public class LaunchActivity extends MvpBindingActivity<LaunchView, LaunchPresent |
| 37 | 35 | getPresenter().destroy(); |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | - private void initData() { | |
| 41 | - getPresenter().initData(this); | |
| 42 | - } | |
| 43 | - | |
| 44 | 38 | public void onClick(View view){ |
| 45 | 39 | switch (view.getId()){ |
| 46 | 40 | case R.id.click: |
| 47 | - getPresenter().startMainPage(); | |
| 41 | + getViewData().setTime(2); | |
| 42 | + startActivity(new Intent(this,LaunchActivity.class)); | |
| 48 | 43 | break; |
| 49 | 44 | } |
| 50 | 45 | } |
| 51 | 46 | |
| 47 | + @Override | |
| 48 | + public void onNewViewStateInstance() { | |
| 49 | + getPresenter().initData(this); | |
| 50 | + } | |
| 52 | 51 | } | ... | ... |
cnlive/build.gradle
cnlive/src/main/java/com/cnlive/libs/base/data/network/RetrofitUtil.java
| ... | ... | @@ -3,13 +3,17 @@ package com.cnlive.libs.base.data.network; |
| 3 | 3 | |
| 4 | 4 | import android.content.Context; |
| 5 | 5 | |
| 6 | - | |
| 7 | 6 | import com.cnlive.libs.base.util.LogUtil; |
| 8 | 7 | |
| 8 | +import java.security.KeyStore; | |
| 9 | +import java.util.Arrays; | |
| 9 | 10 | import java.util.concurrent.TimeUnit; |
| 10 | 11 | |
| 11 | 12 | import javax.net.ssl.SSLContext; |
| 12 | 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 | 18 | import okhttp3.Cache; |
| 15 | 19 | import okhttp3.OkHttpClient; |
| ... | ... | @@ -42,10 +46,19 @@ public class RetrofitUtil { |
| 42 | 46 | OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder(); |
| 43 | 47 | |
| 44 | 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 | 57 | SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 46 | - sslContext.init(null, null, null); | |
| 58 | + sslContext.init(null, new TrustManager[]{trustManager}, null); | |
| 47 | 59 | SSLSocketFactory socketFactory = new Tls12SocketFactory(sslContext.getSocketFactory()); |
| 48 | - okHttpClientBuilder.sslSocketFactory(socketFactory); | |
| 60 | + | |
| 61 | + okHttpClientBuilder.sslSocketFactory(socketFactory, trustManager); | |
| 49 | 62 | } catch (Exception e) { |
| 50 | 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 | 22 | |
| 23 | 23 | public abstract class Subscriber<T extends IBaseInfo> { |
| 24 | 24 | |
| 25 | + private static final String TAG = "NetLoader"; | |
| 26 | + | |
| 25 | 27 | private Response<T> response; |
| 28 | + private Throwable response_error; | |
| 26 | 29 | private Context context; |
| 27 | 30 | private Config config; |
| 28 | 31 | |
| ... | ... | @@ -47,7 +50,7 @@ public abstract class Subscriber<T extends IBaseInfo> { |
| 47 | 50 | config.load_failure_message = reloadString(config.load_failure_message, R.string.load_failure_message); |
| 48 | 51 | config.load_error_message = reloadString(config.load_error_message, R.string.load_error_message); |
| 49 | 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 | 56 | public static Config newConfig() { |
| ... | ... | @@ -57,18 +60,21 @@ public abstract class Subscriber<T extends IBaseInfo> { |
| 57 | 60 | public Consumer<Result<T>> next = new Consumer<Result<T>>() { |
| 58 | 61 | @Override |
| 59 | 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 | 69 | public Consumer<Throwable> error = new Consumer<Throwable>() { |
| 65 | 70 | @Override |
| 66 | 71 | public void accept(Throwable e) throws Exception { |
| 67 | - LogUtil.d("NetLoader", "onError ", e); | |
| 72 | + LogUtil.d(TAG, "onError ", e); | |
| 68 | 73 | if (context != null && !NetworkUtil.isConnectInternet(context)) |
| 69 | 74 | onNetworkNoConnected(); |
| 70 | - else | |
| 75 | + else { | |
| 71 | 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<T extends IBaseInfo> { |
| 77 | 83 | public void run() throws Exception { |
| 78 | 84 | if (response == null) { |
| 79 | 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 | 90 | } else { |
| 82 | 91 | T pageData = response.body(); |
| 83 | 92 | |
| ... | ... | @@ -87,7 +96,8 @@ public abstract class Subscriber<T extends IBaseInfo> { |
| 87 | 96 | if (config.customLoadResponse != null && config.custom_map.containsKey(pageData.getCode())) |
| 88 | 97 | config.customLoadResponse.onCustomEvent(pageData.getCode(), pageData.getMessage(), pageData); |
| 89 | 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 | 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 | 1 | package com.cnlive.libs.base.frame.activity; |
| 2 | 2 | |
| 3 | 3 | import android.os.Bundle; |
| 4 | +import android.os.Parcelable; | |
| 4 | 5 | import android.support.annotation.NonNull; |
| 5 | 6 | |
| 6 | 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 | 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 | 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 | 18 | public V view; |
| 19 | + public MvpViewState<V, D> viewState; | |
| 18 | 20 | |
| 19 | 21 | protected abstract int getLayoutId(); |
| 20 | 22 | |
| ... | ... | @@ -35,10 +37,24 @@ public abstract class MvpViewStateActivity<V extends MvpView, P extends MvpPrese |
| 35 | 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 | 48 | @NonNull |
| 39 | 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 | 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 | 3 | import android.databinding.DataBindingUtil; |
| 4 | 4 | import android.databinding.ViewDataBinding; |
| 5 | 5 | import android.os.Bundle; |
| 6 | +import android.os.Parcelable; | |
| 6 | 7 | import android.support.annotation.NonNull; |
| 7 | 8 | |
| 8 | 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 | 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 | 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 | 20 | public T binding; |
| 19 | - public D data; | |
| 20 | 21 | |
| 21 | 22 | @Override |
| 22 | 23 | protected void onCreate(Bundle savedInstanceState) { |
| 23 | 24 | super.onCreate(savedInstanceState); |
| 24 | 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 | 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 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * MVP Databinding Base Fragment |
| 18 | + * | |
| 18 | 19 | * @param <V> MVP View {@link MvpView} |
| 19 | 20 | * @param <P> MVP Presenter {@link MvpPresenter} |
| 20 | 21 | * @param <D> Databind Page Data |
| ... | ... | @@ -39,7 +40,7 @@ public abstract class MvpBindingFragment<V extends MvpView, P extends MvpPresent |
| 39 | 40 | |
| 40 | 41 | @NonNull |
| 41 | 42 | public D getViewData() { |
| 42 | - if(data == null) data = initLayoutData(); | |
| 43 | + if (data == null) data = initLayoutData(); | |
| 43 | 44 | return data; |
| 44 | 45 | } |
| 45 | 46 | } |
| 46 | 47 | \ No newline at end of file | ... | ... |
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpFragment.java
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 | 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
cnlive/src/main/java/com/cnlive/libs/base/util/LogUtil.java
| ... | ... | @@ -2,26 +2,30 @@ package com.cnlive.libs.base.util; |
| 2 | 2 | |
| 3 | 3 | import android.util.Log; |
| 4 | 4 | |
| 5 | -import com.cnlive.libs.base.BuildConfig; | |
| 6 | - | |
| 7 | 5 | /** |
| 8 | 6 | * Log output tool |
| 9 | 7 | */ |
| 10 | 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 | 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 | 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 | 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 | 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 | } | ... | ... |