@@ -22,7 +22,7 @@ public partial class ApplicationDataStorageHelper : IFileStorageHelper, ISetting
22
22
/// </summary>
23
23
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
24
24
/// <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 )
26
26
{
27
27
var appData = ApplicationData . Current ;
28
28
return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
@@ -34,7 +34,7 @@ public static ApplicationDataStorageHelper GetCurrent(Toolkit.Helpers.IObjectSer
34
34
/// <param name="user">App data user owner.</param>
35
35
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
36
36
/// <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 )
38
38
{
39
39
var appData = await ApplicationData . GetForUserAsync ( user ) ;
40
40
return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
@@ -65,7 +65,7 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
65
65
/// </summary>
66
66
/// <param name="appData">The data store to interact with.</param>
67
67
/// <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 )
69
69
{
70
70
AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
71
71
Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
@@ -137,15 +137,35 @@ public void Clear()
137
137
/// <returns>True if a value exists.</returns>
138
138
public bool KeyExists ( string compositeKey , string key )
139
139
{
140
- if ( KeyExists ( compositeKey ) )
140
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
141
141
{
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 )
144
162
{
145
- return composite . ContainsKey ( key ) ;
163
+ value = Serializer . Deserialize < T > ( compositeValue ) ;
164
+ return true ;
146
165
}
147
166
}
148
167
168
+ value = default ;
149
169
return false ;
150
170
}
151
171
@@ -159,11 +179,9 @@ public bool KeyExists(string compositeKey, string key)
159
179
/// <returns>The T object.</returns>
160
180
public T Read < T > ( string compositeKey , string key , T @default = default )
161
181
{
162
- ApplicationDataCompositeValue composite = ( ApplicationDataCompositeValue ) Settings . Values [ compositeKey ] ;
163
- if ( composite != null )
182
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
164
183
{
165
- string value = ( string ) composite [ key ] ;
166
- if ( value != null )
184
+ if ( composite . TryGetValue ( key , out object valueObj ) && valueObj is string value )
167
185
{
168
186
return Serializer . Deserialize < T > ( value ) ;
169
187
}
@@ -182,10 +200,8 @@ public T Read<T>(string compositeKey, string key, T @default = default)
182
200
/// <param name="values">Objects to save.</param>
183
201
public void Save < T > ( string compositeKey , IDictionary < string , T > values )
184
202
{
185
- if ( KeyExists ( compositeKey ) )
203
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
186
204
{
187
- ApplicationDataCompositeValue composite = ( ApplicationDataCompositeValue ) Settings . Values [ compositeKey ] ;
188
-
189
205
foreach ( KeyValuePair < string , T > setting in values )
190
206
{
191
207
if ( composite . ContainsKey ( setting . Key ) )
@@ -200,7 +216,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
200
216
}
201
217
else
202
218
{
203
- ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue ( ) ;
219
+ composite = new ApplicationDataCompositeValue ( ) ;
204
220
foreach ( KeyValuePair < string , T > setting in values )
205
221
{
206
222
composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
@@ -215,20 +231,15 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
215
231
/// </summary>
216
232
/// <param name="compositeKey">Key of the composite (that contains settings).</param>
217
233
/// <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 )
220
236
{
221
- if ( ! KeyExists ( compositeKey ) )
237
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
222
238
{
223
- throw new KeyNotFoundException ( $ "Composite key not found: { compositeKey } " ) ;
239
+ return composite . Remove ( key ) ;
224
240
}
225
241
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 ;
232
243
}
233
244
234
245
/// <inheritdoc />
@@ -238,7 +249,7 @@ public Task<T> ReadFileAsync<T>(string filePath, T @default = default)
238
249
}
239
250
240
251
/// <inheritdoc />
241
- public Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( string folderPath )
252
+ public Task < IEnumerable < ( DirectoryItemType ItemType , string Name ) > > ReadFolderAsync ( string folderPath )
242
253
{
243
254
return ReadFolderAsync ( Folder , folderPath ) ;
244
255
}
@@ -290,8 +301,8 @@ private async Task<T> ReadFileAsync<T>(StorageFolder folder, string filePath, T
290
301
: item . IsOfType ( StorageItemTypes . Folder ) ? DirectoryItemType . Folder
291
302
: DirectoryItemType . None ;
292
303
293
- return new ValueTuple < DirectoryItemType , string > ( itemType , item . Name ) ;
294
- } ) . ToList ( ) ;
304
+ return ( itemType , item . Name ) ;
305
+ } ) ;
295
306
}
296
307
297
308
private Task < StorageFile > SaveFileAsync < T > ( StorageFolder folder , string filePath , T value )
0 commit comments