Search code examples
delphifiremonkeydelphi-12-athenstframe

How to trigger a procedure in multiple frames?


I have an app with an infinite (many) amount of frames that are dynamically added and removed. I never know which ones are active and which ones are not active.

All of them have a procedure on them like this:

procedure DoAfterPermissions();
begin
  // Code to run after permissions have been refreshed
end;

On my main form, I refresh permissions every now and then. Once the permissions are refreshed, then I need that procedure to be executed on all of my frames that are created.

How can this be done? I do not even know where to start.

Edit: This is a FMX app that runs on Windows, Android and iOS.


Solution

  • I would suggest two things:

    1. Have all of the frame classes derive from a common base class (or implement a common interface) which defines the DoAfterPermissions() method, and then have all of the frames override it.

    2. Store all of the frame instances in a list whose element type is the base class/interface above.

    This way, you can simply iterate through the list when needed, calling the method directly on each frame.

    For example:

    type
      TMyFrameBase = class(TFrame)
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        procedure DoAfterPermissions; virtual; abstract;
      end;
    
    var
      MyFrames: TList<TMyFrameBase>;
    
    ...
    
    constructor TMyFrameBase.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      MyFrames.Add(Self);
    end;
    
    destructor TMyFrameBase.Destroy;
    begin
      MyFrames.Remove(Self);
      inherited Destroy;
    end;
    
    initialization
      MyFrames := TList<TMyFrameBase>;
    finalization
      MyFrames.Free;
    
    type
      TMyFrame = class(TMyFrameBase)
      public
        procedure DoAfterPermissions; override;
      end;
    
    procedure TMyFrame.DoAfterPermissions;
    begin
      // do something...
    end;
    
    RefreshPermissions;
    for I := 0 to MyFrames.Count-1 do
      MyFrames[I].DoAfterPermissions;