Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Formular Position speichern

  Alt 29. Jul 2015, 15:45
Das sind aber geringfügig mehr als die max. 7-10 Zeilen in einer Procedure wie du im deinem Post aus dem anderen Thread behauptest.

SCNR
Ich sehe hier aber auch in keinster Weise irgendwo Clean Code, sondern wie angekündigt einfach nur mal so schnell getippt für einen ungefähren Anhaltspunkt.

Clean Code wäre eher etwas in dieser Richtung:
Delphi-Quellcode:
unit Services;

interface

uses
  System.Classes;

type
  IFormSettingsService = interface
    [ '{37C1665C-8C12-4430-9820-83D82DB76366}' ]
    function GetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
    function SetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
  end;

implementation

end.
Delphi-Quellcode:
unit Services.Impl;

interface

uses
  System.Classes,
  Services;

type
  TIniFormSettingsService = class( TInterfacedObject, IFormSettingsService )
  private
    FFileName: string;
    FSection: string;
  public
    constructor Create( const AFileName, ASection: string );
    function GetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
    function SetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
  end;

implementation

uses
  System.IniFiles;

{ TIniFormSettingsService }

constructor TIniFormSettingsService.Create( const AFileName, ASection: string );
begin
  inherited Create;
  FFileName := AFileName;
  FSection := ASection;
end;

function TIniFormSettingsService.GetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
var
  LIniFile: TIniFile;
begin
  LIniFile := TIniFile.Create( FFileName );
  try
    if LIniFile.SectionExists( FSection ) and LIniFile.ValueExists( FSection, BlockName ) then
    begin
      LIniFile.ReadBinaryStream( FSection, BlockName, ABlockData );
      Result := True;
    end
    else
      Result := False;
  finally
    LIniFile.Free;
  end;
end;

function TIniFormSettingsService.SetBlock( const BlockName: string; const ABlockData: TStream ): Boolean;
var
  LIniFile: TIniFile;
begin
  LIniFile := TIniFile.Create( FFileName );
  try
    ABlockData.Seek( 0, soFromBeginning );
    LIniFile.WriteBinaryStream( FSection, BlockName, ABlockData );
    Result := True;
  finally
    LIniFile.Free;
  end;
end;

end.
Delphi-Quellcode:
unit Configuration;

interface

implementation

uses
  FMX.Platform,
  Services,
  Services.Impl;

initialization

TPlatformServices.Current.AddPlatformService( IFormSettingsService, TIniFormSettingsService.Create( '.\foo.ini', 'forms' ) ); // ja, ja, das ist ein Unding mit dem relativen Pfad :o)

end.
Delphi-Quellcode:
unit Form.BaseForm;

interface

uses
  Services,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TBaseForm = class( TForm )
  private
    FLoaded: Boolean;
    procedure InternalLoadSettings( Service: IFormSettingsService );
    procedure InternalStoreSettings( Service: IFormSettingsService );
  protected
    function GetBlockName: string; virtual;
    procedure DoClose( var Action: TCloseAction ); override;
    procedure DoShow; override;
    procedure DoLoadSettings( Settings: TBinaryReader ); virtual;
    procedure DoStoreSettings( Settings: TBinaryWriter ); virtual;
  public
    procedure LoadSettings;
    procedure StoreSettings;
  end;

var
  BaseForm: TBaseForm;

implementation

{$R *.dfm}

uses
  FMX.Platform;

{ TBaseForm }

procedure TBaseForm.DoClose( var Action: TCloseAction );
var
  LService: IFormSettingsService;
begin
  inherited;
end;

procedure TBaseForm.DoLoadSettings( Settings: TBinaryReader );
begin
end;

procedure TBaseForm.DoShow;
begin
  if not FLoaded then
    LoadSettings;
  FLoaded := True;
  inherited;
end;

procedure TBaseForm.DoStoreSettings( Settings: TBinaryWriter );
begin

end;

function TBaseForm.GetBlockName: string;
begin
  Result := Self.ClassName;
end;

procedure TBaseForm.InternalLoadSettings( Service: IFormSettingsService );
var
  LBlockData: TMemoryStream;
  LReader: TBinaryReader;
begin
  LBlockData := TMemoryStream.Create;
  try
    if Service.GetBlock( GetBlockName, LBlockData ) then
    begin
      LBlockData.Seek( 0, soFromBeginning );
      LReader := TBinaryReader.Create( LBlockData );
      try
        DoLoadSettings( LReader );
      finally
        LReader.Free;
      end;
    end;
  finally
    LBlockData.Free;
  end;
end;

procedure TBaseForm.InternalStoreSettings( Service: IFormSettingsService );
var
  LBlockData: TMemoryStream;
  LWriter: TBinaryWriter;
begin
  LBlockData := TMemoryStream.Create;
  try
    LWriter := TBinaryWriter.Create( LBlockData );
    try
      DoStoreSettings( LWriter );
    finally
      LWriter.Free;
    end;
    LBlockData.Seek( 0, soFromBeginning );
    Service.SetBlock( GetBlockName, LBlockData );
  finally
    LBlockData.Free;
  end;
end;

procedure TBaseForm.LoadSettings;
var
  LService: IFormSettingsService;
begin
  if TPlatformServices.Current.SupportsPlatformService( IFormSettingsService, LService ) then
    InternalLoadSettings( LService );
end;

procedure TBaseForm.StoreSettings;
var
  LService: IFormSettingsService;
begin
  if TPlatformServices.Current.SupportsPlatformService( IFormSettingsService, LService ) then
    InternalStoreSettings( LService );
end;

end.
Delphi-Quellcode:
unit Form.MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Form.BaseForm, Vcl.StdCtrls;

type
  TMainForm = class( TBaseForm )
    Edit1: TEdit;
  private
  protected
    procedure DoLoadSettings( Settings: TBinaryReader ); override;
    procedure DoStoreSettings( Settings: TBinaryWriter ); override;
  public

  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}
{ TMainForm }

procedure TMainForm.DoLoadSettings( Settings: TBinaryReader );
begin
  inherited;
  Top := Settings.ReadInteger;
  Left := Settings.ReadInteger;
  Height := Settings.ReadInteger;
  Width := Settings.ReadInteger;
  Edit1.Text := Settings.ReadString;
end;

procedure TMainForm.DoStoreSettings( Settings: TBinaryWriter );
begin
  inherited;
  Settings.Write( Top );
  Settings.Write( Left );
  Settings.Write( Height );
  Settings.Write( Width );
  Settings.Write( Edit1.Text );
end;

end.
Aber so schön Clean das auch ist, es löst nicht das eigentliche Problem!

Ein Anfang ist es aber schon mal
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)
  Mit Zitat antworten Zitat