Android MVP - Calls the server

Viewed 249

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

4 Answers

Your solution is correct but it's better to use MVVP. You must check alot of conditions which may cause crash your app such as component lifecycle. But in MVVP, there is no need to check this conditions.

From your activity or fragment, call presenter.loginServerCallback() after validations.

Inside the loginServerCallback() in LoginPresenter, handle success and error and update the view as view.loginSuccess() and view.loginFailure("msg")

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");
        }
    });
}

Use this picture:

Use this picture

1.you can use an activity and a fragment as view.

public class AuthenticationActivity extends BaseActivity implements AuthenticationPatternFragment.NavigateToDashboardCallback,
    AuthenticationPasswordFragment.NavigateToDashboardCallback {}

public class AuthenticationPasswordFragment extends Fragment implements AuthenticationContract.View {}

--better for you that has a small activity ,and implement component just navigation drawer ,toolbar ,.. in activity and others in fragment.

2.use a class as presenter for connect to repository.

3.use a class as repository for get,set,getAll,update data in local database and remote server.

public class AuthenticationRepository implements IAuthenticationRepository {

private IAuthenticationRepository mAuthenticationRealmRepository;
private IAuthenticationRepository mAuthenticationRestRepository;

public AuthenticationRepository(IAuthenticationRepository restRepository, IAuthenticationRepository realmRepository) {

    mAuthenticationRestRepository = restRepository;
    mAuthenticationRealmRepository = realmRepository;
}

private AuthenticationRepository() {

}

@Override
public void get(CallRepository<Authentication> callRepository) {

    mAuthenticationRealmRepository.get(callRepository);
}

@Override
public void update(Authentication authentication, CallRepository<Authentication> callRepository) {

    mAuthenticationRealmRepository.update(authentication, callRepository);
}

@Override
public void get(Integer identifier, CallRepository<Authentication> callRepository) {

    mAuthenticationRealmRepository.get(identifier, callRepository);
}

@Override
public void getAll(CallRepository<List<Authentication>> callRepository) {

    mAuthenticationRealmRepository.getAll(callRepository);
}

}

4.create package as models that you can imports all models on it.

5.you can create ClassNameContract as interface for define two other interface as view and presenter like this:

public interface AuthenticationContract {

interface View extends BaseView<Presenter>{

}

interface Presenter extends BasePresenter{

}

----------You can use this example for better review in MVP

Related