markus / MarkusAutoUpdate / src / NetSparkle.UI.Avalonia / UIFactory.cs @ ddc223b4
이력 | 보기 | 이력해설 | 다운로드 (7.74 KB)
1 | d8f5045e | taeseongkim | using Avalonia; |
---|---|---|---|
2 | using Avalonia.Controls; |
||
3 | using Avalonia.Controls.ApplicationLifetimes; |
||
4 | using Avalonia.Media.Imaging; |
||
5 | using NetSparkleUpdater.Interfaces; |
||
6 | using NetSparkleUpdater.Properties; |
||
7 | using NetSparkleUpdater.UI.Avalonia.ViewModels; |
||
8 | using System; |
||
9 | using System.Collections.Generic; |
||
10 | using System.IO; |
||
11 | using System.Threading; |
||
12 | |||
13 | namespace NetSparkleUpdater.UI.Avalonia |
||
14 | { |
||
15 | /// <summary> |
||
16 | /// UI factory for default interface |
||
17 | /// </summary> |
||
18 | public class UIFactory : IUIFactory |
||
19 | { |
||
20 | private WindowIcon _applicationIcon = null; |
||
21 | private string _releaseNotesSeparatorTemplate; |
||
22 | private string _releaseNotesHeadAddition; |
||
23 | |||
24 | private Bitmap _iconBitmap; |
||
25 | |||
26 | public UIFactory() |
||
27 | { |
||
28 | HideReleaseNotes = false; |
||
29 | HideRemindMeLaterButton = false; |
||
30 | HideSkipButton = false; |
||
31 | } |
||
32 | |||
33 | public UIFactory(WindowIcon applicationIcon, string releaseNotesSeparatorTemplate = "", string releaseNotesHeadAddition = "") |
||
34 | { |
||
35 | _applicationIcon = applicationIcon; |
||
36 | if (applicationIcon != null) |
||
37 | { |
||
38 | using (var stream = new MemoryStream()) |
||
39 | { |
||
40 | applicationIcon?.Save(stream); |
||
41 | stream.Position = 0; |
||
42 | _iconBitmap = new Bitmap(stream); |
||
43 | } |
||
44 | } |
||
45 | _releaseNotesSeparatorTemplate = releaseNotesSeparatorTemplate; |
||
46 | _releaseNotesHeadAddition = releaseNotesHeadAddition; |
||
47 | HideReleaseNotes = false; |
||
48 | HideRemindMeLaterButton = false; |
||
49 | HideSkipButton = false; |
||
50 | } |
||
51 | |||
52 | /// <summary> |
||
53 | /// Hides the release notes view when an update is found. |
||
54 | /// </summary> |
||
55 | public bool HideReleaseNotes { get; set; } |
||
56 | |||
57 | /// <summary> |
||
58 | /// Hides the skip this update button when an update is found. |
||
59 | /// </summary> |
||
60 | public bool HideSkipButton { get; set; } |
||
61 | |||
62 | /// <summary> |
||
63 | /// Hides the remind me later button when an update is found. |
||
64 | /// </summary> |
||
65 | public bool HideRemindMeLaterButton { get; set; } |
||
66 | |||
67 | /// <summary> |
||
68 | /// Create sparkle form implementation |
||
69 | /// </summary> |
||
70 | /// <param name="sparkle">The <see cref="SparkleUpdater"/> instance to use</param> |
||
71 | /// <param name="updates">Sorted array of updates from latest to earliest</param> |
||
72 | /// <param name="isUpdateAlreadyDownloaded">If true, make sure UI text shows that the user is about to install the file instead of download it.</param> |
||
73 | public virtual IUpdateAvailable CreateUpdateAvailableWindow(SparkleUpdater sparkle, List<AppCastItem> updates, bool isUpdateAlreadyDownloaded = false) |
||
74 | { |
||
75 | var viewModel = new UpdateAvailableWindowViewModel(); |
||
76 | var window = new UpdateAvailableWindow(viewModel, _iconBitmap) |
||
77 | { |
||
78 | Icon = _applicationIcon |
||
79 | }; |
||
80 | if (HideReleaseNotes) |
||
81 | { |
||
82 | (window as IUpdateAvailable).HideReleaseNotes(); |
||
83 | } |
||
84 | if (HideSkipButton) |
||
85 | { |
||
86 | (window as IUpdateAvailable).HideSkipButton(); |
||
87 | } |
||
88 | if (HideRemindMeLaterButton) |
||
89 | { |
||
90 | (window as IUpdateAvailable).HideRemindMeLaterButton(); |
||
91 | } |
||
92 | viewModel.Initialize(sparkle, updates, isUpdateAlreadyDownloaded, _releaseNotesSeparatorTemplate, _releaseNotesHeadAddition); |
||
93 | return window; |
||
94 | } |
||
95 | |||
96 | /// <summary> |
||
97 | /// Create download progress window |
||
98 | /// </summary> |
||
99 | /// <param name="item">Appcast item to download</param> |
||
100 | public virtual IDownloadProgress CreateProgressWindow(AppCastItem item) |
||
101 | { |
||
102 | var viewModel = new DownloadProgressWindowViewModel() |
||
103 | { |
||
104 | ItemToDownload = item |
||
105 | }; |
||
106 | return new DownloadProgressWindow(viewModel, _iconBitmap) |
||
107 | { |
||
108 | Icon = _applicationIcon |
||
109 | }; |
||
110 | } |
||
111 | |||
112 | /// <summary> |
||
113 | /// Inform user in some way that NetSparkle is checking for updates |
||
114 | /// </summary> |
||
115 | public virtual ICheckingForUpdates ShowCheckingForUpdates() |
||
116 | { |
||
117 | return new CheckingForUpdatesWindow(_iconBitmap) |
||
118 | { |
||
119 | Icon = _applicationIcon |
||
120 | }; |
||
121 | } |
||
122 | |||
123 | /// <summary> |
||
124 | /// Initialize UI. Called when Sparkle is constructed and/or when the UIFactory is set. |
||
125 | /// </summary> |
||
126 | public virtual void Init() |
||
127 | { |
||
128 | } |
||
129 | |||
130 | /// <summary> |
||
131 | /// Show user a message saying downloaded update format is unknown |
||
132 | /// </summary> |
||
133 | /// <param name="downloadFileName">The filename to be inserted into the message text</param> |
||
134 | public virtual void ShowUnknownInstallerFormatMessage(string downloadFileName) |
||
135 | { |
||
136 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, |
||
137 | string.Format(Resources.DefaultUIFactory_ShowUnknownInstallerFormatMessageText, downloadFileName)); |
||
138 | } |
||
139 | |||
140 | /// <summary> |
||
141 | /// Show user that current installed version is up-to-date |
||
142 | /// </summary> |
||
143 | public virtual void ShowVersionIsUpToDate() |
||
144 | { |
||
145 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsUpToDateMessage); |
||
146 | } |
||
147 | |||
148 | /// <summary> |
||
149 | /// Show message that latest update was skipped by user |
||
150 | /// </summary> |
||
151 | public virtual void ShowVersionIsSkippedByUserRequest() |
||
152 | { |
||
153 | ShowMessage(Resources.DefaultUIFactory_MessageTitle, Resources.DefaultUIFactory_ShowVersionIsSkippedByUserRequestMessage); |
||
154 | } |
||
155 | |||
156 | /// <summary> |
||
157 | /// Show message that appcast is not available |
||
158 | /// </summary> |
||
159 | /// <param name="appcastUrl">the URL for the appcast file</param> |
||
160 | public virtual void ShowCannotDownloadAppcast(string appcastUrl) |
||
161 | { |
||
162 | ShowMessage(Resources.DefaultUIFactory_ErrorTitle, Resources.DefaultUIFactory_ShowCannotDownloadAppcastMessage); |
||
163 | } |
||
164 | |||
165 | public virtual bool CanShowToastMessages() |
||
166 | { |
||
167 | return false; |
||
168 | } |
||
169 | |||
170 | /// <summary> |
||
171 | /// Show 'toast' window to notify new version is available |
||
172 | /// </summary> |
||
173 | /// <param name="updates">Appcast updates</param> |
||
174 | /// <param name="clickHandler">handler for click</param> |
||
175 | public virtual void ShowToast(List<AppCastItem> updates, Action<List<AppCastItem>> clickHandler) |
||
176 | { |
||
177 | } |
||
178 | |||
179 | /// <summary> |
||
180 | /// Show message on download error |
||
181 | /// </summary> |
||
182 | /// <param name="message">Error message from exception</param> |
||
183 | /// <param name="appcastUrl">the URL for the appcast file</param> |
||
184 | public virtual void ShowDownloadErrorMessage(string message, string appcastUrl) |
||
185 | { |
||
186 | ShowMessage(Resources.DefaultUIFactory_ErrorTitle, string.Format(Resources.DefaultUIFactory_ShowDownloadErrorMessage, message)); |
||
187 | } |
||
188 | |||
189 | private void ShowMessage(string title, string message) |
||
190 | { |
||
191 | var messageWindow = new MessageNotificationWindow(new MessageNotificationWindowViewModel(message), _iconBitmap) |
||
192 | { |
||
193 | Title = title, |
||
194 | Icon = _applicationIcon |
||
195 | }; |
||
196 | messageWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; |
||
197 | messageWindow.Show(); // TODO: This was ShowDialog; will this break anything? |
||
198 | } |
||
199 | |||
200 | /// <summary> |
||
201 | /// Shut down the UI so we can run an update. |
||
202 | /// If in WPF, System.Windows.Application.Current.Shutdown(). |
||
203 | /// If in WinForms, Application.Exit(). |
||
204 | /// </summary> |
||
205 | public void Shutdown() |
||
206 | { |
||
207 | (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown(); |
||
208 | } |
||
209 | } |
||
210 | } |