观察完成后在视图中显示 AlertDialog

Display AlertDialog in a View after observing a completable

我想在我的 View 中显示一个 AlertDialog 表明结果成功,

private void actionUpdateProfesional() {
    btnSave.setOnClickListener(view -> {
        alertDialog = new AlertDialog.Builder(this)
                .setTitle("Wait!")
                .setMessage("Are you sure you want update your data?")
                .setPositiveButton("YES", (dialogInterface, i) -> presenter.updateProfile())
                .setNegativeButton("NO", null)
                .create();
        alertDialog.show();
    });
}

在我的 Completable 在我的 Presenter 上完成后:

@Override
public void updateProfile() {
    Disposable d = updateInfoInteractor
            .build(new ProfileUpdateInfoInteractor.Param(view.getPhone(), view.getLocation(), view.getDescription()))
            .observeOn(schedulers.main())
            .subscribeWith(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    Timber.d("Profile edited");
                }

                @Override
                public void onError(Throwable e) {
                    Timber.d("Error at edit profile");
                }
            });
}

您实际上并没有提出问题,所以我假设您想知道如何在完成事件时显示警报对话框。您可以通过在 onComplete() 函数中再次实例化它来做到这一点。

@Override
public void updateProfile() {
    Disposable d = updateInfoInteractor
            .build(new ProfileUpdateInfoInteractor.Param(view.getPhone(), view.getLocation(), view.getDescription()))
            .observeOn(schedulers.main())
            .subscribeWith(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    Timber.d("Profile edited");

                    // Show alert dialog here!
                    alertDialog = new AlertDialog.Builder(this)
                        .setTitle("Wait!")
                        .setMessage("Are you sure you want update your data?")
                        .setPositiveButton("YES", (dialogInterface, i) -> 
                            presenter.updateProfile())
                        .setNegativeButton("NO", null)
                        .create();
                    alertDialog.show();
                }

                @Override
                public void onError(Throwable e) {
                    Timber.d("Error at edit profile");
                }
            });
}

希望对您有所帮助!

您应该从 onComplete 方法调用视图的 actionUpdateProfesional() 方法。

您可能需要将 actionUpdateProfesional() 添加到您在演示者中引用的视图界面。

会是这样的:

@Override
public void updateProfile() {
    Disposable d = updateInfoInteractor
            .build(new ProfileUpdateInfoInteractor.Param(view.getPhone(), view.getLocation(), view.getDescription()))
            .observeOn(schedulers.main())
            .subscribeWith(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    Timber.d("Profile edited");
                    if (view != null) {
                        view.actionUpdateProfesional()
                    }
                }

                @Override
                public void onError(Throwable e) {
                    Timber.d("Error at edit profile");
                }
            });
}

但是如果你想通过 MVP 架构解决这个问题,你必须在你的 View 界面中创建新的方法。因为 Presenter 不执行 UI 逻辑,否则您的架构将被破坏。

public interface MyObjectView {
    void resultSuccess(int status);
}


MyObjectView myView

Public MyPresenterConstructor(MyObjectView myView){
    this.myView = myView;
}


@Override
    public void updateProfile() {
        Disposable d = updateInfoInteractor
                .build(new ProfileUpdateInfoInteractor.Param(view.getPhone(), view.getLocation(), view.getDescription()))
                .observeOn(schedulers.main())
                .subscribeWith(new DisposableCompletableObserver() {
                    @Override
                    public void onComplete() {
                        Timber.d("Profile edited");

                        // Show alert dialog here!
            myView.resultSuccess(200)   // Okee

                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.d("Error at edit profile");
                    }
                });
    }

然后,不要忘记在 Activity (UI) 中实现您的 View 界面。然后调用你的 alertDialog.

public class MainActivity extend AppCompatActivity implement MyObjectView{

…….

@Override
Public void resultSuccess(int code){

// call your dialog here

}

…..

}