-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathDownloadProgressList.xaml.cs
More file actions
107 lines (93 loc) · 3.61 KB
/
DownloadProgressList.xaml.cs
File metadata and controls
107 lines (93 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using AIDevGallery.Utils;
using AIDevGallery.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Collections.ObjectModel;
using System.Linq;
namespace AIDevGallery.Controls;
internal sealed partial class DownloadProgressList : UserControl
{
private readonly ObservableCollection<DownloadableModel> downloadProgresses = [];
public DownloadProgressList()
{
this.InitializeComponent();
App.ModelDownloadQueue.ModelsChanged += DownloadQueue_ModelsChanged;
PopulateModels();
}
private void PopulateModels()
{
downloadProgresses.Clear();
foreach (var model in App.ModelDownloadQueue.GetDownloads())
{
downloadProgresses.Add(new DownloadableModel(model));
}
}
private void DownloadQueue_ModelsChanged(ModelDownloadQueue sender)
{
foreach (var model in sender.GetDownloads())
{
var existingDownload = downloadProgresses.FirstOrDefault(x => x.ModelDetails.Url == model.Details.Url);
if (existingDownload != null && existingDownload.Status == DownloadStatus.Canceled)
{
downloadProgresses.Remove(existingDownload);
}
if (existingDownload == null || existingDownload.Status == DownloadStatus.Canceled)
{
downloadProgresses.Add(new DownloadableModel(model));
}
}
}
private void CancelDownloadModelButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is DownloadableModel downloadableModel)
{
downloadableModel.CancelDownload();
}
}
private void GoToModelPageClicked(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is DownloadableModel downloadableModel)
{
var modelDetails = downloadableModel.ModelDetails;
if (modelDetails != null)
{
App.MainWindow.Navigate("Models", modelDetails);
}
}
}
private void RetryDownloadClicked(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is DownloadableModel downloadableModel)
{
downloadProgresses.Remove(downloadableModel);
// ModelDownload lifecycle is managed by ModelDownloadQueue, should not be disposed immediately
#pragma warning disable IDISP004 // Don't ignore created IDisposable
App.ModelDownloadQueue.AddModel(downloadableModel.ModelDetails);
#pragma warning restore IDISP004
}
}
private void ClearHistory_Click(object sender, RoutedEventArgs e)
{
foreach (DownloadableModel model in downloadProgresses.ToList())
{
if (model.Status is DownloadStatus.Completed or DownloadStatus.Canceled or DownloadStatus.VerificationFailed)
{
downloadProgresses.Remove(model);
}
}
}
private void VerificationFailedClicked(object sender, RoutedEventArgs e)
{
// Retry download when verification failed
if (sender is Button button && button.Tag is DownloadableModel downloadableModel)
{
downloadProgresses.Remove(downloadableModel);
// ModelDownload lifecycle is managed by ModelDownloadQueue, should not be disposed immediately
#pragma warning disable IDISP004 // Don't ignore created IDisposable
App.ModelDownloadQueue.AddModel(downloadableModel.ModelDetails);
#pragma warning restore IDISP004
}
}
}