Skip to content
Advertisement

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

  • Activity will get all the fields, send to the presenter, the presenter will validate and if it’s everything as expected the presenter will call the model to send to the server, but this call could take a few seconds, so I need to wait for the callback from the server to call the presenter again and the presenter calls the activity.

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

Advertisement

Answer

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");
        }
    });
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement