Skip to content

Commit

Permalink
chore: Adjusting native vs application
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Feb 20, 2025
1 parent 2e36391 commit 576040b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Windows.Storage;
/// </remarks>
public partial class ApplicationDataContainer : IDisposable
{
private const string InternalSettingPrefix = "__";
internal const string InternalSettingPrefix = "__";
private const string ContainerSeparator = "¬";
private const string ContainerListKey = InternalSettingPrefix + "UnoContainers";

Expand All @@ -42,6 +42,8 @@ internal ApplicationDataContainer(ApplicationDataContainer parent, string name)

internal string ContainerPath => _parent is null ? "" : _parent.ContainerPath + InternalSettingPrefix + Name + ContainerSeparator;

internal string GetSettingKey(string key) => ContainerPath + key;

public ApplicationDataLocality Locality { get; }

public string Name { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Uno.Extensions.Specialized;
using Uno.Storage;
using Windows.Foundation.Collections;

Expand Down Expand Up @@ -36,8 +38,8 @@ internal ApplicationDataContainerSettings(ApplicationDataContainer container, Ap
/// <returns></returns>
public object this[string key]
{
get => _nativeApplicationSettings[key];
set => _nativeApplicationSettings[key] = value;
get => _nativeApplicationSettings[_container.GetSettingKey(key)];
set => _nativeApplicationSettings[_container.GetSettingKey(key)] = value;
}

/// <summary>
Expand All @@ -49,19 +51,22 @@ public int Count
{
get
{
throw new global::System.NotSupportedException();
// Public settings count is equal to the total number of settings in the container excluding internal settings.
var allSettingsCount = _nativeApplicationSettings.GetKeysWithPrefix(_container.ContainerPath).Count();
var internalSettingsPath = _container.GetSettingKey(ApplicationDataContainer.InternalSettingPrefix);
var internalSettingsCount = _nativeApplicationSettings.GetKeysWithPrefix(internalSettingsPath).Count();

return allSettingsCount - internalSettingsCount;
}
}

public bool IsReadOnly => false;

public global::System.Collections.Generic.ICollection<string> Keys
{
get
{
throw new global::System.NotSupportedException();
}
}
public ICollection<string> Keys => _nativeApplicationSettings
.GetKeysWithPrefix(_container.ContainerPath)
.Except(
_nativeApplicationSettings.GetKeysWithPrefix(_container.GetSettingKey(ApplicationDataContainer.InternalSettingPrefix))
);

public global::System.Collections.Generic.ICollection<object> Values
{
Expand Down Expand Up @@ -139,4 +144,8 @@ internal void ClearIncludingInternal()
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private object DeserializeValue(string value) => DataTypeSerializer.Deserialize(value);

private string SerializeValue(object value) => DataTypeSerializer.Serialize(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Uno.Storage;
internal interface INativeApplicationSettings
{
object this[string key] { get; set; }
ICollection<string> Keys { get; }
ICollection<object> Values { get; }

IEnumerable<string> Keys { get; }


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Windows.Storage;

Expand Down Expand Up @@ -45,7 +44,7 @@ public object? this[string key]
{
if (TryGetSetting(key, out var value))
{
return DataTypeSerializer.Deserialize(value);
return DeserializeValue(value);
}

return null;
Expand All @@ -54,7 +53,7 @@ public object? this[string key]
{
if (value is not null)
{
SetSetting(key, DataTypeSerializer.Serialize(value));
SetSetting(key, SerializeValue(value));
}
else
{
Expand All @@ -67,8 +66,10 @@ public object? this[string key]

private partial bool TryGetSetting(string key, out string? value);

internal IEnumerable<string> GetKeysWithPrefix(string prefix)
{
return Keys.Where(kvp => kvp.StartsWith(prefix, StringComparison.InvariantCulture));
}
internal IEnumerable<string> GetKeysWithPrefix(string prefix) =>
Keys.Where(kvp => kvp.StartsWith(prefix, StringComparison.InvariantCulture));

private object? DeserializeValue(string? value) => DataTypeSerializer.Deserialize(value);

private string SerializeValue(object value) => DataTypeSerializer.Serialize(value);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
#nullable enable

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#nullable enable

using Uno.Foundation.Logging;
using Windows.Storage;

Expand All @@ -16,8 +16,8 @@ partial class NativeApplicationSettings
private static partial bool SupportsLocality() => true;

private readonly Dictionary<string, string> _values = new();
private string _folderPath;
private string _filePath;
private string _folderPath = null!;
private string _filePath = null!;

partial void InitializePlatform()
{
Expand All @@ -31,12 +31,6 @@ partial void InitializePlatform()

public ICollection<string> Keys => _values.Keys;

public ICollection<object> Values
=> _values.Values.Select(DataTypeSerializer.Deserialize).ToList();

public int Count
=> _values.Count;

public void Add(string key, object value)
{
if (ContainsKey(key))
Expand Down Expand Up @@ -94,7 +88,6 @@ private void ReadFromFile()
{
try
{

if (File.Exists(_filePath))
{
using (var reader = new BinaryReader(File.OpenRead(_filePath)))
Expand Down

0 comments on commit 576040b

Please sign in to comment.