Skip to content

Commit 8140bff

Browse files
committed
Added support for the LocalCacheFolder to validate extensibility of AppDataStorageHelper
1 parent 1f53d9a commit 8140bff

File tree

2 files changed

+111
-21
lines changed

2 files changed

+111
-21
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Microsoft.Toolkit.Helpers;
5+
using Windows.Storage;
6+
7+
namespace Microsoft.Toolkit.Uwp.Helpers
8+
{
9+
/// <summary>
10+
/// An extension of ApplicationDataStorageHelper with additional features for interop with the LocalCacheFolder.
11+
/// </summary>
12+
public partial class ApplicationDataStorageHelper
13+
{
14+
/// <summary>
15+
/// Gets the storage folder.
16+
/// </summary>
17+
public StorageFolder CacheFolder => AppData.LocalCacheFolder;
18+
19+
/// <summary>
20+
/// Determines if a directory item already exists in the LocalCacheFolder.
21+
/// </summary>
22+
/// <param name="itemName">Key of the file.</param>
23+
/// <returns>True if an item exists.</returns>
24+
public Task<bool> CacheItemExistsAsync(string itemName)
25+
{
26+
return ItemExistsAsync(CacheFolder, itemName);
27+
}
28+
29+
/// <summary>
30+
/// Retrieves an object from a file in the LocalCacheFolder.
31+
/// </summary>
32+
/// <typeparam name="T">Type of object retrieved.</typeparam>
33+
/// <param name="filePath">Path to the file that contains the object.</param>
34+
/// <param name="default">Default value of the object.</param>
35+
/// <returns>Waiting task until completion with the object in the file.</returns>
36+
public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
37+
{
38+
return ReadFileAsync<T>(CacheFolder, filePath, @default);
39+
}
40+
41+
42+
/// <summary>
43+
/// Retrieves the listings for a folder and the item types in the LocalCacheFolder.
44+
/// </summary>
45+
/// <param name="folderPath">The path to the target folder.</param>
46+
/// <returns>A list of file types and names in the target folder.</returns>
47+
public Task<IList<Tuple<DirectoryItemType, string>>> ReadCacheFolderAsync(string folderPath)
48+
{
49+
return ReadFolderAsync(CacheFolder, folderPath);
50+
}
51+
52+
/// <summary>
53+
/// Saves an object inside a file in the LocalCacheFolder.
54+
/// </summary>
55+
/// <typeparam name="T">Type of object saved.</typeparam>
56+
/// <param name="filePath">Path to the file that will contain the object.</param>
57+
/// <param name="value">Object to save.</param>
58+
/// <returns>Waiting task until completion.</returns>
59+
public Task CreateCacheFileAsync<T>(string filePath, T value)
60+
{
61+
return SaveFileAsync<T>(CacheFolder, filePath, value);
62+
}
63+
64+
/// <summary>
65+
/// Ensure a folder exists at the folder path specified in the LocalCacheFolder.
66+
/// </summary>
67+
/// <param name="folderPath">The path and name of the target folder.</param>
68+
/// <returns>Waiting task until completion.</returns>
69+
public Task CreateCacheFolderAsync(string folderPath)
70+
{
71+
return CreateFolderAsync(CacheFolder, folderPath);
72+
}
73+
74+
/// <summary>
75+
/// Deletes a file or folder item in the LocalCacheFolder.
76+
/// </summary>
77+
/// <param name="itemPath">The path to the item for deletion.</param>
78+
/// <returns>Waiting task until completion.</returns>
79+
public Task DeleteCacheItemAsync(string itemPath)
80+
{
81+
return DeleteItemAsync(CacheFolder, itemPath);
82+
}
83+
}
84+
}

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1515
/// <summary>
1616
/// Storage helper for files and folders living in Windows.Storage.ApplicationData storage endpoints.
1717
/// </summary>
18-
public class ApplicationDataStorageHelper : IFileStorageHelper, ISettingsStorageHelper
18+
public partial class ApplicationDataStorageHelper : IFileStorageHelper, ISettingsStorageHelper
1919
{
2020
/// <summary>
2121
/// Get a new instance using ApplicationData.Current and the provided serializer.
@@ -43,11 +43,17 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
4343
/// <summary>
4444
/// Gets the settings container.
4545
/// </summary>
46-
protected ApplicationData AppData { get; private set; }
46+
public ApplicationDataContainer Settings => AppData.LocalSettings;
4747

48-
private ApplicationDataContainer DefaultSettings => AppData.LocalSettings;
48+
/// <summary>
49+
/// Gets the storage folder.
50+
/// </summary>
51+
public StorageFolder Folder => AppData.LocalFolder;
4952

50-
private StorageFolder DefaultFolder => AppData.LocalFolder;
53+
/// <summary>
54+
/// Gets the storage host.
55+
/// </summary>
56+
protected ApplicationData AppData { get; private set; }
5157

5258
private readonly Toolkit.Helpers.IObjectSerializer _serializer;
5359

@@ -65,15 +71,15 @@ public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IOb
6571
/// <inheritdoc />
6672
public bool KeyExists(string key)
6773
{
68-
return DefaultSettings.Values.ContainsKey(key);
74+
return Settings.Values.ContainsKey(key);
6975
}
7076

7177
/// <inheritdoc />
7278
public bool KeyExists(string compositeKey, string key)
7379
{
7480
if (KeyExists(compositeKey))
7581
{
76-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)DefaultSettings.Values[compositeKey];
82+
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
7783
if (composite != null)
7884
{
7985
return composite.ContainsKey(key);
@@ -86,7 +92,7 @@ public bool KeyExists(string compositeKey, string key)
8692
/// <inheritdoc />
8793
public T Read<T>(string key, T @default = default)
8894
{
89-
if (!DefaultSettings.Values.TryGetValue(key, out var value) || value == null)
95+
if (!Settings.Values.TryGetValue(key, out var value) || value == null)
9096
{
9197
return @default;
9298
}
@@ -97,7 +103,7 @@ public T Read<T>(string key, T @default = default)
97103
/// <inheritdoc />
98104
public T Read<T>(string compositeKey, string key, T @default = default)
99105
{
100-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)DefaultSettings.Values[compositeKey];
106+
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
101107
if (composite != null)
102108
{
103109
string value = (string)composite[key];
@@ -113,15 +119,15 @@ public T Read<T>(string compositeKey, string key, T @default = default)
113119
/// <inheritdoc />
114120
public void Save<T>(string key, T value)
115121
{
116-
DefaultSettings.Values[key] = _serializer.Serialize(value);
122+
Settings.Values[key] = _serializer.Serialize(value);
117123
}
118124

119125
/// <inheritdoc />
120126
public void Save<T>(string compositeKey, IDictionary<string, T> values)
121127
{
122128
if (KeyExists(compositeKey))
123129
{
124-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)DefaultSettings.Values[compositeKey];
130+
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
125131

126132
foreach (KeyValuePair<string, T> setting in values)
127133
{
@@ -143,60 +149,60 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
143149
composite.Add(setting.Key, _serializer.Serialize(setting.Value));
144150
}
145151

146-
DefaultSettings.Values[compositeKey] = composite;
152+
Settings.Values[compositeKey] = composite;
147153
}
148154
}
149155

150156
/// <inheritdoc />
151157
public void Delete(string key)
152158
{
153-
DefaultSettings.Values.Remove(key);
159+
Settings.Values.Remove(key);
154160
}
155161

156162
/// <inheritdoc />
157163
public void Delete(string compositeKey, string key)
158164
{
159165
if (KeyExists(compositeKey))
160166
{
161-
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)DefaultSettings.Values[compositeKey];
167+
ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)Settings.Values[compositeKey];
162168
composite.Remove(key);
163169
}
164170
}
165171

166172
/// <inheritdoc />
167173
public Task<bool> ItemExistsAsync(string itemName)
168174
{
169-
return ItemExistsAsync(DefaultFolder, itemName);
175+
return ItemExistsAsync(Folder, itemName);
170176
}
171177

172178
/// <inheritdoc />
173179
public Task<T> ReadFileAsync<T>(string filePath, T @default = default)
174180
{
175-
return ReadFileAsync<T>(DefaultFolder, filePath, @default);
181+
return ReadFileAsync<T>(Folder, filePath, @default);
176182
}
177183

178184
/// <inheritdoc />
179185
public Task<IList<Tuple<DirectoryItemType, string>>> ReadFolderAsync(string folderPath)
180186
{
181-
return ReadFolderAsync(DefaultFolder, folderPath);
187+
return ReadFolderAsync(Folder, folderPath);
182188
}
183189

184190
/// <inheritdoc />
185191
public Task CreateFileAsync<T>(string filePath, T value)
186192
{
187-
return SaveFileAsync<T>(DefaultFolder, filePath, value);
193+
return SaveFileAsync<T>(Folder, filePath, value);
188194
}
189195

190196
/// <inheritdoc />
191197
public Task CreateFolderAsync(string folderPath)
192198
{
193-
return CreateFolderAsync(DefaultFolder, folderPath);
199+
return CreateFolderAsync(Folder, folderPath);
194200
}
195201

196202
/// <inheritdoc />
197203
public Task DeleteItemAsync(string itemPath)
198204
{
199-
return DeleteItemAsync(DefaultFolder, itemPath);
205+
return DeleteItemAsync(Folder, itemPath);
200206
}
201207

202208
/// <summary>
@@ -208,7 +214,7 @@ public Task DeleteItemAsync(string itemPath)
208214
/// <returns>A task with the result of the file query.</returns>
209215
public Task<bool> FileExistsAsync(string fileName, bool isRecursive = false)
210216
{
211-
return FileExistsAsync(DefaultFolder, fileName, isRecursive);
217+
return FileExistsAsync(Folder, fileName, isRecursive);
212218
}
213219

214220
/// <summary>
@@ -220,7 +226,7 @@ public Task<bool> FileExistsAsync(string fileName, bool isRecursive = false)
220226
/// <returns>Waiting task until completion.</returns>
221227
public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
222228
{
223-
return SaveFileAsync<T>(DefaultFolder, filePath, value);
229+
return SaveFileAsync<T>(Folder, filePath, value);
224230
}
225231

226232
private async Task<bool> ItemExistsAsync(StorageFolder folder, string itemName)

0 commit comments

Comments
 (0)