Skip to content

Commit 79d83af

Browse files
committed
Fix update settings to finally remove the need for settings service singleton
1 parent 61b0714 commit 79d83af

File tree

8 files changed

+79
-119
lines changed

8 files changed

+79
-119
lines changed

ILSpy/AboutPage.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Text.RegularExpressions;
2424
using System.Windows;
2525
using System.Windows.Controls;
26+
using System.Windows.Controls.Primitives;
2627
using System.Windows.Data;
2728
using System.Windows.Input;
2829
using System.Windows.Navigation;
@@ -34,7 +35,6 @@
3435
using ICSharpCode.ILSpy.TextView;
3536
using ICSharpCode.ILSpy.Themes;
3637
using ICSharpCode.ILSpy.Updates;
37-
using ICSharpCode.ILSpyX.Settings;
3838

3939
namespace ICSharpCode.ILSpy
4040
{
@@ -53,7 +53,7 @@ public override void Execute(object parameter)
5353

5454
[Export]
5555
[Shared]
56-
public sealed class AboutPage(IEnumerable<IAboutPageAddition> aboutPageAdditions)
56+
public sealed class AboutPage(IEnumerable<IAboutPageAddition> aboutPageAdditions, SettingsService settingsService)
5757
{
5858
public void Display(DecompilerTextView textView)
5959
{
@@ -68,9 +68,10 @@ public void Display(DecompilerTextView textView)
6868

6969
output.AddUIElement(
7070
delegate {
71-
StackPanel stackPanel = new StackPanel();
72-
stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
73-
stackPanel.Orientation = Orientation.Horizontal;
71+
StackPanel stackPanel = new() {
72+
HorizontalAlignment = HorizontalAlignment.Center,
73+
Orientation = Orientation.Horizontal
74+
};
7475
if (NotifyOfUpdatesStrategy.LatestAvailableVersion == null)
7576
{
7677
AddUpdateCheckButton(stackPanel, textView);
@@ -80,11 +81,13 @@ public void Display(DecompilerTextView textView)
8081
// we already retrieved the latest version sometime earlier
8182
ShowAvailableVersion(NotifyOfUpdatesStrategy.LatestAvailableVersion, stackPanel);
8283
}
83-
CheckBox checkBox = new CheckBox();
84-
checkBox.Margin = new Thickness(4);
85-
checkBox.Content = Resources.AutomaticallyCheckUpdatesEveryWeek;
86-
UpdateSettings settings = new UpdateSettings(SettingsService.Instance.SpySettings);
87-
checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("AutomaticUpdateCheckEnabled") { Source = settings });
84+
CheckBox checkBox = new() {
85+
Margin = new Thickness(4),
86+
Content = Resources.AutomaticallyCheckUpdatesEveryWeek
87+
};
88+
89+
var settings = settingsService.GetSettings<UpdateSettings>();
90+
checkBox.SetBinding(ToggleButton.IsCheckedProperty, new Binding("AutomaticUpdateCheckEnabled") { Source = settings });
8891
return new StackPanel {
8992
Margin = new Thickness(0, 4, 0, 0),
9093
Cursor = Cursors.Arrow,

ILSpy/AssemblyTree/AssemblyTreeModel.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
using ICSharpCode.ILSpy.Search;
4343
using ICSharpCode.ILSpy.TextView;
4444
using ICSharpCode.ILSpy.TreeNodes;
45+
using ICSharpCode.ILSpy.Updates;
4546
using ICSharpCode.ILSpy.ViewModels;
4647
using ICSharpCode.ILSpyX;
4748
using ICSharpCode.ILSpyX.Settings;
@@ -169,16 +170,16 @@ private bool HandleCommandLineArguments(CommandLineArguments args)
169170

170171
/// <summary>
171172
/// Called on startup or when passed arguments via WndProc from a second instance.
172-
/// In the format case, spySettings is non-null; in the latter it is null.
173+
/// In the format case, updateSettings is non-null; in the latter it is null.
173174
/// </summary>
174-
private async Task HandleCommandLineArgumentsAfterShowList(CommandLineArguments args, ISettingsProvider? spySettings = null)
175+
private async Task HandleCommandLineArgumentsAfterShowList(CommandLineArguments args, UpdateSettings? updateSettings = null)
175176
{
176177
var sessionSettings = settingsService.SessionSettings;
177178

178179
var relevantAssemblies = commandLineLoadedAssemblies.ToList();
179180
commandLineLoadedAssemblies.Clear(); // clear references once we don't need them anymore
180181

181-
await NavigateOnLaunch(args.NavigateTo, sessionSettings.ActiveTreeViewPath, spySettings, relevantAssemblies);
182+
await NavigateOnLaunch(args.NavigateTo, sessionSettings.ActiveTreeViewPath, updateSettings, relevantAssemblies);
182183

183184
if (args.Search != null)
184185
{
@@ -207,7 +208,7 @@ await Dispatcher.InvokeAsync(async () => {
207208
});
208209
}
209210

210-
private async Task NavigateOnLaunch(string? navigateTo, string[]? activeTreeViewPath, ISettingsProvider? spySettings, List<LoadedAssembly> relevantAssemblies)
211+
private async Task NavigateOnLaunch(string? navigateTo, string[]? activeTreeViewPath, UpdateSettings? updateSettings, List<LoadedAssembly> relevantAssemblies)
211212
{
212213
var initialSelection = SelectedItem;
213214
if (navigateTo != null)
@@ -277,7 +278,7 @@ private async Task NavigateOnLaunch(string? navigateTo, string[]? activeTreeView
277278
SelectNode(asmNode);
278279
}
279280
}
280-
else if (spySettings != null)
281+
else if (updateSettings != null)
281282
{
282283
SharpTreeNode? node = null;
283284
if (activeTreeViewPath?.Length > 0)
@@ -300,7 +301,7 @@ private async Task NavigateOnLaunch(string? navigateTo, string[]? activeTreeView
300301
SelectNode(node);
301302

302303
// only if not showing the about page, perform the update check:
303-
await App.Current.MainWindow.ShowMessageIfUpdatesAvailableAsync(spySettings);
304+
await App.Current.MainWindow.ShowMessageIfUpdatesAvailableAsync(updateSettings);
304305
}
305306
else
306307
{
@@ -399,7 +400,7 @@ public void Initialize()
399400

400401
private async Task OpenAssemblies()
401402
{
402-
await HandleCommandLineArgumentsAfterShowList(App.CommandLineArguments, settingsService.SpySettings);
403+
await HandleCommandLineArgumentsAfterShowList(App.CommandLineArguments, settingsService.GetSettings<UpdateSettings>());
403404

404405
if (FormatExceptions(App.StartupExceptions.ToArray(), out var output))
405406
{

ILSpy/Commands/CheckForUpdatesCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Composition;
2121

2222
using ICSharpCode.ILSpy.Properties;
23+
using ICSharpCode.ILSpy.Updates;
2324

2425
namespace ICSharpCode.ILSpy
2526
{
@@ -29,7 +30,7 @@ sealed class CheckForUpdatesCommand(SettingsService settingsService) : SimpleCom
2930
{
3031
public override async void Execute(object parameter)
3132
{
32-
await App.Current.MainWindow.ShowMessageIfUpdatesAvailableAsync(settingsService.SpySettings, forceCheck: true);
33+
await App.Current.MainWindow.ShowMessageIfUpdatesAvailableAsync(settingsService.GetSettings<UpdateSettings>(), forceCheck: true);
3334
}
3435
}
3536
}

ILSpy/MainWindow.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,16 @@ protected override void OnKeyDown(KeyEventArgs e)
152152

153153
string updateAvailableDownloadUrl;
154154

155-
public async Task ShowMessageIfUpdatesAvailableAsync(ISettingsProvider spySettings, bool forceCheck = false)
155+
public async Task ShowMessageIfUpdatesAvailableAsync(UpdateSettings settings, bool forceCheck = false)
156156
{
157157
string downloadUrl;
158158
if (forceCheck)
159159
{
160-
downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesAsync(spySettings);
160+
downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesAsync(settings);
161161
}
162162
else
163163
{
164-
downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesIfEnabledAsync(spySettings);
164+
downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesIfEnabledAsync(settings);
165165
}
166166

167167
// The Update Panel is only available for NotifyOfUpdatesStrategy, AutoUpdate will have differing UI requirements
@@ -182,7 +182,7 @@ async void DownloadOrCheckUpdateButtonClick(object sender, RoutedEventArgs e)
182182
else
183183
{
184184
updatePanel.Visibility = Visibility.Collapsed;
185-
string downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesAsync(settingsService.SpySettings);
185+
string downloadUrl = await NotifyOfUpdatesStrategy.CheckForUpdatesAsync(settingsService.GetSettings<UpdateSettings>());
186186
AdjustUpdateUIAfterCheck(downloadUrl, true);
187187
}
188188
}

ILSpy/Themes/WindowStyleManagerBehavior.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override void OnAttached()
4343
{
4444
base.OnAttached();
4545

46-
MessageBus<SettingsChangedEventArgs>.Subscribers += (sender, e) => DisplaySettings_PropertyChanged(sender, e);
46+
MessageBus<SettingsChangedEventArgs>.Subscribers += (sender, e) => Settings_PropertyChanged(sender, e);
4747

4848
_foreground = AssociatedObject.Track(Control.ForegroundProperty);
4949
_background = AssociatedObject.Track(Control.BackgroundProperty);
@@ -83,7 +83,7 @@ private static void ShowRestartNotification()
8383
MessageBox.Show(Properties.Resources.SettingsChangeRestartRequired);
8484
}
8585

86-
private void DisplaySettings_PropertyChanged(object sender, PropertyChangedEventArgs e)
86+
private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
8787
{
8888
if (sender is not DisplaySettings displaySettings)
8989
return;

ILSpy/Updates/NotifyOfUpdatesStrategy.cs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,51 +54,40 @@ public static async Task<AvailableVersionInfo> GetLatestVersionAsync()
5454
return LatestAvailableVersion;
5555
}
5656

57-
58-
5957
/// <summary>
6058
/// If automatic update checking is enabled, checks if there are any updates available.
6159
/// Returns the download URL if an update is available.
6260
/// Returns null if no update is available, or if no check was performed.
6361
/// </summary>
64-
public static async Task<string> CheckForUpdatesIfEnabledAsync(ISettingsProvider spySettings)
62+
public static async Task<string> CheckForUpdatesIfEnabledAsync(UpdateSettings settings)
6563
{
66-
UpdateSettings s = new UpdateSettings(spySettings);
67-
6864
// If we're in an MSIX package, updates work differently
69-
if (s.AutomaticUpdateCheckEnabled)
70-
{
71-
// perform update check if we never did one before;
72-
// or if the last check wasn't in the past 7 days
73-
if (s.LastSuccessfulUpdateCheck == null
74-
|| s.LastSuccessfulUpdateCheck < DateTime.UtcNow.AddDays(-7)
75-
|| s.LastSuccessfulUpdateCheck > DateTime.UtcNow)
76-
{
77-
return await CheckForUpdateInternal(s).ConfigureAwait(false);
78-
}
79-
else
80-
{
81-
return null;
82-
}
83-
}
84-
else
85-
{
65+
if (!settings.AutomaticUpdateCheckEnabled)
8666
return null;
67+
68+
// perform update check if we never did one before;
69+
// or if the last check wasn't in the past 7 days
70+
if (settings.LastSuccessfulUpdateCheck == null
71+
|| settings.LastSuccessfulUpdateCheck < DateTime.UtcNow.AddDays(-7)
72+
|| settings.LastSuccessfulUpdateCheck > DateTime.UtcNow)
73+
{
74+
return await CheckForUpdateInternal(settings).ConfigureAwait(false);
8775
}
76+
77+
return null;
8878
}
8979

90-
public static Task<string> CheckForUpdatesAsync(ISettingsProvider spySettings)
80+
public static Task<string> CheckForUpdatesAsync(UpdateSettings settings)
9181
{
92-
UpdateSettings s = new UpdateSettings(spySettings);
93-
return CheckForUpdateInternal(s);
82+
return CheckForUpdateInternal(settings);
9483
}
9584

96-
static async Task<string> CheckForUpdateInternal(UpdateSettings s)
85+
static async Task<string> CheckForUpdateInternal(UpdateSettings settings)
9786
{
9887
try
9988
{
10089
var v = await GetLatestVersionAsync().ConfigureAwait(false);
101-
s.LastSuccessfulUpdateCheck = DateTime.UtcNow;
90+
settings.LastSuccessfulUpdateCheck = DateTime.UtcNow;
10291
if (v.Version > AppUpdateService.CurrentVersion)
10392
return v.DownloadUrl;
10493
else

ILSpy/Updates/UpdateSettings.cs

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,73 +17,56 @@
1717
// DEALINGS IN THE SOFTWARE.
1818

1919
using System;
20-
using System.ComponentModel;
2120
using System.Xml.Linq;
2221

23-
using ICSharpCode.ILSpyX.Settings;
22+
using TomsToolbox.Wpf;
2423

2524
namespace ICSharpCode.ILSpy.Updates
2625
{
27-
sealed class UpdateSettings : INotifyPropertyChanged
26+
public sealed class UpdateSettings : ObservableObjectBase, ISettingsSection
2827
{
29-
public UpdateSettings(ISettingsProvider spySettings)
30-
{
31-
XElement s = spySettings["UpdateSettings"];
32-
this.automaticUpdateCheckEnabled = (bool?)s.Element("AutomaticUpdateCheckEnabled") ?? true;
33-
try
34-
{
35-
this.lastSuccessfulUpdateCheck = (DateTime?)s.Element("LastSuccessfulUpdateCheck");
36-
}
37-
catch (FormatException)
38-
{
39-
// avoid crashing on settings files invalid due to
40-
// https://github.com/icsharpcode/ILSpy/issues/closed/#issue/2
41-
}
42-
}
28+
public XName SectionName => "UpdateSettings";
4329

4430
bool automaticUpdateCheckEnabled;
4531

4632
public bool AutomaticUpdateCheckEnabled {
47-
get { return automaticUpdateCheckEnabled; }
48-
set {
49-
if (automaticUpdateCheckEnabled != value)
50-
{
51-
automaticUpdateCheckEnabled = value;
52-
Save();
53-
OnPropertyChanged(nameof(AutomaticUpdateCheckEnabled));
54-
}
55-
}
33+
get => automaticUpdateCheckEnabled;
34+
set => SetProperty(ref automaticUpdateCheckEnabled, value);
5635
}
5736

5837
DateTime? lastSuccessfulUpdateCheck;
5938

6039
public DateTime? LastSuccessfulUpdateCheck {
61-
get { return lastSuccessfulUpdateCheck; }
62-
set {
63-
if (lastSuccessfulUpdateCheck != value)
64-
{
65-
lastSuccessfulUpdateCheck = value;
66-
Save();
67-
OnPropertyChanged(nameof(LastSuccessfulUpdateCheck));
68-
}
69-
}
40+
get => lastSuccessfulUpdateCheck;
41+
set => SetProperty(ref lastSuccessfulUpdateCheck, value);
7042
}
7143

72-
public void Save()
44+
public void LoadFromXml(XElement section)
7345
{
74-
XElement updateSettings = new XElement("UpdateSettings");
75-
updateSettings.Add(new XElement("AutomaticUpdateCheckEnabled", automaticUpdateCheckEnabled));
76-
if (lastSuccessfulUpdateCheck != null)
77-
updateSettings.Add(new XElement("LastSuccessfulUpdateCheck", lastSuccessfulUpdateCheck));
78-
SettingsService.Instance.SpySettings.SaveSettings(updateSettings);
46+
AutomaticUpdateCheckEnabled = (bool?)section.Element("AutomaticUpdateCheckEnabled") ?? true;
47+
try
48+
{
49+
LastSuccessfulUpdateCheck = (DateTime?)section.Element("LastSuccessfulUpdateCheck");
50+
}
51+
catch (FormatException)
52+
{
53+
// avoid crashing on settings files invalid due to
54+
// https://github.com/icsharpcode/ILSpy/issues/closed/#issue/2
55+
}
7956
}
8057

81-
public event PropertyChangedEventHandler PropertyChanged;
82-
83-
void OnPropertyChanged(string propertyName)
58+
public XElement SaveToXml()
8459
{
85-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
60+
var section = new XElement(SectionName);
61+
62+
section.Add(new XElement("AutomaticUpdateCheckEnabled", AutomaticUpdateCheckEnabled));
63+
64+
if (LastSuccessfulUpdateCheck != null)
65+
{
66+
section.Add(new XElement("LastSuccessfulUpdateCheck", LastSuccessfulUpdateCheck));
67+
}
68+
69+
return section;
8670
}
8771
}
88-
8972
}

0 commit comments

Comments
 (0)