// ------------------------------------------------------------------------------
// DPF.iOS.NSOperationQueue Component
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: NSOperationQueue://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
//
// NSOperationQueue://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.NSOperationQueue;

interface

uses
  System.Classes,
  System.SysUtils,
  System.TypInfo,
  DPF.iOS.BaseControl,
{$IFDEF IOS}
  Macapi.ObjCRuntime,
  DPF.iOS.Common,

  iOSapi.UIKit,
  Macapi.ObjectiveC,
  iOSapi.CocoaTypes,
  iOSapi.CoreGraphics,
  iOSapi.Foundation,
{$ENDIF}
  FMX.Dialogs;

type
{$M+}
  TDPFNSOperationQueue = class;
{$IFDEF IOS}
{$ENDIF}

  // ------------------------------------------------------------------------------
  [ComponentPlatformsAttribute( PidWin32 or PidWin64 or pidLinux32 or pidAndroid or PidOSX32 or PidiOSDevice or PidiOSSimulator )]
  TDPFNSOperationQueue = class( TComponent )
  private
    FMaxConcurrentOperationCount: Integer;
    procedure SetMaxConcurrentOperationCount( const Value: Integer );
  protected
  public
{$IFDEF IOS}
    FNSOperationQueue: NSOperationQueue;
{$ENDIF}
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
{$IFDEF IOS}
    procedure AddOperation( op: NSOperation );
{$ENDIF}
    procedure CancelAllOperations;
  published
    property MaxConcurrentOperationCount: Integer read FMaxConcurrentOperationCount write SetMaxConcurrentOperationCount default 10;
  end;
  // ----------------------------------------------------------------------------

implementation

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

procedure TDPFNSOperationQueue.CancelAllOperations;
begin
{$IFDEF IOS}
  if Assigned( FNSOperationQueue ) then
  begin
    FNSOperationQueue.setSuspended( true );
    FNSOperationQueue.cancelAllOperations;
    FNSOperationQueue.setSuspended( false );
  end;
{$ENDIF}
end;
{$IFDEF IOS}

// ----------------------------------------------------------------------------
procedure TDPFNSOperationQueue.AddOperation( op: NSOperation );
begin
  if Assigned( FNSOperationQueue ) then
    FNSOperationQueue.addOperation( op );
end;
{$ENDIF}

// ----------------------------------------------------------------------------
constructor TDPFNSOperationQueue.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  FMaxConcurrentOperationCount := 10;
{$IFDEF IOS}
  FNSOperationQueue := TNSOperationQueue.Wrap( TNSOperationQueue.Alloc.init );
  FNSOperationQueue.cancelAllOperations;
  FNSOperationQueue.setMaxConcurrentOperationCount( FMaxConcurrentOperationCount );
  FNSOperationQueue.setSuspended( false );
{$ENDIF}
end;

// ----------------------------------------------------------------------------
destructor TDPFNSOperationQueue.Destroy;
begin
{$IFDEF IOS}
  FNSOperationQueue.cancelAllOperations;
  FNSOperationQueue := nil;
{$ENDIF}
  inherited;
end;

// ----------------------------------------------------------------------------
procedure TDPFNSOperationQueue.SetMaxConcurrentOperationCount( const Value: Integer );
begin
  if Value < 1 then
    Exit;

  FMaxConcurrentOperationCount := Value;
{$IFDEF IOS}
  if Assigned( FNSOperationQueue ) then
    FNSOperationQueue.setMaxConcurrentOperationCount( Value );
{$ENDIF}
end;

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