@@ -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 )
0 commit comments