Skip to content

Commit

Permalink
添加Mvp模式支持
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwfqin committed Feb 8, 2018
1 parent 19487d6 commit b2ee2ae
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 49 deletions.
11 changes: 8 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.github.bumptech.glide:glide:4.5.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'

implementation 'com.sdwfqin.quicklib:quicklib:1.0.7'
implementation 'com.sdwfqin.quicklib:quicklib:1.1.0'

> 最低支持api16,编译版本27,gradle4.1
Expand All @@ -20,6 +20,11 @@

# 当前项目依赖Qmui 1.0.7,后期可能会脱离出来

# 支持Mvp与Mvc模式

1. 如果使用Mvc模式,直接继承BaseActivity/BaseFragment即可
2. 如果使用Mvp模式,需要继承MvpActivity/MvpFragment,并且Contract接口或Presenter/View接口需要继承BaseView与BasePresenter<T extends BaseView>,Presenter实现类可以直接实现Presenter接口也可以继承RxPresenter<T extends BaseView>类并实现Presenter接口,他们的区别是RxPresenter里面实现了BasePresenter的接口处理了View绑定并且添加了对RxJava事件的处理

# Apk http://fir.im/x97v

# 本项目中的支付与二维码模块clone自RxTools
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/sdwfqin/quickseed/contract/MainContract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sdwfqin.quickseed.contract;

import com.sdwfqin.quicklib.base.BasePresenter;
import com.sdwfqin.quicklib.base.BaseView;

/**
* 描述:为啥是空的呢?因为没有module啊。
*
* @author zhangqin
* @date 2018/2/8
*/
public interface MainContract {

interface View extends BaseView {
}

interface Presenter extends BasePresenter<View> {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sdwfqin.quickseed.presenter;

import com.sdwfqin.quicklib.base.RxPresenter;
import com.sdwfqin.quickseed.contract.MainContract;

/**
* 描述:
*
* @author zhangqin
* @date 2018/2/8
*/
public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

}
16 changes: 11 additions & 5 deletions app/src/main/java/com/sdwfqin/quickseed/ui/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.sdwfqin.quickseed.ui;

import android.Manifest;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
Expand All @@ -12,13 +10,16 @@
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.sdwfqin.quicklib.base.BaseActivity;
import com.sdwfqin.quicklib.base.BasePresenter;
import com.sdwfqin.quicklib.base.MvpActivity;
import com.sdwfqin.quicklib.view.NoScrollViewPager;
import com.sdwfqin.quickseed.R;
import com.sdwfqin.quickseed.base.Constants;
import com.sdwfqin.quickseed.contract.MainContract;
import com.sdwfqin.quickseed.presenter.MainPresenter;
import com.sdwfqin.quickseed.ui.find.FindFragment;
import com.sdwfqin.quickseed.ui.home.HomeFragment;
import com.sdwfqin.quickseed.ui.my.MyFragment;
import com.sdwfqin.quicklib.view.NoScrollViewPager;

import java.util.HashMap;
import java.util.List;
Expand All @@ -31,7 +32,7 @@
*
* @author 张钦
*/
public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
public class MainActivity extends MvpActivity implements MainContract.View, EasyPermissions.PermissionCallbacks {

@BindView(R.id.pager)
NoScrollViewPager mPager;
Expand Down Expand Up @@ -65,6 +66,11 @@ protected void initEventAndData() {

}

@Override
protected BasePresenter createPresenter() {
return new MainPresenter();
}

private void initTabs() {
int normalColor = QMUIResHelper.getAttrColor(mContext, R.attr.qmui_config_color_gray_6);
int selectColor = QMUIResHelper.getAttrColor(mContext, R.attr.qmui_config_color_blue);
Expand Down
2 changes: 1 addition & 1 deletion quicklib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.7"
version = "1.1.0"

// gradlew install
// gradlew bintrayUpload
Expand Down
34 changes: 14 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 @@ -4,7 +4,6 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
Expand All @@ -18,19 +17,16 @@
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
* 描述:Activity基类
*
* @author 张钦
*/
public abstract class BaseActivity extends AppCompatActivity {
public abstract class BaseActivity extends AppCompatActivity implements BaseView {

protected Activity mContext;
private QMUITipDialog mQmuiTipDialog;
protected CompositeDisposable mCompositeDisposable;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -40,6 +36,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mContext = this;
AppManager.addActivity(this);
QMUIStatusBarHelper.setStatusBarLightMode(mContext);
initPresenter();
initEventAndData();
}

Expand All @@ -61,24 +58,10 @@ protected void onStop() {

@Override
protected void onDestroy() {
if (mCompositeDisposable != null) {
mCompositeDisposable.dispose();
}
removePresenter();
super.onDestroy();
}

/**
* Rx事件管理
*
* @param subscription
*/
protected void addSubscribe(Disposable subscription) {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
mCompositeDisposable.add(subscription);
}

/**
* 是否注册事件分发
*
Expand Down Expand Up @@ -125,13 +108,15 @@ protected void receiveStickyEvent(Event event) {
*
* @param msg
*/
@Override
public void showMsg(String msg) {
ToastUtils.showShort(msg);
}

/**
* 开启加载动画
*/
@Override
public void showProgress() {
if (mQmuiTipDialog == null) {
mQmuiTipDialog = new QMUITipDialog.Builder(mContext)
Expand All @@ -147,6 +132,7 @@ public void showProgress() {
/**
* 关闭加载动画
*/
@Override
public void hideProgress() {
if (mQmuiTipDialog != null) {
if (mQmuiTipDialog.isShowing()) {
Expand Down Expand Up @@ -174,6 +160,14 @@ public void networkError(int errorCode, String errorMsg) {
hideProgress();
}

protected void initPresenter() {

}

protected void removePresenter() {

}

/**
* 加载布局
*/
Expand Down
35 changes: 15 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 @@ -8,7 +8,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
Expand All @@ -21,16 +20,14 @@

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
* 描述:Fragment基类
*
* @author 张钦
* @date 2017/8/3
*/
public abstract class BaseFragment extends Fragment {
public abstract class BaseFragment extends Fragment implements BaseView {

protected View mView;
protected Activity mActivity;
Expand All @@ -51,7 +48,6 @@ public abstract class BaseFragment extends Fragment {
protected boolean isLoad = false;
private Unbinder mUnBinder;
private QMUITipDialog mQmuiTipDialog;
protected CompositeDisposable mCompositeDisposable;

/**
* Fragment的UI是否是可见
Expand Down Expand Up @@ -91,6 +87,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mUnBinder = ButterKnife.bind(this, view);
mInflater = onGetLayoutInflater(savedInstanceState);
initPresenter();
initEventAndData();
// 界面加载完成
isPrepared = true;
Expand Down Expand Up @@ -126,25 +123,11 @@ private void baseLazyLoad() {

@Override
public void onDestroyView() {
if (mCompositeDisposable != null) {
mCompositeDisposable.dispose();
}
removePresenter();
mUnBinder.unbind();
super.onDestroyView();
}

/**
* Rx事件管理
*
* @param subscription
*/
protected void addSubscribe(Disposable subscription) {
if (mCompositeDisposable == null) {
mCompositeDisposable = new CompositeDisposable();
}
mCompositeDisposable.add(subscription);
}

/**
* 是否注册事件分发
*
Expand Down Expand Up @@ -191,6 +174,7 @@ protected void receiveStickyEvent(Event event) {
*
* @param msg
*/
@Override
public void showMsg(String msg) {
// Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
ToastUtils.showShort(msg);
Expand All @@ -199,6 +183,7 @@ public void showMsg(String msg) {
/**
* 开启加载动画
*/
@Override
public void showProgress() {
if (mQmuiTipDialog == null) {
mQmuiTipDialog = new QMUITipDialog.Builder(mContext)
Expand All @@ -214,6 +199,7 @@ public void showProgress() {
/**
* 关闭加载动画
*/
@Override
public void hideProgress() {
if (mQmuiTipDialog != null) {
if (mQmuiTipDialog.isShowing()) {
Expand Down Expand Up @@ -241,6 +227,15 @@ public void networkError(int errorCode, String errorMsg) {
hideProgress();
}

protected void initPresenter() {

}

protected void removePresenter() {

}


/**
* 加载布局
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sdwfqin.quicklib.base;

/**
* 描述:Presenter基类
*
* @author zhangqin
*/
public interface BasePresenter<T extends BaseView> {

void attachView(T view);

void detachView();
}
26 changes: 26 additions & 0 deletions quicklib/src/main/java/com/sdwfqin/quicklib/base/BaseView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sdwfqin.quicklib.base;

/**
* 描述:View的基类
*
* @author zhangqin
*/
public interface BaseView {

/**
* 显示吐司
*
* @param msg
*/
void showMsg(String msg);

/**
* 显示加载动画
*/
void showProgress();

/**
* 关闭加载动画
*/
void hideProgress();
}
Loading

0 comments on commit b2ee2ae

Please sign in to comment.