Android MVP-呼叫服务器

时间:2018-09-17 10:35:54

标签: java android interface mvp

我开始学习MVP,但是我对模型和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);
}}

谢谢, 泰雷兹

4 个答案:

答案 0 :(得分:0)

您的解决方案是正确的,但最好使用MVVP。 您必须检查很多可能导致应用崩溃的情况,例如组件生命周期。 但是在MVVP中,无需检查此条件。

答案 1 :(得分:0)

根据您的活动或片段,致电 presenter.loginServerCallback()经过验证。

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

答案 2 :(得分:0)

再添加一个回调

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

答案 3 :(得分:0)

使用这张照片:

Use this picture

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

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

public class AuthenticationPasswordFragment extends Fragment implements AuthenticationContract.View {}

-更适合您的活动,并在活动中实现导航抽屉,工具栏,..,在片段中实现其他组件。

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

3。使用类作为存储库,以获取本地数据库和远程服务器中的get,set,getAll,update数据。

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。您可以创建ClassNameContract作为接口,以将另外两个接口定义为视图和演示者,如下所示:

public interface AuthenticationContract {

interface View extends BaseView<Presenter>{

}

interface Presenter extends BasePresenter{

}

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