瀏覽代碼

剩余对话框封装问题

詹子聪 5 年之前
父節點
當前提交
4b8fc2cf7b

+ 17 - 0
mvp/src/main/java/com/miekir/mvp/bean/DataResult.java

@@ -1,6 +1,7 @@
 package com.miekir.mvp.bean;
 
 import com.miekir.mvp.constant.BaseMvpResponse;
+import com.miekir.mvp.model.DataSource;
 
 /**
  * Copyright (C), 2019-2020, Miekir
@@ -13,6 +14,7 @@ public class DataResult<D> {
     private int responseCode;
     private String message;
     private D bean;
+    private int dataSource = DataSource.DEFAULT;
 
     public DataResult(int responseCode, String message, D bean) {
         this.responseCode = responseCode;
@@ -20,6 +22,13 @@ public class DataResult<D> {
         this.bean = bean;
     }
 
+    public DataResult(int responseCode, String message, D bean, int dataSource) {
+        this.responseCode = responseCode;
+        this.message = message;
+        this.bean = bean;
+        this.dataSource = dataSource;
+    }
+
     public int getResponseCode() {
         return responseCode;
     }
@@ -47,4 +56,12 @@ public class DataResult<D> {
     public boolean isSuccess() {
         return responseCode == BaseMvpResponse.SUCCESS;
     }
+
+    public int getDataSource() {
+        return dataSource;
+    }
+
+    public void setDataSource(int dataSource) {
+        this.dataSource = dataSource;
+    }
 }

+ 4 - 0
mvp/src/main/java/com/miekir/mvp/jetpack/MvpLiveData.java

@@ -32,4 +32,8 @@ public class MvpLiveData<T> extends MutableLiveData<T> {
         observer.setLiveData(this);
         observeForever(observer);
     }
+
+    public void post(T dataBean) {
+        postValue(dataBean);
+    }
 }

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

@@ -10,6 +10,7 @@ import com.miekir.mvp.constant.DialogAction;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 /**
  * Copyright (C), 2019-2020, Miekir
@@ -25,25 +26,15 @@ public abstract class BasePresenter extends ViewModel {
      * liveData在ViewModel(Presenter)创建,然后提供方法给Activity获取,方便取消观察
      */
     private MutableLiveData<DataResult> liveData = new MutableLiveData<DataResult>();
-    /**
-     * 由于使用了AspectJ,这个变量名和属性不能改动,否则AspectJ会拿不到值
-     */
     private MutableLiveData<DataResult> dialogLiveData = new MutableLiveData<DataResult>();
-
     public MutableLiveData<DataResult> getLiveData() {
         return liveData;
     }
     public MutableLiveData<DataResult> getDialogLiveData() {
         return dialogLiveData;
     }
+    private List<MutableLiveData<Object>> specificLiveDataList = new CopyOnWriteArrayList<>();
 
-    private List<MutableLiveData<Object>> specificLiveDataList = new ArrayList<>();
-
-
-    /**
-     * 由于使用了AspectJ,这个方法名不能改动
-     * 对弹出对话框此时进行计数,归零才dismiss
-     */
     public void showLoading() {
         showLoading("");
     }
@@ -52,16 +43,14 @@ public abstract class BasePresenter extends ViewModel {
         if (dialogLiveData == null) {
             return;
         }
-        // todo 解决加载框的问题
-        //dialogLiveData.postValue(new DataResult(0, message, null, DialogAction.DIALOG_SHOW));
+        dialogLiveData.postValue(new DataResult(0, message, null, DialogAction.DIALOG_SHOW));
     }
 
     public void dismissLoading() {
         if (dialogLiveData == null) {
             return;
         }
-        // todo 解决加载框的问题
-        //dialogLiveData.postValue(new DataResult(0, null, null, DialogAction.DIALOG_DISMISS));
+        dialogLiveData.postValue(new DataResult(0, null, null, DialogAction.DIALOG_DISMISS));
     }
 
     /**

+ 9 - 10
mvp/src/main/java/com/miekir/mvp/view/ViewHelper.java

@@ -148,12 +148,11 @@ public class ViewHelper {
                 final Observer<DataResult> dialogObserver = new Observer<DataResult>() {
                     @Override
                     public void onChanged(final DataResult result) {
-                        // todo 加载框的问题
-//                        if (result.getDataSource() == DialogAction.DIALOG_SHOW) {
-//                            iView.onLoadTimeChange(true, result.getMessage());
-//                        } else if (result.getDataSource() == DialogAction.DIALOG_DISMISS) {
-//                            iView.onLoadTimeChange(false, result.getMessage());
-//                        }
+                        if (result.getDataSource() == DialogAction.DIALOG_SHOW) {
+                            iView.onLoadTimeChange(true, result.getMessage());
+                        } else if (result.getDataSource() == DialogAction.DIALOG_DISMISS) {
+                            iView.onLoadTimeChange(false, result.getMessage());
+                        }
                     }
                 };
 
@@ -196,10 +195,10 @@ public class ViewHelper {
                 dataSource = dataMethod.dataSource();
             }
 
-            // todo 加载框的问题
-//            if (dataSource != result.getDataSource()) {
-//                continue;
-//            }
+            // 加载框的问题
+            if (dataSource != result.getDataSource()) {
+                continue;
+            }
 
             // 判断方法的第三个参数类型是否是回调对象的父类
             Class<?> methodParamClass = method.getParameterTypes()[2];