unit uMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, DPF.iOS.BaseControl,
  System.Math,
  System.DateUtils,
  DPF.iOS.Common,
  DPF.iOS.UIFont,
  DPF.iOS.UITableView,
  DPF.iOS.UITableViewItems,
  DPF.iOS.UILabel,
  DPF.iOS.UIImageView,
  DPF.iOS.UIView,
  DPF.iOS.UIButton,
  DPF.iOS.UISwitch;

type
  TForm3 = class( TForm )
    DPFUITableView1: TDPFUITableView;
    DPFUIView1: TDPFUIView;
    DPFUIView2: TDPFUIView;
    DPFSwitch1: TDPFSwitch;
    DPFButton1: TDPFButton;
    DPFButton2: TDPFButton;
    procedure FormCreate( Sender: TObject );
    procedure DPFUITableView1DrawCell( Sender: TObject; Section, RowNo: Integer; TableItem: TTableItem; var Objects: TArray<DPF.iOS.BaseControl.TDPFiOSBaseControl> );
    procedure DPFUITableView1GetRowHeight( Sender: TObject; Section, RowNo: Integer; var RowHeight: Integer );
    procedure DPFSwitch1Changed( Sender: TObject; ISON: Boolean );
    procedure DPFButton1Click( Sender: TObject );
    procedure DPFButton2Click( Sender: TObject );
  private
    { Private declarations }
  protected
    procedure PaintRects( const UpdateRects: array of TRectF ); override;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.fmx}

procedure TForm3.DPFButton1Click( Sender: TObject );
begin
  FormCreate( nil );
end;

procedure TForm3.DPFButton2Click( Sender: TObject );
begin
  DPFUITableView1.ClearAll( true, true );
end;

procedure TForm3.DPFSwitch1Changed( Sender: TObject; ISON: Boolean );
begin
  DPFUITableView1.AutoScroll := ISON;
end;

// ------------------------------------------------------------------------------
// Notes:
// Dont create again objects, if length( Objects ) > 0
// ------------------------------------------------------------------------------
procedure TForm3.DPFUITableView1DrawCell( Sender: TObject; Section, RowNo: Integer; TableItem: TTableItem; var Objects: TArray<DPF.iOS.BaseControl.TDPFiOSBaseControl> );
const
  icons: array [0 .. 24] of string = ( 'admin', 'businessman', 'businessmen', 'calendar', 'flag_iran', 'flower_blue', 'flower_red', 'flower_white', 'flower_yellow', 'lock', 'lock_open', 'loudspeaker', 'mail', 'news', 'shield_green', 'table', 'trafficlight_green', 'trafficlight_off', 'trafficlight_on', 'trafficlight_red', 'trafficlight_red_yellow',
    'trafficlight_yellow', 'user_headphones', 'users2', 'users4' );
var
  MustBeCreate: Boolean;
begin

  MustBeCreate := length( Objects ) = 0;
  if MustBeCreate then
    SetLength( Objects, 4 );

  if MustBeCreate then
    TDPFImageView( Objects[0] )          := TDPFImageView.Create( nil );
  TDPFImageView( Objects[0] ).Position.X := 0;
  TDPFImageView( Objects[0] ).Position.X := 0;
  TDPFImageView( Objects[0] ).Width      := 64;
  TDPFImageView( Objects[0] ).Shadow     := false;
  TDPFImageView( Objects[0] ).Height     := 64;
  TDPFImageView( Objects[0] ).ImageList.Add( '/Documents/' + icons[RandomRange( 0, 25 )] + '.png' );

  if MustBeCreate then
    TDPFLabel( Objects[1] )             := TDPFLabel.Create( nil );
  TDPFLabel( Objects[1] ).Text          := FormatDateTime( 'hh:mm', IncSecond( now, RandomRange( 1, 10000 ) ) );
  TDPFLabel( Objects[1] ).TextColor     := TAlphaColors.Steelblue;
  TDPFLabel( Objects[1] ).Font.FontName := ios_Verdana_Bold;
  TDPFLabel( Objects[1] ).Width         := 50;
  TDPFLabel( Objects[1] ).Height        := 15;
  TDPFLabel( Objects[1] ).Position.x    := DPFUITableView1.Width - TDPFImageView( Objects[1] ).Width - 1;
  TDPFLabel( Objects[1] ).Position.Y    := 0;

  if MustBeCreate then
    TDPFLabel( Objects[2] )             := TDPFLabel.Create( nil );
  TDPFLabel( Objects[2] ).Text          := 'Custom Label';
  TDPFLabel( Objects[2] ).TextColor     := TAlphaColors.Black;
  TDPFLabel( Objects[2] ).Font.FontName := ios_Verdana_Bold;
  TDPFLabel( Objects[2] ).Width         := 200;
  TDPFLabel( Objects[2] ).Height        := 15;
  TDPFLabel( Objects[2] ).Position.x    := TDPFImageView( Objects[0] ).Position.X + TDPFImageView( Objects[0] ).Width + 1;
  TDPFLabel( Objects[2] ).Position.Y    := 10;

  if MustBeCreate then
    TDPFLabel( Objects[3] )             := TDPFLabel.Create( nil );
  TDPFLabel( Objects[3] ).Text          := 'Custom Detail Label';
  TDPFLabel( Objects[3] ).TextColor     := TAlphaColors.Gray;
  TDPFLabel( Objects[3] ).Font.FontName := ios_Verdana_Bold;
  TDPFLabel( Objects[3] ).Width         := 200;
  TDPFLabel( Objects[3] ).Height        := 15;
  TDPFLabel( Objects[3] ).Position.x    := TDPFImageView( Objects[0] ).Position.X + TDPFImageView( Objects[0] ).Width + 1;
  TDPFLabel( Objects[3] ).Position.Y    := 40;
end;

procedure TForm3.DPFUITableView1GetRowHeight( Sender: TObject; Section, RowNo: Integer; var RowHeight: Integer );
begin
  RowHeight := 80;
end;

procedure TForm3.FormCreate( Sender: TObject );
var
  I: Integer;
begin
  GetSharedApplication.setStatusBarHidden( true );
  DPFUITableView1.ClearAll( false, true );
  with DPFUITableView1.Sections.Add do
  begin
    for I := 0 to 50 do
    begin
      with TableItems.Add do
      begin
        BackgroundColor := TAlphaColors.Red;
        ItemText.Text := 'Item ' + IntToStr( i );
        ImageName     := '/Documents/admin.png';
      end;
    end;
  end;
  DPFUITableView1.RefreshNeeded;
end;

procedure TForm3.PaintRects( const UpdateRects: array of TRectF );
begin
  { }
end;

end.
