项目框架总结

前端之家收集整理的这篇文章主要介绍了项目框架总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、lambda 缩写形式,studio 按键 alt+enter

2、studio 安装插件,gsonformat、butterknife插件
提交代码使用svn插件管理。
设置studio插件svn
本地文件过滤:

3、MVP的使用
一个模块对应一个 package包

主页home
activity
adapter
domain 业务处理 usecase
fragment 这个如果activity支持刷新,添加一个BaseListFragment
presenter
view
widget  自定义的view

mvp使用列子:
详情界面

1. 定义一个view 接口

public interface PeriodInvestProdDetailView extends LoadDataView {
    void onShowPeriodInvestProdDetail(FixedPeriodInvestProdResp fixedPeriodInvestProdResp);//产品详情
}

 2. usecase 做业务 处理,一般耗时操作

package com.lcwr.android.modules.investment.domain;

import com.lcwr.android.data.entities.response.FixedPeriodInvestProdResp;
import com.lcwr.android.data.repository.Repository;
import com.lcwr.android.mvp.domain.UseCase;

import javax.inject.Inject;

import rx.Observable;

/** * author lihui * version 1.0 * date: on 2017/7/13 */

public class PeriodInvestProdDetailUseCase extends UseCase<FixedPeriodInvestProdResp> {
    private Repository mRepository;
    private String prdId;

    public void setPrdId(String prdId) {
        this.prdId = prdId;
    }

    @Inject
    public PeriodInvestProdDetailUseCase(Repository repository) {
        this.mRepository = repository;
    }

    @Override
    protected Observable<FixedPeriodInvestProdResp> buildObservable() {
        return mRepository.getFixedPeriodInvestProdDetail(prdId);
    }

}
注:
mRepository.getFixedPeriodInvestProdDetail(prdId);这个里面处理耗时操作的,如请求服务器。
Observable使用了rxjava+rxandroid请求。

 3. 对应presenter,管理view 和 usecase交互。

package com.lcwr.android.modules.investment.presenter;

import com.fernandocejas.frodo.annotation.RxLogSubscriber;
import com.lcwr.android.data.entities.response.FixedPeriodInvestProdResp;
import com.lcwr.android.modules.investment.domain.ElcAccountUseCase;
import com.lcwr.android.modules.investment.domain.PeriodInvestProdDetailUseCase;
import com.lcwr.android.modules.investment.view.PeriodInvestProdDetailView;
import com.lcwr.android.mvp.presenters.Presenter;
import com.lcwr.android.mvp.subscribers.LoadDataSubscriber;
import com.lcwr.android.mvp.views.InterfaceView;
import com.lcwr.android.mvp.views.LoadDataView;
import com.xitaiinfo.library.compat.errorview.ErrorView;
import com.xitaiinfo.library.injections.ActivityScope;

import javax.inject.Inject;

/** 1. author qibo 2. version 1.0 3. Create by 2017/7/20 上午11:58 4. 描述:理财详情 */
@ActivityScope
public class PeriodInvestProdDetailPresenter implements Presenter {


    private PeriodInvestProdDetailView view;
    private PeriodInvestProdDetailUseCase periodInvestProdDetailUseCase;

    private String mPrdId;


    @Inject
    public PeriodInvestProdDetailPresenter(PeriodInvestProdDetailUseCase periodInvestProdDetailUseCase,ElcAccountUseCase elcAccountUseCase) {
        this.periodInvestProdDetailUseCase = periodInvestProdDetailUseCase;
    }


    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        periodInvestProdDetailUseCase.unSubscribe();
    }

    @Override
    public void attachView(InterfaceView v) {
        this.view = (PeriodInvestProdDetailView) v;
    }

    /** * 产品具体详情 */
    public void getPeriodInvestProductDetail(String prdId) {
        this.mPrdId = prdId;
        periodInvestProdDetailUseCase.setPrdId(prdId);
        periodInvestProdDetailUseCase.execute(new PeriodInvestProductDetailSubscriber(view,() -> {
            getPeriodInvestProductDetail(mPrdId);
        },null));
    }


    @RxLogSubscriber
    private class PeriodInvestProductDetailSubscriber extends LoadDataSubscriber<FixedPeriodInvestProdResp> {

        public PeriodInvestProductDetailSubscriber(LoadDataView view,ErrorView.OnRetryListener onRetryListener,ErrorView.Config config) {
            super(view,onRetryListener,config);
        }


        @Override
        public void onNext(FixedPeriodInvestProdResp fixedPeriodInvestProdResp) {
            super.onNext(fixedPeriodInvestProdResp);
            view.onShowPeriodInvestProdDetail(fixedPeriodInvestProdResp);
        }
    }
}
注:
PeriodInvestProductDetailSubscriber 这个是rxjava中的订阅者,内部方法被回调。

 4. 实现接口

接口都是需要被实现的(PeriodInvestProdDetailView ),
可以由Activity或者fragment实现,由于这里Activity实现接口。
public class PeriodInvestProdDetailActivity extends ToolBarActivity implements PeriodInvestProdDetailView,View.OnClickListener {

。。。
    @Override
    public void onShowPeriodInvestProdDetail(FixedPeriodInvestProdResp fixedPeriodInvestProdResp) {
 mPrdName.setText(fixedPeriodInvestProdResp.getPrdName());
        //限新手标
        if (!TextUtils.isEmpty(fixedPeriodInvestProdResp.getPrdRecommend())) {
            mPrdType.setText(fixedPeriodInvestProdResp.getPrdRecommend());
        }

        String rate = "0.0%";
        if (TextUtils.isEmpty(fixedPeriodInvestProdResp.getRate())) {
            rate = getString(R.string.home_hot_product_rate,fixedPeriodInvestProdResp.getPrdRate());
        } else {
            //4.5+1.5%
            rate = getString(R.string.product1_rate,fixedPeriodInvestProdResp.getPrdRate(),fixedPeriodInvestProdResp.getRate());
        }

        mPrdrate.setText(rate);//6.2+7%
        mPrdTerm.setText(fixedPeriodInvestProdResp.getPrdTerm() + "天");

        //5种类型
        mAvilAmount.setText(StringUtils.format(fixedPeriodInvestProdResp.getAvilAmount()) + "元");
        mChargeAmount.setText(StringUtils.format(fixedPeriodInvestProdResp.getChargeAmount()) + "元");
        mStartAmount.setText(StringUtils.format(fixedPeriodInvestProdResp.getStartAmount()) + "元");
        String incomeMode = fixedPeriodInvestProdResp.getIncomeMode();
        if (!TextUtils.isEmpty(incomeMode)) {
            mIncomeMode.setText(incomeMode);
        }
        String convFalg = fixedPeriodInvestProdResp.getConvFalg();
        if (!TextUtils.isEmpty(convFalg)) {
            mConvFlag.setText(convFalg);
        }
}
。。。
}

Model:usercase

presenter:管理者

view:数据返回接口,Activity/fragment实现该接口

4、框架
注解框架

//ButterKnife
    compile "com.jakewharton:butterknife:${libs.butterknife}"
    annotationProcessor "com.jakewharton:butterknife-compiler:${libs.butterknife}"
RxBinding 是 Jake Wharton 的一个开源库,它提供了一套在 Android 平台上的基于 RxJava的 Binding API。所谓 Binding,就是类似设置 OnClickListener 、设置 TextWatcher 这样的注册绑定对象的API,对普通的控件,V4,V7等等做了相应的支持。

在Rxbinding的基础上可以很方便的实现很多功能,例如:快点点击两次只取一次、长按事件监听、编辑框快速输入时候不搜索等等功能。
 //rxbinding
    compile "com.jakewharton.rxbinding:rxbinding:${libs.rxbinding}"
    compile "com.jakewharton.rxbinding:rxbinding-support-v4:${libs.rxbinding}"
    compile "com.jakewharton.rxbinding:rxbinding-appcompat-v7:${libs.rxbinding}"
    compile "com.jakewharton.rxbinding:rxbinding-design:${libs.rxbinding}"
    compile "com.jakewharton.rxbinding:rxbinding-recyclerview-v7:${libs.rxbinding}"


com.jakewharton.rxbinding.widget.RxTextView
  RxTextView.textChanges(etBuyAmount)
                .filter(charSequence -> productBuyAddResp != null)
                .subscribe(inputAmount -> {

                    if (!TextUtils.isEmpty(inputAmount)) {
                        double inputAmountD = parseDouble(inputAmount.toString());
                        double chargeAmount = parseDouble(productBuyAddResp.getChargeAmount());
                        etProjectedEarnings.setText(calcProfit(productBuyAddResp.getRate(),productBuyAddResp.getPrdRate(),productBuyAddResp.getPrdTerm(),inputAmountD));
                        double amountX = chargeAmount - inputAmountD;
                        etRestAmount.setText(amountX > 0 ? StringUtils.format(String.valueOf(amountX)) : "0.00");
                        if (amountX < 0) {
                            etBuyAmount.setText(productBuyAddResp.getChargeAmount());
                        }

                    } else {
                        etRestAmount.setText(productBuyAddResp.getChargeAmount());
                        etProjectedEarnings.setText("0.00");
                    }

                });


com.jakewharton.rxbinding.support.v7.widget.RxToolbar
com.xitaiinfo.library.commons.rx.Rx
点击过滤处理
  //绑定 消息点击事件
        RxToolbar.itemClicks(mToolbar)
                .filter(menuItem -> Predicate.userLoginPredicate(getContext()).call(null))
                .subscribe(menuItem -> {
                    if (menuItem.getItemId() == R.id.action_message) {
                        //首页右上角消息按钮
                        getNavigator().navigateToMsg(getContext(),null);
                    }
                });

      //绑定 资讯 事件
        Rx.click(mHeaderViewHolder.mLlNews,aVoid -> {
            //跳转到newsactivity
            getNavigator().navigateToInfoList(getContext(),null);
        });
        //绑定 邀请好友 事件 登录
        Rx.click(mHeaderViewHolder.mLlInvite,Predicate.userLoginPredicate(getContext()),aVoid -> {
            getNavigator().navigateToInviteFriends(getContext(),null);

        });
        //绑定 服务 事件 登录
        Rx.click(mHeaderViewHolder.mLlService,aVoid -> {
            getNavigator().navigateToService(getContext(),null);
        });
// Google
    compile "com.google.dagger:dagger:${libs.dagger}"
    compile "com.google.dagger:dagger-android:${libs.dagger}"
    compile "com.google.dagger:dagger-android-support:${libs.dagger}"
    annotationProcessor "com.google.dagger:dagger-compiler:${libs.dagger}"
    annotationProcessor "com.google.dagger:dagger-android-processor:${libs.dagger}"
Dagger2是Dagger的升级版,是一个依赖注入框架,现在由Google接手维护
使用Dagger2实现解耦

网络访问:
    compile "com.squareup.okhttp3:okhttp:${libs.okhttp}"


其他框架
    compile 'com.xitaiinfo.library:xt-common:1.1.0@aar'
    compile 'com.xitaiinfo.library:xt-compat:1.0.0@aar'
    compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar'
    compile 'me.dm7.barcodescanner:zxing:1.9'
    compile    'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.7.8'
    compile 'jp.wasabeef:glide-transformations:2.0.1'
    compile 'com.squareup.sqlbrite:sqlbrite:0.8.0'
    compile 'com.bigkoo:convenientbanner:2.0.5'
    compile 'com.orhanobut:dialogplus:1.10@aar'
    compile 'info.hoang8f:android-segmented:1.0.6'
    compile 'org.jsoup:jsoup:1.9.2'
    compile 'com.github.kingideayou:tagcloudview:1.0.2'
    compile 'com.google.android:flexBox:0.2.5'
    compile 'com.github.pedrovgs:renderers:3.0.0'
    compile 'com.facebook.rebound:rebound:0.3.8'
    compile 'com.kyleduo.switchbutton:library:1.4.4'
    compile 'com.pixplicity.htmlcompat:library:1.0.0'
    compile 'com.github.lecho:hellocharts-android:v1.5.8'

    compile 'com.xitaiinfo.library:xt-common:1.1.0@aar'

这个里面包含了
compile ‘io.reactivex:rxjava:1.1.5’
compile ‘io.reactivex:rxandroid:1.2.0

猜你在找的设计模式相关文章