Main.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. unit Main;
  2. interface
  3. uses
  4. System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  5. FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  6. System.Generics.Collections, System.IOUtils,
  7. Quick.Config.Json, FMX.StdCtrls, FMX.Controls.Presentation,
  8. FMX.ScrollBox, FMX.Memo;
  9. type
  10. TMyPriority = (msLow, msMed, msHigh);
  11. TWinPos = record
  12. public
  13. PosX : Integer;
  14. PosY : Integer;
  15. end;
  16. TProcessType = record
  17. Id : Integer;
  18. Priority : TMyPriority;
  19. Redundant : Boolean;
  20. end;
  21. TWorker = class
  22. private
  23. fName : string;
  24. fActive : Boolean;
  25. published
  26. property Name : string read fName write fName;
  27. property Active : Boolean read fActive write fActive;
  28. end;
  29. TMyConfig = class(TAppConfigJson)
  30. private
  31. fTitle : string;
  32. fHidden : Boolean;
  33. fSessionName: string;
  34. fSizes : TArray<Integer>;
  35. fLastFilename : string;
  36. fWindowPos : TWinPos;
  37. fHistory : TArray<TProcessType>;
  38. fComplex : TProcessType;
  39. fModifyDate : TDateTime;
  40. fWorkList : TObjectList<TWorker>;
  41. published
  42. property Sizes : TArray<Integer> read fSizes write fSizes;
  43. property LastFilename : string read fLastFilename write fLastFilename;
  44. property WindowPos : TWinPos read fWindowPos write fWindowPos;
  45. property History : TArray<TProcessType> read fHistory write fHistory;
  46. property Complex : TProcessType read fComplex write fComplex;
  47. property ModifyDate : TDateTime read fModifyDate write fModifyDate;
  48. property Title : string read fTitle write fTitle;
  49. property SessionName : string read fSessionName write fSessionName;
  50. property WorkList : TObjectList<TWorker> read fWorkList write fWorkList;
  51. public
  52. constructor Create;
  53. destructor Destroy; override;
  54. procedure DefaultValues; override;
  55. end;
  56. TMainForm = class(TForm)
  57. meInfo: TMemo;
  58. Panel1: TPanel;
  59. btnLoadJson: TSpeedButton;
  60. btnSaveJson: TSpeedButton;
  61. procedure FormCreate(Sender: TObject);
  62. procedure SetConfig(cConfig: TMyConfig);
  63. function TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  64. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  65. procedure btnLoadJsonClick(Sender: TObject);
  66. procedure btnSaveJsonClick(Sender: TObject);
  67. end;
  68. var
  69. MainForm: TMainForm;
  70. ConfigTest : TMyConfig;
  71. ConfigJson : TMyConfig;
  72. implementation
  73. {$R *.fmx}
  74. procedure TMainForm.btnLoadJsonClick(Sender: TObject);
  75. begin
  76. meInfo.Lines.Add('Load ConfigReg');
  77. ConfigJson.Load;
  78. meInfo.Lines.Add(ConfigJson.ToJSON);
  79. if TestConfig(configtest,ConfigJson) then meInfo.Lines.Add('Test passed successfully!');
  80. end;
  81. procedure TMainForm.btnSaveJsonClick(Sender: TObject);
  82. begin
  83. SetConfig(ConfigJson);
  84. ConfigJson.Save;
  85. meInfo.Lines.Add('Saved Config in Registry at ' + DateTimeToStr(ConfigJson.LastSaved));
  86. end;
  87. procedure TMainForm.SetConfig(cConfig : TMyConfig);
  88. var
  89. winpos : TWinpos;
  90. protype : TProcessType;
  91. i : Integer;
  92. worker : TWorker;
  93. begin
  94. cConfig.LastFilename := 'library.txt';
  95. cConfig.Sizes := [23,11,554,12,34,29,77,30,48,59,773,221,98,3,22,983,122,231,433,12,31,987];
  96. winpos.PosX := 640;
  97. winpos.PosX := 480;
  98. cConfig.WindowPos := winpos;
  99. protype.Priority := msHigh;
  100. protype.Redundant := False;
  101. cConfig.Complex := protype;
  102. cConfig.Title := 'a fresh title';
  103. cConfig.SessionName := 'First Session';
  104. for I := 0 to 22 do
  105. begin
  106. worker := TWorker.Create;
  107. worker.Name := 'Process ' + i.ToString;
  108. worker.Active := Boolean(Random(1));
  109. cConfig.WorkList.Add(worker);
  110. end;
  111. for i := 0 to 15 do
  112. begin
  113. protype.Id := i;
  114. protype.Priority := msLow;
  115. protype.Redundant := True;
  116. cConfig.History := cConfig.History + [protype];
  117. end;
  118. cConfig.ModifyDate := Now();
  119. end;
  120. function TMainForm.TestConfig(cConfig1, cConfig2 : TMyConfig) : Boolean;
  121. var
  122. i : Integer;
  123. begin
  124. Result := False;
  125. try
  126. Assert(cConfig1.LastFilename = cConfig2.LastFilename);
  127. for i := Low(cConfig1.Sizes) to High(cConfig1.Sizes) do
  128. Assert(cConfig1.Sizes[i] = cConfig2.Sizes[i]);
  129. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  130. Assert(cConfig1.WindowPos.PosX = cConfig2.WindowPos.PosX);
  131. Assert(cConfig1.Complex.Priority = cConfig2.Complex.Priority);
  132. Assert(cConfig1.Complex.Redundant = cConfig2.Complex.Redundant);
  133. Assert(cConfig1.Title = cConfig2.Title);
  134. for i := 0 to cConfig1.WorkList.Count - 1 do
  135. begin
  136. Assert(cConfig1.WorkList[i].Name = cConfig2.WorkList[i].Name);
  137. Assert(cConfig1.WorkList[i].Active = cConfig2.WorkList[i].Active);
  138. end;
  139. for i := 0 to High(cConfig1.History) do
  140. begin
  141. Assert(cConfig1.History[i].Priority = cConfig2.History[i].Priority);
  142. Assert(cConfig1.History[i].Redundant = cConfig2.History[i].Redundant);
  143. end;
  144. Result := True;
  145. except
  146. ShowMessage('Configuration not has been saved previously or has a corruption problem');
  147. end;
  148. end;
  149. procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
  150. begin
  151. if Assigned(ConfigJson) then ConfigJson.Free;
  152. if Assigned(ConfigTest) then ConfigTest.Free;
  153. end;
  154. procedure TMainForm.FormCreate(Sender: TObject);
  155. begin
  156. ConfigJson := TMyConfig.Create;
  157. {$IFDEF NEXTGEN}
  158. ConfigJson.Provider.Filename := TPath.GetDocumentsPath + '/config.json';
  159. {$ELSE}
  160. ConfigJson.Provider.Filename := '.\config.json';
  161. {$ENDIF}
  162. //create config test to compare later
  163. ConfigTest := TMyConfig.Create;
  164. SetConfig(ConfigTest);
  165. end;
  166. { TMyConfig }
  167. constructor TMyConfig.Create;
  168. begin
  169. inherited Create;
  170. WorkList := TObjectList<TWorker>.Create(True);
  171. DefaultValues;
  172. end;
  173. procedure TMyConfig.DefaultValues;
  174. begin
  175. inherited;
  176. fTitle := 'Default value';
  177. end;
  178. destructor TMyConfig.Destroy;
  179. begin
  180. if Assigned(WorkList) then WorkList.Free;
  181. inherited;
  182. end;
  183. end.