I've been experimenting with using flow and mortar as an alternative architecture for our Android apps. I've been working on an app which at the minute is only a single phone layout, but I was wondering how the flow and mortar architecture might work if you want to have a different layout for tablets. Master details might be the simplest example, but there are obviously other examples.
I have a few ideas how this could work, but I wanted to know what the square developers might have thought about already around this topic.
We're still working on a canonical answer for this, but the basic idea is that you let the resource system change what views you're showing in which situation. So your activity sets its content view to, say,
R.layout.root_view
. The tablet version of that layout (we put it inres/layout-sw600dp
) can be tied to different views, which might inject different presenters, and so on.For cases where you need to make a runtime decision, define a boolean resource in
values/bools .xml
and
values-sw600dp/bools.xml
Expose it to the rest of the app via dagger. Use this binding annotation:
and a provider method like:
But wait there's more! Suppose you want to have a single screen / blueprint definition that manufactures different modules for different form factors. We've started using an annotation scheme to simplify that kind of thing. Instead of making our screen classes all implement
BluePrint
, we've started using some annotations to declare their interface class. In that world here's how a screen can selectively choose what modules to use for tablet or mobile.Magic, right? Here's what's behind the curtain. First, a
ModuleFactory
is some static class that is given access to the screen and the resources and spits out a dagger module.Our trixie
ResponsiveModuleFactory
subclass looks like this. (Remember howShowTabletUi.java
defined the resource id as a constant? This is why.)To make all this go, we have a ScreenScoper class (below). In the Mortar sample code, you'd make the ScreenConductor use one of these to create and destroy scopes. Sooner or later (soon I hope) Mortar and/or its samples will be updated to include this stuff.