Browse Source

安全可行的但是太丑陋了

詹子聪 5 years ago
parent
commit
ad35eb68a0

+ 16 - 11
app/src/main/java/com/miekir/newmvp/MainActivity.java

@@ -6,7 +6,9 @@ import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import com.miekir.mvp.jetpack.ResultObserver;
+import com.miekir.mvp.bean.DataResult;
+import com.miekir.mvp.jetpack.BaseObserver;
+import com.miekir.mvp.jetpack.WrapLiveData;
 import com.miekir.mvp.presenter.InjectPresenter;
 import com.miekir.mvp.view.BaseMvpActivity;
 
@@ -42,19 +44,22 @@ public class MainActivity extends BaseMvpActivity implements View.OnClickListene
         switch (v.getId()) {
             case R.id.view_test:
                 // 点击之后,后台执行耗时操作
-//                presenter.goWithResult().observeOnce(new MvpObserver<DataResult<List<TestBean1>>, Thread>() {
+                WrapLiveData<List<TestBean1>> liveData = (WrapLiveData<List<TestBean1>>) presenter.<List<TestBean1>>getWrapperLiveData(true)
+                        .observeOnce(new BaseObserver<DataResult<List<TestBean1>>>() {
+                            @Override
+                            public void onDataChanged(DataResult<List<TestBean1>> listDataResult) {
+                                Toast.makeText(getApplicationContext(), listDataResult.getBean().get(0).getName(), Toast.LENGTH_SHORT).show();
+                            }
+                        });
+                presenter.goWithWrapper(liveData);
+
+//                RawLiveData<List<TestBean1>> liveData = presenter.<List<TestBean1>>getRawLiveData(true).observeOnce(new BaseObserver<List<TestBean1>>() {
 //                    @Override
-//                    public void onDataChanged(DataResult<List<TestBean1>> result) {
-//                        Toast.makeText(getApplicationContext(), result.getBean().get(0).getName(), Toast.LENGTH_SHORT).show();
+//                    public void onDataChanged(List<TestBean1> testBean1s) {
+//                        Toast.makeText(getApplicationContext(), testBean1s.get(0).getName(), Toast.LENGTH_SHORT).show();
 //                    }
 //                });
-
-                presenter.goWithoutWrap().observeOnce(new ResultObserver<List<TestBean1>, Void>() {
-                    @Override
-                    public void onDataChanged(List<TestBean1> testBean1s) {
-                        Toast.makeText(getApplicationContext(), testBean1s.get(0).getName(), Toast.LENGTH_SHORT).show();
-                    }
-                });
+//                presenter.goWithoutWrap(liveData);
                 break;
             default:
                 break;

+ 4 - 6
app/src/main/java/com/miekir/newmvp/TestPresenter.java

@@ -24,8 +24,7 @@ public class TestPresenter extends BasePresenter {
     }
 
 
-    public WrapLiveData<List<TestBean1>, Thread> goWithWrapper() {
-        final WrapLiveData<List<TestBean1>, Thread> liveData = getWrapperLiveData(true);
+    public Thread goWithWrapper(final WrapLiveData<List<TestBean1>> liveData) {
         Thread thread = new Thread(new Runnable() {
             @Override
             public void run() {
@@ -45,11 +44,10 @@ public class TestPresenter extends BasePresenter {
             }
         });
         thread.start();
-        return liveData;
+        return thread;
     }
 
-    public RawLiveData<List<TestBean1>, Void> goWithoutWrap() {
-        final RawLiveData<List<TestBean1>, Void> liveData = getRawLiveData(true);
+    public Thread goWithoutWrap(final RawLiveData<List<TestBean1>> liveData) {
         Thread thread = new Thread(new Runnable() {
             @Override
             public void run() {
@@ -69,7 +67,7 @@ public class TestPresenter extends BasePresenter {
             }
         });
         thread.start();
-        return liveData;
+        return thread;
     }
 
     @Override

+ 3 - 4
mvp/src/main/java/com/miekir/mvp/jetpack/ResultObserver.java

@@ -9,11 +9,10 @@ import androidx.lifecycle.Observer;
  * @date 2020/11/14 20:51
  * Description:
  * @param <T> 监听者里面的数据类型
- * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
  */
-public abstract class ResultObserver<T, C> implements Observer<T> {
-    private RawLiveData<T, C> liveData;
-    public void setLiveData(RawLiveData<T, C> liveData) {
+public abstract class BaseObserver<T> implements Observer<T> {
+    private RawLiveData<T> liveData;
+    public void setLiveData(RawLiveData<T> liveData) {
         this.liveData = liveData;
     }
 

+ 4 - 9
mvp/src/main/java/com/miekir/mvp/jetpack/RawLiveData.java

@@ -23,9 +23,8 @@ import com.miekir.mvp.presenter.BasePresenter;
  * @date 2020/11/14 20:45
  * Description:
  * @param <T> 监听者里面的数据类型
- * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
  */
-public class RawLiveData<T, C> extends MutableLiveData<T> {
+public class RawLiveData<T> extends MutableLiveData<T> {
     protected BasePresenter mPresenter;
     protected boolean mWidthProgress;
     public RawLiveData(BasePresenter mPresenter, boolean withProgress) {
@@ -33,7 +32,7 @@ public class RawLiveData<T, C> extends MutableLiveData<T> {
         this.mWidthProgress = withProgress;
     }
 
-    private ResultObserver<T, C> mObserver;
+    private BaseObserver<T> mObserver;
     public void cancelObserve() {
         mObserver.setLiveData(null);
         removeObserver(mObserver);
@@ -41,19 +40,15 @@ public class RawLiveData<T, C> extends MutableLiveData<T> {
         mPresenter = null;
     }
 
-    private C mTask;
-    public void setCancelableTask(C cancelableTask) {
-        mTask = cancelableTask;
-    }
     /**
      * 只监听一次的LiveData
      * @param observer
      */
-    public C observeOnce(ResultObserver<T, C> observer) {
+    public RawLiveData<T> observeOnce(BaseObserver<T> observer) {
         mObserver = observer;
         observer.setLiveData(this);
         observeForever(observer);
-        return mTask;
+        return this;
     }
 
     public void post(T dataBean) {

+ 1 - 2
mvp/src/main/java/com/miekir/mvp/jetpack/WrapLiveData.java

@@ -11,9 +11,8 @@ import com.miekir.mvp.presenter.BasePresenter;
  * @date 2020/11/14 21:36
  * Description:
  * @param <T> 监听者里面的数据类型
- * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
  */
-public class WrapLiveData<T, C> extends RawLiveData<DataResult<T>, C> {
+public class WrapLiveData<T> extends RawLiveData<DataResult<T>> {
     public WrapLiveData(BasePresenter mPresenter, boolean withProgress) {
         super(mPresenter, withProgress);
     }

+ 68 - 0
mvp/src/main/java/com/miekir/mvp/jetpack/backup/RawLiveData.java

@@ -0,0 +1,68 @@
+package com.miekir.mvp.jetpack.backup;
+
+//得到一个LiveData,用于传递Observer参数的形式,这里暂时用不到
+//private <T> MutableLiveData<T> observeOnce(final Observer<T> observer) {
+//final MutableLiveData<T> specificLiveData = new MutableLiveData<>();
+//        specificLiveData.observeForever(new Observer<T>() {
+//@Override
+//public void onChanged(T t) {
+//specificLiveData.removeObserver(this);
+//observer.onChanged(t);
+//}
+//});
+//return specificLiveData;
+//}
+
+/**
+ * Copyright (C), 2019-2020, Miekir
+ *
+ * @author Miekir
+ * @date 2020/11/14 20:45
+ * Description:
+ * @param <T> 监听者里面的数据类型
+ * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
+ */
+//public class RawLiveData<T, C> extends MutableLiveData<T> {
+//    protected BasePresenter mPresenter;
+//    protected boolean mWidthProgress;
+//    public RawLiveData(BasePresenter mPresenter, boolean withProgress) {
+//        this.mPresenter = mPresenter;
+//        this.mWidthProgress = withProgress;
+//    }
+//
+//    private ResultObserver<T, C> mObserver;
+//    public void cancelObserve() {
+//        mObserver.setLiveData(null);
+//        removeObserver(mObserver);
+//        mObserver = null;
+//        mPresenter = null;
+//    }
+//
+//    private C mTask;
+//    public void setCancelableTask(C cancelableTask) {
+//        mTask = cancelableTask;
+//    }
+//    /**
+//     * 只监听一次的LiveData
+//     * @param observer
+//     */
+//    public C observeOnce(ResultObserver<T, C> observer) {
+//        mObserver = observer;
+//        observer.setLiveData(this);
+//        observeForever(observer);
+//        return mTask;
+//    }
+//
+//    public void post(T dataBean) {
+//        if (mPresenter == null) {
+//            return;
+//        }
+//
+//        if (mWidthProgress) {
+//            mPresenter.dismissLoading();
+//        }
+//
+//        mPresenter.getLiveDataList().remove(this);
+//        postValue(dataBean);
+//    }
+//}

+ 38 - 0
mvp/src/main/java/com/miekir/mvp/jetpack/backup/WrapLiveData.java

@@ -0,0 +1,38 @@
+package com.miekir.mvp.jetpack.backup;
+
+/**
+ * Copyright (C), 2019-2020, Miekir
+ *
+ * @author Miekir
+ * @date 2020/11/14 21:36
+ * Description:
+ * @param <T> 监听者里面的数据类型
+ * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
+ */
+//public class WrapLiveData<T, C> extends RawLiveData<DataResult<T>, C> {
+//    public WrapLiveData(BasePresenter mPresenter, boolean withProgress) {
+//        super(mPresenter, withProgress);
+//    }
+//
+//    public void post(int responseCode, String message, T dataBean) {
+//        if (mPresenter == null) {
+//            return;
+//        }
+//
+//        if (mWidthProgress) {
+//            mPresenter.dismissLoading();
+//        }
+//
+//        mPresenter.getLiveDataList().remove(this);
+//        postValue(new DataResult<T>(responseCode, message, dataBean));
+//    }
+//
+//    public void postSuccess(T dataBean) {
+//        post(MvpResponse.SUCCESS, "", dataBean);
+//    }
+//
+//    public void postFail(String message) {
+//        post(MvpResponse.FAIL, message, null);
+//    }
+//
+//}

+ 8 - 8
mvp/src/main/java/com/miekir/mvp/presenter/BasePresenter.java

@@ -33,8 +33,8 @@ public abstract class BasePresenter extends ViewModel {
         return dialogLiveData;
     }
 
-    private List<RawLiveData<?, ?>> liveDataList = new CopyOnWriteArrayList<>();
-    public List<RawLiveData<?, ?>> getLiveDataList() {
+    private List<RawLiveData<?>> liveDataList = new CopyOnWriteArrayList<>();
+    public List<RawLiveData<?>> getLiveDataList() {
         return liveDataList;
     }
 
@@ -42,21 +42,21 @@ public abstract class BasePresenter extends ViewModel {
      * 返回带有结果状态的实体
      * @param withProgress 是否显示加载对话框
      * @param <T> 监听者里面的数据类型
-     * @param <C> 返回的任务,应该是可取消的类型
+     * @param <P> 具体的Presenter
      */
-    protected <T, C> WrapLiveData<T, C> getWrapperLiveData(boolean withProgress) {
+    public <T> WrapLiveData<T> getWrapperLiveData(boolean withProgress) {
         if (withProgress) {
             showLoading();
         }
-        WrapLiveData<T, C> liveData = new WrapLiveData<>(this, withProgress);
+        WrapLiveData<T> liveData = new WrapLiveData<>(this, withProgress);
         liveDataList.add(liveData);
         return liveData;
     }
-    protected <T, C> RawLiveData<T, C> getRawLiveData(boolean withProgress) {
+    public <T> RawLiveData<T> getRawLiveData(boolean withProgress) {
         if (withProgress) {
             showLoading();
         }
-        RawLiveData<T, C> liveData = new RawLiveData<>(this, withProgress);
+        RawLiveData<T> liveData = new RawLiveData<>(this, withProgress);
         liveDataList.add(liveData);
         return liveData;
     }
@@ -107,7 +107,7 @@ public abstract class BasePresenter extends ViewModel {
      * 当任务被取消时,界面还存在
      */
     public void onTaskCancel() {
-        for (RawLiveData<?, ?> liveData : liveDataList) {
+        for (RawLiveData<?> liveData : liveDataList) {
             liveData.cancelObserve();
         }
         liveDataList.clear();