安卓开发 第四篇 我的安卓应用架构设计-----Presenter类

我的项目架构中使用了MVP模式,Presenter则代表MVP中的P。如果有不了解MVP模式的建议先了解一下MVP模式再来看这篇文章。

我的架构中,每个类型的类都有一个基类,其他此类型的类都继承于这个基类,好处是我们可以将公共部分抽象到基类中,以后如果有公共的改变,就直接修改基类,这样会减少很多工作量。同样Presenter也有基类BasePresenter:

/**
 * BasePresenter 是应用中所有Presenter的顶级抽象类,规定了Presenter的参数类型
 * <p>
 * Presenter:MVP架构中的P。
 * <p>
 * Created by tianlai on 16-3-3.
 */
public abstract class BasePresenter<V extends BaseUiView> {
    protected static final String TAG = "BasePresenter";

    protected V mUiView;

    protected Context context;

    protected CompositeSubscription subscriptions;

    public BasePresenter(Context context) {
        this.context = context;

        subscriptions = new CompositeSubscription();
    }

    /**
     * Presenter 的 oncreate()生命周期,在Activity中的 oncreate()中调用
     * 作用:恢复状态,初始化数据
     *
     * @param savedInstanceState
     */
    public void oncreate(Bundle savedInstanceState, V uiView) {
        Icepick.restoreInstanceState(this, savedInstanceState);
        this.mUiView = uiView;

    }


    /**
     * Presenter 的 onSave()生命周期,在Activity中的 onSaveInstance()中调用
     * 作用: 保存数据
     *
     * @param state
     */
    public void onSave(Bundle state) {
        Icepick.saveInstanceState(this, state);
    }


    /**
     * Presenter 的 onPause()生命周期,在Activity中的 onPause()中调用
     * 作用:解绑View,销毁View
     */
    public void onStop() {
        cancle();
    }

    /**
     * Presenter 的 onDestroy()生命周期,在Activity中的 onDestroy()中调用
     * 作用:销毁持有的对象
     */
    public void onDestroy() {
        mUiView = null;
        context = null;
        subscriptions = null;
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    public User getUser() {
        return UserManager.get().getUser();
    }

    /**
     * 更新当前用户
     *
     * @return
     */
    public void updateUser(User newUser) {
        UserManager.get().update(newUser);
    }


    /**
     * 将数据添加到总线中
     *
     * @param object
     */
    public void postBus(Object object) {
        RxBus.getDefault().post(object);
    }

    /**
     * 订阅事件
     *
     * @param c
     */
    public <T> void busEvent(Class<T> c, Action1<T> a) {
        Subscription sub = RxBus.getDefault().toObservable(c)
                .subscribe(a, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        loadError(throwable);
                    }
                });

        subscriptions.add(sub);
    }

    /**
     * 取消加载的方法
     */
    public void cancle() {
        if (subscriptions != null) {
            subscriptions.unsubscribe();
        }
    }

    /**
     * 加载发生错误
     */
    public void loadError(Throwable e) {

        e.printStackTrace();

        if (e instanceof SocketTimeoutException) {
            mUiView.showError("超时,请稍后重试");
        } else if (e instanceof ConnectException) {
            mUiView.showError("连接失败,请稍检查您的网络");
        }

        dismissLoading();
    }

    /**
     * 加载完成
     */
    public void loadComplete() {
        dismissLoading();
    }

    /**
     * 显示加载对话框
     *
     * @param isShow
     */
    protected void showLoading(boolean isShow) {
        if (isShow && mUiView != null) {
            mUiView.showLoading();
        }
    }

    /**
     * 取消加载对话框
     */
    protected void dismissLoading() {
        if (mUiView != null) {
            mUiView.dismissLoading();
        }
    }


    /**
     * 将网络请求添加到任务列表中
     *
     * @param sub
     */
    public void addNetTask(Subscription sub) {
        if (subscriptions != null) {
            subscriptions.add(sub);
        }
    }

    /**
     * 从任务列表移除请求任务
     *
     * @param sub
     */
    public void removeNetTask(Subscription sub) {
        if (subscriptions != null) {
            subscriptions.remove(sub);
        }
    }


}

我设计的Presenter有自己的生命周期,分别为onCreate,onStop,onSave,onDestroy,这五个生命周期分别对应Activity的onCreate,onStop,onSaveInstance,onDestroy生命周期。

然后是具体的Presenter:

/**
 * Created by tianlai on 16-3-3.
 */
public class LoginPresenter extends BasePresenter<LoginView> {

    @Inject
    LoginRepo mRepository;

    @Inject
    public LoginPresenter(Context context) {
        super(context);
    }


    /**
     * 登录应用
     */
    public void login(final String phone,final String passwd) {

        //检查账号是否合法
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.showToast(context, "请输入用户名或邮箱");
            return;
        }


        //检查密码是否合法
        if (TextUtils.isEmpty(passwd)) {
            ToastUtil.showToast(context, "请输入密码");
            return;
        }

        //密码MD5加密
       final String securityPsw = EncryptUtil.md5Encode(passwd);

        //进行登录
        mRepository.login(phone, securityPsw)
                .subscribe(new MineSubscriber<LoginData>(this) {
                    @Override
                    public void onNext(LoginData loginData) {
                        if (loginData != null) {

                            //TODO 登陆成功逻辑处理
                        }
                    }
                });

    }

}

其中使用了dagger2(依赖注入框架),retrofit(网络请求),Icepick(数据保存),rxJava(异步操作)

BaseUiView 是操作view的接口
BaseRepository 是数据仓库
LoginResponce 是登录结果的接收类

好了,到这里就结束了。

如果有更深的理解,本文将会修改;
如果有错误的地方,欢迎指正;
如果你有更好的理解,欢迎交流。

本文为原创文章,版权归博主所有,转载请注明出处。

更多资料:

我的github地址以及使用demo: https://github.com/naivor/naivorapp

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值