Skip to content

Refactor Application Settings and Support for Packaged/Unpackaged Mode #1924 #2014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions WinUIGallery/Controls/PageHeader.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void UserControl_Loaded(object sender, RoutedEventArgs e)
}
if (Item != null)
{
FavoriteButton.IsChecked = SettingsHelper.Contains(SettingsKeys.Favorites, Item.UniqueId);
FavoriteButton.IsChecked = SettingsHelper.Current.Favorites.Contains(Item.UniqueId);
}
}

Expand All @@ -121,11 +121,11 @@ private void FavoriteButton_Click(object sender, RoutedEventArgs e)
{
if (toggleButton.IsChecked == true)
{
SettingsHelper.TryAddItem(SettingsKeys.Favorites, Item.UniqueId, InsertPosition.Last);
SettingsHelper.Current.UpdateFavorites(items => items.AddToLast(Item.UniqueId));
}
else
{
SettingsHelper.TryRemoveItem(SettingsKeys.Favorites, Item.UniqueId);
SettingsHelper.Current.UpdateFavorites(items => items.Remove(Item.UniqueId));
}
}
}
Expand Down
27 changes: 2 additions & 25 deletions WinUIGallery/Helpers/NavigationOrientationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,20 @@
// Licensed under the MIT License.

using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.Storage;

namespace WinUIGallery.Helpers;

public static partial class NavigationOrientationHelper
{
private static bool _isLeftMode = true;
private static ApplicationData appData = ApplicationData.GetDefault();
public static bool IsLeftMode()
{
if (NativeMethods.IsAppPackaged)
{
var valueFromSettings = appData.LocalSettings.Values[SettingsKeys.IsLeftMode];
if (valueFromSettings == null)
{
appData.LocalSettings.Values[SettingsKeys.IsLeftMode] = true;
valueFromSettings = true;
}
return (bool)valueFromSettings;
}
else
{
return _isLeftMode;
}
return SettingsHelper.Current.IsLeftMode;
}

public static void IsLeftModeForElement(bool isLeftMode)
{
UpdateNavigationViewForElement(isLeftMode);
if (NativeMethods.IsAppPackaged)
{
appData.LocalSettings.Values[SettingsKeys.IsLeftMode] = isLeftMode;
}
else
{
_isLeftMode = isLeftMode;
}
SettingsHelper.Current.IsLeftMode = isLeftMode;
}

public static void UpdateNavigationViewForElement(bool isLeftMode)
Expand Down
28 changes: 2 additions & 26 deletions WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,16 @@ namespace WinUIGallery.Helpers;
/// </summary>
public static partial class ProtocolActivationClipboardHelper
{
private static bool _showCopyLinkTeachingTip = true;
private static ApplicationData appData = ApplicationData.GetDefault();

public static bool ShowCopyLinkTeachingTip
{
get
{
if (NativeMethods.IsAppPackaged)
{
object valueFromSettings = appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip];
if (valueFromSettings == null)
{
appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip] = true;
valueFromSettings = true;
}
return (bool)valueFromSettings;
}
else
{
return _showCopyLinkTeachingTip;
}
return SettingsHelper.Current.IsShowCopyLinkTeachingTip;
}

set
{
if (NativeMethods.IsAppPackaged)
{
appData.LocalSettings.Values[SettingsKeys.ShowCopyLinkTeachingTip] = value;

}
else
{
_showCopyLinkTeachingTip = value;
}
SettingsHelper.Current.IsShowCopyLinkTeachingTip = value;
}
}

Expand Down
192 changes: 0 additions & 192 deletions WinUIGallery/Helpers/SettingsHelper.cs

This file was deleted.

34 changes: 34 additions & 0 deletions WinUIGallery/Helpers/SettingsHelper/Internals/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;

namespace WinUIGallery.Helpers;

internal static class ListExtensions
{
public static void AddToFirst<T>(this List<T> list, T item, int? maxSize = null)
{
if (item == null || (item is string data && string.IsNullOrWhiteSpace(data)))
return;

list.Remove(item);
list.Insert(0, item);

if (maxSize.HasValue && maxSize.Value > 0 && list.Count > maxSize.Value)
{
list.RemoveRange(maxSize.Value, list.Count - maxSize.Value);
}
}

public static void AddToLast<T>(this List<T> list, T item, int? maxSize = null)
{
if (item == null || (item is string data && string.IsNullOrWhiteSpace(data)))
return;

list.Remove(item);
list.Add(item);

if (maxSize.HasValue && maxSize.Value > 0 && list.Count > maxSize.Value)
{
list.RemoveRange(0, list.Count - maxSize.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace WinUIGallery.Helpers;

[JsonSourceGenerationOptions(WriteIndented = true, GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Dictionary<string, System.Text.Json.JsonElement>))]
[JsonSerializable(typeof(SettingsHelper))]
internal partial class SettingsJsonContext : JsonSerializerContext
{
}
43 changes: 43 additions & 0 deletions WinUIGallery/Helpers/SettingsHelper/ObservableSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WinUIGallery.Helpers;

public partial class ObservableSettings : INotifyPropertyChanged
{
private readonly ISettingsProvider provider;

public ObservableSettings(ISettingsProvider provider)
{
this.provider = provider;
}

public event PropertyChangedEventHandler PropertyChanged;

protected bool Set<T>(T value, [CallerMemberName] string propertyName = null)
{
if (provider.Contains(propertyName))
{
var currentValue = provider.Get<T>(propertyName);
if (Equals(currentValue, value))
return false;
}

provider.Set(propertyName, value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}

protected T Get<T>([CallerMemberName] string propertyName = null)
{
return provider.Get<T>(propertyName);
}

protected T GetOrCreateDefault<T>(T defaultValue, [CallerMemberName] string propertyName = null)
{
if (!provider.Contains(propertyName))
Set(defaultValue, propertyName);

return Get<T>(propertyName);
}
}
Loading