Skip to content

Commit 11d1a5e

Browse files
committed
PR updates
1 parent 331f7dd commit 11d1a5e

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.CacheFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
3737
/// </summary>
3838
/// <param name="folderPath">The path to the target folder.</param>
3939
/// <returns>A list of file types and names in the target folder.</returns>
40-
public Task<IEnumerable<(DirectoryItemType, string)>> ReadCacheFolderAsync(string folderPath)
40+
public Task<IEnumerable<(DirectoryItemType ItemType, string Name)>> ReadCacheFolderAsync(string folderPath)
4141
{
4242
return ReadFolderAsync(CacheFolder, folderPath);
4343
}

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class ApplicationDataStorageHelper : IFileStorageHelper, ISetting
2222
/// </summary>
2323
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
2424
/// <returns>A new instance of ApplicationDataStorageHelper.</returns>
25-
public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSerializer objectSerializer = null)
25+
public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSerializer? objectSerializer = null)
2626
{
2727
var appData = ApplicationData.Current;
2828
return new ApplicationDataStorageHelper(appData, objectSerializer);
@@ -34,7 +34,7 @@ public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSer
3434
/// <param name="user">App data user owner.</param>
3535
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
3636
/// <returns>A new instance of ApplicationDataStorageHelper.</returns>
37-
public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user, Toolkit.Helpers.IObjectSerializer objectSerializer = null)
37+
public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user, Toolkit.Helpers.IObjectSerializer? objectSerializer = null)
3838
{
3939
var appData = await ApplicationData.GetForUserAsync(user);
4040
return new ApplicationDataStorageHelper(appData, objectSerializer);
@@ -65,7 +65,7 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
6565
/// </summary>
6666
/// <param name="appData">The data store to interact with.</param>
6767
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
68-
public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IObjectSerializer objectSerializer = null)
68+
public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IObjectSerializer? objectSerializer = null)
6969
{
7070
AppData = appData ?? throw new ArgumentNullException(nameof(appData));
7171
Serializer = objectSerializer ?? new Toolkit.Helpers.SystemSerializer();
@@ -137,15 +137,35 @@ public void Clear()
137137
/// <returns>True if a value exists.</returns>
138138
public bool KeyExists(string compositeKey, string key)
139139
{
140-
if (KeyExists(compositeKey))
140+
if (TryRead(compositeKey, out ApplicationDataCompositeValue composite))
141141
{
142-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
143-
if (composite != null)
142+
return composite.ContainsKey(key);
143+
}
144+
145+
return false;
146+
}
147+
148+
/// <summary>
149+
/// Attempts to retrieve a single item by its key in composite.
150+
/// </summary>
151+
/// <typeparam name="T">Type of object retrieved.</typeparam>
152+
/// <param name="compositeKey">Key of the composite (that contains settings).</param>
153+
/// <param name="key">Key of the object.</param>
154+
/// <param name="value">The value of the object retrieved.</param>
155+
/// <returns>The T object.</returns>
156+
public bool TryRead<T>(string compositeKey, string key, out T value)
157+
{
158+
if (TryRead(compositeKey, out ApplicationDataCompositeValue composite))
159+
{
160+
string compositeValue = (string)composite[key];
161+
if (compositeValue != null)
144162
{
145-
return composite.ContainsKey(key);
163+
value = Serializer.Deserialize<T>(compositeValue);
164+
return true;
146165
}
147166
}
148167

168+
value = default;
149169
return false;
150170
}
151171

@@ -159,11 +179,9 @@ public bool KeyExists(string compositeKey, string key)
159179
/// <returns>The T object.</returns>
160180
public T Read<T>(string compositeKey, string key, T @default = default)
161181
{
162-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
163-
if (composite != null)
182+
if (TryRead(compositeKey, out ApplicationDataCompositeValue composite))
164183
{
165-
string value = (string)composite[key];
166-
if (value != null)
184+
if (composite.TryGetValue(key, out object valueObj) && valueObj is string value)
167185
{
168186
return Serializer.Deserialize<T>(value);
169187
}
@@ -182,10 +200,8 @@ public T Read<T>(string compositeKey, string key, T @default = default)
182200
/// <param name="values">Objects to save.</param>
183201
public void Save<T>(string compositeKey, IDictionary<string, T> values)
184202
{
185-
if (KeyExists(compositeKey))
203+
if (TryRead(compositeKey, out ApplicationDataCompositeValue composite))
186204
{
187-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
188-
189205
foreach (KeyValuePair<string, T> setting in values)
190206
{
191207
if (composite.ContainsKey(setting.Key))
@@ -200,7 +216,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
200216
}
201217
else
202218
{
203-
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
219+
composite = new ApplicationDataCompositeValue();
204220
foreach (KeyValuePair<string, T> setting in values)
205221
{
206222
composite.Add(setting.Key, Serializer.Serialize(setting.Value));
@@ -215,20 +231,15 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
215231
/// </summary>
216232
/// <param name="compositeKey">Key of the composite (that contains settings).</param>
217233
/// <param name="key">Key of the object.</param>
218-
/// <exception cref="KeyNotFoundException">Throws when the specified composite/settings key is not found.</exception>
219-
public void Delete(string compositeKey, string key)
234+
/// <returns>A boolean indicator of success.</returns>
235+
public bool TryDelete(string compositeKey, string key)
220236
{
221-
if (!KeyExists(compositeKey))
237+
if (TryRead(compositeKey, out ApplicationDataCompositeValue composite))
222238
{
223-
throw new KeyNotFoundException($"Composite key not found: {compositeKey}");
239+
return composite.Remove(key);
224240
}
225241

226-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
227-
228-
if (!composite.Remove(key))
229-
{
230-
throw new KeyNotFoundException($"Settings key not found: {key}");
231-
}
242+
return false;
232243
}
233244

234245
/// <inheritdoc />
@@ -238,7 +249,7 @@ public Task<T> ReadFileAsync<T>(string filePath, T @default = default)
238249
}
239250

240251
/// <inheritdoc />
241-
public Task<IEnumerable<(DirectoryItemType, string)>> ReadFolderAsync(string folderPath)
252+
public Task<IEnumerable<(DirectoryItemType ItemType, string Name)>> ReadFolderAsync(string folderPath)
242253
{
243254
return ReadFolderAsync(Folder, folderPath);
244255
}
@@ -290,8 +301,8 @@ private async Task<T> ReadFileAsync<T>(StorageFolder folder, string filePath, T
290301
: item.IsOfType(StorageItemTypes.Folder) ? DirectoryItemType.Folder
291302
: DirectoryItemType.None;
292303

293-
return new ValueTuple<DirectoryItemType, string>(itemType, item.Name);
294-
}).ToList();
304+
return (itemType, item.Name);
305+
});
295306
}
296307

297308
private Task<StorageFile> SaveFileAsync<T>(StorageFolder folder, string filePath, T value)

Microsoft.Toolkit/Extensions/ISettingsStorageHelperExtensions.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,18 @@ public static class ISettingsStorageHelperExtensions
2222
/// <typeparam name="TValue">The type of object value expected.</typeparam>
2323
/// <param name="storageHelper">The storage helper instance fo read from.</param>
2424
/// <param name="key">The key of the target object.</param>
25-
/// <param name="value">The value of the target object, or the fallback value.</param>
2625
/// <param name="fallback">An alternative value returned if the read fails.</param>
27-
/// <returns>A boolean indicator of success.</returns>
28-
public static bool TryGetValueOrDefault<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key, out TValue? value, TValue? fallback = default)
26+
/// <returns>The value of the target object, or the fallback value.</returns>
27+
public static TValue GetValueOrDefault<TKey, TValue>(this ISettingsStorageHelper<TKey> storageHelper, TKey key, TValue? fallback = default)
2928
where TKey : notnull
3029
{
3130
if (storageHelper.TryRead<TValue>(key, out TValue? storedValue))
3231
{
33-
value = storedValue;
34-
return true;
32+
return storedValue;
3533
}
3634
else
3735
{
38-
value = fallback;
39-
return false;
36+
return fallback;
4037
}
4138
}
4239

@@ -58,7 +55,8 @@ public static bool TryGetValueOrDefault<TKey, TValue>(this ISettingsStorageHelpe
5855
}
5956
else
6057
{
61-
throw new KeyNotFoundException();
58+
ThrowKeyNotFoundException(key);
59+
return default;
6260
}
6361
}
6462

@@ -74,8 +72,13 @@ public static void Delete<TKey>(this ISettingsStorageHelper<TKey> storageHelper,
7472
{
7573
if (!storageHelper.TryDelete(key))
7674
{
77-
throw new KeyNotFoundException();
75+
ThrowKeyNotFoundException(key);
7876
}
7977
}
78+
79+
private static void ThrowKeyNotFoundException<TKey>(TKey key)
80+
{
81+
throw new KeyNotFoundException($"The given key '{key}' was not present");
82+
}
8083
}
8184
}

0 commit comments

Comments
 (0)