Rendering PDF in AIR with Flash (not Flex)

I googled for methods to render PDF in AIR with Flash. But most of examples were for Flex applications. Actually it also very simple to render PDF with Flash (pure ActionScript project instead of Flex project).

The most important thing for rendering PDF in Flash, the stage scale mode must be NO_SCALE, while by default, a pure ActionScript AIR application running in SHOW_ALL mode.

PDF in AIR Application, Limitations

Although PDF and Flash both technologies from Adobe, in AIR seems they’re separated, PDF always render on top of AIR stage, not really added into display list inside Flash. PDF can be controlled through Javascript, but nothing else. Very limit functionality.

AIR 2 support to open PDF files with native default application, which might be more friendly for users than put PDF inside an AIR application.

Flex Interface Design with Scale9Grid Bitmap Class, another approach

Recently I changed my job to part time, so got some spare time in my own project and some researches on Flex developments. I’m happy with that as I found more and more solutions in these days when I’m not pressed by full-time tasks as a major/lead developer and the only senior/firefighter for a team.

What I get most excited is getting some more ideas on interface design methods for Flex/AIR applications. I’m not a designer, most of my time focus on Flex/Flash coding and I constantly feel bad especially interface design in Flex. Not only Flex itself has no good work flow yet designers can easy to work, also it very hard for team/companies to find a really good designer for Flex projects.

Ok, I’m talking here is another optional approach in interface design for Flex. See follow I did in my own Flex/Air project:

Have you seen somewhere like this? Well, I don’t have designer on my own project, but like good designs when I write codes and testing, so I ‘borrowed’ some graphics from CS4 for use temperorily. It looks beautiful isn’t it? I hate any bad ugly designs in development, man, it’s Flash platform. I like beauties when I’m working and that’s why I choose to work as my career.

Requirements to make it?

The example above I used three images:

  • left highlight skin image
  • right skin image
  • dropdown button image

Scale9Grid bitmap class, you can get from bytearray.org. (I made some small modifications so its Scale9Bitmap class in this example. May write a new UIComponent-based class myself from scratch later when have time.)

How to make it?

The example above is the dock header, each one is a single object based on UIComponent. I show here how to make the right skin inside that component.

createChildren function, load bitmap with url:

Complete handler:

updateDisplayList function:

The component wouldn’t create bitmap until loader complete, then insert into bottom layer as its the background image. (In this example I put another skin image above so it will be inserted into layer 1 when the bottom layer has already added, that’s why I use a flag _rightSkinLoaded.)

The bitmap wouldn’t renderer until invalidation display list is called, which make maximum performance based on Flex framework. In updateDisplayList function, two skin images will be scaled with scale9Grid rectangles, that’s where the magic happens.

There’s one more thing, to get best result in my example, scale 9 bitmaps works better when positioning in pixels, I use int(xx) as followed:

Discussion

There already lots of approaches for interface design/skinning, like Flex/CS4 skinning kits, Degrafa. These approaches ‘work’ but I myself much like use scale9grid bitmap inside component directly. Here’s what I think:

  • Any designer can make graphics design, can support developers easy to use this approach, in case of scale 9 grid can work perfect (I think most standard interface components?).
  • Degrafa can be hard to ‘code’ graphics result what designer expect, but image with scale9 rectangle easy.
  • Less code (code lines after compiling) and better performance. If there’s tons of components in your application, you have to take care of performance. If you can, don’t use CSS skins for highly reused components.
  • Bitmap don’t need to be embedded, which make less size of application and less compiling time if there’s many bitmaps.

Of course, this is just an optional approach for Flex interface design, as scale 9 slices may not work in some designs.

SQLite as Service in AIR (Flash/Flex)

I introduce here is my approach (not fully complete framework), to use SQLite like an external service in AIR, in most simple way.

The following code is how it to be used in Flash/Flex application:

That simple? Sure it is.

Don’t forget, in AIR applications, using SQLite we MUST use asynchronous connections for user experiences. The framework will do rest hard jobs for you.

Framework Features:

  • SQLConnection instances reused.
  • SQLStatement instances reused.
  • Statements executed in queue automatically, no extra control required.
  • Dealing with SQLite locks inside framework.
  • Feedback(result/fault) functions can be reused, similar with pureMVC’s handleNotifications.
  • Support to run feedback functions not original object, i.e. a view sent a query then closed, after query complete, run feedback function in another object/view.
  • All statement/connection objects managed and resources can be cleaned.

SQLite Performance Best Practices

It’s important to get benefit of maximum performance of database, SQLite has its own specifications. One of them, Adobe has already documented, is the cached statements, cached statements can be reused and increase performance largely.

The second one, I didn’t see anyone mentioned it (maybe I missed), is the lock system of SQLite.

SQLite has a special lock system, have a look the figure followed:

Here’s several important rules about SQLite lock system:

  • A connection can have only one transaction (working statement) at a time.
  • SQLite can have multiple read transactions(read statement) and one single write transaction(write statement) at the same time.
  • When the write transaction working or waiting, no more new connections can work.
  • The waiting write transaction will be executed only after all other read transactions complete.

The third one, using SQLTransactionLockType.IMMEDIATE for write transaction can improve performance when necessary.

My solution implement first and second, the last one I didn’t find a good solution to use it in asynchronous connection mode, will talk it later.

Framework Structure

The framework include three major classes:

  • SQLManager
  • ConnectionPool, manage and reuse connections for statements.
  • StatementManager, add/remove statements.

These object classes/interface used by the framework:

  • SQLiteStatement, after an operation finished, it run feedback based on caller and function(string). Feedback function can be different to the original view(object).
  • SQLiteConnection
  • CachedStatement, statement cannot to be run immediately will be cached as a CachedStatement in waiting list.
  • ISQLResponder, optional interface for caller view/object.

Framework workflow

  • Create instance for a database file (file can not exist even)
  • Add statements when needed, statements will be initialized.
  • View call for execute one or several statements with statement_id, set result/fault function, and flag whether need to ignore fault (if ignore, sqlite will continue to run waiting statements)
  • SQLManager get statement instance, or cache your request to a waiting list if already used.
  • SQLManager ask ConnectionPool to apply a SQLiteConnection for statement to execute.
  • Statement try to execute, if SQLConnection not connected, waiting for that connection and execute.
  • Statement completed/fault, call feedback function with statement_id (if asked), and execute next waiting statement if exist.

The framework keep minimum instances of SQLConnection and SQLStatement, and easy to be removed as they’re managed for garbage collection.

I’m not going to publish source code right now as it’s not a complete framework yet.