1. MAXIMIZING CODE
REUSE ACROSS SCREENS
Flash Builder 4.5 and Flex SDK 4.5
(including mobile Flex)
www.jasonhanson.com/360flex
2. Your Presenter
• Jason Hanson
• twitter @jayfour000
• flexfood.blogspot.com
• www.jasonhanson.com
• Building for Flash Platform since 1999
• Working with Hero SDK since Aug,
2010 (beta)
www.jasonhanson.com/360flex
3. Stuff to check out if
you get bored
• http://www.jasonhanson.com/360flex
www.jasonhanson.com/360flex
5. So many screens
So little time
• Desktop
• Laptop
• Phone
• Tablet
• TV
• Car Dashboard
• Fridge, windows, clothing, coffee table
www.jasonhanson.com/360flex
7. LOCATION, Location,
location
• Take your business to your users
• Your business stays the same
• Your apps need to change based on how
the user interactions with the screen of
their choosing
• Your business should seamlessly flow
between screens
www.jasonhanson.com/360flex
10. Flash Platform
Offers One Option
• Write core business code once
• Reuse on each new screen target
• Build custom views appropriate for
each screen target
• Modify core code as needed for
special cases
www.jasonhanson.com/360flex
25. core
lib core lib
• Flex Library Project
• Remove “view” libs from Library path
• Keep framework.swc for data classes
like ArrayCollection and BindingUtils
• Keep rpc.swc for HTTPService and
AsyncToken
www.jasonhanson.com/360flex
27. test
runner Test Runner
• Flex Project
• FlexUnit 4 built
into FlashBuilder
• You can build
out most of the
core lib with
only unit tests
• Mock services
www.jasonhanson.com/360flex
28. Screen Targets
• Web (.swf)
• AIR (.air)
• AIR for Android (.apk)
• iOS (.ipa)
• AIR for BlackBerry Playbook (.bar)
• AIR for * (.***)
www.jasonhanson.com/360flex
34. Architecture
• MVC (with presenters) i es controller
e rt
r op
p
method
• Model re
calls
o
event
st
• View
• Presenter model presenter
event
• Controller
method
calls
• Passive view
view
view
view
view
view
www.jasonhanson.com/360flex
35. presenter
Presenter
• Same basic concept as Presentation Model
• Separates business logic from view
• Views change more frequently then any
other part of the app
• Views for different screen targets may
have vastly different display code
www.jasonhanson.com/360flex
36. presenter
Presenter
• Public methods to do ‘stuff’
• Contract exposed with interface
• Communicates by dispatching events
• Dispatches events to controller
• Dispatches events to views
• Never holds instance of views
www.jasonhanson.com/360flex
37. presenter
Presenter
• Why Bother?
• Same presenter can be used for multiple
views in multiple screen target apps
• Code in presenters can be unit tested,
while code in views is often very hard to
test
www.jasonhanson.com/360flex
38. controller
Controller
• Control messaging (Grand Central)
• Manage state
• Listen for events from presenters
• Make service calls
• Set properties on presenters
• Set properties on the models
www.jasonhanson.com/360flex
39. model
Model
• Hold instances of top-level presenters
• Sparingly hold instances of data
shared between multiple presenters
• Singleton
• Easy to ‘inject
• Only place a singleton is used
• Not an IEventDispatcher
www.jasonhanson.com/360flex
40. view
view
view
view
view View
• Never in core lib
• Screen specific display code
• Holds instance of presenter (interface)
• Makes function calls on presenter
• Listens to events on presenter
• Holds little or no data
www.jasonhanson.com/360flex
43. view
view
Screen
view
view
view Targets
• View code is “throw-away” code
• Resist the urge to over-engineer views
• Often faster to just rewrite view code
then it is to build configurable
components
• Screen specific view code
www.jasonhanson.com/360flex
51. Application State
• Application state per functional area
• Presenter for each application state
• Presenter to view ratio may not be 1:1
• Some views may reuse the same
presenter
www.jasonhanson.com/360flex
52. Application State
• Presenter currentState property
• For MXML view bind the
presenter.currentState to the currentState
of the view
• Change state on the presenter changes
state on the view
• In most cases views and presenters do not
modify currentState, the controller does
www.jasonhanson.com/360flex
54. Tip: No States as a
String-Based Contracts
• Bad idea in general
• Typos break apps at runtime :(
• Use enums or constants for state
names
• Spark state name property does not
support constants values. Must use a
string in this case.
www.jasonhanson.com/360flex
56. Spark States
User error failure point.
Strings not compile checked
against enum.
www.jasonhanson.com/360flex
57. ViewNavigator
State Translator
• Mobile View-Based Applications use a
ViewNavigator
• Similar to a stack of cards
• pushView(viewClass:Class, ....)
• replaceView()
• popView(), popToFirstView()
• not state friendly by default
www.jasonhanson.com/360flex
58. ViewNavigator
State Translator
• ViewNavigator methods require a
view class type
• State is held in the ‘core’ presenters
• Presenters have no knowledge of
screen-specific view classes
• Need to translate states to view
classes
www.jasonhanson.com/360flex
59. ViewNavigator
State Translator
www.jasonhanson.com/360flex
60. ViewNavigator
State Translator
www.jasonhanson.com/360flex
61. ViewNavigator
State Translator
www.jasonhanson.com/360flex
65. Where to go
From Here?
• Get Flash Builder 4.5
• Check out other talks this week for
more details on different parts of what
I covered today
• Go build something!
www.jasonhanson.com/360flex
67. Advert:
My Other Talk
• Deep Dive into Flex Mobile Item
Renderers
• Wed 10:50am - 12:00pm
• Snip from that preso on next slide
www.jasonhanson.com/360flex