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