2

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

例如,如果按下按钮登录,活动将是这样的,使用黄油刀@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,模型如何将业务逻辑的结果反馈给视图,以便视图可以处理这些结果?

谢谢你。

4

1 回答 1

3

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

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
    }

}

希望这可以帮助。

对不起我的英语不好。

于 2017-07-06T13:24:31.427 回答