Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place for AlertDialog in Android app based on MVP architecture

Can anyone tell me where should I put AlertDialogs in my app according to MVP architecture? Should I leave them in my Activity or put to presenter?

like image 782
temnoi Avatar asked Jan 08 '16 10:01

temnoi


1 Answers

As MVP is not a specific platform principle, it can be used to write platform-agnostic applications. Since M and P are pretty easy to be written in plain Java (not always true for M, but almost always true for P), V is practically a component of the target platform (say, Swing, GWT, JavaFX, Android, and even CLI [command line interface]). Suppose you write such a cross-platform application and you have a specific view, INotificationView that is declared as follows:

public interface INotificationView {

    void reportError(@Nonnull String errorMessage);

}

Your base platform-agnostic P does not known anything on which platform it's being executed or supposed to be executed on (at least at the compile-time), and it only holds a reference to an INotificationView instance. Thus you can inject platform-specific views like:

public final class AndroidNotificationView
        extends Activity
        implements INotificationView {

    @Override
    public void reportError(@Nonnull final String errorMessage) {
        Toast.makeText(this, "ERROR: " errorMessage, Toast.LENGTH_LONG).show();
    }

}

Note that this approach lets you to write regular unit tests for your presenters as it holds no platform-specific dependencies anymore. Another point is, let's say, you port your application to CLI, so the interface above might be implemented as follows:

public final class CliNotificationView
        implements INotificationView {

    @Override
    public void reportError(@Nonnull final String errorMessage) {
        System.err.print("ERROR: ");
        System.err.println(errormessage);
    }

}

Pretty different than the Android implementation, right? This is basically what MVP is about.

Also note, that the name of the notification view method is not named displayError or showError or showAlertDisplay just because the view interface does not dictate how the error will be notified to a user -- it's still error reporting somehow from the V perspective.

Thus, you can easily put your AlertDialog to your view implementation.

like image 104
Lyubomyr Shaydariv Avatar answered Sep 28 '22 07:09

Lyubomyr Shaydariv