@@ -15,7 +15,7 @@ namespace Microsoft.Toolkit.Uwp.Helpers
15
15
/// <summary>
16
16
/// Storage helper for files and folders living in Windows.Storage.ApplicationData storage endpoints.
17
17
/// </summary>
18
- public partial class ApplicationDataStorageHelper : IFileStorageHelper , ISettingsStorageHelper
18
+ public partial class ApplicationDataStorageHelper : IFileStorageHelper , ISettingsStorageHelper < string >
19
19
{
20
20
/// <summary>
21
21
/// Get a new instance using ApplicationData.Current and the provided serializer.
@@ -53,9 +53,12 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
53
53
/// <summary>
54
54
/// Gets the storage host.
55
55
/// </summary>
56
- protected ApplicationData AppData { get ; private set ; }
56
+ protected ApplicationData AppData { get ; }
57
57
58
- private readonly Toolkit . Helpers . IObjectSerializer _serializer ;
58
+ /// <summary>
59
+ /// Gets the serializer for converting stored values.
60
+ /// </summary>
61
+ protected Toolkit . Helpers . IObjectSerializer Serializer { get ; }
59
62
60
63
/// <summary>
61
64
/// Initializes a new instance of the <see cref="ApplicationDataStorageHelper"/> class.
@@ -65,16 +68,54 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
65
68
public ApplicationDataStorageHelper ( ApplicationData appData , Toolkit . Helpers . IObjectSerializer objectSerializer = null )
66
69
{
67
70
AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
68
- _serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
71
+ Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
69
72
}
70
73
71
- /// <inheritdoc />
74
+ /// <summary>
75
+ /// Determines whether a setting already exists.
76
+ /// </summary>
77
+ /// <param name="key">Key of the setting (that contains object).</param>
78
+ /// <returns>True if a value exists.</returns>
72
79
public bool KeyExists ( string key )
73
80
{
74
81
return Settings . Values . ContainsKey ( key ) ;
75
82
}
76
83
77
84
/// <inheritdoc />
85
+ public T Read < T > ( string key , T @default = default )
86
+ {
87
+ if ( ! Settings . Values . TryGetValue ( key , out var valueObj ) || valueObj == null )
88
+ {
89
+ return @default ;
90
+ }
91
+
92
+ return Serializer . Deserialize < T > ( valueObj as string ) ;
93
+ }
94
+
95
+ /// <inheritdoc />
96
+ public void Save < T > ( string key , T value )
97
+ {
98
+ Settings . Values [ key ] = Serializer . Serialize ( value ) ;
99
+ }
100
+
101
+ /// <inheritdoc />
102
+ public void Delete ( string key )
103
+ {
104
+ Settings . Values . Remove ( key ) ;
105
+ }
106
+
107
+ /// <inheritdoc />
108
+ public void Clear ( )
109
+ {
110
+ Settings . Values . Clear ( ) ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// Determines whether a setting already exists in composite.
115
+ /// </summary>
116
+ /// <param name="compositeKey">Key of the composite (that contains settings).</param>
117
+ /// <param name="key">Key of the setting (that contains object).</param>
118
+ /// <returns>True if a value exists.</returns>
78
119
public bool KeyExists ( string compositeKey , string key )
79
120
{
80
121
if ( KeyExists ( compositeKey ) )
@@ -89,18 +130,14 @@ public bool KeyExists(string compositeKey, string key)
89
130
return false ;
90
131
}
91
132
92
- /// <inheritdoc />
93
- public T Read < T > ( string key , T @default = default )
94
- {
95
- if ( ! Settings . Values . TryGetValue ( key , out var value ) || value == null )
96
- {
97
- return @default ;
98
- }
99
-
100
- return _serializer . Deserialize < T > ( value ) ;
101
- }
102
-
103
- /// <inheritdoc />
133
+ /// <summary>
134
+ /// Retrieves a single item by its key in composite.
135
+ /// </summary>
136
+ /// <typeparam name="T">Type of object retrieved.</typeparam>
137
+ /// <param name="compositeKey">Key of the composite (that contains settings).</param>
138
+ /// <param name="key">Key of the object.</param>
139
+ /// <param name="default">Default value of the object.</param>
140
+ /// <returns>The T object.</returns>
104
141
public T Read < T > ( string compositeKey , string key , T @default = default )
105
142
{
106
143
ApplicationDataCompositeValue composite = ( ApplicationDataCompositeValue ) Settings . Values [ compositeKey ] ;
@@ -109,20 +146,21 @@ public T Read<T>(string compositeKey, string key, T @default = default)
109
146
string value = ( string ) composite [ key ] ;
110
147
if ( value != null )
111
148
{
112
- return _serializer . Deserialize < T > ( value ) ;
149
+ return Serializer . Deserialize < T > ( value ) ;
113
150
}
114
151
}
115
152
116
153
return @default ;
117
154
}
118
155
119
- /// <inheritdoc />
120
- public void Save < T > ( string key , T value )
121
- {
122
- Settings . Values [ key ] = _serializer . Serialize ( value ) ;
123
- }
124
-
125
- /// <inheritdoc />
156
+ /// <summary>
157
+ /// Saves a group of items by its key in a composite.
158
+ /// This method should be considered for objects that do not exceed 8k bytes during the lifetime of the application
159
+ /// and for groups of settings which need to be treated in an atomic way.
160
+ /// </summary>
161
+ /// <typeparam name="T">Type of object saved.</typeparam>
162
+ /// <param name="compositeKey">Key of the composite (that contains settings).</param>
163
+ /// <param name="values">Objects to save.</param>
126
164
public void Save < T > ( string compositeKey , IDictionary < string , T > values )
127
165
{
128
166
if ( KeyExists ( compositeKey ) )
@@ -133,11 +171,11 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
133
171
{
134
172
if ( composite . ContainsKey ( setting . Key ) )
135
173
{
136
- composite [ setting . Key ] = _serializer . Serialize ( setting . Value ) ;
174
+ composite [ setting . Key ] = Serializer . Serialize ( setting . Value ) ;
137
175
}
138
176
else
139
177
{
140
- composite . Add ( setting . Key , _serializer . Serialize ( setting . Value ) ) ;
178
+ composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
141
179
}
142
180
}
143
181
}
@@ -146,20 +184,18 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
146
184
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue ( ) ;
147
185
foreach ( KeyValuePair < string , T > setting in values )
148
186
{
149
- composite . Add ( setting . Key , _serializer . Serialize ( setting . Value ) ) ;
187
+ composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
150
188
}
151
189
152
190
Settings . Values [ compositeKey ] = composite ;
153
191
}
154
192
}
155
193
156
- /// <inheritdoc />
157
- public void Delete ( string key )
158
- {
159
- Settings . Values . Remove ( key ) ;
160
- }
161
-
162
- /// <inheritdoc />
194
+ /// <summary>
195
+ /// Deletes a single item by its key in composite.
196
+ /// </summary>
197
+ /// <param name="compositeKey">Key of the composite (that contains settings).</param>
198
+ /// <param name="key">Key of the object.</param>
163
199
public void Delete ( string compositeKey , string key )
164
200
{
165
201
if ( KeyExists ( compositeKey ) )
@@ -169,20 +205,14 @@ public void Delete(string compositeKey, string key)
169
205
}
170
206
}
171
207
172
- /// <inheritdoc />
173
- public Task < bool > ItemExistsAsync ( string itemName )
174
- {
175
- return ItemExistsAsync ( Folder , itemName ) ;
176
- }
177
-
178
208
/// <inheritdoc />
179
209
public Task < T > ReadFileAsync < T > ( string filePath , T @default = default )
180
210
{
181
211
return ReadFileAsync < T > ( Folder , filePath , @default ) ;
182
212
}
183
213
184
214
/// <inheritdoc />
185
- public Task < IList < Tuple < DirectoryItemType , string > > > ReadFolderAsync ( string folderPath )
215
+ public Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( string folderPath )
186
216
{
187
217
return ReadFolderAsync ( Folder , folderPath ) ;
188
218
}
@@ -205,18 +235,6 @@ public Task DeleteItemAsync(string itemPath)
205
235
return DeleteItemAsync ( Folder , itemPath ) ;
206
236
}
207
237
208
- /// <summary>
209
- /// Determine the existance of a file at the specified path.
210
- /// To check for folders, use <see cref="ItemExistsAsync(string)" />.
211
- /// </summary>
212
- /// <param name="fileName">The name of the file.</param>
213
- /// <param name="isRecursive">Whether the file should be searched for recursively.</param>
214
- /// <returns>A task with the result of the file query.</returns>
215
- public Task < bool > FileExistsAsync ( string fileName , bool isRecursive = false )
216
- {
217
- return FileExistsAsync ( Folder , fileName , isRecursive ) ;
218
- }
219
-
220
238
/// <summary>
221
239
/// Saves an object inside a file.
222
240
/// </summary>
@@ -229,24 +247,13 @@ public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
229
247
return SaveFileAsync < T > ( Folder , filePath , value ) ;
230
248
}
231
249
232
- private async Task < bool > ItemExistsAsync ( StorageFolder folder , string itemName )
233
- {
234
- var item = await folder . TryGetItemAsync ( itemName ) ;
235
- return item != null ;
236
- }
237
-
238
- private Task < bool > FileExistsAsync ( StorageFolder folder , string fileName , bool isRecursive )
239
- {
240
- return folder . FileExistsAsync ( fileName , isRecursive ) ;
241
- }
242
-
243
250
private async Task < T > ReadFileAsync < T > ( StorageFolder folder , string filePath , T @default = default )
244
251
{
245
252
string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , filePath ) ;
246
- return ( value != null ) ? _serializer . Deserialize < T > ( value ) : @default ;
253
+ return ( value != null ) ? Serializer . Deserialize < T > ( value ) : @default ;
247
254
}
248
255
249
- private async Task < IList < Tuple < DirectoryItemType , string > > > ReadFolderAsync ( StorageFolder folder , string folderPath )
256
+ private async Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( StorageFolder folder , string folderPath )
250
257
{
251
258
var targetFolder = await folder . GetFolderAsync ( folderPath ) ;
252
259
var items = await targetFolder . GetItemsAsync ( ) ;
@@ -257,13 +264,13 @@ private async Task<IList<Tuple<DirectoryItemType, string>>> ReadFolderAsync(Stor
257
264
: item . IsOfType ( StorageItemTypes . Folder ) ? DirectoryItemType . Folder
258
265
: DirectoryItemType . None ;
259
266
260
- return new Tuple < DirectoryItemType , string > ( itemType , item . Name ) ;
267
+ return new ValueTuple < DirectoryItemType , string > ( itemType , item . Name ) ;
261
268
} ) . ToList ( ) ;
262
269
}
263
270
264
271
private Task < StorageFile > SaveFileAsync < T > ( StorageFolder folder , string filePath , T value )
265
272
{
266
- return StorageFileHelper . WriteTextToFileAsync ( folder , _serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
273
+ return StorageFileHelper . WriteTextToFileAsync ( folder , Serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
267
274
}
268
275
269
276
private async Task CreateFolderAsync ( StorageFolder folder , string folderPath )
0 commit comments