javaandroidinterfacemvp

Android MVP - Calls the server


I started to learn MVP but I have a few questions related the communication between the Model and the Presenter, for example a login feature

My question is: How is the best way to do that? At the moment I added a loginServerCallback() in my presenter and I pass the reference to the Model, so when the model finishes, I call the loginServerCallback() in the presenter and the presenter analyse the response and call the method in the View. Am I doing that right?

public interface LoginMVP {
interface View {
    void loginSuccess();
    void loginFailured(String message);
}
interface Presenter {
    void validateFields(String email, String password);
    void loginServerCallback();
}
interface Model {
    void loginServer(String email, String password);
}}

Thanks, Thales


Solution

  • add one more callback

     public interface LoginMVP {
        interface View {
            void showLoadingIndicator(boolean active);
            void loginSuccess();
            void loginFailured(String message);
        }
        interface Presenter {
            void validateFields(String email, String password);
            void loginServerCallback();
        }
    
        interface OnLoginCallBack{
            void onSuccess();
            void onError();
        }
        interface Model {
            void loginServer(String email, String password);
        }
    }
    

    And call login method in presenter like this

    public void doLogin(String userName, String password) {
        view.showLoadingIndicator(true);
        modal.loginServer(userName, password, new LoginMVP.OnLoginCallBack() {
            @Override
            public void onSuccess() {
                view.showLoadingIndicator(false);
                view.loginSuccess();
            }
    
            @Override
            public void onError() {
                view.showLoadingIndicator(false);
                view.loginFailured("SomeError");
            }
        });
    }