Kaynağa Gözat

增加没有包装类的情况

詹子聪 5 yıl önce
ebeveyn
işleme
6e4ff962ea

+ 10 - 4
app/src/main/java/com/miekir/newmvp/MainActivity.java

@@ -6,7 +6,6 @@ import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import com.miekir.mvp.bean.DataResult;
 import com.miekir.mvp.jetpack.MvpObserver;
 import com.miekir.mvp.presenter.InjectPresenter;
 import com.miekir.mvp.view.BaseMvpActivity;
@@ -43,10 +42,17 @@ public class MainActivity extends BaseMvpActivity implements View.OnClickListene
         switch (v.getId()) {
             case R.id.view_test:
                 // 点击之后,后台执行耗时操作
-                presenter.go().observeOnce(new MvpObserver<DataResult<List<TestBean1>>, Thread>() {
+//                presenter.goWithResult().observeOnce(new MvpObserver<DataResult<List<TestBean1>>, Thread>() {
+//                    @Override
+//                    public void onDataChanged(DataResult<List<TestBean1>> result) {
+//                        Toast.makeText(getApplicationContext(), result.getBean().get(0).getName(), Toast.LENGTH_SHORT).show();
+//                    }
+//                });
+
+                presenter.goWithoutResult().observeOnce(new MvpObserver<List<TestBean1>, Void>() {
                     @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();
                     }
                 });
                 break;

+ 29 - 11
app/src/main/java/com/miekir/newmvp/TestPresenter.java

@@ -3,6 +3,7 @@ package com.miekir.newmvp;
 import android.util.Log;
 
 import com.miekir.mvp.jetpack.BeanLiveData;
+import com.miekir.mvp.jetpack.MvpLiveData;
 import com.miekir.mvp.presenter.BasePresenter;
 
 import java.util.ArrayList;
@@ -22,10 +23,10 @@ public class TestPresenter extends BasePresenter {
 
     }
 
-    private Thread thread;
-    public BeanLiveData<List<TestBean1>, Thread> go() {
-        final BeanLiveData<List<TestBean1>, Thread> liveData = getLiveData(true);
-        thread = new Thread(new Runnable() {
+
+    public BeanLiveData<List<TestBean1>, Thread> goWithResult() {
+        final BeanLiveData<List<TestBean1>, Thread> liveData = getResultLiveData(true);
+        Thread thread = new Thread(new Runnable() {
             @Override
             public void run() {
                 try {
@@ -47,15 +48,32 @@ public class TestPresenter extends BasePresenter {
         return liveData;
     }
 
+    public MvpLiveData<List<TestBean1>, Void> goWithoutResult() {
+        final MvpLiveData<List<TestBean1>, Void> liveData = getRawLiveData(true);
+        Thread thread = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    for (int i = 0; i < 20; i++) {
+                        Thread.sleep(1000);
+                        Log.i("test", "io thread..." + i);
+                    }
+
+                    TestBean1 bean = new TestBean1(3, "ViewModel Jason");
+                    List<TestBean1> list = new ArrayList<>();
+                    list.add(bean);
+                    liveData.post(list);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+        thread.start();
+        return liveData;
+    }
+
     @Override
     public void onTaskCancel() {
         super.onTaskCancel();
-        if (thread != null) {
-            try {
-                thread.interrupt();
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
     }
 }

+ 13 - 5
mvp/src/main/java/com/miekir/mvp/jetpack/MvpLiveData.java

@@ -33,11 +33,6 @@ public class MvpLiveData<T, C> extends MutableLiveData<T> {
         this.mWidthProgress = withProgress;
     }
 
-    private C mTask;
-    public void setCancelableTask(C cancelableTask) {
-        mTask = cancelableTask;
-    }
-
     private MvpObserver<T, C> mObserver;
     public void cancelObserve() {
         removeObserver(mObserver);
@@ -46,6 +41,10 @@ public class MvpLiveData<T, C> extends MutableLiveData<T> {
         mPresenter = null;
     }
 
+    private C mTask;
+    public void setCancelableTask(C cancelableTask) {
+        mTask = cancelableTask;
+    }
     /**
      * 只监听一次的LiveData
      * @param observer
@@ -58,6 +57,15 @@ public class MvpLiveData<T, C> extends MutableLiveData<T> {
     }
 
     public void post(T dataBean) {
+        if (mPresenter == null) {
+            return;
+        }
+
+        if (mWidthProgress) {
+            mPresenter.dismissLoading();
+        }
+
+        mPresenter.getLiveDataList().remove(this);
         postValue(dataBean);
     }
 }

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

@@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel;
 import com.miekir.mvp.bean.DataResult;
 import com.miekir.mvp.constant.DialogAction;
 import com.miekir.mvp.jetpack.BeanLiveData;
+import com.miekir.mvp.jetpack.MvpLiveData;
 
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
@@ -32,8 +33,8 @@ public abstract class BasePresenter extends ViewModel {
         return dialogLiveData;
     }
 
-    private List<BeanLiveData<?, ?>> liveDataList = new CopyOnWriteArrayList<>();
-    public List<BeanLiveData<?, ?>> getLiveDataList() {
+    private List<MvpLiveData<?, ?>> liveDataList = new CopyOnWriteArrayList<>();
+    public List<MvpLiveData<?, ?>> getLiveDataList() {
         return liveDataList;
     }
 
@@ -43,7 +44,7 @@ public abstract class BasePresenter extends ViewModel {
      * @param <T> 监听者里面的数据类型
      * @param <C> 返回的任务,应该是可取消的类型
      */
-    protected <T, C> BeanLiveData<T, C> getLiveData(boolean withProgress) {
+    protected <T, C> BeanLiveData<T, C> getResultLiveData(boolean withProgress) {
         if (withProgress) {
             showLoading();
         }
@@ -51,8 +52,16 @@ public abstract class BasePresenter extends ViewModel {
         liveDataList.add(liveData);
         return liveData;
     }
-    protected <T> BeanLiveData<T, ?> getLiveData() {
-        return getLiveData(false);
+    protected <T, C> MvpLiveData<T, C> getRawLiveData(boolean withProgress) {
+        if (withProgress) {
+            showLoading();
+        }
+        MvpLiveData<T, C> liveData = new MvpLiveData<>(this, withProgress);
+        liveDataList.add(liveData);
+        return liveData;
+    }
+    protected <T> BeanLiveData<T, ?> getResultLiveData() {
+        return getResultLiveData(false);
     }
 
     /**
@@ -101,7 +110,7 @@ public abstract class BasePresenter extends ViewModel {
      * 当任务被取消时,界面还存在
      */
     public void onTaskCancel() {
-        for (BeanLiveData<?, ?> liveData : liveDataList) {
+        for (MvpLiveData<?, ?> liveData : liveDataList) {
             liveData.cancelObserve();
         }
         liveDataList.clear();