Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
fix package update
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Mar 22, 2024
1 parent b7d6a82 commit 1728fdb
Showing 1 changed file with 68 additions and 18 deletions.
86 changes: 68 additions & 18 deletions src/OneWare.Essentials/Models/PackageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@

namespace OneWare.Essentials.Models;

public abstract class PackageModel(
Package package,
string packageType,
string extractionFolder,
IHttpService httpService,
ILogger logger,
IApplicationStateService applicationStateService)
: ObservableObject
public abstract class PackageModel : ObservableObject
{
private PackageVersion? _installedVersion;
public PackageVersion? InstalledVersion
{
get => _installedVersion;
set => SetProperty(ref _installedVersion, value);
set
{
SetProperty(ref _installedVersion, value);
UpdateStatus();
}
}

private string? _warningText;
Expand All @@ -33,25 +30,55 @@ public string? WarningText
public PackageStatus Status
{
get => _status;
set => SetProperty(ref _status, value);
protected set => SetProperty(ref _status, value);
}

private float _progress;
private Package _package;
private readonly IHttpService _httpService;
private readonly ILogger _logger;
private readonly IApplicationStateService _applicationStateService;

protected PackageModel(Package package,
string packageType,
string extractionFolder,
IHttpService httpService,
ILogger logger,
IApplicationStateService applicationStateService)
{
_package = package;
_httpService = httpService;
_logger = logger;
_applicationStateService = applicationStateService;
ExtractionFolder = extractionFolder;
PackageType = packageType;

UpdateStatus();
}

public float Progress
{
get => _progress;
private set => SetProperty(ref _progress, value);
}

protected string ExtractionFolder { get; } = extractionFolder;
protected string ExtractionFolder { get; }

protected string PackageType { get; } = packageType;
protected string PackageType { get; }

public event EventHandler? Installed;

public event EventHandler? Removed;

public Package Package { get; set; } = package;
public Package Package
{
get => _package;
set
{
SetProperty(ref _package, value);
UpdateStatus();
}
}

public async Task<bool> UpdateAsync(PackageVersion version)
{
Expand All @@ -76,7 +103,7 @@ public async Task<bool> DownloadAsync(PackageVersion version)

if (target is {Url: not null})
{
var state = applicationStateService.AddState($"Downloading {Package.Id}...", AppState.Loading);
var state = _applicationStateService.AddState($"Downloading {Package.Id}...", AppState.Loading);

var progress = new Progress<float>(x =>
{
Expand All @@ -85,15 +112,15 @@ public async Task<bool> DownloadAsync(PackageVersion version)
});

//Download
if (!await httpService.DownloadAndExtractArchiveAsync(target.Url, ExtractionFolder, progress))
if (!await _httpService.DownloadAndExtractArchiveAsync(target.Url, ExtractionFolder, progress))
{
Status = PackageStatus.Available;
return false;
}

PlatformHelper.ChmodFolder(ExtractionFolder);

applicationStateService.RemoveState(state);
_applicationStateService.RemoveState(state);

Install(target);

Expand All @@ -108,7 +135,7 @@ public async Task<bool> DownloadAsync(PackageVersion version)
}
catch (Exception e)
{
logger.Error(e.Message, e);
_logger.Error(e.Message, e);
Status = PackageStatus.Available;
return false;
}
Expand Down Expand Up @@ -153,7 +180,7 @@ public async Task<bool> RemoveAsync()
}
catch (Exception e)
{
logger.Error(e.Message, e);
_logger.Error(e.Message, e);
return false;
}
}
Expand All @@ -166,4 +193,27 @@ public async Task<bool> RemoveAsync()

return true;
}

private void UpdateStatus()
{
var lV = Version.TryParse(Package.Versions?.LastOrDefault()?.Version, out var lastVersion);
var iV = Version.TryParse(InstalledVersion?.Version ?? "", out var installedVersion);

if (lV && iV && lastVersion > installedVersion)
{
Status = PackageStatus.UpdateAvailable;
}
else if (iV)
{
Status = PackageStatus.Installed;
}
else if (!iV && lV)
{
Status = PackageStatus.Available;
}
else
{
Status = PackageStatus.Unavailable;
}
}
}

0 comments on commit 1728fdb

Please sign in to comment.