Skip to content

Commit 0cdc509

Browse files
Add downloading of AppImage updates
1 parent 595fe2a commit 0cdc509

File tree

7 files changed

+83
-44
lines changed

7 files changed

+83
-44
lines changed

MSURandomizer/App.axaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Versioning;
2+
using AppImageManager;
13
using Avalonia;
24
using Avalonia.Controls.ApplicationLifetimes;
35
using Avalonia.Markup.Xaml;
@@ -9,6 +11,9 @@ namespace MSURandomizer;
911

1012
public class App : Application
1113
{
14+
public const string AppId = "org.mattequalscoder.msurandomizer";
15+
public const string AppName = "MSU Randomizer";
16+
1217
public override void Initialize()
1318
{
1419
AvaloniaXamlLoader.Load(this);
@@ -25,4 +30,12 @@ public override void OnFrameworkInitializationCompleted()
2530

2631
base.OnFrameworkInitializationCompleted();
2732
}
33+
34+
[SupportedOSPlatform("linux")]
35+
internal static CreateDesktopFileResponse BuildLinuxDesktopFile()
36+
{
37+
return new DesktopFileBuilder(AppId, AppName)
38+
.AddUninstallAction(Directories.AppDataFolder)
39+
.Build();
40+
}
2841
}

MSURandomizer/MSURandomizer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<!--Condition
3535
below is needed to remove Avalonia.Diagnostics package from build output in Release
3636
configuration.-->
37-
<PackageReference Include="MattEqualsCoder.AppImageDesktopFileCreator" Version="0.3.6" />
37+
<PackageReference Include="MattEqualsCoder.AppImageManager" Version="0.1.2" />
3838
<PackageReference Include="MattEqualsCoder.AvaloniaControls" Version="1.9.1" />
3939
<PackageReference Include="MattEqualsCoder.GitHubReleaseChecker" Version="1.1.3" />
4040
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.9" />

MSURandomizer/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Runtime.Versioning;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using AppImageDesktopFileCreator;
98
using Avalonia.Threading;
109
using AvaloniaControls.Controls;
1110
using AvaloniaControls.Services;
@@ -92,14 +91,6 @@ public static void Main(string[] args)
9291
}
9392
}
9493

95-
[SupportedOSPlatform("linux")]
96-
internal static CreateDesktopFileResponse BuildLinuxDesktopFile()
97-
{
98-
return new DesktopFileBuilder("org.mattequalscoder.msurandomizer", "MSU Randomizer")
99-
.AddUninstallAction(Directories.AppDataFolder)
100-
.Build();
101-
}
102-
10394
// Avalonia configuration, don't remove; also used by visual designer.
10495
private static AppBuilder BuildAvaloniaApp()
10596
=> AppBuilder.Configure<App>()

MSURandomizer/Services/AppInitializationService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using AvaloniaControls.Controls;
88
using GitHubReleaseChecker;
9-
using Google.Protobuf;
109
using Microsoft.Extensions.Logging;
1110
using MSURandomizerLibrary.Models;
1211
using MSURandomizerLibrary.Services;
@@ -87,6 +86,7 @@ public void FinishInitialization()
8786

8887
public GitHubRelease? LatestFullRelease { get; private set; }
8988
public GitHubRelease? LatestPreRelease { get; private set; }
89+
public string? ReleaseDownloadUrl { get; private set; }
9090
public bool IsLoading { get; private set; }
9191

9292
public event EventHandler? InitializationComplete;
@@ -120,6 +120,11 @@ private async Task VerifyVersionNumber()
120120
{
121121
LatestPreRelease = latestRelease;
122122
}
123+
124+
if (OperatingSystem.IsLinux())
125+
{
126+
ReleaseDownloadUrl = LatestFullRelease?.Asset.FirstOrDefault(x => x.Url.ToLower().EndsWith(".appimage"))?.Url;
127+
}
123128

124129
IsLoading = false;
125130
InitializationComplete?.Invoke(this, EventArgs.Empty);

MSURandomizer/Services/MsuWindowService.cs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6-
using AppImageDesktopFileCreator;
6+
using AppImageManager;
77
using Avalonia.Platform.Storage;
88
using Avalonia.Threading;
99
using AvaloniaControls;
@@ -88,7 +88,7 @@ public MsuWindowViewModel InitializeModel()
8888
Model.DisplaySettingsWindowOnLoad = Model is { MsuWindowDisplayOptionsButton: true, HasMsuFolder: false };
8989

9090
if (OperatingSystem.IsLinux() && Model.MsuWindowDisplayOptionsButton && !userOptions.MsuUserOptions.SkipDesktopFile &&
91-
!DesktopFileCreator.DoesDesktopFileExist("org.mattequalscoder.msurandomizer"))
91+
!AppImage.DoesDesktopFileExist(App.AppId))
9292
{
9393
Model.DisplayDesktopPopupOnLoad = true;
9494
}
@@ -100,7 +100,7 @@ public void HandleUserDesktopResponse(bool addDesktopFile)
100100
{
101101
if (addDesktopFile && OperatingSystem.IsLinux())
102102
{
103-
Program.BuildLinuxDesktopFile();
103+
App.BuildLinuxDesktopFile();
104104
}
105105
else
106106
{
@@ -115,8 +115,11 @@ private void AppInitializationServiceOnInitializationComplete(object? sender, Ev
115115
{
116116
return;
117117
}
118+
119+
var downloadUrl = appInitializationService.ReleaseDownloadUrl;
120+
var hasDownloadUrl = !string.IsNullOrEmpty(downloadUrl);
118121

119-
Dispatcher.UIThread.Invoke(() =>
122+
Dispatcher.UIThread.Invoke(async () =>
120123
{
121124
var messageWindow = new MessageWindow(new MessageWindowRequest
122125
{
@@ -126,17 +129,45 @@ private void AppInitializationServiceOnInitializationComplete(object? sender, Ev
126129
LinkUrl = appInitializationService.LatestFullRelease.Url,
127130
Icon = MessageWindowIcon.Info,
128131
CheckBoxText = "Do not check for updates",
129-
Buttons = MessageWindowButtons.OK
132+
Buttons = hasDownloadUrl ? MessageWindowButtons.YesNo : MessageWindowButtons.OK,
133+
PrimaryButtonText = hasDownloadUrl ? "Download Update" : "OK",
134+
SecondaryButtonText = "Close"
130135
});
131-
messageWindow.Closed += (_, _) =>
136+
await messageWindow.ShowDialog(MessageWindow.GlobalParentWindow!);
137+
138+
if (messageWindow.DialogResult?.CheckedBox == true)
139+
{
140+
userOptions.MsuUserOptions.PromptOnUpdate = false;
141+
userOptions.Save();
142+
}
143+
144+
if (hasDownloadUrl && messageWindow.DialogResult?.PressedAcceptButton == true)
132145
{
133-
if (messageWindow.DialogResult?.CheckedBox == true)
146+
if (OperatingSystem.IsLinux())
134147
{
135-
userOptions.MsuUserOptions.PromptOnUpdate = false;
136-
userOptions.Save();
148+
var downloadResult = await AppImage.DownloadAsync(new DownloadAppImageRequest
149+
{
150+
Url = downloadUrl!
151+
});
152+
153+
if (downloadResult.Success)
154+
{
155+
MessageWindow.GlobalParentWindow!.Close();
156+
}
157+
else if (downloadResult.DownloadedSuccessfully)
158+
{
159+
await MessageWindow.ShowErrorDialog("AppImage was downloaded, but it could not be launched.");
160+
}
161+
else
162+
{
163+
await MessageWindow.ShowErrorDialog("Failed downloading AppImage");
164+
}
137165
}
138-
};
139-
messageWindow.ShowDialog();
166+
else
167+
{
168+
throw new InvalidOperationException("Download functionality is only available on Linux");
169+
}
170+
}
140171
});
141172

142173
}

MSURandomizer/Views/MsuWindow.axaml.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,26 @@ private void Control_OnLoaded(object? sender, RoutedEventArgs e)
8484

8585
if (!Design.IsDesignMode && (_model.DisplayDesktopPopupOnLoad || _model.DisplaySettingsWindowOnLoad))
8686
{
87-
ITaskService.Run(async () =>
88-
{
89-
await Task.Delay(TimeSpan.FromSeconds(.5));
90-
_ = Dispatcher.UIThread.Invoke(async () =>
91-
{
92-
if (_model.DisplayDesktopPopupOnLoad)
93-
{
94-
_model.DisplayDesktopPopupOnLoad = false;
95-
var response = await MessageWindow.ShowYesNoDialog(
96-
"Would you like to add the MSU Randomizer to your menu by creating a desktop file?",
97-
"MSU Randomizer", this);
98-
_service!.HandleUserDesktopResponse(response);
99-
}
100-
101-
if (_model.DisplaySettingsWindowOnLoad)
102-
{
103-
var settingsWindow = new SettingsWindow();
104-
await settingsWindow.ShowDialog(this);
105-
}
106-
});
107-
});
87+
_ = Dispatcher.UIThread.InvokeAsync(OpenStartingWindows);
88+
}
89+
}
90+
91+
private async Task OpenStartingWindows()
92+
{
93+
await Task.Delay(TimeSpan.FromSeconds(.5));
94+
if (_model.DisplayDesktopPopupOnLoad)
95+
{
96+
_model.DisplayDesktopPopupOnLoad = false;
97+
var response = await MessageWindow.ShowYesNoDialog(
98+
"Would you like to add the MSU Randomizer to your menu by creating a desktop file?",
99+
"MSU Randomizer", this);
100+
_service!.HandleUserDesktopResponse(response);
101+
}
102+
103+
if (_model.DisplaySettingsWindowOnLoad)
104+
{
105+
var settingsWindow = new SettingsWindow();
106+
await settingsWindow.ShowDialog(this);
108107
}
109108
}
110109

MSURandomizer/Views/SettingsWindow.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void CreateDesktopFileButton_OnClick(object? sender, RoutedEventArgs e)
8686
{
8787
if (OperatingSystem.IsLinux())
8888
{
89-
Program.BuildLinuxDesktopFile();
89+
App.BuildLinuxDesktopFile();
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)