Skip to content

Commit

Permalink
添加下载完成折叠块 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy authored May 10, 2024
1 parent 5496bdd commit 96a7549
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 36 deletions.
10 changes: 3 additions & 7 deletions src/RodelDownloader.Console/Program.Civitai.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,8 @@ private static async Task RunCivitaiDownloadAsync(bool ignoreConfig = false)

private static string AskCivitaiToken()
{
var optionalStr = GetString("Optional");
var token = AnsiConsole.Ask<string>(GetString("CivitaiToken"), optionalStr);
if (token == optionalStr)
{
token = string.Empty;
}
var token = AnsiConsole.Prompt(new TextPrompt<string>($"{GetString("CivitaiTokenTip")} [green]({GetString("Optional")})[/]: ")
.AllowEmpty());

return token;
}
Expand All @@ -129,7 +125,7 @@ private static string ConvertModelItemToString(ModelItem item)
{
var sb = new StringBuilder();
sb.AppendLine($"[bold]{item.Name}[/]");
sb.AppendLine($"[grey]{item.Description.Replace("[", "{").Replace("]", "}").Replace("\n", string.Empty)[..18]}[/]");
sb.AppendLine($"[grey]{item.Description.EscapeMarkup()[..18]}[/]");
return sb.ToString();
}
}
2 changes: 1 addition & 1 deletion src/RodelDownloader.Console/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<value>Select the file you want to download</value>
</data>
<data name="CivitaiToken" xml:space="preserve">
<value>Please enter the Civitai access token:</value>
<value>Please enter the Civitai access token</value>
</data>
<data name="DownloadCompleted" xml:space="preserve">
<value>All files are downloaded</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<value>选择要下载的文件</value>
</data>
<data name="CivitaiToken" xml:space="preserve">
<value>请输入 Civitai 访问令牌</value>
<value>请输入 Civitai 访问令牌</value>
</data>
<data name="DownloadCompleted" xml:space="preserve">
<value>所有文件下载完成</value>
Expand Down
97 changes: 87 additions & 10 deletions src/RodelDownloader.UI/Pages/DownloadPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,96 @@
Padding="16,0"
Style="{StaticResource PageScrollViewerStyle}"
Visibility="{x:Bind ViewModel.HasDownloadItems, Mode=OneWay}">
<ItemsRepeater Margin="0,8,0,20" ItemsSource="{x:Bind ViewModel.Items}">
<ItemsRepeater.Layout>
<StackLayout Spacing="4" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="viewmodels:DownloadItemViewModel">
<components:DownloadItemControl ViewModel="{x:Bind}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
<Grid Margin="0,8,0,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsRepeater ItemsSource="{x:Bind ViewModel.DownloadingItems}">
<ItemsRepeater.Layout>
<StackLayout Spacing="4" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="viewmodels:DownloadItemViewModel">
<components:DownloadItemControl ViewModel="{x:Bind}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>

<StackPanel
Grid.Row="1"
Margin="0,8,0,0"
Visibility="{x:Bind ViewModel.HasDownloadedItems, Mode=OneWay}">
<controls:CardPanel
x:Name="DownloadedButton"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
CheckedBorderBrush="Transparent"
CheckedPointerOverBorderBrush="Transparent"
CheckedPressedBorderBrush="Transparent"
IsChecked="False"
IsEnableCheck="True">
<Grid Margin="16,12">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Spacing="8">
<controls:FluentIcon
VerticalAlignment="Center"
FontSize="14"
Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
Symbol="ClockArrowDownload" />
<TextBlock
VerticalAlignment="Center"
Text="{ext:Locale Name=Downloaded}"
TextLineBounds="Tight" />
</StackPanel>

<controls:FluentIcon
Grid.Column="1"
VerticalAlignment="Center"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Symbol="ChevronDown"
Visibility="{x:Bind DownloadedButton.IsChecked, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}" />

<controls:FluentIcon
Grid.Column="1"
VerticalAlignment="Center"
FontSize="14"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Symbol="ChevronUp"
Visibility="{x:Bind DownloadedButton.IsChecked, Mode=OneWay}" />
</Grid>

</controls:CardPanel>

<Grid
Padding="12"
Background="{ThemeResource LayerFillColorDefaultBrush}"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1,0,1,1"
CornerRadius="0,0,4,4"
Visibility="{x:Bind DownloadedButton.IsChecked, Mode=OneWay}">
<ItemsRepeater ItemsSource="{x:Bind ViewModel.DownloadedItems}">
<ItemsRepeater.Layout>
<StackLayout Spacing="4" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate x:DataType="viewmodels:DownloadItemViewModel">
<components:DownloadItemControl ViewModel="{x:Bind}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</Grid>
</StackPanel>
</Grid>
</ScrollViewer>

<controls:EmptyHolder
Title="{ext:Locale Name=DownloadListEmptyTitle}"
Grid.RowSpan="2"
Description="{ext:Locale Name=DownloadListEmptyDescription}"
Emoji="&#x1F60E;"
Visibility="{x:Bind ViewModel.HasDownloadItems, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}" />
Expand Down
3 changes: 3 additions & 0 deletions src/RodelDownloader.UI/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
<data name="DeselectAll" xml:space="preserve">
<value>Deselect all</value>
</data>
<data name="Downloaded" xml:space="preserve">
<value>Downloaded</value>
</data>
<data name="DownloadListEmptyDescription" xml:space="preserve">
<value>Which model would you like to download this time?</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/RodelDownloader.UI/Resources/zh-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
<data name="DeselectAll" xml:space="preserve">
<value>取消全选</value>
</data>
<data name="Downloaded" xml:space="preserve">
<value>已下载</value>
</data>
<data name="DownloadListEmptyDescription" xml:space="preserve">
<value>这次想下载哪个模型?</value>
</data>
Expand Down
4 changes: 2 additions & 2 deletions src/RodelDownloader.UI/RodelDownloader.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240404000" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240428000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.3233" />
<PackageReference Include="NeoSmart.PrettySize" Version="3.1.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="Roman-Numerals" Version="2.0.0" />
<PackageReference Include="WinUIEx" Version="2.3.4" />
<Manifest Include="$(ApplicationManifest)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public sealed partial class DownloadPageViewModel
[ObservableProperty]
private bool _isPreparingDownload;

[ObservableProperty]
private bool _hasDownloadedItems;

[ObservableProperty]
private bool _canPauseAll;

Expand All @@ -55,5 +58,10 @@ public sealed partial class DownloadPageViewModel
/// <summary>
/// 下载列表.
/// </summary>
public ObservableCollection<DownloadItemViewModel> Items { get; } = new();
public ObservableCollection<DownloadItemViewModel> DownloadingItems { get; } = new();

/// <summary>
/// 已下载列表.
/// </summary>
public ObservableCollection<DownloadItemViewModel> DownloadedItems { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void CheckDownloadEnabled()
[RelayCommand]
private async Task AddDownloadItemsAsync(List<DownloadItem> items)
{
var availableItems = items.Where(item => !Items.Any(p => p.SavePath == Path.Combine(item.TargetFolder, item.FileName))).ToList();
var availableItems = items.Where(item => !DownloadingItems.Any(p => p.SavePath == Path.Combine(item.TargetFolder, item.FileName))).ToList();
var tasks = new List<Task>();
var ariaConfigPath = Path.Combine(Package.Current.InstalledPath, "aria2.conf");
var token = SelectedSource == DownloadSource.HuggingFace
Expand Down Expand Up @@ -113,22 +113,23 @@ private async Task AddDownloadItemsAsync(List<DownloadItem> items)
_dispatcherQueue.TryEnqueue(() =>
{
vm.Id = id;
Items.Add(vm);
DownloadingItems.Add(vm);
});
});
tasks.Add(task);
}

await Task.WhenAll(tasks);
HasDownloadItems = Items.Count > 0;
CheckDownloadingItems();
CheckDownloadedItems();
_timer.Start();
}

[RelayCommand]
private async Task ResumeAllAsync()
{
await _ariaClient.UnpauseAllAsync();
foreach (var item in Items.Where(p => p.IsPaused || p.IsError))
foreach (var item in DownloadingItems.Where(p => p.IsPaused || p.IsError))
{
item.Status = DownloadStatus.Fetching;
}
Expand All @@ -138,21 +139,21 @@ private async Task ResumeAllAsync()
private async Task PauseAllAsync()
{
await _ariaClient.PauseAllAsync();
foreach (var item in Items.Where(p => p.IsDownloading))
foreach (var item in DownloadingItems.Where(p => p.IsDownloading))
{
item.Status = DownloadStatus.Paused;
}
}

private void CheckPauseAllStatus()
{
CanPauseAll = Items.Any(p => p.IsDownloading);
CanResumeAll = Items.Any(p => p.IsPaused || p.IsError);
CanPauseAll = DownloadingItems.Any(p => p.IsDownloading);
CanResumeAll = DownloadingItems.Any(p => p.IsPaused || p.IsError);
}

private async void OnTimerTickAsync(object sender, object e)
{
var isAllCompleted = Items.All(p => p.IsCompleted);
var isAllCompleted = DownloadingItems.All(p => p.IsCompleted);
if (isAllCompleted)
{
_timer.Stop();
Expand All @@ -161,7 +162,7 @@ private async void OnTimerTickAsync(object sender, object e)
}

var tasks = new List<Task>();
foreach (var item in Items)
foreach (var item in DownloadingItems)
{
if (item.IsDownloading || item.IsFetching)
{
Expand Down Expand Up @@ -195,7 +196,8 @@ private async void OnTimerTickAsync(object sender, object e)

if (i.IsCompleted || i.IsError)
{
Items.Move(Items.IndexOf(i), Items.Count - 1);
DownloadingItems.Remove(i);
DownloadedItems.Insert(0, i);
}

if (i.IsError)
Expand Down Expand Up @@ -229,15 +231,16 @@ private async void OnTimerTickAsync(object sender, object e)

await Task.WhenAll(tasks);
CheckDownloadProgress();
CheckDownloadedItems();
CheckPauseAllStatus();
}

private void CheckDownloadProgress()
{
if (HasDownloadItems)
{
DownloadProgress = $"{Items.Count(p => p.IsCompleted)}/{Items.Count}";
DownloadProgressTip = string.Format(ResourceToolkit.GetLocalizedString(StringNames.DownloadProgressTipTemplate), Items.Count, Items.Count(p => p.IsCompleted));
DownloadProgress = $"{DownloadedItems.Count}/{DownloadingItems.Count + DownloadedItems.Count}";
DownloadProgressTip = string.Format(ResourceToolkit.GetLocalizedString(StringNames.DownloadProgressTipTemplate), DownloadingItems.Count, DownloadingItems.Count(p => p.IsCompleted));
}
else
{
Expand All @@ -246,6 +249,12 @@ private void CheckDownloadProgress()
}
}

private void CheckDownloadingItems()
=> HasDownloadItems = DownloadingItems.Count > 0 || DownloadedItems.Count > 0;

private void CheckDownloadedItems()
=> HasDownloadedItems = DownloadedItems.Count > 0;

private void PauseItem(DownloadItemViewModel item)
{
Task.Run(async () =>
Expand All @@ -256,6 +265,12 @@ private void PauseItem(DownloadItemViewModel item)

private void ResumeItem(DownloadItemViewModel item)
{
if (DownloadedItems.Contains(item))
{
DownloadedItems.Remove(item);
DownloadingItems.Add(item);
}

Task.Run(async () =>
{
await _ariaClient.UnpauseAsync(item.Id);
Expand All @@ -272,8 +287,10 @@ private void RemoveItem(DownloadItemViewModel item)
});
}

Items.Remove(item);
HasDownloadItems = Items.Count > 0;
DownloadingItems.Remove(item);
DownloadedItems.Remove(item);
CheckDownloadingItems();
CheckDownloadedItems();
CheckDownloadProgress();
CheckPauseAllStatus();
}
Expand Down

0 comments on commit 96a7549

Please sign in to comment.