我正在使用 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
,模型如何将业务逻辑的结果反馈给视图,以便视图可以处理这些结果?
谢谢你。