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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With