【问题标题】:Display AlertDialog in a View after observing a completable观察完成后在视图中显示 AlertDialog
【发布时间】:2019-06-27 14:00:05
【问题描述】:

我想在我的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 上创建 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");
                }

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

【问题讨论】:

    标签: java android mvp


    【解决方案1】:

    但是如果你想通过 MVP 架构解决这个问题,你必须在你的视图界面中创建新的方法。因为 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
    
    }
    
    …..
    
    }
    

    【讨论】:

    • 您应该始终检查view 实现是否为空。如果 Activity 已完成且异步工作尚未处理,它可能会崩溃。
    • 你必须在你的活动被销毁时处理它,在onDestroy方法中。在致电super.onDestroy()之前。
    • 是的,没错。在系统销毁您所依赖的数据之前完成您的工作(在活动的销毁器方法中)是一种很好的做法。
    • 你可以在这里更深入地了解它stackoverflow.com/questions/18821481/…
    • 关于演示者,尽量让活动不知道演示者的行为。我会将演示者方法命名为 onScreenFinished 或类似名称。演示者应该只知道发生了他需要释放资源的事件。
    【解决方案2】:

    您应该从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");
                    }
                });
    }
    

    【讨论】:

      【解决方案3】:

      您实际上并没有问过问题,所以我假设您想知道如何在完整事件上显示警报对话框。您只需在 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");
                      }
                  });
      }
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 2020-12-07
        • 2021-11-08
        • 2017-11-26
        相关资源
        最近更新 更多