FMX Mobile Update

My adventures in Delphi have been very enjoyable. It’s great to see it continually evolving.

This mobile application uses the Delphi FMX (FireMonkey) framework. It is an experimental rewrite of the temperaments application I had previously written in Flutter and Xamarin.

I’ve just begun work on the Articles section so it’s rough. There’s still a lot of work to do.

It is much more performant and responsive now.

Project Structure

This is the project structure to date:

All the tricks and workarounds are in the Utils folder.

The Data folder is simple thanks to Delphi’s Unit structure, for example the Profile Unit:

Using this unit couldn’t be simpler:

{ find all profiles }
for Profile in Profiles.FindAll

{ delete the selected profile }
Profiles.Delete(SelectedProfileId);

{ add a profile }
var Profile := Profiles.Add(name);

{ set an answer as checked }
FProfile.Answer[id] := IsChecked;

Controller

There is one main form which is used to host individual views, and a controller is responsible for managing the views and application navigation.

This is the function which creates views, the Id is used to identify a persisted view:

T is constrained in the interface section:

function CreateView<T:TView>(AId: TViewId): T;

And the code which docks a view looks like this:

An unanticipated change of view happens due to two events:

  • User Input
  • The OS puts the app in the background

Both these cases are managed. The view gets a chance to apply changes as it is notified of navigation events, such as the back button was pressed, and its state is also managed when moving to the background.

The main form delegates persistence of state to the controller which works with the active view:

Or in the case of recovery, to restore the persisted view and its state:

Frames, Frames, and More Frames

The views all inherit from the base TView Frame.

Individual UI controls are also created as frames each with their own behaviour.

The views are composed of these frames.

This high cohesion and low coupling makes it very simple to maintain the code base. It also takes advantage of the UI designer in Delphi. I love the separation of code and UI Delphi affords, it keeps the code clutter free. For example, the UI and class definition for TPictureFrame:

There’s actually very little to the views. Consider this view which allows the user to select an image:

It has this UI structure – it’s a TFrame descendant with a TVertScrollBox:

And just these few lines of code behind:

That’s the beauty of Delphi, it offers great support for component based design. You have so much freedom to design the application in the way you see fit, there’s few constraints, and state managements is simple.

I also appreciate how easy it is to apply professional styling. Delphi ships with a number of nice styles, but there are other providers available.

Flexibility

FMX is very flexible. Any control can parent other controls, placing them relative to itself. That’s on top of a comprehensive layout system, and controls like TLayout and TFrame. Animations and effects are also simple to employ. Here’s a contrived example of creating a reusable button:

The rectangle is the parent of the controls, GlowButton is just a TFrame host enabling reuse:

It’s like a combination of WPF flexibility with WinForms simplicity.

Summary

It has been fun, I like using FMX.

The project has been coming along, but I must say FMX still lacks the stability and polish of the VCL. There’s a number of issues you have to workaround, and not so many third party plugins compared to Flutter etc. The biggest pain point for an Indie or Hobbyist is the cost, annual renewal fees are expensive. If your application makes money then that’s not a problem, but if not, you’ll suffer vendor lock in. If you are a Delphi shop, or professional, FMX makes a lot of sense.

Embarcadero is committed to a Desktop First approach – makes sense given Delphi’s strengths and history – but I hope they continue to improve FMX, it has a lot of potential in an IoT world.

Of the cross-platform frameworks I’ve tried, FMX is my favourite so far.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s