Photo search with PureMVC in AS3

23rd April, 2011

Since I have promised to share a PureMVC site template a long time ago, and it is still bound to happen, I will now share another project I also built using the aforesaid framework.

This little project was built about four months ago, to solve a skill test for a Senior Flash Developer position in a London based agency. It took around 10 hours to complete during after-work time. A working demonstration is available here:

The Project.

The test required to develop a small image gallery in Actionscript 3 storing the images set in an XML file and allowing a tag based search functionality. Also, all coding should follow Object Oriented Programming through classes and Actionscript’s coding conventions and best practices.

The visual side of the application was also important. And, in my opinion, this is a point that marks a great difference between “hardcore” developers — with strong technical skills but, usually, low sensibility to visual design – and front-end developers in general, where a Flash Developer should be included. Any good front-end developer should comfortably know the elements and principles of design.

Take time defining the problem, and then solve it fast.

Having recently learned how to use PureMVC, this test seemed like a great way to strengthen my experience with the framework. It was fairly simple and only had to implement a couple of basic actions: show the photos with or without a tag filter applied and show each photo in detail.

For the visual layout, I opted for a grid-less structure simulating a real life environment: a wood floor with the photos scattered all over. It wasn’t the most common solution, but it was still easy to use and hopefully eye-candy enough to stand out from the crowd (the other developers applying for the same position).

The architecture.

As any regular MVC project, the classes needed to be grouped in three folders / packages: model (storing all the data that the app will need), view (where anything-visual is kept and the user actions are captured) and controller (providing communication between view and model).

The stage was created in a single Flash file using the Main class as the document class, with all the visual assets in the library being linked to classes in the view package.

Gluing it all together.

From this point on, you should have the source code nearby in order to check it as I briefly explain the logic behind it. The source code is available to browse and download at my GitHub repository:

Startup and the Controller.

It all starts in the Main class in the app package. As soon as the stage is available, the AppFacade singleton is fired, initialising the PureMVC framework, overriding the sendNotification method — to allow debug control, by tracing the notification arguments — and registering the possible controller command calls with specific notifications:
– the StartupCommand is triggered by the STARTUP notification. It registers and initialises the data sources available (StageProxy, StateProxy and PhotosProxy), and the AppMediator (attaching it to the stage) and then requests a new photo search, without any filter applied, to populate the initial layout.
– the PhotoSearchCommand will listen for PHOTO_SEARCH notifications, handling all searches, through the PhotosProxy.
– the PhotoDetailCommand will respond to SHOW and HIDE notifications, setting the app state using the StateProxy, and showing or hiding the detail of a photo.

The Model.

The StageProxy allows access to the app stage, and dispatches notifications every time the stage is resized.
The StateProxy works as a central point to store information about the current state of the app. Namely the current photo selected, if any, and dispatches notification when the state is changed.
The PhotosProxy loads and provides methods to access the information stored in the XML, like search and returns the result coupled to a PHOTO_SEARCH_RESULT notification.

And the View.

The AppMediator registers mediators for the visual components of the layout: the GalleryMediator, the SearchBoxMediator and the DetailMediator.

The GalleryMediator controls the gallery movieclip on the stage, that will load and display the photo thumbnails. It subscribes to PHOTO_CHANGED, PHOTO_SEARCH_RESULT and STAGE_RESIZE notifications in order to update the gallery movieclip as needed. It also listens for clicks in the photo thumbnails and dispatches notifications to the AppMediator when it happens.

The SearchBoxMediator holds a reference to the searchBox movieclip on the stage. It subscribes to PHOTO_SEARCH_RESULT and STAGE_RESIZE notifications and dispatches a PHOTO_SEARCH notification when the user submits the search.

The DetailMediator is linked to the detail movieclip on the stage, updating the display when the subscribed notifications PHOTO_CHANGED or STAGE_RESIZE are received, and dispatches a PHOTO_HIDE notification when the user clicks on the detail movieclip.

All the visual components in the library linked to classes in the app.view.components package have simple methods to update the display or dispatch custom events that will then be picked up by the associated mediators and processed as need be.

Wrapping up.

This was an overview of the logic behind the app.
Feel free to browse and change the code to see what happens and learn with it.
If you end up facing problems or doubts, just drop me an email or comment using the form bellow and I will happily give you all the help needed.

Again, the source code is available here:

and the working demo here:

Thanks for reading this article!