Skip to content

Commit

Permalink
Added Windows limitations for LocalSettings
Browse files Browse the repository at this point in the history
Fixed #64
  • Loading branch information
AndreasReitberger committed Sep 17, 2024
1 parent 2922e0e commit dce8c56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<WindowsSdkPackageVersion>10.0.19041.41</WindowsSdkPackageVersion>

<Version>1.0.9</Version>
<Version>1.0.10</Version>
<PackageIcon>ar_128.png</PackageIcon>
<NeutralLanguage>en</NeutralLanguage>
<PackageProjectUrl>https://github.com/AndreasReitberger/MauiSettings</PackageProjectUrl>
Expand Down
33 changes: 31 additions & 2 deletions src/MauiSettings/Helper/MauiSettingsHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
using Newtonsoft.Json;
#if WINDOWS
using System.Text;
#endif

namespace AndreasReitberger.Maui.Helper
{
internal class MauiSettingsHelper
{
/*
LocalSettings restricts the preference key names to 255 characters or less. Each preference value can be up to 8K bytes in size,
and each composite setting can be up to 64 K bytes in size.
*/
public static int MaxKeyLength { get; set; } = 255;
public static int MaxContentSize { get; set; } = 8 * 1024;
#region Methods
// Docs: https://docs.microsoft.com/en-us/dotnet/maui/platform-integration/storage/preferences
/*
Expand All @@ -18,6 +27,10 @@ internal class MauiSettingsHelper
*/
public static T? GetSettingsValue<T>(string key, Type? targetType, T? defaultValue)
{
#if WINDOWS
ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
#endif
object? returnValue = null;
if (targetType != defaultValue?.GetType())
{
Expand Down Expand Up @@ -81,6 +94,10 @@ internal class MauiSettingsHelper
[Obsolete("Use the new method with the `targetType` parameter instead")]
public static T? GetSettingsValue<T>(string key, T defaultValue)
{
#if WINDOWS
ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
#endif
object? returnValue = null;
try
{
Expand Down Expand Up @@ -150,6 +167,10 @@ public static async Task<string> GetSecureSettingsValueAsync(string key, string?

public static void SetSettingsValue(string key, object? value)
{
#if WINDOWS
ArgumentOutOfRangeException.ThrowIfNullOrEmpty(key, nameof(key));
ArgumentOutOfRangeException.ThrowIfGreaterThan(key.Length, MaxKeyLength, nameof(key));
#endif
switch (value)
{
case bool b:
Expand All @@ -176,7 +197,15 @@ public static void SetSettingsValue(string key, object? value)
default:
// For all other types try to serialize it as JSON
string? jsonString = JsonConvert.SerializeObject(value, Formatting.Indented);
Preferences.Set(key, jsonString);
if (!string.IsNullOrWhiteSpace(jsonString))
{
#if WINDOWS && DEBUG
// For testing, at the moment only for debugging
byte[] bytes = Encoding.Default.GetBytes(jsonString);
ArgumentOutOfRangeException.ThrowIfGreaterThan(bytes.LongLength, MaxContentSize, nameof(value));
#endif
Preferences.Set(key, jsonString);
}
break;
}
}
Expand All @@ -194,6 +223,6 @@ public static async Task SetSecureSettingsValueAsync(string key, string value)

public static void ClearSecureSettings() => SecureStorage.Default.RemoveAll();

#endregion
#endregion
}
}

0 comments on commit dce8c56

Please sign in to comment.