Skip to content

Commit 91a43e9

Browse files
fix: setting persistence across clickonce upgrades
1 parent 7ab74f7 commit 91a43e9

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Arius.Explorer.Settings;
2+
using Shouldly;
3+
4+
namespace Arius.Explorer.Tests.Settings;
5+
6+
public class ApplicationSettingsTests
7+
{
8+
[Fact]
9+
public void UpgradeRequired_DefaultValue_ShouldBeTrue()
10+
{
11+
// Arrange & Act
12+
var settings = ApplicationSettings.Default;
13+
14+
// Assert
15+
// On first run, UpgradeRequired should default to true
16+
// After Upgrade() is called and saved, it becomes false
17+
settings.UpgradeRequired.ShouldBe(true);
18+
}
19+
20+
[Fact]
21+
public void RecentRepositories_DefaultValue_ShouldBeEmpty()
22+
{
23+
// Arrange & Act
24+
var settings = ApplicationSettings.Default;
25+
26+
// Assert
27+
settings.RecentRepositories.ShouldNotBeNull();
28+
}
29+
30+
[Fact]
31+
public void RecentLimit_DefaultValue_ShouldBe10()
32+
{
33+
// Arrange & Act
34+
var settings = ApplicationSettings.Default;
35+
36+
// Assert
37+
settings.RecentLimit.ShouldBe(10);
38+
}
39+
}

src/Arius.Explorer/App.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ protected override void OnStartup(StartupEventArgs e)
2727
var logger = ServiceProvider.GetRequiredService<ILogger<App>>();
2828
logger.LogInformation("Application starting up");
2929

30+
// Upgrade settings from previous ClickOnce version if needed
31+
try
32+
{
33+
if (Settings.ApplicationSettings.Default.UpgradeRequired)
34+
{
35+
logger.LogInformation("Upgrading application settings from previous version");
36+
Settings.ApplicationSettings.Default.Upgrade();
37+
Settings.ApplicationSettings.Default.UpgradeRequired = false;
38+
Settings.ApplicationSettings.Default.Save();
39+
logger.LogInformation("Settings upgraded successfully");
40+
}
41+
}
42+
catch (Exception ex)
43+
{
44+
logger.LogWarning(ex, "Settings upgrade failed, continuing with default settings");
45+
}
46+
3047
// Get the repository explorer window from DI
3148
var repositoryWindow = ServiceProvider.GetRequiredService<RepositoryExplorer.RepositoryExplorerWindow>();
3249
MainWindow = repositoryWindow;

src/Arius.Explorer/Arius.Explorer.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<!--<PackageId>Arius.Explorer</PackageId>-->
1919
<Product>Arius Explorer</Product>
2020
<EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
21+
22+
<!-- Strong name signing for ClickOnce settings persistence -->
23+
<SignAssembly>true</SignAssembly>
24+
<AssemblyOriginatorKeyFile>Arius.Explorer.snk</AssemblyOriginatorKeyFile>
2125
</PropertyGroup>
2226

2327
<!--
596 Bytes
Binary file not shown.

src/Arius.Explorer/Settings/ApplicationSettings.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public class ApplicationSettings : ApplicationSettingsBase, IApplicationSettings
1414
{
1515
private static ApplicationSettings? defaultInstance;
1616

17-
private bool upgradeChecked;
18-
1917
public static ApplicationSettings Default
2018
{
2119
get
@@ -28,34 +26,6 @@ public static ApplicationSettings Default
2826
}
2927
}
3028

31-
public ApplicationSettings()
32-
{
33-
EnsureUpgraded();
34-
}
35-
36-
private void EnsureUpgraded()
37-
{
38-
if (upgradeChecked)
39-
{
40-
return;
41-
}
42-
43-
upgradeChecked = true;
44-
45-
var currentVersion = typeof(ApplicationSettings)
46-
.Assembly
47-
.GetName()
48-
.Version?
49-
.ToString() ?? string.Empty;
50-
51-
if (!string.Equals(LastUpgradedVersion, currentVersion, StringComparison.Ordinal))
52-
{
53-
Upgrade();
54-
LastUpgradedVersion = currentVersion;
55-
Save();
56-
}
57-
}
58-
5929
[UserScopedSetting]
6030
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
6131
public ObservableCollection<RepositoryOptions> RecentRepositories
@@ -74,11 +44,11 @@ public int RecentLimit
7444
}
7545

7646
[UserScopedSetting]
77-
[DefaultSettingValue("")]
78-
public string LastUpgradedVersion
47+
[DefaultSettingValue("True")]
48+
public bool UpgradeRequired
7949
{
80-
get => (string)(this[nameof(LastUpgradedVersion)] ?? string.Empty);
81-
set => this[nameof(LastUpgradedVersion)] = value;
50+
get => (bool)(this[nameof(UpgradeRequired)] ?? true);
51+
set => this[nameof(UpgradeRequired)] = value;
8252
}
8353
}
8454

0 commit comments

Comments
 (0)