1

I want to display an AlertDialog in my View showing that the result was successful,

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

after my Completable made onComplete on my 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");
                }
            });
}
David
  • 414
  • 7
  • 17

3 Answers3

1

You should call the actionUpdateProfesional() method of your view from the onComplete method.

You'll probably need to add the actionUpdateProfesional() to your View interface that you reference in your presenter.

It would be something like this:

@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");
                }
            });
}
Jaime Suarez
  • 660
  • 4
  • 11
1

But if you want to solve this problem by MVP Architecture you have to create new method in your View interface. Because presenter does not do UI logic or your architecture will be broken.

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

Then, do not forget to implement your View interface in your Activity (UI). then call your alertDialog.

public class MainActivity extend AppCompatActivity implement MyObjectView{

…….

@Override
Public void resultSuccess(int code){

// call your dialog here

}

…..

}
Nanda Z
  • 1,604
  • 4
  • 15
  • 37
  • 2
    You should always check if the `view` implementation is null. If the activity is finished and the async work has not been disposed, it could crash. – Jaime Suarez Jun 27 '19 at 15:15
  • @JaimeSuárez and where I should dispose my Disposable? – David Jun 27 '19 at 15:29
  • 1
    You have to dispose it when your activity is destroyed, in the `onDestroy` method. Before calling the `super.onDestroy()`. – Jaime Suarez Jun 27 '19 at 15:32
  • @JaimeSuárez so in my activity onDestroy method I should do something like this: `presenter.killDispos()`- it will be a method who makes me a `disposable.dispos()` right? (Because is good call a present method from the View? it should be the opposite) – David Jun 27 '19 at 15:46
  • and why should be before the super()? – David Jun 27 '19 at 15:57
  • 1
    Yes, that's correct. It is a good practice to do your work (in the destroyers methods of an activity) before the system destroys data you depend on. – Jaime Suarez Jun 27 '19 at 16:27
  • 1
    You can go deeper into it here https://stackoverflow.com/questions/18821481/what-is-the-correct-order-of-calling-superclass-methods-in-onpause-onstop-and-o?rq=1 – Jaime Suarez Jun 27 '19 at 16:27
  • 1
    About the presenter, try to keep the activity agnostic about what the presenter does. I would name the presenter method onScreenFinished or something similar. The presenter should only know that there was an event in which he needs to liberate resources. – Jaime Suarez Jun 27 '19 at 16:30
0

You have not actually asked a question, so I'm assuming that you are wondering how to show your alert dialog on a complete event. You can to this by simply instantiating it again in the onComplete() function.

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

Hope this helps!

Chase Farmer
  • 113
  • 9