Skip to content

Commit 817812c

Browse files
Merge branch 'Development'
2 parents 1bad8ff + 1bbd436 commit 817812c

23 files changed

+1160
-838
lines changed

Components/uGitHub.pas

Lines changed: 0 additions & 594 deletions
This file was deleted.

Components/uUpdate.pas

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,61 @@
22

33
interface
44

5-
uses REST.Client, uGitHub, REST.JSON, JSON,
6-
IPPeerClient, SysUtils, System.Threading, Classes, Pkg.JSON.Mapper;
5+
uses
6+
System.JSON, System.SysUtils, System.Threading, System.Classes, IPPeerClient,
7+
8+
DTO.GitHUB.Release;
79

810
const
911
ProgramVersion: double = 2.2;
1012
UpdateUrl = 'https://api.github.com/repos/JensBorrisholt/Delphi-JsonToDelphiClass/releases';
1113
ProgramUrl = 'https://github.com/JensBorrisholt/Delphi-JsonToDelphiClass';
1214

13-
function InternalCheckForUpdate: TObject;
14-
procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);
15-
16-
var
17-
PointDsFormatSettings: TFormatSettings;
15+
procedure CheckForUpdate(AOnFinish: TProc<TRelease, string>);
1816

1917
implementation
2018

2119
uses
22-
Math;
20+
System.Generics.Collections,
2321

24-
function InternalCheckForUpdate: TObject;
25-
var
26-
LRestClient: TRESTClient;
27-
LRestRequest: TRESTRequest;
28-
LRestResponse: TRESTResponse;
29-
LRelease, LResult: TObject;
30-
LJsonArray: TJsonArray;
31-
LJsonValue: TJsonValue;
32-
LTag: double;
22+
Pkg.JSON.SerializableObject;
23+
24+
procedure CheckForUpdate(AOnFinish: TProc<TRelease, string>);
3325
begin
34-
LResult := nil;
35-
try
36-
LRestClient := TRESTClient.Create('');
37-
try
38-
LRestClient.BaseURL := UpdateUrl;
39-
LRestResponse := TRESTResponse.Create(nil);
26+
TTask.Run(
27+
procedure
28+
var
29+
LResult: TRelease;
30+
LErrorMessage: string;
31+
Releases: TObjectList<TRelease>;
32+
begin
4033
try
41-
LRestRequest := TRESTRequest.Create(nil);
42-
try
43-
LRestRequest.Client := LRestClient;
44-
LRestRequest.Response := LRestResponse;
45-
LRestRequest.Timeout := 10000;
46-
47-
LRestRequest.Execute;
34+
Releases := TUGitHubSerializableObject.RestRequest<TReleasesDTO>(UpdateUrl).Releases;
35+
if Releases.Count >= 0 then
36+
LResult := Releases.Last;
4837

49-
if LRestResponse.StatusCode = 200 then
50-
begin
51-
LJsonArray := TJSONObject.ParseJSONValue(LRestResponse.Content) as TJsonArray;
52-
try
53-
for LJsonValue in LJsonArray do
54-
begin
55-
LRelease := TReleaseClass.FromJsonString(LJsonValue.ToJSON);
56-
LTag := StrToFloat((LRelease as TReleaseClass).tag_name, PointDsFormatSettings);
57-
if Math.CompareValue(LTag, ProgramVersion) = 1 then
58-
begin
59-
LResult := LRelease;
60-
break;
61-
end
62-
else
63-
LRelease.Free;
64-
end;
65-
finally
66-
LJsonArray.Free;
67-
end;
68-
end
69-
else
70-
LResult := TErrorClass.FromJsonString(LRestResponse.Content);
38+
if JsonToFloat(LResult.Tag_Name) <= ProgramVersion then
39+
FreeAndNil(LResult);
7140

72-
finally
73-
LRestRequest.Free;
74-
end;
75-
76-
finally
77-
LRestResponse.Free;
41+
LErrorMessage := '';
42+
except
43+
on e: Exception do
44+
LErrorMessage := e.message;
7845
end;
7946

80-
finally
81-
LRestClient.Free;
82-
end;
83-
84-
except
85-
on e: Exception do
86-
begin
87-
LResult := TErrorClass.Create;
88-
(LResult as TErrorClass).message := e.message;
89-
end;
90-
end;
91-
92-
result := LResult;
93-
end;
94-
95-
procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);
96-
begin
97-
TTask.Run(
98-
procedure
99-
var
100-
LResult: TObject;
101-
begin
102-
// Asynchronously check for update
103-
LResult := InternalCheckForUpdate();
10447
try
10548
// Execute AOnFinish in the context of the Main Thread
10649
TThread.Synchronize(nil,
107-
procedure
50+
procedure
10851
begin
109-
AOnFinish(LResult);
52+
AOnFinish(LResult, LErrorMessage);
11053
end);
11154
except
11255
end;
11356
end);
57+
11458
end;
11559

11660
initialization
11761

118-
PointDsFormatSettings := TFormatSettings.Create();
119-
PointDsFormatSettings.DecimalSeparator := '.';
120-
12162
end.

DTO/GitHUB/DTO.GitHUB.Release.pas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
unit DTO.GitHUB.Release;
2+
3+
interface
4+
5+
uses
6+
DTO.GitHUB.ReleaseDTO, System.Generics.Collections, Pkg.Json.DTO;
7+
8+
type
9+
TRelease = TItemsDTO;
10+
11+
TReleasesDTO = class(TReleaseDTO)
12+
strict protected
13+
property Items;
14+
published
15+
property Releases: TObjectList<TRelease> read GetItems;
16+
end;
17+
18+
implementation
19+
20+
21+
end.

0 commit comments

Comments
 (0)