Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#12

AW: Immer Ärger mit ARC

  Alt 25. Okt 2014, 00:41
Im Anhang mal das ganze Projekt.

Zu den Änderungen:

Eine zentrale Instanz TAppStarter bekommt eine anonyme Factory mitgegeben um den Presenter zu erzeugen.

Aufgerufen wird diese Factory dann bei den Mobile-Plattformen (IOS/ANDROID) über den Delphi-Referenz durchsuchenIFMXApplicationEventService und zwar beim Event Delphi-Referenz durchsuchenTApplicationEvent.FinishedLaunching. Bei allen anderen Plattformen wird zunächst Delphi-Referenz durchsuchenTApplication.RealCreateForms aufgerufen und dann TAppStarter.CreateMainPresenter (die ruft dann die Factory auf).
Delphi-Quellcode:
unit App.Starter;

interface

{$INCLUDE 'Conditional.inc'}

uses
  System.SysUtils,
  FMX.Platform;

type
  TAppStarter = class
  private
    FPresenterFactory: TFunc<IInterface>;
    FMainPresenter: IInterface;
    FApplicationEventService: IFMXApplicationEventService;
    procedure GetPlatformServices;
    function ApplicationEventHandler( AAppEvent: TApplicationEvent; AContext: TObject ): Boolean;
{$IFDEF MOBILE}
  private // bei Mobile-Platform verstecken
{$ELSE}
  public // sonst öffentlich aufrufbar
{$ENDIF}
    procedure CreateMainPresenter;
  public
    constructor Create( PresenterFactory: TFunc<IInterface> );

  end;

implementation

{ TAppStarter }

function TAppStarter.ApplicationEventHandler( AAppEvent: TApplicationEvent; AContext: TObject ): Boolean;
begin
  if AAppEvent = TApplicationEvent.FinishedLaunching
  then
    CreateMainPresenter;
  Result := True;
end;

constructor TAppStarter.Create( PresenterFactory: TFunc<IInterface> );
begin
  inherited Create;
  FPresenterFactory := PresenterFactory;
  GetPlatformServices;
end;

procedure TAppStarter.CreateMainPresenter;
begin
  if not Assigned( FMainPresenter )
  then
    FMainPresenter := FPresenterFactory( );
end;

procedure TAppStarter.GetPlatformServices;
begin
  if TPlatformServices.Current.SupportsPlatformService( IFMXApplicationEventService, FApplicationEventService )
  then
    FApplicationEventService.SetApplicationEventHandler( Self.ApplicationEventHandler );
end;

end.
und die Projekt-Datei
Delphi-Quellcode:
program dp_182444;

{$INCLUDE 'Conditional.inc'}

uses
  System.StartUpCopy,
  FMX.Forms,
  Form.Main in 'Form.Main.pas{MainForm} ,
  Interfaces.Main in 'Interfaces.Main.pas',
  Model.Main in 'Model.Main.pas',
  MVP.Base in 'MVP.Base.pas',
  Presenter.Main in 'Presenter.Main.pas',
  App.Starter in 'App.Starter.pas';

{$R *.res}

procedure Main;
var
  LAppStarter: TAppStarter;
begin
  Application.Initialize;

  LAppStarter := TAppStarter.Create(
      function: IInterface
    var
      LPresenter: IMainPresenter;
    begin
      LPresenter := TMainPresenter.Create( MainForm, TMainModel.Create );
      LPresenter.SetAppStarter( LAppStarter );
      Result := LPresenter;
    end );
  try

    Application.CreateForm( TMainForm, MainForm );
{$IFNDEF MOBILE}
    Application.RealCreateForms;
    LAppStarter.CreateMainPresenter;
{$ENDIF}
    Application.Run;
  finally
    LAppStarter.Free;
  end;
end;

begin
  ReportMemoryLeaksOnShutdown := True;
  Main;

end.
Angehängte Dateien
Dateityp: zip dp_182444.zip (4,0 KB, 3x aufgerufen)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (25. Okt 2014 um 07:33 Uhr)
  Mit Zitat antworten Zitat