Skip to content

Commit

Permalink
Merge branch 'Development'
Browse files Browse the repository at this point in the history
  • Loading branch information
JensBorrisholt committed Aug 2, 2020
2 parents 1bad8ff + 1bbd436 commit 817812c
Show file tree
Hide file tree
Showing 23 changed files with 1,160 additions and 838 deletions.
594 changes: 0 additions & 594 deletions Components/uGitHub.pas

This file was deleted.

115 changes: 28 additions & 87 deletions Components/uUpdate.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,61 @@

interface

uses REST.Client, uGitHub, REST.JSON, JSON,
IPPeerClient, SysUtils, System.Threading, Classes, Pkg.JSON.Mapper;
uses
System.JSON, System.SysUtils, System.Threading, System.Classes, IPPeerClient,

DTO.GitHUB.Release;

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

function InternalCheckForUpdate: TObject;
procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);

var
PointDsFormatSettings: TFormatSettings;
procedure CheckForUpdate(AOnFinish: TProc<TRelease, string>);

implementation

uses
Math;
System.Generics.Collections,

function InternalCheckForUpdate: TObject;
var
LRestClient: TRESTClient;
LRestRequest: TRESTRequest;
LRestResponse: TRESTResponse;
LRelease, LResult: TObject;
LJsonArray: TJsonArray;
LJsonValue: TJsonValue;
LTag: double;
Pkg.JSON.SerializableObject;

procedure CheckForUpdate(AOnFinish: TProc<TRelease, string>);
begin
LResult := nil;
try
LRestClient := TRESTClient.Create('');
try
LRestClient.BaseURL := UpdateUrl;
LRestResponse := TRESTResponse.Create(nil);
TTask.Run(
procedure
var
LResult: TRelease;
LErrorMessage: string;
Releases: TObjectList<TRelease>;
begin
try
LRestRequest := TRESTRequest.Create(nil);
try
LRestRequest.Client := LRestClient;
LRestRequest.Response := LRestResponse;
LRestRequest.Timeout := 10000;

LRestRequest.Execute;
Releases := TUGitHubSerializableObject.RestRequest<TReleasesDTO>(UpdateUrl).Releases;
if Releases.Count >= 0 then
LResult := Releases.Last;

if LRestResponse.StatusCode = 200 then
begin
LJsonArray := TJSONObject.ParseJSONValue(LRestResponse.Content) as TJsonArray;
try
for LJsonValue in LJsonArray do
begin
LRelease := TReleaseClass.FromJsonString(LJsonValue.ToJSON);
LTag := StrToFloat((LRelease as TReleaseClass).tag_name, PointDsFormatSettings);
if Math.CompareValue(LTag, ProgramVersion) = 1 then
begin
LResult := LRelease;
break;
end
else
LRelease.Free;
end;
finally
LJsonArray.Free;
end;
end
else
LResult := TErrorClass.FromJsonString(LRestResponse.Content);
if JsonToFloat(LResult.Tag_Name) <= ProgramVersion then
FreeAndNil(LResult);

finally
LRestRequest.Free;
end;

finally
LRestResponse.Free;
LErrorMessage := '';
except
on e: Exception do
LErrorMessage := e.message;
end;

finally
LRestClient.Free;
end;

except
on e: Exception do
begin
LResult := TErrorClass.Create;
(LResult as TErrorClass).message := e.message;
end;
end;

result := LResult;
end;

procedure NewCheckForUpdateTask(AOnFinish: TProc<TObject>);
begin
TTask.Run(
procedure
var
LResult: TObject;
begin
// Asynchronously check for update
LResult := InternalCheckForUpdate();
try
// Execute AOnFinish in the context of the Main Thread
TThread.Synchronize(nil,
procedure
procedure
begin
AOnFinish(LResult);
AOnFinish(LResult, LErrorMessage);
end);
except
end;
end);

end;

initialization

PointDsFormatSettings := TFormatSettings.Create();
PointDsFormatSettings.DecimalSeparator := '.';

end.
21 changes: 21 additions & 0 deletions DTO/GitHUB/DTO.GitHUB.Release.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
unit DTO.GitHUB.Release;

interface

uses
DTO.GitHUB.ReleaseDTO, System.Generics.Collections, Pkg.Json.DTO;

type
TRelease = TItemsDTO;

TReleasesDTO = class(TReleaseDTO)
strict protected
property Items;
published
property Releases: TObjectList<TRelease> read GetItems;
end;

implementation


end.
Loading

0 comments on commit 817812c

Please sign in to comment.