Bladeren bron

增加注释

詹子聪 5 jaren geleden
bovenliggende
commit
a6494ca724

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

@@ -43,7 +43,7 @@ public class MainActivity extends BaseMvpActivity implements View.OnClickListene
         switch (v.getId()) {
             case R.id.view_test:
                 // 点击之后,后台执行耗时操作
-                Thread thread = presenter.go().observeOnce(new MvpObserver<DataResult<List<TestBean1>>, Thread>() {
+                presenter.go().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();

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

@@ -25,7 +25,6 @@ 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() {
             @Override
             public void run() {
@@ -44,9 +43,7 @@ public class TestPresenter extends BasePresenter {
                 }
             }
         });
-        liveData.setCancelableTask(thread);
         thread.start();
-
         return liveData;
     }
 
@@ -59,7 +56,6 @@ public class TestPresenter extends BasePresenter {
             } catch (Exception e) {
                 e.printStackTrace();
             }
-
         }
     }
 }

+ 6 - 4
mvp/src/main/java/com/miekir/mvp/jetpack/BeanLiveData.java

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

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

@@ -22,21 +22,22 @@ import com.miekir.mvp.presenter.BasePresenter;
  * @author Miekir
  * @date 2020/11/14 20:45
  * Description:
+ * @param <T> 监听者里面的数据类型
+ * @param <C> 返回的任务,是可取消的类型,一般定义为Void即可,除非要在Activity上执行特定操作取消某个特定任务
  */
 public class MvpLiveData<T, C> extends MutableLiveData<T> {
     protected BasePresenter mPresenter;
     protected boolean mWidthProgress;
+    public MvpLiveData(BasePresenter mPresenter, boolean withProgress) {
+        this.mPresenter = mPresenter;
+        this.mWidthProgress = withProgress;
+    }
 
     private C mTask;
     public void setCancelableTask(C cancelableTask) {
         mTask = cancelableTask;
     }
 
-    public MvpLiveData(BasePresenter mPresenter, boolean withProgress) {
-        this.mPresenter = mPresenter;
-        this.mWidthProgress = withProgress;
-    }
-
     private MvpObserver<T, C> mObserver;
     public void cancelObserve() {
         removeObserver(mObserver);

+ 3 - 1
mvp/src/main/java/com/miekir/mvp/jetpack/MvpObserver.java

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

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

@@ -37,6 +37,12 @@ public abstract class BasePresenter extends ViewModel {
         return liveDataList;
     }
 
+    /**
+     *
+     * @param withProgress 是否显示加载对话框
+     * @param <T> 监听者里面的数据类型
+     * @param <C> 返回的任务,应该是可取消的类型
+     */
     protected <T, C> BeanLiveData<T, C> getLiveData(boolean withProgress) {
         if (withProgress) {
             showLoading();
@@ -49,10 +55,17 @@ public abstract class BasePresenter extends ViewModel {
         return getLiveData(false);
     }
 
+    /**
+     * 显示默认文字的进度框
+     */
     public void showLoading() {
         showLoading("");
     }
 
+    /**
+     * 显示带有具体文字提示的进度对话框
+     * @param message 具体文字提示
+     */
     public void showLoading(String message) {
         if (dialogLiveData == null) {
             return;
@@ -60,6 +73,9 @@ public abstract class BasePresenter extends ViewModel {
         dialogLiveData.postValue(new DataResult(0, message, null, DialogAction.DIALOG_SHOW));
     }
 
+    /**
+     * 让进度对话框消失
+     */
     public void dismissLoading() {
         if (dialogLiveData == null) {
             return;