Skip to content

Commit 1d59dfe

Browse files
refactor: migrate to Microsoft.Windows.Storage.ApplicationData and centralize settings keys (#1900)
## Description - Replace `Windows.Storage.ApplicationData` with `Microsoft.Windows.Storage.ApplicationData`. - Centralize Settings Keys in a `SettingsKeys` Class. - define key constants in `ScratchPadPage`. ## Motivation and Context - Closes #1892 - Closes #1893 ## How Has This Been Tested? **Manually tested** ## Screenshots (if appropriate): ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) Co-authored-by: Niels Laute <[email protected]>
1 parent 3b8619e commit 1d59dfe

File tree

6 files changed

+67
-42
lines changed

6 files changed

+67
-42
lines changed

WinUIGallery/Helpers/NavigationOrientationHelper.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33

44
using Microsoft.UI.Xaml;
55
using Microsoft.UI.Xaml.Controls;
6-
using Windows.Storage;
6+
using Microsoft.Windows.Storage;
77
using WinUIGallery.Pages;
88

99
namespace WinUIGallery.Helpers;
1010

1111
public static class NavigationOrientationHelper
1212
{
13-
14-
private const string IsLeftModeKey = "NavigationIsOnLeftMode";
1513
private static bool _isLeftMode = true;
16-
14+
private static ApplicationData appData = ApplicationData.GetDefault();
1715
public static bool IsLeftMode()
1816
{
1917
if (NativeHelper.IsAppPackaged)
2018
{
21-
var valueFromSettings = ApplicationData.Current.LocalSettings.Values[IsLeftModeKey];
19+
var valueFromSettings = appData.LocalSettings.Values[SettingsKeys.IsLeftMode];
2220
if (valueFromSettings == null)
2321
{
24-
ApplicationData.Current.LocalSettings.Values[IsLeftModeKey] = true;
22+
appData.LocalSettings.Values[SettingsKeys.IsLeftMode] = true;
2523
valueFromSettings = true;
2624
}
2725
return (bool)valueFromSettings;
@@ -37,7 +35,7 @@ public static void IsLeftModeForElement(bool isLeftMode, UIElement element)
3735
UpdateNavigationViewForElement(isLeftMode, element);
3836
if (NativeHelper.IsAppPackaged)
3937
{
40-
ApplicationData.Current.LocalSettings.Values[IsLeftModeKey] = isLeftMode;
38+
appData.LocalSettings.Values[SettingsKeys.IsLeftMode] = isLeftMode;
4139
}
4240
else
4341
{

WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using Windows.ApplicationModel;
66
using Windows.ApplicationModel.DataTransfer;
7-
using Windows.Storage;
7+
using Microsoft.Windows.Storage;
88
using WinUIGallery.Models;
99

1010
namespace WinUIGallery.Helpers;
@@ -14,19 +14,19 @@ namespace WinUIGallery.Helpers;
1414
/// </summary>
1515
public static class ProtocolActivationClipboardHelper
1616
{
17-
private const string ShowCopyLinkTeachingTipKey = "ShowCopyLinkTeachingTip";
1817
private static bool _showCopyLinkTeachingTip = true;
18+
private static ApplicationData appData = ApplicationData.GetDefault();
1919

2020
public static bool ShowCopyLinkTeachingTip
2121
{
2222
get
2323
{
2424
if (NativeHelper.IsAppPackaged)
2525
{
26-
object valueFromSettings = ApplicationData.Current.LocalSettings.Values[ShowCopyLinkTeachingTipKey];
26+
object valueFromSettings = appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip];
2727
if (valueFromSettings == null)
2828
{
29-
ApplicationData.Current.LocalSettings.Values[ShowCopyLinkTeachingTipKey] = true;
29+
appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip] = true;
3030
valueFromSettings = true;
3131
}
3232
return (bool)valueFromSettings;
@@ -41,7 +41,7 @@ public static bool ShowCopyLinkTeachingTip
4141
{
4242
if (NativeHelper.IsAppPackaged)
4343
{
44-
ApplicationData.Current.LocalSettings.Values[ShowCopyLinkTeachingTipKey] = value;
44+
appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip] = value;
4545

4646
}
4747
else

WinUIGallery/Helpers/SettingsHelper.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,3 @@ private static bool TrySaveList(string key, List<string> items)
190190
return true;
191191
}
192192
}
193-
194-
/// <summary>
195-
/// Provides constant keys used for accessing application settings.
196-
/// </summary>
197-
public static class SettingsKeys
198-
{
199-
/// <summary>
200-
/// Key for the list of favorited Pages.
201-
/// </summary>
202-
public const string Favorites = "Favorites";
203-
204-
/// <summary>
205-
/// Key for the list of recently visited Pages.
206-
/// </summary>
207-
public const string RecentlyVisited = "RecentlyVisited";
208-
}

WinUIGallery/Helpers/SettingsKeys.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
namespace WinUIGallery.Helpers;
4+
5+
/// <summary>
6+
/// Provides constant keys used for accessing application settings.
7+
/// </summary>
8+
public static class SettingsKeys
9+
{
10+
/// <summary>
11+
/// The key used to store or retrieve the selected application theme.
12+
/// </summary>
13+
public const string SelectedAppTheme = "SelectedAppTheme";
14+
15+
/// <summary>
16+
/// Represents the key used to determine whether the navigation is in "left mode."
17+
/// </summary>
18+
public const string IsLeftMode = "NavigationIsOnLeftMode";
19+
20+
/// <summary>
21+
/// Key for the list of recently visited Pages.
22+
/// </summary>
23+
public const string RecentlyVisited = "RecentlyVisited";
24+
25+
/// <summary>
26+
/// Key for the list of favorited Pages.
27+
/// </summary>
28+
public const string Favorites = "Favorites";
29+
30+
/// <summary>
31+
/// Represents the key used to identify the "Show Copy Link" teaching tip of the protocol activation clipboard.
32+
/// </summary>
33+
public const string ShowCopyLinkTeachingTip = "ShowCopyLinkTeachingTip";
34+
}

WinUIGallery/Helpers/ThemeHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.UI.Xaml;
5-
using Windows.Storage;
5+
using Microsoft.Windows.Storage;
66

77
namespace WinUIGallery.Helpers;
88

@@ -11,7 +11,7 @@ namespace WinUIGallery.Helpers;
1111
/// </summary>
1212
public static class ThemeHelper
1313
{
14-
private const string SelectedAppThemeKey = "SelectedAppTheme";
14+
private static ApplicationData appData = ApplicationData.GetDefault();
1515

1616
/// <summary>
1717
/// Gets the current actual theme of the app based on the requested theme of the
@@ -65,7 +65,7 @@ public static ElementTheme RootTheme
6565

6666
if (NativeHelper.IsAppPackaged)
6767
{
68-
ApplicationData.Current.LocalSettings.Values[SelectedAppThemeKey] = value.ToString();
68+
appData.LocalSettings.Values[SettingsKeys.SelectedAppTheme] = value.ToString();
6969
}
7070
}
7171
}
@@ -74,7 +74,7 @@ public static void Initialize()
7474
{
7575
if (NativeHelper.IsAppPackaged)
7676
{
77-
string savedTheme = ApplicationData.Current.LocalSettings.Values[SelectedAppThemeKey]?.ToString();
77+
string savedTheme = appData.LocalSettings.Values[SettingsKeys.SelectedAppTheme]?.ToString();
7878

7979
if (savedTheme != null)
8080
{

WinUIGallery/Samples/ControlPages/Fundamentals/ScratchPadPage.xaml.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ namespace WinUIGallery.ControlPages;
1616

1717
public sealed partial class ScratchPadPage : Page
1818
{
19+
// The name of the ApplicationData container used to store all ScratchPad-related settings.
20+
private const string containerKey = "ScratchPad";
21+
22+
// The key under the ScratchPad container for storing the XAML composite value.
23+
private const string xamlCompositeValueKey = "ScratchPadXAML";
24+
25+
// The key within the XAML composite value that stores the number of 4KB segments.
26+
private const string xamlSegmentCountKey = "count";
27+
1928
public ScratchPadPage()
2029
{
2130
this.InitializeComponent();
@@ -62,16 +71,16 @@ private string GetDefaultScratchXAML()
6271

6372
public string ReadScratchPadXAMLinLocalSettings()
6473
{
65-
var appData = ApplicationData.Current;
66-
if (appData.LocalSettings.Containers.ContainsKey("ScratchPad"))
74+
var appData = Microsoft.Windows.Storage.ApplicationData.GetDefault();
75+
if (appData.LocalSettings.Containers.ContainsKey(containerKey))
6776
{
68-
var scratchPadContainer = appData.LocalSettings.CreateContainer("ScratchPad", ApplicationDataCreateDisposition.Existing);
69-
if (scratchPadContainer != null && scratchPadContainer.Values.ContainsKey("ScratchPadXAML"))
77+
var scratchPadContainer = appData.LocalSettings.CreateContainer(containerKey, Microsoft.Windows.Storage.ApplicationDataCreateDisposition.Existing);
78+
if (scratchPadContainer != null && scratchPadContainer.Values.ContainsKey(xamlCompositeValueKey))
7079
{
7180
// String values are limited to 4K characters. Use a composite value to support a longer string.
72-
var compositeStr = scratchPadContainer.Values["ScratchPadXAML"] as ApplicationDataCompositeValue;
81+
var compositeStr = scratchPadContainer.Values[xamlCompositeValueKey] as ApplicationDataCompositeValue;
7382
var xamlStr = "";
74-
int count = (int)compositeStr["count"];
83+
int count = (int)compositeStr[xamlSegmentCountKey];
7584
for (int i = 0; i < count; i++)
7685
{
7786
xamlStr += compositeStr[i.ToString()];
@@ -84,8 +93,8 @@ public string ReadScratchPadXAMLinLocalSettings()
8493

8594
public void SaveScratchPadXAMLinLocalSettings(string xamlStr)
8695
{
87-
var appData = ApplicationData.Current;
88-
var scratchPadContainer = appData.LocalSettings.CreateContainer("ScratchPad", ApplicationDataCreateDisposition.Always);
96+
var appData = Microsoft.Windows.Storage.ApplicationData.GetDefault();
97+
var scratchPadContainer = appData.LocalSettings.CreateContainer(containerKey, Microsoft.Windows.Storage.ApplicationDataCreateDisposition.Existing);
8998
// String values are limited to 4K characters. Use a composite value to support a longer string.
9099
var compositeStr = new ApplicationDataCompositeValue();
91100
int count = 0;
@@ -96,8 +105,8 @@ public void SaveScratchPadXAMLinLocalSettings(string xamlStr)
96105
count++;
97106
xamlStr = xamlStr.Substring(len);
98107
}
99-
compositeStr["count"] = count;
100-
scratchPadContainer.Values["ScratchPadXAML"] = compositeStr;
108+
compositeStr[xamlSegmentCountKey] = count;
109+
scratchPadContainer.Values[xamlCompositeValueKey] = compositeStr;
101110
}
102111

103112
private async void ResetToDefaultClick(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)