Android MVP - 调用服务器

Android MVP - Calls the server

我开始学习 MVP,但我有几个问题与 Model 和 Presenter 之间的通信有关,例如登录功能

我的问题是:最好的方法是什么?目前我在我的演示者中添加了一个 loginServerCallback() 并将引用传递给模型,所以当模型完成时,我在演示者中调用 loginServerCallback() 并且演示者分析响应并调用该方法在视图中。我这样做对吗?

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

谢谢, 泰雷兹

您的解决方案是正确的,但最好使用 MVVP。 您必须检查许多可能导致应用程序崩溃的条件,例如组件生命周期。 但是在MVVP中,不需要检查这个条件。

从您的 activity 或片段中调用 presenter.loginServerCallback() 验证后。

LoginPresenter 中的 loginServerCallback() 中,处理成功和错误并将视图更新为 view.loginSuccess()view.loginFailure("msg")

再添加一个回调

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

然后像这样在演示者中调用登录方法

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

使用这张图片:

1.you 可以使用一个 activity 和一个片段作为视图。

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

public class AuthenticationPasswordFragment extends Fragment implements AuthenticationContract.View {}

-- 对你来说更好的是有一个小的 activity,并且实现组件只是导航抽屉,工具栏,..在 activity 和其他片段中。

2.use a class 作为演示者连接到存储库。

3.use a class 作为 get、set、getAll、更新本地数据库和远程服务器中数据的存储库。

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打包为模型,你可以导入所有模型。

5.you 可以创建 ClassNameContract 作为接口来定义另外两个接口作为视图和演示者,如下所示:

public interface AuthenticationContract {

interface View extends BaseView<Presenter>{

}

interface Presenter extends BasePresenter{

}

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