Skip to content

State & Events ​

FMXER has no state-management library of its own: elements are created by closures, and the closures are where state lives. This page collects the patterns used in the demos, and the rules that keep them safe.

Element events are closures ​

Elements expose their events as anonymous-method properties or setters, not as TNotifyEvents:

ElementEvent
TButtonFrameSetOnClickProc(TProc) (also SetOnClickHandler, OnClickHandler), OnUpdateHandler: TProc<TAction>
TEditFrameOnChangeProc: TProc<Boolean> — True while typing (OnChangeTracking), False on OnChange
TTrackbarFrameOnChangeProc: TProc<Single>
TColorPickerFrameOnChangeProc: TProc<TAlphaColor>
TTextFrameSetOnClickProc(TProc) (also SetOnClick)
TGlyphFrameSetOnClickProc(TProc)
TChipFrameSetOnClickProc(TProc) (also OnClickHandler)
TActionButtonFrameSetOnClickProc(TProc) (also OnClickProc)
TImageFrame, TSVGFrame, TAnimatedImageFrame, TQRCodeFrameSetOnTapHandler / OnTapHandler (tap on mobile, mouse up on Windows)
TListViewFramea TListViewItemClickProc per item (AddItem), SetOnSearchChange

Assign them in the configuration procedure:

pascal
Col.AddFrame<TTrackbarFrame>(70
, procedure (Track: TTrackbarFrame)
  begin
    Track
    .SetCaption('Volume')
    .SetMin(0).SetMax(100).SetValue(50)
    .SetOnChangeProc(
      procedure (AValue: Single)
      begin
        Track.SetExtraText(Round(AValue).ToString + '%');
      end
    );
  end
);

Sharing values between elements ​

Captured variables ​

Elements built in the same procedure can share a local variable. The HelloWorld demo keeps a reference to an edit frame and reads it from a button of another element:

pascal
var LEditFrame: TEditFrame := nil;

Col
.AddFrame<TEditFrame>(100
, procedure (Frame: TEditFrame)
  begin
    Frame.SetCaption('Name');
    LEditFrame := Frame;              // keep it for later
  end
)
.AddFrame<TButtonFrame>(60
, procedure (Button: TButtonFrame)
  begin
    Button.SetText('Greet').SetOnClickHandler(
      procedure
      begin
        ShowMessage('Hello, ' + LEditFrame.GetText);
      end
    );
  end
);

This is safe as long as both elements belong to the same screen: they are freed together.

Closures in a loop capture the variable, not its value

An anonymous method captures variables, not their values. An inline variable declared in the body of a loop is not a new variable at each iteration: it is one variable, shared by all the closures created in the loop. A handler that runs later, such as a tap or a timer, sees the value of the last iteration:

pascal
for LIndex := 0 to High(Orders) do
begin
  var LOrder := LIndex;
  List.AddItem(Orders[LIndex].Customer, '', -1
  , procedure (const AItem: TListViewItem)
    begin
      OpenOrder(LOrder);   // wrong: the last order, whatever the item tapped
    end
  );
end;

Give each handler its own value instead:

  • from the element that calls it, for example the Tag of the list item;
  • or through a function that takes the value as a parameter and returns the closure, since each call has its own parameter.
pascal
for LIndex := 0 to High(Orders) do
  List.AddItem(Orders[LIndex].Customer, '', -1
  , procedure (const AItem: TListViewItem)
    begin
      OpenOrder(AItem.Tag);   // the item tapped
    end
  ).Tag := LIndex;

The same holds for the loop variable itself. Configuration procedures are not affected: they run immediately, during the iteration, as with AddFrame and AddTabFrame.

The state of a route ​

Most screens work on something: an order, a customer, a form being filled. A route defined with a state gets its own TComponent (usually a TDataModule, designed in the IDE with its FireDAC components and LiveBindings), created with the route and freed with it. It is the FMXER counterpart of the State of a Flutter StatefulWidget, with dispose done by the Delphi ownership.

pascal
type
  TOrderState = class(TDataModule)
  public
    procedure Load(const AIndex: Integer);   // the arguments of the route
    procedure Save;
    property Order: TOrder ...;
  end;

Navigator.DefineRoute<TScaffoldForm, TOrderState>('order'
, procedure (Scaffold: TScaffoldForm; State: TOrderState)
  begin
    Scaffold.SetTitle(State.Order.Customer);
    // ... the elements read and write State
  end
);
  • The navigator creates the state with the route, before the view, through the virtual constructor Create(nil), so a data module loads its design. The view then owns it: closing the route frees the state with the view.
  • Navigator.RouteState<TOrderState>('order') returns the state while the route is active, HasRouteState tells whether there is one.
  • With AKeepAlive = True (third parameter) the navigator owns the state instead. It survives the route and the route finds it again, with its values, when it is opened again. It is freed with the navigator.

The route gets its arguments in its state, from whoever opens it, and can give back a result:

pascal
Navigator.RouteTo<TOrderState>('order'
, procedure (State: TOrderState)
  begin
    State.Load(LIndex);   // before the view is built
  end
, procedure (AResult: TValue)
  begin
    if AResult.IsEmpty then
      Scaffold.ShowSnackBar('Not saved', 1500)
    else
      List.RefreshList;
  end
);

// in the order route, the Save button:
State.Save;
Navigator.CloseRouteWithResult('order', State.Order.Customer);

See Arguments and results for the rules, and the Orders demo.

Since v.1.8

The state of a route, RouteTo<TState>, RouteToForResult and CloseRouteWithResult are new in v.1.8.

Application state in a data module ​

For values that outlive a screen — the user, a selection, a setting — use a data module or an object created at startup, and let routes read it when they are built. The LibraryStore demo stores the selected author in RemoteData.selectedAuthorId before routing to the books of that author:

pascal
List.AddItem(Author.Surname + ' ' + Author.Name, '', -1
, procedure (const AItem: TListViewItem)
  begin
    RemoteData.selectedAuthorId := AItem.Tag;
    Navigator.RouteTo('books_by_author');   // reads selectedAuthorId while it builds
  end
);

Because a route is rebuilt each time it is opened, this is often all you need.

Observable values ​

A value read when a screen is built does not update the screen when it changes. TObservable<T> (unit FMXER.Observable) is a value that notifies its listeners when it changes, the FMXER counterpart of a Flutter ValueNotifier used with ValueListenableBuilder. There are two differences from Flutter: the listener updates its element instead of rebuilding a widget tree, and the Delphi ownership removes the listener when the element is freed.

pascal
type
  TOrderState = class(TDataModule)
  public
    Paid: TObservable<Boolean>;
    constructor Create(AOwner: TComponent); override;
  end;

constructor TOrderState.Create(AOwner: TComponent);
begin
  inherited;
  Paid := TObservable<Boolean>.Create(Self, False);   // a component: freed with the state
end;

// in the route: the text listens to the state
Col.AddFrame<TTextFrame>(30
, procedure (Text: TTextFrame)
  begin
    State.Paid.Listen(Text,          // the owner: when Text is freed, it stops listening
      procedure (APaid: Boolean)
      begin
        Text.SetContent(IfThen(APaid, 'Paid', 'To be paid'));
      end);                          // called now too: the text starts with the current value
  end
);

// anywhere, a background thread too
State.Paid.Value := True;
MemberDescription
Create(AOwner; AInitial)a TComponent: create it owned by the state (a data module, the state of a route), which frees it
Valuesetting a different value notifies the listeners (equality: TEqualityComparer<T>.Default)
Listen(AOwner, AProc: TProc<T>; ACallNow = True): TObservable<T>AProc gets every new value. With ACallNow it is called now too, with the current value, so you initialize and update the element in one place. The listener is removed when AOwner is freed (nil: never). Call it from the main thread
Unlisten(AOwner)removes the listeners of AOwner before it is freed
Update(AFunc: TFunc<T, T>)Value := AFunc(Value): change a field of a record
Notifynotifies the current value even if it did not change
ListenerCount

TChangeNotifier, in the same unit, is the counterpart of ChangeNotifier: a notification without a value. Use it to say "this state changed" as a whole, for example after loading it: Listen(AOwner, AProc: TProc; ACallNow = False), NotifyChanged, Unlisten.

The rules:

  • The owner of a listener is the element it updates, not the route or the form. When the element is freed (its route closed, a list rebuilt), the listener goes with it, so it never runs on a freed element. This replaces the subscription guard of System.Messaging.
  • Notifications run in the main thread. A value set by a background thread is stored at once, and its listeners are called through TThread.Queue. A notification still queued when the observable is freed is dropped.
  • Equality is the default comparer of T. For records it compares the bytes, so a string field may give an extra, harmless notification. An object changed inside gives none: call Notify.
  • Two-way: a switch or an edit writes the value in its OnChangeProc (State.Paid.Value := AChecked). A listener of the same value then runs: it must not write the value back to the element, or it can loop. The bindings do both for you.

The Orders demo uses all of it: observables in the state of the order route, bound to its elements, a TChangeNotifier of the app that rebuilds the list when an order is saved, and a "last sync" set every 3 seconds by a background task.

Bindings ​

The Bind* methods of the elements write the Listen and the OnChangeProc for you. An input shows the observable and writes it back when the user changes it (two-way); a text, a progress bar or a button only shows it (one-way):

pascal
Switch.SetText('Paid').BindChecked(State.Paid);          // two-way
Edit.SetCaption('Note').BindText(State.Note);            // two-way, at every keystroke
Button.SetText('Save').BindEnabled(State.Modified);      // one-way
Text.BindContent<Currency>(State.Total,                  // one-way, formatted
  function (V: Currency): string
  begin
    Result := FormatCurr('#,##0.00', V);
  end);
ElementBinding
TSwitchFrame, TCheckBoxFrameBindChecked(TObservable<Boolean>), two-way
TEditFrameBindText(TObservable<string>; AOnEditEnd = False), two-way: at every keystroke, or when the edit ends with AOnEditEnd
TTrackbarFrameBindValue(TObservable<Single>), two-way (set Min and Max first)
TComboBoxFrame, TRadioGroupFrameBindItemIndex(TObservable<Integer>), two-way (set the items or options first)
TDateEditFrameBindDate(TObservable<TDate>), two-way
TTextFrameBindContent(TObservable<string>), BindContent<T>(TObservable<T>; AFormat: TFunc<T, string>)
TProgressBarFrameBindValue(TObservable<Single>)
TButtonFrameBindEnabled(TObservable<Boolean>), the Enabled of its action
any element or control (FMXER.UI.Misc helper)BindVisible(TObservable<Boolean>), BindEnabled(TObservable<Boolean>)
  • The element owns the binding, as with Listen: it goes with the element. An observable freed before the element only stops the updates.
  • The value comes from the observable: the element shows it at once. Set the range, items or options before binding.
  • OnChangeProc still works: the binding uses its own hook. A binding that changes a switch or a check box from the observable calls OnChangeProc, as SetChecked does.
  • No loops: the element is updated only when it shows a different value, so the caret of an edit stays where it is while typing.
  • One binding per property: a second BindChecked (or BindContent, ...) on the same element replaces the first.

Since v.1.9

FMXER.Observable and the Bind* methods are new in v.1.9.

Notifications across screens ​

When an open screen must react to a change made elsewhere, an observable value is usually the simplest way. For a part of the app that should not know the state it reacts to, the demos use System.Messaging. TAppState in the HelloWorld demo sends a message whenever its value changes:

pascal
type
  TSomethingChangedMsg = class(TMessage<string>);

procedure TAppState.SetSomething(const Value: string);
begin
  if FSomething <> Value then
  begin
    FSomething := Value;
    TMessageManager.DefaultManager.SendMessage(Self, TSomethingChangedMsg.Create(FSomething));
  end;
end;

and an element subscribes in its configuration procedure:

pascal
TMessageManager.DefaultManager.SubscribeToMessage(TSomethingChangedMsg
, procedure (const Sender: TObject; const M: TMessage)
  begin
    QR.SetContent((M as TSomethingChangedMsg).Value);
  end
);

Subscriptions and the lifetime of elements ​

Unsubscribe when the element goes away

A subscription lives in the global message manager, the element does not: when its route closes, the element is freed and the subscription stays. The next message runs the closure on a freed object (an access violation, or worse), and opening the route again adds a second subscription.

Tie the subscription to the element. A tiny component owned by the element does it — when the element is freed, so is the guard, and the guard unsubscribes:

pascal
uses System.Classes, System.Messaging;

type
  TSubscriptionGuard = class(TComponent)
  private
    FMessageClass: TClass;
    FId: Integer;
  public
    constructor Create(const AOwner: TComponent; const AMessageClass: TClass;
      const AListener: TMessageListenerMethod); reintroduce; overload;
    constructor Create(const AOwner: TComponent; const AMessageClass: TClass;
      const AListener: TMessageListener); reintroduce; overload;
    destructor Destroy; override;
  end;

constructor TSubscriptionGuard.Create(const AOwner: TComponent;
  const AMessageClass: TClass; const AListener: TMessageListener);
begin
  inherited Create(AOwner);
  FMessageClass := AMessageClass;
  FId := TMessageManager.DefaultManager.SubscribeToMessage(AMessageClass, AListener);
end;

constructor TSubscriptionGuard.Create(const AOwner: TComponent;
  const AMessageClass: TClass; const AListener: TMessageListenerMethod);
begin
  inherited Create(AOwner);
  FMessageClass := AMessageClass;
  FId := TMessageManager.DefaultManager.SubscribeToMessage(AMessageClass, AListener);
end;

destructor TSubscriptionGuard.Destroy;
begin
  TMessageManager.DefaultManager.Unsubscribe(FMessageClass, FId);
  inherited;
end;
pascal
TSubscriptionGuard.Create(QR, TSomethingChangedMsg
, procedure (const Sender: TObject; const M: TMessage)
  begin
    QR.SetContent((M as TSomethingChangedMsg).Value);
  end
);

The demos use this guard: HelloWorld declares it in Data.AppState and gives one to each QR code that follows the edit; LibraryStore wraps it in TRetrieveMsg.Subscribe(ABF, ...) (Utils.Msg), owned by the activity animation in the title detail area. Note that hiding an element (HideTitleDetailContent) does not free it: the subscription stays active until the route closes.

The same rule applies to anything that can call back later: timers, TDelayedAction, background threads, data-module listener lists (the QRCode demo clears its camera listeners in OnCloseRoute).

Doing work in the background ​

Long operations (REST calls, database queries) should not block the UI thread while a screen is built. The LibraryStore demo runs its MARS requests asynchronously and fills the list in the callback, while the scaffold's title detail area shows an activity animation:

pascal
List.ItemBuilderProc :=
  procedure
  begin
    RemoteData.RetrieveBooks(
      procedure (ABooks: TArray<TBook>)      // called on the UI thread
      begin
        for var LBook in ABooks do
          List.AddItem(LBook.Title, LBook.Author.Surname);
      end
    , procedure (AError: string)
      begin
        Scaffold.ShowSnackBar(AError, 3000);
      end
    );
  end;

When the callback can arrive after the screen was closed, check that the route is still active (Navigator.IsRouteActive) before touching its elements, as the Skia101 demo does for its QR code timer.

Released under the Mozilla Public License 2.0.