Skip to content
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

ObjectStorage enhancements #4102

Merged
57 commits merged into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
223437c
Deprecated existing ObjectStorage classes and migrated some to the *.…
shweaver-MSFT Jul 8, 2021
5629f4d
Code style tweak
shweaver-MSFT Jul 8, 2021
c26c3f1
Updated headers and bad namespace
shweaver-MSFT Jul 8, 2021
5b26ce8
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 8, 2021
41da215
Fixed possible null reference warnings
shweaver-MSFT Jul 8, 2021
8948003
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 8, 2021
e809fab
Fixed another potential null
shweaver-MSFT Jul 8, 2021
4eaceec
Updated storage related unit tests
shweaver-MSFT Jul 9, 2021
be6555d
Updated ambiguous references in comments
shweaver-MSFT Jul 9, 2021
d9e43fb
Updated ObjectStorage references in samples
shweaver-MSFT Jul 12, 2021
c6184ed
Update Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/IObjectSerializer.cs
shweaver-MSFT Jul 13, 2021
870c85f
Update Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/SystemSerializer.cs
shweaver-MSFT Jul 13, 2021
e36c955
PR updates
shweaver-MSFT Jul 14, 2021
09cf213
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 14, 2021
7edade1
Renamed SaveFile/FolderAsync to CreateFile/FolderAsync
shweaver-MSFT Jul 14, 2021
bf97b45
Readded SaveFileAsync to match previous implementation
shweaver-MSFT Jul 14, 2021
cf294f2
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 16, 2021
5707c6d
Fixed typo in IObjectStorageHelper. Updated var name to be more accur…
shweaver-MSFT Jul 16, 2021
5927987
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 16, 2021
f70e369
Updated storage var names to be consistent across solution
shweaver-MSFT Jul 19, 2021
fd8a1fd
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 19, 2021
1f53d9a
Making serializer optional for ApplicationDataStorageHelper and defau…
shweaver-MSFT Jul 19, 2021
8140bff
Added support for the LocalCacheFolder to validate extensibility of A…
shweaver-MSFT Jul 19, 2021
949e820
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 19, 2021
ddcf8c2
Adding license header
shweaver-MSFT Jul 19, 2021
28ee577
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 19, 2021
655caf6
Whitespace fix
shweaver-MSFT Jul 19, 2021
1869039
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 20, 2021
a0e4e49
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 21, 2021
8476f70
PR updates
shweaver-MSFT Jul 21, 2021
e921d89
Updated ISettingsStorageHelper references
shweaver-MSFT Jul 22, 2021
57d872b
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 22, 2021
3e083a3
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 22, 2021
e61c0cd
Update Microsoft.Toolkit/Helpers/ObjectStorage/IFileStorageHelper.cs
shweaver-MSFT Jul 22, 2021
0e414ec
Removed default param from ISettingsStorageHelper.Read method and upd…
shweaver-MSFT Jul 22, 2021
11cab80
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 22, 2021
51d58cd
Updated JsonObjectSerializer and SystemTextJsonSerializer in Uwp test…
shweaver-MSFT Jul 22, 2021
522d55b
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 22, 2021
27cae2e
Added extension methods for ISettingsStorageHelper
shweaver-MSFT Jul 22, 2021
5fd25ce
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 22, 2021
22068f1
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 22, 2021
9504193
Merge branch 'main' into shweaver/storage-helpers
michael-hawker Jul 23, 2021
3f3868a
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 23, 2021
13c3e2f
Update Microsoft.Toolkit/Helpers/ObjectStorage/ISettingsStorageHelper.cs
shweaver-MSFT Jul 26, 2021
b472cc8
PR updates
shweaver-MSFT Jul 26, 2021
c36db17
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 26, 2021
82c2ba3
PR updates
shweaver-MSFT Jul 26, 2021
f3080d0
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 26, 2021
dc218aa
The ever important file header!
shweaver-MSFT Jul 26, 2021
93efeaf
Merge branch 'shweaver/storage-helpers' of https://github.com/windows…
shweaver-MSFT Jul 26, 2021
f4207ea
Added Obsolete attributes to old storage helper tests
shweaver-MSFT Jul 27, 2021
331f7dd
Comment cref updates in test app
shweaver-MSFT Jul 27, 2021
11d1a5e
PR updates
shweaver-MSFT Jul 28, 2021
b551d43
Update Microsoft.Toolkit/Extensions/ISettingsStorageHelperExtensions.cs
shweaver-MSFT Jul 28, 2021
e65332b
Merge branch 'main' into shweaver/storage-helpers
shweaver-MSFT Jul 28, 2021
76aa847
Update Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataSto…
shweaver-MSFT Jul 29, 2021
b0a33c5
Fixed warnings in ObjectStorage classes
shweaver-MSFT Jul 29, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Parsers.Markdown;
using Microsoft.Toolkit.Parsers.Markdown.Blocks;
using Microsoft.Toolkit.Parsers.Markdown.Inlines;
Expand Down Expand Up @@ -407,19 +408,19 @@ public string DesiredLang
{
get
{
return storage.Read<string>(DesiredLangKey);
return settingsStorage.Read<string>(DesiredLangKey);
}

set
{
storage.Save(DesiredLangKey, value);
settingsStorage.Save(DesiredLangKey, value);
}
}

/// <summary>
/// The Local Storage Helper.
/// The local app data storage helper for storing settings.
/// </summary>
private LocalObjectStorageHelper storage = new LocalObjectStorageHelper(new SystemSerializer());
private readonly ApplicationDataStorageHelper settingsStorage = ApplicationDataStorageHelper.GetCurrent();

/// <summary>
/// DocFX note types and styling info, keyed by identifier.
Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/Models/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// TODO Reintroduce graph controls
// using Microsoft.Toolkit.Graph.Converters;
// using Microsoft.Toolkit.Graph.Providers;
using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Uwp.Helpers;
using Microsoft.Toolkit.Uwp.Input.GazeInteraction;
using Microsoft.Toolkit.Uwp.SampleApp.Models;
Expand All @@ -45,7 +46,7 @@ public class Sample

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

var onlineDocsSHA = await GetDocsSHA();
var cacheSHA = settingsStorage.Read<string>(_cacheSHAKey);
Expand Down
7 changes: 4 additions & 3 deletions Microsoft.Toolkit.Uwp.SampleApp/Models/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Uwp.Helpers;

namespace Microsoft.Toolkit.Uwp.SampleApp
Expand All @@ -21,7 +22,7 @@ public static class Samples
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1);

private static LinkedList<Sample> _recentSamples;
private static LocalObjectStorageHelper _localObjectStorageHelper = new LocalObjectStorageHelper(new SystemSerializer());
private static ApplicationDataStorageHelper _settingsStorage = ApplicationDataStorageHelper.GetCurrent();

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

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

var str = string.Join(";", _recentSamples.Take(10).Select(s => s.Name).ToArray());
_localObjectStorageHelper.Save<string>(_recentSamplesStorageKey, str);
_settingsStorage.Save<string>(_recentSamplesStorageKey, str);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
var localObjectStorageHelper = new LocalObjectStorageHelper();
var roamingObjectStorageHelper = new RoamingObjectStorageHelper();
ApplicationDataStorageHelper appDataStorageHelper = ApplicationDataStorageHelper.GetCurrent(new Toolkit.Helpers.SystemSerializer());

// Read and Save with simple objects
string keySimpleObject = "simple";
string result = localObjectStorageHelper.Read<string>(keySimpleObject);
localObjectStorageHelper.Save(keySimpleObject, 47);
string result = appDataStorageHelper.Read<string>(keySimpleObject);
appDataStorageHelper.Save(keySimpleObject, 47);

// Read and Save with complex/large objects
string keyLargeObject = "large";
var result = localObjectStorageHelper.ReadFileAsync<MyLargeObject>(keyLargeObject);
string complexObjectKey = "complexObject";
var complexObject = await appDataStorageHelper.ReadFileAsync<MyLargeObject>(complexObjectKey);

var o = new MyLargeObject
var myComplexObject = new MyComplexObject()
{
...
};
localObjectStorageHelper.SaveFileAsync(keySimpleObject, o);
await appDataStorageHelper.SaveFileAsync(complexObjectKey, myComplexObject);

// Complex object
public class MyLargeObject
public class MyComplexObject
{
public string MyContent { get; set; }
public List<string> MyContents { get; set; }
public List<MyLargeObject> MyObjects { get; set; }
public List<MyComplexObject> MyObjects { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Toolkit.Helpers;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.UI.Xaml;

namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
public sealed partial class ObjectStoragePage
{
private readonly IObjectStorageHelper localStorageHelper = new LocalObjectStorageHelper(new SystemSerializer());
private readonly ApplicationDataStorageHelper _settingsStorage = ApplicationDataStorageHelper.GetCurrent();

public ObjectStoragePage()
{
Expand All @@ -24,9 +25,9 @@ private void ReadButton_Click(object sender, RoutedEventArgs e)
}

// Read from local storage
if (localStorageHelper.KeyExists(KeyTextBox.Text))
if (_settingsStorage.KeyExists(KeyTextBox.Text))
{
ContentTextBox.Text = localStorageHelper.Read<string>(KeyTextBox.Text);
ContentTextBox.Text = _settingsStorage.Read<string>(KeyTextBox.Text);
}
}

Expand All @@ -43,7 +44,7 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
}

// Save into local storage
localStorageHelper.Save(KeyTextBox.Text, ContentTextBox.Text);
_settingsStorage.Save(KeyTextBox.Text, ContentTextBox.Text);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Toolkit.Helpers;
using Windows.Storage;

namespace Microsoft.Toolkit.Uwp.Helpers
{
/// <summary>
/// An extension of ApplicationDataStorageHelper with additional features for interop with the LocalCacheFolder.
/// </summary>
public partial class ApplicationDataStorageHelper
{
/// <summary>
/// Gets the local cache folder.
/// </summary>
public StorageFolder CacheFolder => AppData.LocalCacheFolder;

/// <summary>
/// Retrieves an object from a file in the LocalCacheFolder.
/// </summary>
/// <typeparam name="T">Type of object retrieved.</typeparam>
/// <param name="filePath">Path to the file that contains the object.</param>
/// <param name="default">Default value of the object.</param>
/// <returns>Waiting task until completion with the object in the file.</returns>
public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
{
return ReadFileAsync<T>(CacheFolder, filePath, @default);
}

/// <summary>
/// Retrieves the listings for a folder and the item types in the LocalCacheFolder.
/// </summary>
/// <param name="folderPath">The path to the target folder.</param>
/// <returns>A list of file types and names in the target folder.</returns>
public Task<IEnumerable<(DirectoryItemType ItemType, string Name)>> ReadCacheFolderAsync(string folderPath)
{
return ReadFolderAsync(CacheFolder, folderPath);
}

/// <summary>
/// Saves an object inside a file in the LocalCacheFolder.
/// </summary>
/// <typeparam name="T">Type of object saved.</typeparam>
/// <param name="filePath">Path to the file that will contain the object.</param>
/// <param name="value">Object to save.</param>
/// <returns>Waiting task until completion.</returns>
public Task CreateCacheFileAsync<T>(string filePath, T value)
{
return SaveFileAsync<T>(CacheFolder, filePath, value);
}

/// <summary>
/// Ensure a folder exists at the folder path specified in the LocalCacheFolder.
/// </summary>
/// <param name="folderPath">The path and name of the target folder.</param>
/// <returns>Waiting task until completion.</returns>
public Task CreateCacheFolderAsync(string folderPath)
{
return CreateFolderAsync(CacheFolder, folderPath);
}

/// <summary>
/// Deletes a file or folder item in the LocalCacheFolder.
/// </summary>
/// <param name="itemPath">The path to the item for deletion.</param>
/// <returns>Waiting task until completion.</returns>
public Task DeleteCacheItemAsync(string itemPath)
{
return DeleteItemAsync(CacheFolder, itemPath);
}
}
}
Loading