|
2 | 2 |
|
3 | 3 | interface |
4 | 4 |
|
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; |
7 | 9 |
|
8 | 10 | const |
9 | 11 | ProgramVersion: double = 2.2; |
10 | 12 | UpdateUrl = 'https://api.github.com/repos/JensBorrisholt/Delphi-JsonToDelphiClass/releases'; |
11 | 13 | ProgramUrl = 'https://github.com/JensBorrisholt/Delphi-JsonToDelphiClass'; |
12 | 14 |
|
13 | | -function InternalCheckForUpdate: TObject; |
14 | | -procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>); |
15 | | - |
16 | | -var |
17 | | - PointDsFormatSettings: TFormatSettings; |
| 15 | +procedure CheckForUpdate(AOnFinish: TProc<TRelease, string>); |
18 | 16 |
|
19 | 17 | implementation |
20 | 18 |
|
21 | 19 | uses |
22 | | - Math; |
| 20 | + System.Generics.Collections, |
23 | 21 |
|
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>); |
33 | 25 | 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 |
40 | 33 | 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; |
48 | 37 |
|
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); |
71 | 40 |
|
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; |
78 | 45 | end; |
79 | 46 |
|
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(); |
104 | 47 | try |
105 | 48 | // Execute AOnFinish in the context of the Main Thread |
106 | 49 | TThread.Synchronize(nil, |
107 | | - procedure |
| 50 | + procedure |
108 | 51 | begin |
109 | | - AOnFinish(LResult); |
| 52 | + AOnFinish(LResult, LErrorMessage); |
110 | 53 | end); |
111 | 54 | except |
112 | 55 | end; |
113 | 56 | end); |
| 57 | + |
114 | 58 | end; |
115 | 59 |
|
116 | 60 | initialization |
117 | 61 |
|
118 | | -PointDsFormatSettings := TFormatSettings.Create(); |
119 | | -PointDsFormatSettings.DecimalSeparator := '.'; |
120 | | - |
121 | 62 | end. |
0 commit comments