android MVP + dagger2 + Retrofit + Rxjava+okhttp android基礎項目框架搭建(2)–之MVP引入

android MVP + dagger2 + Retrofit + Rxjava+okhttp    android基礎項目框架搭建(2)–之MVP引入,上一篇文章中,我們已經成功的引入瞭Dagger2;今天我們將繼續使用android MVP模式對代碼進行實現。

#1 將MainActivity.java文件移動到ui包下,並在ui包下建立view包; view包下存放接口。包及類結構如圖

2 新建presenter包,並在presenter下建立相關的類, 如圖

說明:圖中RxMapPresenter為Presenter的基類, 該類繼承BaseMapPresenter類; BaseMapPresenter提供子類實例化Presenter與View之間的接口對象的方法。RxMapPresenter 負責實例化CompositeSubscription對象, 子類使用該對象進行網絡請求。

MainActivity.java代碼如下

public class MainActivity extends BaseAcitvity implements MainView, HasComponent {


    @BindView(R.id.test)
    TextView test;


    @Inject
    MainPresenter mainPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getComponent().inject(this);


        //重要
        mainPresenter.attachView(this);
    }




    @OnClick(R.id.test)
    public void onClick() {
        mainPresenter.login("", "");
    }


    @Override
    public void loginSuccess(String userInfo) {
        Toast.makeText(this, userInfo, Toast.LENGTH_LONG).show();
    }


    @Override
    public void loginFail() {


    }


    @Override
    public void loginException() {


    }


    @Override
    public MainComponent getComponent() {
        return DaggerMainComponent.builder()
                .applicationComponent(Application.get(this).getComponent())
                .activityModule(new ActivityModule(this))
                .mainModule(new MainModule())
                .build();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainPresenter.detachView();
    }
}

BaseAcitvity.java

public class BaseAcitvity extends Activity {
    private ActivityComponent mActivityComponent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public ActivityComponent getActivityComponent() {
        if (mActivityComponent == null) {
            mActivityComponent = DaggerActivityComponent.builder()
                    .activityModule(new ActivityModule(this))
                    .applicationComponent(Application.get(this).getComponent())
                    .build();
        }
        return mActivityComponent;
    }
}

MainView.java

public interface MainView extends MvpView{
    void loginSuccess(String userInfo);
    void loginFail();
    void loginException();
}

HasComponent.java

public interface HasComponent {


    C getComponent();
}

MainPresenter .java

public class MainPresenter extends RxMvpPresenter {


    @Inject
    public MainPresenter() {


    }


    @Inject
    Application context;


    public void login(String username, String password) {
        getMvpView().loginSuccess("MVP成功搭建");
    }
}

RxMvpPresenter.java

public class RxMvpPresenter extends BaseMvpPresenter {


    protected CompositeSubscription mCompositeSubscription;


    @Override
    public void attachView(V mvpView) {
        super.attachView(mvpView);


        mCompositeSubscription = new CompositeSubscription();
    }


    @Override
    public void detachView() {
        super.detachView();


        mCompositeSubscription.clear();
        mCompositeSubscription = null;
    }
}

BaseMvpPresenter.java

public class BaseMvpPresenter implements MvpPresenter {


    private V mMvpView;


    @Override
    public void attachView(V mvpView) {
        mMvpView = mvpView;
    }


    @Override
    public void detachView() {
        mMvpView = null;
    }


    public boolean isViewAttached() {
        return mMvpView != null;
    }


    public V getMvpView() {
        return mMvpView;
    }


    public void checkViewAttached() {
        if (!isViewAttached()) throw new MvpViewNotAttachedException();
    }


    public static class MvpViewNotAttachedException extends RuntimeException {
        public MvpViewNotAttachedException() {
            super("Please call Presenter.attachView(MvpView) before" +
                    " requesting data to the Presenter");
        }
    }
}

MvpPresenter.java

public interface MvpPresenter {



    @UiThread
    void attachView(V view);




    @UiThread
    void detachView();


}

BaseAcitvity.java

public class BaseAcitvity extends Activity {
    private ActivityComponent mActivityComponent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public ActivityComponent getActivityComponent() {
        if (mActivityComponent == null) {
            mActivityComponent = DaggerActivityComponent.builder()
                    .activityModule(new ActivityModule(this))
                    .applicationComponent(Application.get(this).getComponent())
                    .build();
        }
        return mActivityComponent;
    }
}

MvpView.java

public interface MvpView {
}

3. 運行項目, 點擊屏幕上的HelloWold!文字, 如果出現 “MVP搭建成功”的文字說明android MVP環境搭建成功。

**下一篇我們將講解如何引入Retrofit RxJava**

You May Also Like