Skip to content

Commit

Permalink
优化mvp生命周期
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwfqin committed Aug 17, 2020
1 parent cc97a24 commit de443db
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 106 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
# 导入指南
``` groovy
def quicklib = "4.4.0"
def quicklib = "4.5.0"
// quicklib(Base)
implementation "com.sdwfqin.quicklib:quicklib:$quicklib"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sdwfqin.quickseed.mvpretrofit;

import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBaseView;

/**
* 描述:
Expand All @@ -24,7 +24,7 @@ public String toString() {
'}';
}

public boolean isOk(BaseView view) {
public boolean isOk(IBaseView view) {
if (statusCode == 1) {
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sdwfqin.quickseed.mvpretrofit;

import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBaseView;

/**
* 描述:网络统一异常处理
Expand All @@ -10,7 +10,7 @@
*/
public class NetworkError {

public static void error(BaseView view, Throwable throwable) {
public static void error(IBaseView view, Throwable throwable) {
RetrofitException.ResponeThrowable responeThrowable = RetrofitException.retrofitException(throwable);
// 此处可以通过判断错误代码来实现根据不同的错误代码做出相应的反应
switch (responeThrowable.code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBaseView;

import java.lang.ref.WeakReference;

Expand All @@ -19,9 +19,9 @@
*/
public abstract class RetrofitSubscriber<T> implements Observer<T> {

private final WeakReference<BaseView> mView;
private final WeakReference<IBaseView> mView;

public RetrofitSubscriber(BaseView view) {
public RetrofitSubscriber(IBaseView view) {
super();
mView = new WeakReference<>(view);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void initEventAndData() {

@Override
protected WeatherContract.WeatherPresenter createPresenter() {
return new WeatherPresenterImpl();
return new WeatherPresenterImpl(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sdwfqin.quickseed.ui.mvp;

import com.sdwfqin.quicklib.mvp.SamplePresenter;
import androidx.lifecycle.LifecycleOwner;

import com.sdwfqin.quicklib.mvp.BasePresenter;
import com.sdwfqin.quicklib.utils.rx.RxSchedulersUtils;
import com.sdwfqin.quickseed.mvpretrofit.RetrofitClient;
import com.sdwfqin.quickseed.mvpretrofit.RetrofitSubscriber;
Expand All @@ -17,7 +19,11 @@
* @author 张钦
* @date 2020/4/16
*/
public class WeatherPresenterImpl extends SamplePresenter<WeatherContract.WeatherView> implements WeatherContract.WeatherPresenter {
public class WeatherPresenterImpl extends BasePresenter<WeatherContract.WeatherView> implements WeatherContract.WeatherPresenter {

public WeatherPresenterImpl(LifecycleOwner owner) {
super(owner);
}

@Override
public void loadData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sdwfqin.quickseed.ui.mvp.contract;

import com.sdwfqin.quicklib.mvp.BasePresenter;
import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBasePresenter;
import com.sdwfqin.quicklib.mvp.IBaseView;
import com.sdwfqin.quickseed.ui.mvvm.WeatherBean;

/**
Expand All @@ -13,10 +13,10 @@
*/
public interface WeatherContract {

interface WeatherView extends BaseView {
interface WeatherView extends IBaseView {
void refreshView(WeatherBean weatherBean);
}
interface WeatherPresenter extends BasePresenter<WeatherView> {
interface WeatherPresenter extends IBasePresenter<WeatherView> {
void loadData();
}
}
29 changes: 15 additions & 14 deletions config.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
ext {

versionCode = 202007301
versionName = "4.4.0"
versionCode = 202008171
versionName = "4.5.0"

qmui = "2.0.0-alpha10"
okhttp = "4.7.2"
retrofit = "2.9.0"
glide = "4.11.0"
vlayout = "1.2.36"
camerax_version = "1.0.0-beta07"
smartrefresh = "2.0.0"
smartrefresh = "2.0.1"

androidVersion = [
compileSdkVersion: 29,
Expand Down Expand Up @@ -47,15 +47,16 @@ ext {

viewDependencies = [
// qmui
"qmui" : "com.qmuiteam:qmui:$qmui",
"arch" : "com.qmuiteam:arch:$qmui",
"qmui" : "com.qmuiteam:qmui:$qmui",
"arch" : "com.qmuiteam:arch:$qmui",
// brvah
"brvah" : "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4",
"brvah" : "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4",
// smartrefresh
"smartrefresh" : "com.scwang.smart:refresh-layout-kernel:$smartrefresh",
"smartrefresh-classics": "com.scwang.smart:refresh-header-classics:$smartrefresh",
"smartrefresh" : "com.scwang.smart:refresh-layout-kernel:$smartrefresh",
"smartrefresh-classics" : "com.scwang.smart:refresh-header-classics:$smartrefresh",
"smartrefresh-classics-footer": "com.scwang.smart:refresh-footer-classics:$smartrefresh",
// pickerview
"pickerview" : "com.contrarywind:Android-PickerView:4.1.9",
"pickerview" : "com.contrarywind:Android-PickerView:4.1.9",
]

imageDependencies = [
Expand All @@ -74,13 +75,13 @@ ext {
utilsDependencies = [
"permissionx": "com.permissionx.guolindev:permissionx:1.3.0",
// utils
"utilcodex" : "com.blankj:utilcodex:1.29.0",
"utilcodex" : "com.blankj:utilcodex:1.29.0",
// agentweb
"agentweb" : "com.just.agentweb:agentweb:4.1.3",
"agentweb" : "com.just.agentweb:agentweb:4.1.3",
// arouter
"arouter" : "com.alibaba:arouter-api:1.5.0",
"gson" : "com.google.code.gson:gson:2.8.6",
"zxing" : "com.google.zxing:core:3.4.0",
"arouter" : "com.alibaba:arouter-api:1.5.0",
"gson" : "com.google.code.gson:gson:2.8.6",
"zxing" : "com.google.zxing:core:3.4.0",
]

thirdDependencies = [
Expand Down
30 changes: 10 additions & 20 deletions quicklib/src/main/java/com/sdwfqin/quicklib/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.sdwfqin.quicklib.R;
import com.sdwfqin.quicklib.dialog.PermissionCustomDialog;
import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBaseView;
import com.sdwfqin.quicklib.utils.AppManager;
import com.sdwfqin.quicklib.utils.eventbus.Event;
import com.sdwfqin.quicklib.utils.eventbus.EventBusUtil;
import com.sdwfqin.quicklib.utils.rx.RxJavaLifecycleManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

/**
* 描述:Activity基类
*
* @author 张钦
*/
public abstract class BaseActivity<V extends ViewBinding> extends QMUIActivity implements BaseView {
public abstract class BaseActivity<V extends ViewBinding> extends QMUIActivity implements IBaseView {

protected Activity mContext;
protected V mBinding;
protected LinearLayoutCompat mQuickBaseView;
/**
* Rxjava 订阅管理
*/
protected CompositeDisposable mCompositeDisposable;
protected RxJavaLifecycleManager mRxJavaLifecycleManager;
/**
* 顶部标题栏
*/
Expand All @@ -59,6 +59,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mTopBar = findViewById(R.id.quick_base_topbar);
mContext = this;
AppManager.addActivity(this);
mRxJavaLifecycleManager = new RxJavaLifecycleManager(this);
initPresenter();
initViewModel();
initEventAndData();
Expand All @@ -84,7 +85,6 @@ protected void onStop() {

@Override
protected void onDestroy() {
unSubscribe();
removePresenter();
AppManager.removeActivity(this);
super.onDestroy();
Expand Down Expand Up @@ -212,21 +212,7 @@ public void startActivitySample(Class<?> cls) {
*/
@Override
public void addSubscribe(Disposable subscription) {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
mCompositeDisposable.add(subscription);
}

/**
* RxJava 解除所有订阅者
*/
public void unSubscribe() {
if (mCompositeDisposable != null) {
mCompositeDisposable.dispose();
mCompositeDisposable.clear();
mCompositeDisposable = new CompositeDisposable();
}
mRxJavaLifecycleManager.addDisposable(subscription);
}

// ==================== 权限管理 ====================
Expand Down Expand Up @@ -273,6 +259,10 @@ protected void initPresenter() {

}

/**
* 改为Lifecycle管理
*/
@Deprecated
protected void removePresenter() {

}
Expand Down
30 changes: 10 additions & 20 deletions quicklib/src/main/java/com/sdwfqin/quicklib/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.mvp.IBaseView;
import com.sdwfqin.quicklib.utils.eventbus.Event;
import com.sdwfqin.quicklib.utils.eventbus.EventBusUtil;
import com.sdwfqin.quicklib.utils.rx.RxJavaLifecycleManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

/**
Expand All @@ -30,7 +30,7 @@
* @author 张钦
* @date 2017/8/3
*/
public abstract class BaseFragment<V extends ViewBinding> extends Fragment implements BaseView {
public abstract class BaseFragment<V extends ViewBinding> extends Fragment implements IBaseView {

protected V mBinding;
protected BaseActivity mActivity;
Expand All @@ -53,7 +53,7 @@ public abstract class BaseFragment<V extends ViewBinding> extends Fragment imple
@Deprecated
protected boolean mIsLoad = false;
private QMUITipDialog mQmuiTipDialog;
protected CompositeDisposable mCompositeDisposable;
protected RxJavaLifecycleManager mRxJavaLifecycleManager;

/**
* Fragment的UI是否是可见
Expand All @@ -76,6 +76,7 @@ public void setUserVisibleHint(boolean isVisibleToUser) {
public void onAttach(Context context) {
mActivity = (BaseActivity) context;
mContext = context;
mRxJavaLifecycleManager = new RxJavaLifecycleManager(this);
super.onAttach(context);
}

Expand Down Expand Up @@ -136,7 +137,6 @@ private void baseLazyLoad() {
@Override
public void onDestroyView() {
mIsPrepared = false;
unSubscribe();
removePresenter();
mBinding = null;
super.onDestroyView();
Expand Down Expand Up @@ -256,21 +256,7 @@ public void startActivitySample(Class<?> cls) {
*/
@Override
public void addSubscribe(Disposable subscription) {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
mCompositeDisposable.add(subscription);
}

/**
* RxJava 解除所有订阅者
*/
public void unSubscribe() {
if (mCompositeDisposable != null) {
mCompositeDisposable.dispose();
mCompositeDisposable.clear();
mCompositeDisposable = new CompositeDisposable();
}
mRxJavaLifecycleManager.addDisposable(subscription);
}

protected void initViewModel() {
Expand All @@ -281,6 +267,10 @@ protected void initPresenter() {

}

/**
* 改为Lifecycle管理
*/
@Deprecated
protected void removePresenter() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author 张钦
*/
public abstract class BaseMvpActivity<V extends ViewBinding, T extends BasePresenter> extends BaseActivity<V> {
public abstract class BaseMvpActivity<V extends ViewBinding, T extends IBasePresenter> extends BaseActivity<V> {

protected T mPresenter;

Expand All @@ -21,12 +21,12 @@ protected void initPresenter() {
}
}

@Override
protected void removePresenter() {
if (mPresenter != null) {
mPresenter.detachView();
}
}
// @Override
// protected void removePresenter() {
// if (mPresenter != null) {
// mPresenter.detachView();
// }
// }

/**
* 创建Presenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author 张钦
* @date 2017/8/3
*/
public abstract class BaseMvpFragment<V extends ViewBinding, T extends BasePresenter> extends BaseFragment<V> {
public abstract class BaseMvpFragment<V extends ViewBinding, T extends IBasePresenter> extends BaseFragment<V> {

protected T mPresenter;

Expand Down
Loading

0 comments on commit de443db

Please sign in to comment.