Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GazeInputTest/GazeInputTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.19041.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public string DesiredLang
/// <summary>
/// The Local Storage Helper.
/// </summary>
private LocalObjectStorageHelper storage = new LocalObjectStorageHelper();
private LocalObjectStorageHelper storage = new LocalObjectStorageHelper(new SystemSerializer());

/// <summary>
/// DocFX note types and styling info, keyed by identifier.
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Sample

public static async void EnsureCacheLatest()
{
var settingsStorage = new LocalObjectStorageHelper();
var settingsStorage = new LocalObjectStorageHelper(new SystemSerializer());

var onlineDocsSHA = await GetDocsSHA();
var cacheSHA = settingsStorage.Read<string>(_cacheSHAKey);
Expand Down
6 changes: 3 additions & 3 deletions Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Samples
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1);

private static LinkedList<Sample> _recentSamples;
private static RoamingObjectStorageHelper _roamingObjectStorageHelper = new RoamingObjectStorageHelper();
private static LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(new SystemSerializer());

public static async Task<SampleCategory> GetCategoryBySample(Sample sample)
{
Expand Down Expand Up @@ -98,7 +98,7 @@ public static async Task<LinkedList<Sample>> GetRecentSamples()
if (_recentSamples == null)
{
_recentSamples = new LinkedList<Sample>();
var savedSamples = _roamingObjectStorageHelper.Read<string>(_recentSamplesStorageKey);
var savedSamples = _localObjectStorageHelper.Read<string>(_recentSamplesStorageKey);

if (savedSamples != null)
{
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void SaveRecentSamples()
}

var str = string.Join(";", _recentSamples.Take(10).Select(s => s.Name).ToArray());
_roamingObjectStorageHelper.Save<string>(_recentSamplesStorageKey, str);
_localObjectStorageHelper.Save<string>(_recentSamplesStorageKey, str);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
<Page
x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.ObjectStoragePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.ObjectStoragePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<ScrollViewer>
<StackPanel Margin="20">
<ToggleSwitch x:Name="StorageModeToggle" HorizontalAlignment="Center"
OnContent="Roaming storage (save across all your devices)"
OffContent="Local storage (save on this device only)" />
<TextBox x:Name="KeyTextBox"
Margin="0,10,0,0"
PlaceholderText="Key" />

<TextBox x:Name="KeyTextBox" PlaceholderText="Key" Margin="0 10 0 0" />
<TextBox x:Name="ContentTextBox"
Margin="0,10,0,0"
PlaceholderText="Content to save" />

<TextBox x:Name="ContentTextBox" PlaceholderText="Content to save" Margin="0 10 0 0" />

<StackPanel Margin="0 10 0 0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="ReadButton" Content="Read from key" HorizontalAlignment="Center" Margin="5 0"
Click="ReadButton_Click" />
<Button x:Name="SaveButton" Content="Save" HorizontalAlignment="Center" Margin="5 0"
Click="SaveButton_Click" />
<StackPanel Margin="0,10,0,0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button x:Name="ReadButton"
Margin="5,0"
HorizontalAlignment="Center"
Click="ReadButton_Click"
Content="Read from key" />
<Button x:Name="SaveButton"
Margin="5,0"
HorizontalAlignment="Center"
Click="SaveButton_Click"
Content="Save" />
</StackPanel>
</StackPanel>
</ScrollViewer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
public sealed partial class ObjectStoragePage
{
private readonly IObjectStorageHelper localStorageHelper = new LocalObjectStorageHelper();
private readonly IObjectStorageHelper roamingStorageHelper = new RoamingObjectStorageHelper();
private readonly IObjectStorageHelper localStorageHelper = new LocalObjectStorageHelper(new SystemSerializer());

public ObjectStoragePage()
{
Expand All @@ -24,21 +23,10 @@ private void ReadButton_Click(object sender, RoutedEventArgs e)
return;
}

if (StorageModeToggle.IsOn)
// Read from local storage
if (localStorageHelper.KeyExists(KeyTextBox.Text))
{
// Read from roaming storage
if (roamingStorageHelper.KeyExists(KeyTextBox.Text))
{
ContentTextBox.Text = roamingStorageHelper.Read<string>(KeyTextBox.Text);
}
}
else
{
// Read from local storage
if (localStorageHelper.KeyExists(KeyTextBox.Text))
{
ContentTextBox.Text = localStorageHelper.Read<string>(KeyTextBox.Text);
}
ContentTextBox.Text = localStorageHelper.Read<string>(KeyTextBox.Text);
}
}

Expand All @@ -54,16 +42,8 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
return;
}

if (StorageModeToggle.IsOn)
{
// Save into roaming storage
roamingStorageHelper.Save(KeyTextBox.Text, ContentTextBox.Text);
}
else
{
// Save into local storage
localStorageHelper.Save(KeyTextBox.Text, ContentTextBox.Text);
}
// Save into local storage
localStorageHelper.Save(KeyTextBox.Text, ContentTextBox.Text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public abstract class BaseObjectStorageHelper : IObjectStorageHelper
/// <summary>
/// Initializes a new instance of the <see cref="BaseObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public BaseObjectStorageHelper(IObjectSerializer objectSerializer = null)
public BaseObjectStorageHelper(IObjectSerializer objectSerializer)
{
serializer = objectSerializer ?? new JsonObjectSerializer();
serializer = objectSerializer ?? throw new ArgumentNullException(nameof(objectSerializer));
}

/// <summary>
Expand Down Expand Up @@ -85,15 +84,7 @@ public bool KeyExists(string compositeKey, string key)
return @default;
}

var type = typeof(T);
var typeInfo = type.GetTypeInfo();

if (typeInfo.IsPrimitive || type == typeof(string))
{
return (T)Convert.ChangeType(value, type);
}

return serializer.Deserialize<T>((string)value);
return serializer.Deserialize<T>(value);
}

/// <summary>
Expand Down Expand Up @@ -132,14 +123,7 @@ public void Save<T>(string key, T value)
var type = typeof(T);
var typeInfo = type.GetTypeInfo();

if (typeInfo.IsPrimitive || type == typeof(string))
{
Settings.Values[key] = value;
}
else
{
Settings.Values[key] = serializer.Serialize(value);
}
Settings.Values[key] = serializer.Serialize(value);
}

/// <summary>
Expand Down Expand Up @@ -214,7 +198,7 @@ public Task<bool> FileExistsAsync(string filePath)
/// <returns>The <see cref="StorageFile"/> where the object was saved</returns>
public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
{
return StorageFileHelper.WriteTextToFileAsync(Folder, serializer.Serialize(value), filePath, CreationCollisionOption.ReplaceExisting);
return StorageFileHelper.WriteTextToFileAsync(Folder, serializer.Serialize(value)?.ToString(), filePath, CreationCollisionOption.ReplaceExisting);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace Microsoft.Toolkit.Uwp.Helpers
public interface IObjectSerializer
{
/// <summary>
/// Serialize an object into a string.
/// Serialize an object into a string. It is recommended to use strings as the final format for objects if you plan to use the <see cref="BaseObjectStorageHelper.SaveFileAsync{T}(string, T)"/> method.
/// </summary>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <param name="value">The object to serialize.</param>
/// <returns>The serialized object.</returns>
string Serialize<T>(T value);
object Serialize<T>(T value);

/// <summary>
/// Deserialize a string into an object.
/// Deserialize a primitive or string into an object of the given type.
/// </summary>
/// <typeparam name="T">The type of the deserialized object.</typeparam>
/// <param name="value">The string to deserialize.</param>
/// <returns>The deserialized object.</returns>
T Deserialize<T>(string value);
T Deserialize<T>(object value);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ public class LocalObjectStorageHelper : BaseObjectStorageHelper
/// <summary>
/// Initializes a new instance of the <see cref="LocalObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json.
/// To implement an <see cref="IObjectSerializer"/> based on System.Text.Json, Newtonsoft.Json, or DataContractJsonSerializer see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public LocalObjectStorageHelper(IObjectSerializer objectSerializer = null)
public LocalObjectStorageHelper(IObjectSerializer objectSerializer)
: base(objectSerializer)
{
Settings = ApplicationData.Current.LocalSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.Serialization;
using Windows.Storage;

Expand All @@ -10,17 +11,17 @@ namespace Microsoft.Toolkit.Uwp.Helpers
/// <summary>
/// Store data in the Roaming environment (shared across all user devices).
/// </summary>
[Obsolete("Package State Roaming will be removed in a futures Windows Update, see https://docs.microsoft.com/windows/deployment/planning/windows-10-deprecated-features for more information.")]
public class RoamingObjectStorageHelper : BaseObjectStorageHelper
{
/// <summary>
/// Initializes a new instance of the <see cref="RoamingObjectStorageHelper"/> class,
/// which can read and write data using the provided <see cref="IObjectSerializer"/>;
/// if none is provided, a default Json serializer will be used (based on <see cref="DataContractSerializer"/>).
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json and the new default Serializer may behave differently.
/// To implement a <see cref="IObjectSerializer"/> based on Newtonsoft.Json or System.Text.Json see https://aka.ms/wct/storagehelper-migration
/// In 6.1 and older the default Serializer was based on Newtonsoft.Json.
/// To implement an <see cref="IObjectSerializer"/> based on System.Text.Json, Newtonsoft.Json, or DataContractJsonSerializer see https://aka.ms/wct/storagehelper-migration
/// </summary>
/// <param name="objectSerializer">The serializer to use.</param>
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer = null)
public RoamingObjectStorageHelper(IObjectSerializer objectSerializer)
: base(objectSerializer)
{
Settings = ApplicationData.Current.RoamingSettings;
Expand Down
48 changes: 48 additions & 0 deletions Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/SystemSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Reflection;
using Microsoft.Toolkit.Diagnostics;
using Windows.Storage;

namespace Microsoft.Toolkit.Uwp.Helpers
{
/// <summary>
/// A bare-bones serializer which knows how to deal with primitive types and strings only. It will store them directly based on the <see cref="ApplicationDataContainer"/> API.
/// It is recommended for more complex scenarios to implement your own <see cref="IObjectSerializer"/> based on System.Text.Json, Newtonsoft.Json, or DataContractJsonSerializer see https://aka.ms/wct/storagehelper-migration
/// </summary>
public class SystemSerializer : IObjectSerializer
{
/// <summary>
/// Take a primitive value from storage and return it as the requested type using the <see cref="Convert.ChangeType(object, Type)"/> API.
/// </summary>
/// <typeparam name="T">Type to convert value to.</typeparam>
/// <param name="value">Value from storage to convert.</param>
/// <returns>Deserialized value or default value.</returns>
public T Deserialize<T>(object value)
{
var type = typeof(T);
var typeInfo = type.GetTypeInfo();

if (typeInfo.IsPrimitive || type == typeof(string))
{
return (T)Convert.ChangeType(value, type);
}

return ThrowHelper.ThrowNotSupportedException<T>("This serializer can only handle primitive types and strings. Please implement your own IObjectSerializer for more complex scenarios.");
}

/// <summary>
/// Returns the value so that it can be serialized by the <see cref="ApplicationDataContainer"/> API directly.
/// </summary>
/// <typeparam name="T">Type to serialize from.</typeparam>
/// <param name="value">Value to serialize.</param>
/// <returns>String representation of value.</returns>
public object Serialize<T>(T value)
{
return value;
}
}
}
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Uwp/Helpers/SystemInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Toolkit.Uwp.Helpers
/// </summary>
public sealed class SystemInformation
{
private readonly LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper();
private readonly LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(new SystemSerializer());
private DateTime _sessionStart;

/// <summary>
Expand Down
Loading