Skip to content

Commit

Permalink
UpdateManger refactor, misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Oct 28, 2024
1 parent c08eb3b commit c72b275
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 77 deletions.
6 changes: 6 additions & 0 deletions src/PicView.Avalonia/StartUp/StartUpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using PicView.Avalonia.Navigation;
using PicView.Avalonia.SettingsManagement;
using PicView.Avalonia.UI;
using PicView.Avalonia.Update;
using PicView.Avalonia.ViewModels;
using PicView.Avalonia.Views;
using PicView.Avalonia.WindowBehavior;
Expand Down Expand Up @@ -34,6 +35,11 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS
{
HandleMultipleInstances(args);
}
else if (args.Length > 1 && args[1].Equals("update", StringComparison.InvariantCultureIgnoreCase))
{
Task.Run(async () => await UpdateManager.UpdateCurrentVersion(vm));
return;
}
}

vm.IsLoading = true;
Expand Down
152 changes: 76 additions & 76 deletions src/PicView.Avalonia/Update/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,43 @@ public partial class UpdateSourceGenerationContext : JsonSerializerContext;

public static class UpdateManager
{
public static void CheckForUpdates()
{
}

public static async Task UpdateCurrentVersion(MainViewModel vm)
{
var currentDirectory = new DirectoryInfo(Environment.ProcessPath);
var currentVersion = VersionHelper.GetCurrentVersionAsVersion();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
bool isAdmin;
try
{
isAdmin = currentDirectory.GetAccessControl().AreAccessRulesProtected;
}
catch (Exception)
{
isAdmin = false;
}

if (isAdmin)
{
// Restart the application as admin
var process = new Process
{
StartInfo = new ProcessStartInfo
{
Verb = "runas",
UseShellExecute = true,
FileName = "PicView.exe",
Arguments = "update",
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory
}
};
process.Start();
Environment.Exit(0);
}
}

// ReSharper disable once RedundantAssignment
var currentVersion = VersionHelper.GetAssemblyVersion();
var url = "https://picview.org/update.json";
var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + "PicView");
Directory.CreateDirectory(tempPath);
Expand Down Expand Up @@ -78,16 +107,6 @@ public static async Task UpdateCurrentVersion(MainViewModel vm)

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var isAdmin = false;
try
{
isAdmin = currentDirectory.GetAccessControl().AreAccessRulesProtected;
}
catch (Exception)
{
isAdmin = false;
}

var isInstalled = CheckIfIsInstalled();

var architecture = RuntimeInformation.ProcessArchitecture switch
Expand All @@ -105,70 +124,49 @@ public static async Task UpdateCurrentVersion(MainViewModel vm)
return;
}

if (isAdmin)
switch (architecture)
{
// Restart the application as admin
var process = new Process
{
StartInfo = new ProcessStartInfo
case InstalledArchitecture.Arm64Install:
var fileName = Path.GetFileName(updateInfo.X64Install);
var tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.Arm64Install, tempFileDownloadPath);
var process = new Process
{
Verb = "runas",
UseShellExecute = true,
FileName = "PicView.exe",
Arguments = $"update,{architecture},{currentVersion},{tempPath}",
WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory
}
};
process.Start();
Environment.Exit(0);
}
else
{
switch (architecture)
{
case InstalledArchitecture.Arm64Install:
var fileName = Path.GetFileName(updateInfo.X64Install);
var tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.Arm64Install, tempFileDownloadPath);
var process = new Process
StartInfo = new ProcessStartInfo
{
StartInfo = new ProcessStartInfo
{
Verb = "runas",
UseShellExecute = true,
FileName = tempFileDownloadPath
}
};
process.Start();
return;
case InstalledArchitecture.Arm64Portable:
fileName = Path.GetFileName(updateInfo.X64Portable);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.Arm64Portable, tempFileDownloadPath);
vm.PlatformService.LocateOnDisk(tempFileDownloadPath);
return;
case InstalledArchitecture.X64Install:
fileName = Path.GetFileName(updateInfo.X64Install);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.X64Install, tempFileDownloadPath);
process = new Process
Verb = "runas",
UseShellExecute = true,
FileName = tempFileDownloadPath
}
};
process.Start();
return;
case InstalledArchitecture.Arm64Portable:
fileName = Path.GetFileName(updateInfo.X64Portable);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.Arm64Portable, tempFileDownloadPath);
vm.PlatformService.LocateOnDisk(tempFileDownloadPath);
return;
case InstalledArchitecture.X64Install:
fileName = Path.GetFileName(updateInfo.X64Install);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.X64Install, tempFileDownloadPath);
process = new Process
{
StartInfo = new ProcessStartInfo
{
StartInfo = new ProcessStartInfo
{
Verb = "runas",
UseShellExecute = true,
FileName = tempFileDownloadPath
}
};
process.Start();
return;
case InstalledArchitecture.X64Portable:
fileName = Path.GetFileName(updateInfo.X64Portable);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.X64Portable, tempFileDownloadPath);
vm.PlatformService.LocateOnDisk(tempFileDownloadPath);
return;
}
UseShellExecute = true,
FileName = tempFileDownloadPath
}
};
process.Start();
return;
case InstalledArchitecture.X64Portable:
fileName = Path.GetFileName(updateInfo.X64Portable);
tempFileDownloadPath = Path.Combine(tempPath, fileName);
await StartFileDownloader(vm, updateInfo.X64Portable, tempFileDownloadPath);
vm.PlatformService.LocateOnDisk(tempFileDownloadPath);
return;
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Expand Down Expand Up @@ -250,12 +248,14 @@ private static async Task StartFileDownloader(MainViewModel vm, string downloadU
}
}

private static void ProgressChanged(MainViewModel vm, long? totalfilesize, long? totalbytesdownloaded, double? progresspercentage)
private static void ProgressChanged(MainViewModel vm, long? totalfilesize, long? totalbytesdownloaded,
double? progresspercentage)
{
if (totalfilesize is null || totalbytesdownloaded is null || progresspercentage is null)
{
return;
}

vm.PlatformService.SetTaskbarProgress((ulong)totalbytesdownloaded, (ulong)totalfilesize);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PicView.Core/Config/VersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class VersionHelper
}
}

public static Version? GetCurrentVersionAsVersion()
public static Version? GetAssemblyVersion()
{
try
{
Expand Down

0 comments on commit c72b275

Please sign in to comment.