Skip to content

Commit

Permalink
added support for multiple downloads, updated to 0.26.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfds committed Nov 17, 2024
1 parent 3b54245 commit 0675b4e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.25.2</Version>
<Version>0.26.0</Version>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
24 changes: 17 additions & 7 deletions src/Avalonia.Desktop/Controls/DownloadsControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<helpers:ImagePathToBitmapConverter x:Key="ImagePathToBitmapConverter"/>
</UserControl.Resources>

<Grid RowDefinitions="auto,*,auto" ColumnDefinitions="2*,*">
<Grid RowDefinitions="auto,*,auto,auto" ColumnDefinitions="2*,*">

<Grid ColumnDefinitions="auto,*,auto"
Grid.Row="0" Grid.Column="0"
Expand Down Expand Up @@ -48,7 +48,7 @@
Padding="2"
ItemsSource="{Binding DownloadableList}"
SelectedItem="{Binding SelectedDownloadable}"
SelectionMode="Single"
SelectionChanged="DataGrid_SelectionChanged"
GridLinesVisibility="Horizontal"
IsReadOnly="True"
CornerRadius="4"
Expand Down Expand Up @@ -84,29 +84,39 @@

</md:MarkdownScrollViewer>

<Grid ColumnDefinitions="auto, *, auto"
Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="1"
<TextBlock Grid.Row="2" Margin="0,3,0,0" Text="Hold Ctrl or Shift to select multiple files"/>

<!--Buttons and progress bar-->
<Grid ColumnDefinitions="auto,*,auto"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="1"
Margin="0,12">

<Button Command="{Binding DownloadAddonCommand}"
<Button Name="DownloadButton"
Command="{Binding DownloadAddonCommand}"
Grid.Column="0"
Margin="0,5,5,5"
Padding="5"
Content="{Binding DownloadButtonText, FallbackValue= Download}"
IsVisible="{Binding !IsInProgress}"/>

<Button Command="{Binding CancelDownloadCommand}"
<Button Name="CancelButton"
Command="{Binding CancelDownloadCommand}"
Grid.Column="0"
Margin="0,5,5,5"
Padding="5"
Content="Cancel"
IsVisible="{Binding IsInProgress}"/>

<ProgressBar Value="{Binding ProgressBarValue}"
Height="10"
Height="{Binding #DownloadButton.Bounds.Height}"
HorizontalAlignment="Stretch"
Grid.Column="1"/>

<TextBlock Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding ProgressMessage}"/>

<Button Grid.Column="2"
IsVisible="{Binding !IsInProgress}"
HorizontalAlignment="Right"
Expand Down
15 changes: 15 additions & 0 deletions src/Avalonia.Desktop/Controls/DownloadsControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia.Controls;
using Avalonia.Desktop.ViewModels;
using Common.Entities;
using Common.Helpers;
using CommunityToolkit.Mvvm.Input;

Expand Down Expand Up @@ -40,4 +41,18 @@ private void AddContextMenuButtons(DataGrid dataGrid)

_ = dataGrid.ContextMenu.Items.Add(downloadButton);
}

private void DataGrid_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
var viewModel = DataContext as DownloadsViewModel;

List<DownloadableAddonEntity> list = [];

foreach (var a in DownloadableList.SelectedItems)
{
list.Add((DownloadableAddonEntity)a);
}

viewModel!.SelectedDownloadableList = list;
}
}
50 changes: 40 additions & 10 deletions src/Avalonia.Desktop/ViewModels/DownloadsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public ImmutableList<IDownloadableAddon> DownloadableList
[NotifyCanExecuteChangedFor(nameof(DownloadAddonCommand))]
private DownloadableAddonEntity? _selectedDownloadable;

/// <summary>
/// Currently selected downloadable campaign, map or mod
/// </summary>
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DownloadButtonText))]
private List<DownloadableAddonEntity>? _selectedDownloadableList;

[ObservableProperty]
private string _progressMessage = string.Empty;

/// <summary>
/// Description of the selected addon
/// </summary>
Expand All @@ -109,18 +119,18 @@ public string DownloadButtonText
{
get
{
if (SelectedDownloadable is null)
if (SelectedDownloadableList is null or [])
{
return "Download";
}
else
{
return $"Download ({SelectedDownloadable.FileSize.ToSizeString()})";
return $"Download ({SelectedDownloadableList.Sum(x => x.FileSize).ToSizeString()})";
}
}
}

public List<FilterItemEnum> FilterItems => Enum.GetValues(typeof(FilterItemEnum)).Cast<FilterItemEnum>().ToList();
public List<FilterItemEnum> FilterItems => [.. Enum.GetValues<FilterItemEnum>()];

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DownloadableList))]
Expand Down Expand Up @@ -206,21 +216,38 @@ private async Task DownloadAddon()
{
try
{
if (SelectedDownloadableList is null or [])
{
return;
}

List<DownloadableAddonEntity> filesToDownload = [.. SelectedDownloadableList];

IsInProgress = true;

_cancellationTokenSource = new();

if (SelectedDownloadable is null)
_downloadableAddonsProvider.Progress.ProgressChanged += OnProgressChanged;

byte downloadCount = 1;

foreach (var item in filesToDownload)
{
return;
}
if (_cancellationTokenSource.IsCancellationRequested)
{
ThrowHelper.ThrowOperationCanceledException();
}

_downloadableAddonsProvider.Progress.ProgressChanged += OnProgressChanged;
ProgressMessage = $"Downloading {item.Title}. File {downloadCount} of {filesToDownload.Count}.";

await _downloadableAddonsProvider.DownloadAddonAsync(SelectedDownloadable, _cancellationTokenSource.Token).ConfigureAwait(true);
await _downloadableAddonsProvider.DownloadAddonAsync(item, _cancellationTokenSource.Token).ConfigureAwait(true);

_downloadableAddonsProvider.Progress.ProgressChanged -= OnProgressChanged;
OnProgressChanged(null, 0);
downloadCount++;
}
}
catch (OperationCanceledException)
{
//nothing to do
}
catch (Exception ex)
{
Expand All @@ -236,7 +263,10 @@ private async Task DownloadAddon()
}
finally
{
_downloadableAddonsProvider.Progress.ProgressChanged -= OnProgressChanged;
OnProgressChanged(null, 0);
IsInProgress = false;
ProgressMessage = string.Empty;
}
}
private bool DownloadSelectedAddonCanExecute => SelectedDownloadable is not null;
Expand Down
14 changes: 7 additions & 7 deletions src/Common.Common/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public static class Extensions
/// <param name="size">File size</param>
public static string ToSizeString(this long size)
{
if (size < 1024)
if (size < 1000)
{
return $"{size} B";
return size.ToString("0") + "B";
}
else if (size < 1024 * 1024)
else if (size < 1000 * 1000)
{
return $"{size / 1024} KB";
return (size / 1000).ToString("0") + "KB";
}
else if (size < 1024 * 1024 * 1024)
else if (size < 1000 * 1000 * 1000)
{
return $"{size / 1024 / 1024} MB";
return (size * 1e-6).ToString("0") + "MB";
}

return $"{size / 1024 / 1024 / 1024} GB";
return (size * 1e-9).ToString("0.##") + "GB";
}

/// <summary>
Expand Down

0 comments on commit 0675b4e

Please sign in to comment.