模型如何反馈到Android MVP中的视图?

时间:2017-07-06 12:52:55

标签: android android-mvp

我正在使用MVP在我的Android应用程序中解耦我的视图和模型。我需要知道模型应该如何将业务逻辑的结果反馈给视图。

例如,如果按下某个按钮进行登录,则活动将如下所示,使用butterknife @OnClick注释:

@OnClick(R.id.login_button)
public void login() {
    String email = mEmailEditText.getText().toString();
    String password = mPasswordEditText.getText().toString();
    LoginCredentials loginCredentials = new LoginCredentials(email, password);
    mPresenter.loginWithEmail(loginCredentials);
}

然后,演示者将验证并向存储库发出请求:

public void loginWithEmail(LoginCredentials loginCredentials) {
    boolean isEmailValid = AuthValidator.validateEmail(loginCredentials.getUsername());
    boolean isPasswordValid = AuthValidator.validatePassword(loginCredentials.getPassword());

    if(isEmailValid && isPasswordValid) repository.loginEmailUser(loginCredentials);

    if (!isEmailValid) view.handleInvalidEmail();

    if (!isPasswordValid) view.handleInvalidPassword();
}

然后,存储库将执行业务逻辑:

@Override
public void loginEmailUser(LoginCredentials loginCredentials) {
    Call<Token> call = userServiceApi.loginInToken(loginCredentials);
    call.enqueue(new Callback<Token>() {
        @Override
        public void onResponse(@NonNull Call<Token> call, @NonNull Response<Token> response) {
            if (response.isSuccessful()) {
                // handle successful login
            } else {
                // Handle unsuccessful login
            }
        }

        @Override
        public void onFailure(@NonNull Call<Token> call, @NonNull Throwable t) {
            // Handle failed request
        }
    });

如果评论说// handle unsuccessful something,模型如何反馈观察业务逻辑的结果,以便视图可以处理这些结果?

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用接口作为回调,例如:

public interface RepositoryCallback {

   void onLoginEmailUserSuccess(/*paramaters if you need*/);
   void onLoginEmailUserError(/*paramaters if you need*/);
   void onRequestFailed(/*paramaters if you need*/)

}

在存储库中定义了监听器

public class MyRepository {

    private RepositoryCallback mListener;

    @Override
    public void loginEmailUser(LoginCredentials loginCredentials) {
        Call<Token> call = userServiceApi.loginInToken(loginCredentials);
        call.enqueue(new Callback<Token>() {
            @Override
            public void onResponse(@NonNull Call<Token> call, @NonNull Response<Token> response) {
                if (response.isSuccessful()) {
                    // handle successful login
                    if (mListener != null) {
                        mListener.onLoginEmailUserSuccess()
                    }
                } else {
                    // Handle unsuccessful login
                   if (mListener != null) {
                       mListener.onLoginEmailUserError()
                   }
                }
            }

            @Override
            public void onFailure(@NonNull Call<Token> call, @NonNull Throwable t) {
                // Handle failed request
                if (mListener != null) {
                    mListener.onRequestFailed()
                }
            }
        });

    public void setRepositoryCallback(RepositoryCallback listener) {
        mListener = listener;
    }

}

然后将演示者设置为侦听器:

public class MyPresenter implements RepositoryCallback {


    public void loginWithEmail(LoginCredentials loginCredentials) {
        repository.setRepositoryCallback(this) // here or in the presenter constructor
        boolean isEmailValid = AuthValidator.validateEmail(loginCredentials.getUsername());
        boolean isPasswordValid = AuthValidator.validatePassword(loginCredentials.getPassword());

        if(isEmailValid && isPasswordValid) repository.loginEmailUser(loginCredentials);

        if (!isEmailValid) view.handleInvalidEmail();

        if (!isPasswordValid) view.handleInvalidPassword();
    }

    @Override
    public void onLoginEmailUserSuccess(//paramaters if you need){
        // your code
    }

    @Override
    public void onLoginEmailUserError(//paramaters if you need){
        // your code
    }

    @Override
    public void onRequestFailed(//paramaters if you need){
        // your code
    }

}

希望这有帮助。

抱歉我的英文。