// ------------------------------------------------------------------------------
// DPF.iOS.ApplicationManager Component
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: http://www.dpfaragir.com
//
// Developed By: Babak Yaghoobi
//
// Email #1: yaghoobi@dpfaragir.com
// Email #2: b_yaghobi@yahoo.com
//
// ------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ------------------------------------------------------------------------------
unit DPF.iOS.ApplicationManager;

interface

{$I DPF.iOS.Defs.inc}

uses
  System.SysUtils,
  System.Classes,
  System.Types,
  System.UITypes,
  System.Variants,

  System.TypInfo,
  FMX.VirtualKeyboard,
  DPF.iOS.BaseControl,
{$IFDEF IOS}
  Macapi.ObjectiveC,
  Macapi.ObjCRuntime,
  IOSapi.CocoaTypes,
  IOSapi.Foundation,
  IOSapi.Uikit,
  IOSapi.CoreGraphics,
  IOSapi.CoreImage,
  FMX.Platform.iOS,
  DPF.iOS.Common,
{$ENDIF}
  FMX.Platform,
  FMX.Layouts, FMX.Memo,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls;

type

  // ------------------------------------------------------------------------------
  TDPFAppOnOpenURL = procedure( Sender: TObject; Url: string; sourceApplication: string ) of object;

  [ComponentPlatformsAttribute( PidWin32 or PidWin64 or pidLinux32 or pidAndroid or PidOSX32 or PidiOSDevice or PidiOSSimulator )]
  TDPFApplicationManager = class( TComponent )
  private
    FOnMemoryWarning       : TNotifyEvent;
    FOnBecameActive        : TNotifyEvent;
    FOnEnteredBackground   : TNotifyEvent;
    FOnTimeChange          : TNotifyEvent;
    FOnWillBecomeForeground: TNotifyEvent;
    FOnWillBecomeInactive  : TNotifyEvent;
    FOnWillTerminate       : TNotifyEvent;
    FOnOpenURL             : TDPFAppOnOpenURL;
{$IFDEF IOS}
{$ENDIF}
  protected
    function AppEvent( AAppEvent: TApplicationEvent; AContext: TObject ): Boolean;
  public
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
  published
    property OnMemoryWarning       : TNotifyEvent read FOnMemoryWarning write FOnMemoryWarning;
    property OnBecameActive        : TNotifyEvent read FOnBecameActive write FOnBecameActive;
    property OnEnteredBackground   : TNotifyEvent read FOnEnteredBackground write FOnEnteredBackground;
    property OnTimeChange          : TNotifyEvent read FOnTimeChange write FOnTimeChange;
    property OnWillBecomeForeground: TNotifyEvent read FOnWillBecomeForeground write FOnWillBecomeForeground;
    property OnWillBecomeInactive  : TNotifyEvent read FOnWillBecomeInactive write FOnWillBecomeInactive;
    property OnOnWillTerminate     : TNotifyEvent read FOnWillTerminate write FOnWillTerminate;
    property OnOpenURL             : TDPFAppOnOpenURL read FOnOpenURL write FOnOpenURL;
  end;

  // ------------------------------------------------------------------------------

implementation

// ------------------------------------------------------------------------------
{ TDPFApplicationManager }
// ------------------------------------------------------------------------------
constructor TDPFApplicationManager.Create( AOwner: TComponent );
var
  AppEventSvc: IFMXApplicationEventService;
begin
  inherited Create( AOwner );
  if TPlatformServices.Current.SupportsPlatformService( IFMXApplicationEventService, IInterface( AppEventSvc ) ) then
    AppEventSvc.SetApplicationEventHandler( AppEvent );
end;

// ------------------------------------------------------------------------------
destructor TDPFApplicationManager.Destroy;
begin
{$IFDEF IOS}
{$ENDIF}
  inherited;
end;

// ------------------------------------------------------------------------------
function TDPFApplicationManager.AppEvent( AAppEvent: TApplicationEvent; AContext: TObject ): Boolean;
begin
  result := true;
{$IFDEF IOS}
  DPFNSLog( 'TDPFApplicationManager.AppEvent: ' + IntToStr( Integer( AAppEvent ) ) );
{$ENDIF}
  case AAppEvent of

    // ---------------------------------------------------------
    TApplicationEvent.aeLowMemory:
      begin
        if Assigned( FOnMemoryWarning ) then
          FOnMemoryWarning( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeBecameActive:
      begin
        if Assigned( FOnBecameActive ) then
          FOnBecameActive( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeEnteredBackground:
      begin
        if Assigned( FOnEnteredBackground ) then
          FOnEnteredBackground( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeTimeChange:
      begin
        if Assigned( FOnTimeChange ) then
          FOnTimeChange( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeWillBecomeForeground:
      begin
        if Assigned( FOnWillBecomeForeground ) then
          FOnWillBecomeForeground( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeWillBecomeInactive:
      begin
        if Assigned( FOnWillBecomeInactive ) then
          FOnWillBecomeInactive( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeWillTerminate:
      begin
        if Assigned( FOnWillTerminate ) then
          FOnWillTerminate( Self );
      end;

    // ---------------------------------------------------------
    TApplicationEvent.aeOpenURL:
      begin
        if Assigned( FOnOpenURL ) then
        begin
{$IFDEF IOS}
          FOnOpenURL( Self, TiOSOpenApplicationContext( AContext ).URL, TiOSOpenApplicationContext( AContext ).SourceApp );
{$ENDIF}
        end;
      end;
  end;
end;

// ------------------------------------------------------------------------------
end.
