markus / MarkusAutoUpdate / src / NetSparkle.UI.WPF / UIFactory.cs @ 4d2c8ee1
이력 | 보기 | 이력해설 | 다운로드 (5.86 KB)
1 | d8f5045e | taeseongkim | using System; |
---|---|---|---|
2 | using System.Drawing; |
||
3 | using NetSparkleUpdater.Interfaces; |
||
4 | using NetSparkleUpdater.Properties; |
||
5 | using System.Windows.Media; |
||
6 | using System.Windows; |
||
7 | using System.Threading; |
||
8 | using System.Collections.Generic; |
||
9 | using NetSparkleUpdater.UI.WPF.ViewModels; |
||
10 | |||
11 | namespace NetSparkleUpdater.UI.WPF |
||
12 | { |
||
13 | /// <summary> |
||
14 | /// UI factory for default interface |
||
15 | /// </summary> |
||
16 | public class UIFactory : IUIFactory |
||
17 | { |
||
18 | private ImageSource _applicationIcon = null; |
||
19 | |||
20 | /// <summary> |
||
21 | /// Create a new UIFactory for WPF applications |
||
22 | /// </summary> |
||
23 | public UIFactory() |
||
24 | { |
||
25 | HideReleaseNotes = false; |
||
26 | HideRemindMeLaterButton = false; |
||
27 | HideSkipButton = false; |
||
28 | } |
||
29 | |||
30 | /// <summary> |
||
31 | /// Create a new UIFactory for WPF applications with the given |
||
32 | /// application icon to show in all update windows |
||
33 | /// </summary> |
||
34 | /// <param name="applicationIcon">the <see cref="ImageSource"/> to show in all windows</param> |
||
35 | public UIFactory(ImageSource applicationIcon) |
||
36 | { |
||
37 | _applicationIcon = applicationIcon; |
||
38 | _applicationIcon?.Freeze(); |
||
39 | HideReleaseNotes = false; |
||
40 | HideRemindMeLaterButton = false; |
||
41 | HideSkipButton = false; |
||
42 | } |
||
43 | |||
44 | /// <inheritdoc/> |
||
45 | public bool HideReleaseNotes { get; set; } |
||
46 | |||
47 | /// <inheritdoc/> |
||
48 | public bool HideSkipButton { get; set; } |
||
49 | |||
50 | /// <inheritdoc/> |
||
51 | public bool HideRemindMeLaterButton { get; set; } |
||
52 | |||
53 | /// <inheritdoc/> |
||
54 | public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater sparkle, List<AppCastItem> updates, bool isUpdateAlreadyDownloaded = false) |
||
55 | { |
||
56 | var viewModel = new UpdateAvailableWindowViewModel(); |
||
57 | var window = new UpdateAvailableWindow(viewModel) |
||
58 | { |
||
59 | Icon = _applicationIcon |
||
60 | }; |
||
61 | if (HideReleaseNotes) |
||
62 | { |
||
63 | (window as IUpdateAvailable).HideReleaseNotes(); |
||
64 | } |
||
65 | if (HideSkipButton) |
||
66 | { |
||
67 | (window as IUpdateAvailable).HideSkipButton(); |
||
68 | } |
||
69 | if (HideRemindMeLaterButton) |
||
70 | { |
||
71 | (window as IUpdateAvailable).HideRemindMeLaterButton(); |
||
72 | } |
||
73 | viewModel.Initialize(sparkle, updates, isUpdateAlreadyDownloaded); |
||
74 | return window; |
||
75 | } |
||
76 | |||
77 | /// <inheritdoc/> |
||
78 | public virtual IDownloadProgress CreateProgressWindow(AppCastItem item) |
||
79 | { |
||
80 | var viewModel = new DownloadProgressWindowViewModel() |
||
81 | { |
||
82 | ItemToDownload = item |
||
83 | }; |
||
84 | return new DownloadProgressWindow(viewModel) |
||
85 | { |
||
86 | Icon = _applicationIcon |
||
87 | }; |
||
88 | } |
||
89 | |||
90 | /// <inheritdoc/> |
||
91 | public virtual ICheckingForUpdates ShowCheckingForUpdates() |
||
92 | { |
||
93 | return new CheckingForUpdatesWindow { Icon = _applicationIcon }; |
||
94 | } |
||
95 | |||
96 | /// <inheritdoc/> |
||
97 | public virtual void Init() |
||
98 | { |
||
99 | } |
||
100 | |||
101 | /// <inheritdoc/> |
||
102 | public virtual void ShowUnknownInstallerFormatMessage(string downloadFileName) |
||
103 | { |
||
104 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, |
||
105 | string.Format(Resources.DefaultUIFactory_ShowUnknownInstallerFormatMessageText, downloadFileName)); |
||
106 | } |
||
107 | |||
108 | /// <inheritdoc/> |
||
109 | public virtual void ShowVersionIsUpToDate() |
||
110 | { |
||
111 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsUpToDateMessage); |
||
112 | } |
||
113 | |||
114 | /// <inheritdoc/> |
||
115 | public virtual void ShowVersionIsSkippedByUserRequest() |
||
116 | { |
||
117 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsSkippedByUserRequestMessage); |
||
118 | } |
||
119 | |||
120 | /// <inheritdoc/> |
||
121 | public virtual void ShowCannotDownloadAppcast(string appcastUrl) |
||
122 | { |
||
123 | ShowMessage(Resources.DefaultUIFactory_ErrorTitle, Resources.DefaultUIFactory_ShowCannotDownloadAppcastMessage); |
||
124 | } |
||
125 | |||
126 | /// <inheritdoc/> |
||
127 | public virtual bool CanShowToastMessages() |
||
128 | { |
||
129 | return true; |
||
130 | } |
||
131 | |||
132 | /// <inheritdoc/> |
||
133 | public virtual void ShowToast(List<AppCastItem> updates, Action<List<AppCastItem>> clickHandler) |
||
134 | { |
||
135 | Thread thread = new Thread(() => |
||
136 | { |
||
137 | var toast = new ToastNotification() |
||
138 | { |
||
139 | ClickAction = clickHandler, |
||
140 | Updates = updates, |
||
141 | Icon = _applicationIcon |
||
142 | }; |
||
143 | try |
||
144 | { |
||
145 | toast.Show(Resources.DefaultUIFactory_ToastMessage, Resources.DefaultUIFactory_ToastCallToAction, 5); |
||
146 | System.Windows.Threading.Dispatcher.Run(); |
||
147 | } |
||
148 | catch (ThreadAbortException) |
||
149 | { |
||
150 | toast.Dispatcher.InvokeShutdown(); |
||
151 | } |
||
152 | }); |
||
153 | thread.SetApartmentState(ApartmentState.STA); |
||
154 | thread.Start(); |
||
155 | } |
||
156 | |||
157 | /// <inheritdoc/> |
||
158 | public virtual void ShowDownloadErrorMessage(string message, string appcastUrl) |
||
159 | { |
||
160 | ShowMessage(Resources.DefaultUIFactory_ErrorTitle, string.Format(Resources.DefaultUIFactory_ShowDownloadErrorMessage, message)); |
||
161 | } |
||
162 | |||
163 | private void ShowMessage(string title, string message) |
||
164 | { |
||
165 | var messageWindow = new MessageNotificationWindow(new MessageNotificationWindowViewModel(message)) |
||
166 | { |
||
167 | Title = title, |
||
168 | Icon = _applicationIcon |
||
169 | }; |
||
170 | messageWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; |
||
171 | messageWindow.ShowDialog(); |
||
172 | } |
||
173 | |||
174 | /// <inheritdoc/> |
||
175 | public void Shutdown() |
||
176 | { |
||
177 | System.Windows.Application.Current.Shutdown(); |
||
178 | } |
||
179 | } |
||
180 | } |