【问题标题】:Android MVP - Calls the serverAndroid MVP - 调用服务器
【发布时间】:2018-09-17 10:35:53
【问题描述】:

我开始学习 MVP,但我有一些关于模型和演示者之间通信的问题,例如登录功能

  • Activity 将获取所有字段,发送给演示者,演示者将验证,如果一切都符合预期,演示者将调用模型发送到服务器,但此调用可能需要几秒钟,所以我需要等待服务器回调再次调用presenter,presenter调用activity。

我的问题是:最好的方法是什么?目前我在演示者中添加了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);
}}

谢谢, 泰雷兹

【问题讨论】:

    标签: java android interface mvp


    【解决方案1】:

    再添加一个回调

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

    然后像这样在presenter中调用登录方法

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

    【讨论】:

    • 我喜欢你创建一个新界面的想法,但如果我有一个更复杂的模型/演示者,我是否应该为我想从模型中获取的每个回调创建一个界面?
    • 在这种情况下,最好使用像这样的通用接口接口 onResponseCallBack{ void onSuccess(Object result);无效 onError(对象错误); }。根据您的要求,您可以输入您的响应和错误
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 感谢您的回答,但我想知道模型将消息发送回演示者的最佳方式是什么?然后演示者将分析模型的响应并调用view.loginSuccess()view.loginFailure("msg")
      • 其实模型不是很必要。验证部分可以通过data-binding
      【解决方案4】:

      使用这张图片:

      1.您可以使用一个活动和一个片段作为视图。

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

      -- 更适合你有一个小活动,并在活动中实现组件只是导航抽屉、工具栏、.. 和片段中的其他组件。

      2.使用一个类作为连接到存储库的演示者。

      3.使用类作为获取、设置、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.将包创建为模型,您可以在其上导入所有模型。

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

      public interface AuthenticationContract {
      
      interface View extends BaseView<Presenter>{
      
      }
      
      interface Presenter extends BasePresenter{
      
      }
      

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

      【讨论】:

        猜你喜欢
        • 2018-04-11
        • 2017-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        相关资源
        最近更新 更多