SaveState d'une fiche

kuetedonald
Messages postés
49
Date d'inscription
vendredi 30 mai 2014
Statut
Membre
Dernière intervention
4 octobre 2016
- 26 nov. 2014 à 16:48
bonjour j'utilise le code ci-dessous pour effectuer le saveState d'une fiche de mon application mais lorsque je restore l'application l'application ne presente pas certain composants a l'etat ou j'ai laisser(ScrollBar,Timer......) . ces comme ci ca n'enregistre pas tout les proprietes du composant avant de faire le SaveState
unit SaveStateHelper;

interface

uses
FMX.Forms;

type

TSaveStateHelper = class helper for TCustomForm
public const
DATA_FIELD = 'data';
procedure SaveFormState;
procedure LoadFormState;
end;

implementation

uses System.JSON, FMX.Types, System.Classes, System.SysUtils;

{ TSaveStateHelper }

procedure TSaveStateHelper.LoadFormState;
var
R: TBinaryReader;
FormJSONObject: TJSONObject;
I: Integer;
FMXObj: TFmxObject;
FMXJObj: TJSONObject;
begin
try
if not(SaveState.Stream.Size > 0) then
exit;
// Recover previously typed values in all control.
R := TBinaryReader.Create(SaveState.Stream);
try
FormJSONObject := TJSONObject.ParseJSONValue(R.ReadString) as TJSONObject;
try
for I := 0 to Self.ComponentCount - 1 do
begin
if not(Self.Components[I] is TFmxObject) then
continue;
FMXObj := Self.Components[I] as TFmxObject;
FMXJObj := FormJSONObject.Values[FMXObj.Name] as TJSONObject;
case FMXObj.Data.Kind of
tkUnknown:
;
tkInteger:
FMXObj.Data :=
(FMXJObj.GetValue(DATA_FIELD) as TJSONNumber).AsInt;
tkChar:
;
tkEnumeration:
if FMXJObj.GetValue(DATA_FIELD) is TJSONTrue then
FMXObj.Data := true
else
FMXObj.Data := false;
tkFloat:
FMXObj.Data := (FMXJObj.GetValue(DATA_FIELD) as TJSONNumber).AsDouble;
tkString, tkUString, tkLString, tkWString:
FMXObj.Data := (FMXJObj.GetValue(DATA_FIELD) as TJSONString).Value;
tkSet:
;
tkClass:
;
tkMethod:
;
tkWChar:
;
tkVariant:
;
tkArray:
;
tkRecord:
;
tkInterface:
;
tkInt64:
FMXObj.Data := (FMXJObj.GetValue(DATA_FIELD) as TJSONNumber).AsInt64;
tkDynArray:
;
tkClassRef:
;
tkPointer:
;
tkProcedure:
;
end;
end;
finally
FormJSONObject.Free;
end;
finally
R.Free;
end;
except
on e: Exception do
Log.d('SaveStateHelper', Self, e.Message);
end;

end;

procedure TSaveStateHelper.SaveFormState;
var
FormJSONObject: TJSONObject;
I: Integer;
FMXObj: TFmxObject;
FMXJObj: TJSONObject;
W: TBinaryWriter;
begin
try
FormJSONObject := TJSONObject.Create;
try
for I := 0 to Self.ComponentCount - 1 do
begin
if not(Self.Components[I] is TFmxObject) then
continue;
FMXObj := Self.Components[I] as TFmxObject;
FMXJObj := TJSONObject.Create;
case FMXObj.Data.Kind of
tkUnknown:
;
tkInteger:
FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsInteger));
tkChar:
;
tkEnumeration:
if FMXObj.Data.AsBoolean then
FMXJObj.AddPair(DATA_FIELD, TJSONTrue.Create)
else
FMXJObj.AddPair(DATA_FIELD, TJSONFalse.Create);
tkFloat:
FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsExtended));
tkString, tkUString, tkLString, tkWString:
FMXJObj.AddPair(DATA_FIELD, FMXObj.Data.AsString);
tkSet:
;
tkClass:
;
tkMethod:
;
tkWChar:
;
tkVariant:
;
tkArray:
;
tkRecord:
;
tkInterface:
;
tkInt64:
FMXJObj.AddPair(DATA_FIELD, TJSONNumber.Create(FMXObj.Data.AsInt64));
tkDynArray:
;
tkClassRef:
;
tkPointer:
;
tkProcedure:
;
end;
FormJSONObject.AddPair(FMXObj.Name, FMXJObj)
end;
SaveState.Stream.Clear;
W := TBinaryWriter.Create(SaveState.Stream);
try
W.Write(FormJSONObject.ToJSON);
finally
W.Free;
end;
finally
FormJSONObject.Free;
end;
except
on e: Exception do
Log.d('SaveStateHelper', Self, e.Message);
end;
end;

end.

Svp aide moi.merci d'avance.