Added download canceling back into downloads

This commit is contained in:
Pat Hartl 2024-01-25 00:46:10 -06:00
parent 69dfa2627c
commit 71ccf03fb0
8 changed files with 85 additions and 5 deletions

View file

@ -35,6 +35,9 @@ namespace LANCommander.PlaynitePlugin
public delegate void OnInstallFailHandler(Game game);
public event OnInstallFailHandler OnInstallFail;
public delegate void OnInstallCancelledHandler(Game game);
public event OnInstallCancelledHandler OnInstallCancelled;
public DownloadQueueController(LANCommanderLibraryPlugin plugin)
{
Plugin = plugin;
@ -98,7 +101,17 @@ namespace LANCommander.PlaynitePlugin
private void ChangeCurrentItemStatus(DownloadQueueItemStatus status)
{
Plugin.PlayniteApi.MainView.UIDispatcher.Invoke(() => {
DownloadQueue.CurrentItem.Status = status;
switch (status)
{
case DownloadQueueItemStatus.Downloading:
DownloadQueue.CurrentItem.ProgressIndeterminate = false;
DownloadQueue.CurrentItem.Status = status;
break;
default:
DownloadQueue.CurrentItem.ProgressIndeterminate = true;
DownloadQueue.CurrentItem.Status = status;
break;
}
});
}
@ -187,6 +200,15 @@ namespace LANCommander.PlaynitePlugin
Task.Run(() => Install());
}
public void CancelInstall()
{
if (DownloadQueue.CurrentItem.Status == DownloadQueueItemStatus.Downloading)
{
ChangeCurrentItemStatus(DownloadQueueItemStatus.Canceled);
Plugin.LANCommanderClient.Games.CancelInstall();
}
}
private void Install()
{
if (DownloadQueue.CurrentItem == null)
@ -222,6 +244,15 @@ namespace LANCommander.PlaynitePlugin
{
installDirectory = Plugin.LANCommanderClient.Games.Install(game.Id);
}
catch (InstallCanceledException ex)
{
OnInstallCancelled?.Invoke(DownloadQueue.CurrentItem.Game);
Remove(DownloadQueue.CurrentItem);
Stopwatch.Stop();
return;
}
catch (InstallException ex)
{
Plugin.PlayniteApi.Notifications.Add($"InstallFail-{DownloadQueue.CurrentItem.Game.Id}", ex.Message, NotificationType.Error);

View file

@ -56,6 +56,7 @@ namespace LANCommander.PlaynitePlugin
Plugin.DownloadQueue.Add(Game);
Plugin.DownloadQueue.OnInstallComplete += MarkInstalled;
Plugin.DownloadQueue.OnInstallFail += OnInstallFail;
Plugin.DownloadQueue.OnInstallCancelled += OnInstallCancelled;
}
private void OnInstallFail(Playnite.SDK.Models.Game game)
@ -66,6 +67,16 @@ namespace LANCommander.PlaynitePlugin
Plugin.PlayniteApi.Database.Games.Update(game);
}
private void OnInstallCancelled(Playnite.SDK.Models.Game game)
{
Plugin.LANCommanderClient.Games.CancelInstall();
game.IsInstalling = false;
game.IsInstalled = false;
Plugin.PlayniteApi.Database.Games.Update(game);
}
public void MarkInstalled(Playnite.SDK.Models.Game game, string installDirectory)
{
if (game.Id == Game.Id)

View file

@ -10,7 +10,8 @@ namespace LANCommander.PlaynitePlugin.Models
Downloading,
InstallingRedistributables,
RunningScripts,
DownloadingSaves
DownloadingSaves,
Canceled
}
public class DownloadQueueItem : ObservableObject
@ -72,6 +73,8 @@ namespace LANCommander.PlaynitePlugin.Models
return "Running Scripts";
case DownloadQueueItemStatus.DownloadingSaves:
return "Downloading Saves";
case DownloadQueueItemStatus.Canceled:
return "Canceling";
case DownloadQueueItemStatus.Idle:
default:
return "Idle";

View file

@ -31,7 +31,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="32" />
<ColumnDefinition Width="48" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding DownloadQueue.CurrentItem.CoverPath}" Height="100" />
<Grid Grid.Column="1">
@ -104,6 +104,12 @@
</StackPanel>
</Grid>
</Grid>
<TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right">
<Hyperlink Click="CancelDownload">
<TextBlock Text="&#xec4f;" FontFamily="{DynamicResource FontIcoFont}" FontSize="16" ToolTip="Cancel Download" />
</Hyperlink>
</TextBlock>
</Grid>
</ItemsControl>
@ -133,7 +139,7 @@
<TextBlock Grid.Column="0" Text="{Binding Title}" VerticalAlignment="Center" FontSize="20" Style="{StaticResource BaseTextBlockStyle}" />
</Grid>
<TextBlock Grid.Column="2" VerticalAlignment="Center">
<TextBlock Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right">
<Hyperlink Click="RemoveItem">
<TextBlock Text="&#xec4f;" FontFamily="{DynamicResource FontIcoFont}" FontSize="16" ToolTip="Remove from Queue" />
</Hyperlink>

View file

@ -30,6 +30,11 @@ namespace LANCommander.PlaynitePlugin.Views
InitializeComponent();
}
private void CancelDownload(object sender, EventArgs e)
{
Plugin.DownloadQueue.CancelInstall();
}
private void BackToLibrary(object sender, RoutedEventArgs e)
{
Plugin.PlayniteApi.MainView.SwitchToLibraryView();

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LANCommander.SDK.Exceptions
{
public class InstallCanceledException : Exception
{
public InstallCanceledException(string message) : base(message) { }
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.SDK.Exceptions
{
public class InstallException : Exception
{
public InstallException(string message) : base(message) { }
}
}

View file

@ -168,7 +168,7 @@ namespace LANCommander.SDK
if (!result.Success && !result.Canceled)
throw new InstallException("Could not extract the installer. Retry the install or check your connection");
else if (result.Canceled)
return "";
throw new InstallCanceledException("Game install was canceled");
game.InstallDirectory = result.Directory;