android MVP + dagger2 + Retrofit + Rxjava+okhttp android基礎專案框架搭建(2)--之MVP引入
阿新 • • 發佈:2019-02-19
上一篇文章中,我們已經成功的引入了Dagger2;今天我們將繼續使用android MVP模式對程式碼進行實現。
MainActivity.java程式碼如下
#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<MainComponent> { @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> {
C getComponent();
}
MainPresenter .java
public class MainPresenter extends RxMvpPresenter<MainView> { @Inject public MainPresenter() { } @Inject Application context; public void login(String username, String password) { getMvpView().loginSuccess("MVP成功搭建"); } }
RxMvpPresenter.java
public class RxMvpPresenter<V extends MvpView> extends BaseMvpPresenter<V> {
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<V> implements MvpPresenter<V> {
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<V> {
@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 {
}