This code is a base for every android project with MVP architecture we have used some libraries
- Support libraries
- RecyclerViews
- Retrofit 2
- Dagger 2
- Butterknife
- Glide
Imagine you have to implement a sign in screen.
- Create a new package under
uicalledsignin - Create an new Activity called
ActivitySignIn. You could also use a Fragment. - Define the view interface that your Activity is going to implement. Create a new interface called
SignInViewthat extendsBaseView. Add the methods that you think will be necessary, e.g.showSignInSuccessful() - Create a
SignInPresenterclass that extendsBasePresenter<SignInView> - Implement the methods in
SignInPresenterthat your Activity requires to perform the necessary actions, e.g.signIn(String email). Once the sign in action finishes you should callgetView().showSignInSuccessful(). - Create a
SignInPresenterTestand write unit tests forsignIn(email). Remember to mock theSignInViewand also theDataManager. - Make your
ActivitySignInimplementSignInViewand implement the required methods likeshowSignInSuccessful() - In your activity, inject a new instance of
SignInPresenterand callpresenter.attach(this)fromonCreateandpresenter.detach()fromonDestroy(). Also, set up a click listener in your button that callspresenter.signIn(email).
when you want to emit an event from anywhere and subscribe to this event from any other place (ex. presenter):
- create event class
class MyEvent{
private String message;
private Integer identifier;
public MyEvent(Integer id, String msg){
message = msg;
identifier = id;
}
public String getMsg(){
return message;
}
}
- subscribe this event anywhere
// attach(View)
// ...
Consumer<MyEvent> consumer = new Consumer<MyEvent>() {
@Override
public void accept(MyEvent e) throws Exception {
// your code go here
Toast.makeText((Activity)getView(), e.getMsg(), Toast.LENGTH_LONG)
.show();
}
};
busDisposable = bus.filteredObservable(String.class)
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(consumer);
- where you want to emit the event
bus.post(password);
- don't forget to despose
busDisposable.dispose();