|
|
@@ -1,14 +1,12 @@
|
|
|
package com.miekir.mvp.presenter;
|
|
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
-import androidx.lifecycle.Observer;
|
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
|
|
import com.miekir.mvp.bean.DataResult;
|
|
|
-import com.miekir.mvp.constant.BaseMvpResponse;
|
|
|
import com.miekir.mvp.constant.DialogAction;
|
|
|
+import com.miekir.mvp.jetpack.BeanLiveData;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
|
@@ -25,15 +23,31 @@ public abstract class BasePresenter extends ViewModel {
|
|
|
* ViewModel的生命周期和Activity保持一致;在onStart()、onResume()、onPause()时调用LiveData的setValue才会通知观察者
|
|
|
* liveData在ViewModel(Presenter)创建,然后提供方法给Activity获取,方便取消观察
|
|
|
*/
|
|
|
- private MutableLiveData<DataResult> liveData = new MutableLiveData<DataResult>();
|
|
|
- private MutableLiveData<DataResult> dialogLiveData = new MutableLiveData<DataResult>();
|
|
|
- public MutableLiveData<DataResult> getLiveData() {
|
|
|
- return liveData;
|
|
|
+ private MutableLiveData<DataResult<?>> commonLiveData = new MutableLiveData<>();
|
|
|
+ private MutableLiveData<DataResult<?>> dialogLiveData = new MutableLiveData<>();
|
|
|
+ public MutableLiveData<DataResult<?>> getCommonLiveData() {
|
|
|
+ return commonLiveData;
|
|
|
}
|
|
|
- public MutableLiveData<DataResult> getDialogLiveData() {
|
|
|
+ public MutableLiveData<DataResult<?>> getDialogLiveData() {
|
|
|
return dialogLiveData;
|
|
|
}
|
|
|
- private List<MutableLiveData<Object>> specificLiveDataList = new CopyOnWriteArrayList<>();
|
|
|
+
|
|
|
+ private List<BeanLiveData<?>> liveDataList = new CopyOnWriteArrayList<>();
|
|
|
+ public List<BeanLiveData<?>> getLiveDataList() {
|
|
|
+ return liveDataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> BeanLiveData<T> getLiveData(boolean withDialog) {
|
|
|
+ if (withDialog) {
|
|
|
+ showLoading();
|
|
|
+ }
|
|
|
+ BeanLiveData<T> liveData = new BeanLiveData<>(this, withDialog);
|
|
|
+ liveDataList.add(liveData);
|
|
|
+ return liveData;
|
|
|
+ }
|
|
|
+ protected <T> BeanLiveData<T> getLiveData() {
|
|
|
+ return getLiveData(false);
|
|
|
+ }
|
|
|
|
|
|
public void showLoading() {
|
|
|
showLoading("");
|
|
|
@@ -59,16 +73,21 @@ public abstract class BasePresenter extends ViewModel {
|
|
|
public abstract void init();
|
|
|
|
|
|
/**
|
|
|
- * 视图被销毁时
|
|
|
+ * 视图被销毁时,任务被取消,界面也不存在了
|
|
|
*/
|
|
|
public void detachView() {
|
|
|
- liveData = null;
|
|
|
+ commonLiveData = null;
|
|
|
dialogLiveData = null;
|
|
|
onTaskCancel();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 当任务被取消时
|
|
|
+ * 当任务被取消时,界面还存在
|
|
|
*/
|
|
|
- public abstract void onTaskCancel();
|
|
|
+ public void onTaskCancel() {
|
|
|
+ for (BeanLiveData<?> liveData : liveDataList) {
|
|
|
+ liveData.cancelObserve();
|
|
|
+ }
|
|
|
+ liveDataList.clear();
|
|
|
+ }
|
|
|
}
|