markus / MarkusAutoUpdate / src / NetSparkle.UI.Avalonia / ViewModels / UpdateAvailableWindowViewModel.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (6.11 KB)
1 | d8f5045e | taeseongkim | using NetSparkleUpdater.Enums; |
---|---|---|---|
2 | using NetSparkleUpdater.UI.Avalonia.Helpers; |
||
3 | using NetSparkleUpdater.UI.Avalonia.Interfaces; |
||
4 | using System; |
||
5 | using System.Collections.Generic; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Threading; |
||
9 | using System.Windows.Input; |
||
10 | |||
11 | namespace NetSparkleUpdater.UI.Avalonia.ViewModels |
||
12 | { |
||
13 | public class UpdateAvailableWindowViewModel : ChangeNotifier |
||
14 | { |
||
15 | private SparkleUpdater _sparkle; |
||
16 | private List<AppCastItem> _updates; |
||
17 | private ReleaseNotesGrabber _releaseNotesGrabber; |
||
18 | |||
19 | private CancellationToken _cancellationToken; |
||
20 | private CancellationTokenSource _cancellationTokenSource; |
||
21 | |||
22 | private string _titleHeaderText; |
||
23 | private string _infoText; |
||
24 | |||
25 | private bool _isRemindMeLaterEnabled; |
||
26 | private bool _isSkipEnabled; |
||
27 | |||
28 | private bool _areReleaseNotesVisible; |
||
29 | private bool _isRemindMeLaterVisible; |
||
30 | private bool _isSkipVisible; |
||
31 | |||
32 | private UpdateAvailableResult _userResponse; |
||
33 | |||
34 | public UpdateAvailableWindowViewModel() |
||
35 | { |
||
36 | _updates = new List<AppCastItem>(); |
||
37 | _areReleaseNotesVisible = true; |
||
38 | _isRemindMeLaterVisible = true; |
||
39 | _isSkipVisible = true; |
||
40 | _userResponse = UpdateAvailableResult.None; |
||
41 | } |
||
42 | |||
43 | public IReleaseNotesUpdater ReleaseNotesUpdater { get; set; } |
||
44 | public IUserRespondedToUpdateCheck UserRespondedHandler { get; set; } |
||
45 | |||
46 | public string TitleHeaderText |
||
47 | { |
||
48 | get => _titleHeaderText; |
||
49 | set { _titleHeaderText = value; NotifyPropertyChanged(); } |
||
50 | } |
||
51 | |||
52 | public string InfoText |
||
53 | { |
||
54 | get => _infoText; |
||
55 | set { _infoText = value; NotifyPropertyChanged(); } |
||
56 | } |
||
57 | |||
58 | public bool IsRemindMeLaterEnabled |
||
59 | { |
||
60 | get => _isRemindMeLaterEnabled; |
||
61 | set { _isRemindMeLaterEnabled = value; NotifyPropertyChanged(); } |
||
62 | } |
||
63 | |||
64 | public bool IsSkipEnabled |
||
65 | { |
||
66 | get => _isSkipEnabled; |
||
67 | set { _isSkipEnabled = value; NotifyPropertyChanged(); } |
||
68 | } |
||
69 | |||
70 | public List<AppCastItem> Updates |
||
71 | { |
||
72 | get => _updates; |
||
73 | } |
||
74 | |||
75 | public bool AreReleaseNotesVisible |
||
76 | { |
||
77 | get => _areReleaseNotesVisible; |
||
78 | set { _areReleaseNotesVisible = value; NotifyPropertyChanged(); } |
||
79 | } |
||
80 | |||
81 | public bool IsRemindMeLaterVisible |
||
82 | { |
||
83 | get => _isRemindMeLaterVisible; |
||
84 | set { _isRemindMeLaterVisible = value; NotifyPropertyChanged(); } |
||
85 | } |
||
86 | |||
87 | public bool IsSkipVisible |
||
88 | { |
||
89 | get => _isSkipVisible; |
||
90 | set { _isSkipVisible = value; NotifyPropertyChanged(); } |
||
91 | } |
||
92 | |||
93 | public UpdateAvailableResult UserResponse |
||
94 | { |
||
95 | get => _userResponse; |
||
96 | } |
||
97 | |||
98 | public ICommand Skip => new RelayCommand(PerformSkip); |
||
99 | public ICommand RemindMeLater => new RelayCommand(PerformRemindMeLater); |
||
100 | public ICommand DownloadInstall => new RelayCommand(PerformDownloadInstall); |
||
101 | |||
102 | public void PerformSkip() |
||
103 | { |
||
104 | SendResponse(UpdateAvailableResult.SkipUpdate); |
||
105 | } |
||
106 | public void PerformRemindMeLater() |
||
107 | { |
||
108 | SendResponse(UpdateAvailableResult.RemindMeLater); |
||
109 | } |
||
110 | public void PerformDownloadInstall() |
||
111 | { |
||
112 | SendResponse(UpdateAvailableResult.InstallUpdate); |
||
113 | } |
||
114 | |||
115 | private void SendResponse(UpdateAvailableResult response) |
||
116 | { |
||
117 | _userResponse = response; |
||
118 | UserRespondedHandler?.UserRespondedToUpdateCheck(response); |
||
119 | Cancel(); |
||
120 | } |
||
121 | |||
122 | public void Initialize(SparkleUpdater sparkle, List<AppCastItem> items, bool isUpdateAlreadyDownloaded = false, |
||
123 | string separatorTemplate = "", string headAddition = "") |
||
124 | { |
||
125 | _sparkle = sparkle; |
||
126 | _updates = items; |
||
127 | |||
128 | _releaseNotesGrabber = new ReleaseNotesGrabber(separatorTemplate, headAddition, sparkle); |
||
129 | |||
130 | AppCastItem item = items.FirstOrDefault(); |
||
131 | |||
132 | // TODO: string translations |
||
133 | TitleHeaderText = string.Format("A new version of {0} is available.", item?.AppName ?? "the application"); |
||
134 | var downloadInstallText = isUpdateAlreadyDownloaded ? "install" : "download"; |
||
135 | if (item != null) |
||
136 | { |
||
137 | var versionString = ""; |
||
138 | try |
||
139 | { |
||
140 | // Use try/catch since Version constructor can throw an exception and we don't want to |
||
141 | // die just because the user has a malformed version string |
||
142 | Version versionObj = new Version(item.AppVersionInstalled); |
||
143 | versionString = NetSparkleUpdater.Utilities.GetVersionString(versionObj); |
||
144 | } |
||
145 | catch |
||
146 | { |
||
147 | versionString = "?"; |
||
148 | } |
||
149 | InfoText = string.Format("{0} {3} is now available (you have {1}). Would you like to {2} it now?", item.AppName, versionString, |
||
150 | downloadInstallText, item.Version); |
||
151 | } |
||
152 | else |
||
153 | { |
||
154 | InfoText = string.Format("Would you like to {0} it now?", downloadInstallText); |
||
155 | } |
||
156 | |||
157 | bool isUserMissingCriticalUpdate = items.Any(x => x.IsCriticalUpdate); |
||
158 | IsRemindMeLaterEnabled = isUserMissingCriticalUpdate == false; |
||
159 | IsSkipEnabled = isUserMissingCriticalUpdate == false; |
||
160 | |||
161 | _cancellationTokenSource = new CancellationTokenSource(); |
||
162 | _cancellationToken = _cancellationTokenSource.Token; |
||
163 | |||
164 | ReleaseNotesUpdater?.ShowReleaseNotes(_releaseNotesGrabber.GetLoadingText()); |
||
165 | LoadReleaseNotes(items); |
||
166 | } |
||
167 | |||
168 | private async void LoadReleaseNotes(List<AppCastItem> items) |
||
169 | { |
||
170 | AppCastItem latestVersion = items.OrderByDescending(p => p.Version).FirstOrDefault(); |
||
171 | string releaseNotes = await _releaseNotesGrabber.DownloadAllReleaseNotes(items, latestVersion, _cancellationToken); |
||
172 | ReleaseNotesUpdater?.ShowReleaseNotes(releaseNotes); |
||
173 | } |
||
174 | |||
175 | public void Cancel() |
||
176 | { |
||
177 | _cancellationTokenSource?.Cancel(); |
||
178 | } |
||
179 | } |
||
180 | } |