markus / MarkusAutoUpdate / src / NetSparkle.UI.Avalonia / DownloadProgressWindow.xaml.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (4.24 KB)
1 |
using Avalonia; |
---|---|
2 |
using Avalonia.Controls; |
3 |
using Avalonia.Interactivity; |
4 |
using Avalonia.Markup.Xaml; |
5 |
using Avalonia.Media; |
6 |
using Avalonia.Media.Imaging; |
7 |
using Avalonia.Threading; |
8 |
using NetSparkleUpdater.Events; |
9 |
using NetSparkleUpdater.Interfaces; |
10 |
using NetSparkleUpdater.UI.Avalonia.Controls; |
11 |
using NetSparkleUpdater.UI.Avalonia.ViewModels; |
12 |
using System.Net; |
13 |
|
14 |
namespace NetSparkleUpdater.UI.Avalonia |
15 |
{ |
16 |
public class DownloadProgressWindow : BaseWindow, IDownloadProgress |
17 |
{ |
18 |
private bool _didCallDownloadProcessCompletedHandler = false; |
19 |
|
20 |
private DownloadProgressWindowViewModel _dataContext; |
21 |
|
22 |
private Button _actionButton; |
23 |
|
24 |
public DownloadProgressWindow() |
25 |
{ |
26 |
this.InitializeComponent(); |
27 |
#if DEBUG |
28 |
this.AttachDevTools(); |
29 |
#endif |
30 |
Closing += DownloadProgressWindow_Closing; |
31 |
} |
32 |
|
33 |
public DownloadProgressWindow(DownloadProgressWindowViewModel viewModel, IBitmap iconBitmap) |
34 |
{ |
35 |
InitializeComponent(); |
36 |
#if DEBUG |
37 |
this.AttachDevTools(); |
38 |
#endif |
39 |
Closing += DownloadProgressWindow_Closing; |
40 |
DataContext = _dataContext = viewModel; |
41 |
var imageControl = this.FindControl<Image>("AppIcon"); |
42 |
if (imageControl != null) |
43 |
{ |
44 |
imageControl.Source = iconBitmap; |
45 |
} |
46 |
} |
47 |
|
48 |
private void InitializeComponent() |
49 |
{ |
50 |
AvaloniaXamlLoader.Load(this); |
51 |
_actionButton = this.FindControl<Button>("ActionButton"); |
52 |
} |
53 |
|
54 |
private void DownloadProgressWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) |
55 |
{ |
56 |
if (!_didCallDownloadProcessCompletedHandler) |
57 |
{ |
58 |
_didCallDownloadProcessCompletedHandler = true; |
59 |
DownloadProcessCompleted?.Invoke(this, new DownloadInstallEventArgs(false)); |
60 |
} |
61 |
Closing -= DownloadProgressWindow_Closing; |
62 |
if (!_isOnMainThread && !_hasInitiatedShutdown) |
63 |
{ |
64 |
_hasInitiatedShutdown = true; |
65 |
_cancellationTokenSource.Cancel(); |
66 |
} |
67 |
} |
68 |
|
69 |
/// <summary> |
70 |
/// Event to fire when the download UI is complete; tells you |
71 |
/// if the install process should happen or not |
72 |
/// </summary> |
73 |
public event DownloadInstallEventHandler DownloadProcessCompleted; |
74 |
|
75 |
bool IDownloadProgress.DisplayErrorMessage(string errorMessage) |
76 |
{ |
77 |
if (_dataContext != null) |
78 |
{ |
79 |
_dataContext.ErrorMessageText = errorMessage; |
80 |
_dataContext.IsErrorMessageVisible = true; |
81 |
} |
82 |
return true; |
83 |
} |
84 |
|
85 |
void IDownloadProgress.FinishedDownloadingFile(bool isDownloadedFileValid) |
86 |
{ |
87 |
_dataContext?.SetFinishedDownloading(isDownloadedFileValid); |
88 |
if (!isDownloadedFileValid) |
89 |
{ |
90 |
Dispatcher.UIThread.Post(() => |
91 |
{ |
92 |
this.Background = new SolidColorBrush(Colors.Tomato); |
93 |
}); |
94 |
} |
95 |
} |
96 |
|
97 |
void IDownloadProgress.Close() |
98 |
{ |
99 |
CloseWindow(); |
100 |
} |
101 |
|
102 |
/// <summary> |
103 |
/// Event called when the client download progress changes |
104 |
/// </summary> |
105 |
private void OnDownloadProgressChanged(object sender, long bytesReceived, long totalBytesToReceive, int percentage) |
106 |
{ |
107 |
_dataContext?.UpdateProgress(bytesReceived, totalBytesToReceive, percentage); |
108 |
} |
109 |
|
110 |
void IDownloadProgress.OnDownloadProgressChanged(object sender, ItemDownloadProgressEventArgs e) |
111 |
{ |
112 |
OnDownloadProgressChanged(sender, e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage); |
113 |
} |
114 |
|
115 |
void IDownloadProgress.SetDownloadAndInstallButtonEnabled(bool shouldBeEnabled) |
116 |
{ |
117 |
_actionButton.IsEnabled = shouldBeEnabled; |
118 |
} |
119 |
|
120 |
void IDownloadProgress.Show(bool isOnMainThread) |
121 |
{ |
122 |
ShowWindow(isOnMainThread); |
123 |
} |
124 |
|
125 |
public void ActionButton_Click(object sender, RoutedEventArgs e) |
126 |
{ |
127 |
_didCallDownloadProcessCompletedHandler = true; |
128 |
DownloadProcessCompleted?.Invoke(this, new DownloadInstallEventArgs(!(_dataContext?.IsDownloading ?? true))); |
129 |
} |
130 |
} |
131 |
} |