David
David

Reputation: 414

Display AlertDialog in a View after observing a completable

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

Upvotes: 1

Views: 279

Answers (3)

Nanda Z
Nanda Z

Reputation: 1876

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

}

…..

}

Upvotes: 1

Jaime Suarez
Jaime Suarez

Reputation: 660

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

Upvotes: 1

Chase Farmer
Chase Farmer

Reputation: 113

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!

Upvotes: 0

Related Questions