@@ -20,37 +20,25 @@ namespace Microsoft.Toolkit.Uwp.Helpers
2020 public partial class ApplicationDataStorageHelper : IFileStorageHelper , ISettingsStorageHelper < string >
2121 {
2222 /// <summary>
23- /// Get a new instance using ApplicationData.Current and the provided serializer.
24- /// </summary>
25- /// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
26- /// <returns>A new instance of ApplicationDataStorageHelper.</returns>
27- public static ApplicationDataStorageHelper GetCurrent ( Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
28- {
29- var appData = ApplicationData . Current ;
30- return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
31- }
32-
33- /// <summary>
34- /// Get a new instance using the ApplicationData for the provided user and serializer.
23+ /// Initializes a new instance of the <see cref="ApplicationDataStorageHelper"/> class.
3524 /// </summary>
36- /// <param name="user">App data user owner .</param>
25+ /// <param name="appData">The data store to interact with .</param>
3726 /// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
38- /// <returns>A new instance of ApplicationDataStorageHelper.</returns>
39- public static async Task < ApplicationDataStorageHelper > GetForUserAsync ( User user , Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
27+ public ApplicationDataStorageHelper ( ApplicationData appData , Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
4028 {
41- var appData = await ApplicationData . GetForUserAsync ( user ) ;
42- return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
29+ this . AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
30+ this . Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
4331 }
4432
4533 /// <summary>
4634 /// Gets the settings container.
4735 /// </summary>
48- public ApplicationDataContainer Settings => AppData . LocalSettings ;
36+ public ApplicationDataContainer Settings => this . AppData . LocalSettings ;
4937
5038 /// <summary>
5139 /// Gets the storage folder.
5240 /// </summary>
53- public StorageFolder Folder => AppData . LocalFolder ;
41+ public StorageFolder Folder => this . AppData . LocalFolder ;
5442
5543 /// <summary>
5644 /// Gets the storage host.
@@ -63,14 +51,26 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
6351 protected Toolkit . Helpers . IObjectSerializer Serializer { get ; }
6452
6553 /// <summary>
66- /// Initializes a new instance of the <see cref="ApplicationDataStorageHelper"/> class .
54+ /// Get a new instance using ApplicationData.Current and the provided serializer .
6755 /// </summary>
68- /// <param name="appData">The data store to interact with.</param>
6956 /// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
70- public ApplicationDataStorageHelper ( ApplicationData appData , Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
57+ /// <returns>A new instance of ApplicationDataStorageHelper.</returns>
58+ public static ApplicationDataStorageHelper GetCurrent ( Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
59+ {
60+ var appData = ApplicationData . Current ;
61+ return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
62+ }
63+
64+ /// <summary>
65+ /// Get a new instance using the ApplicationData for the provided user and serializer.
66+ /// </summary>
67+ /// <param name="user">App data user owner.</param>
68+ /// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
69+ /// <returns>A new instance of ApplicationDataStorageHelper.</returns>
70+ public static async Task < ApplicationDataStorageHelper > GetForUserAsync ( User user , Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
7171 {
72- AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
73- Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
72+ var appData = await ApplicationData . GetForUserAsync ( user ) ;
73+ return new ApplicationDataStorageHelper ( appData , objectSerializer ) ;
7474 }
7575
7676 /// <summary>
@@ -80,7 +80,7 @@ public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IOb
8080 /// <returns>True if a value exists.</returns>
8181 public bool KeyExists ( string key )
8282 {
83- return Settings . Values . ContainsKey ( key ) ;
83+ return this . Settings . Values . ContainsKey ( key ) ;
8484 }
8585
8686 /// <summary>
@@ -89,23 +89,23 @@ public bool KeyExists(string key)
8989 /// <typeparam name="T">Type of object retrieved.</typeparam>
9090 /// <param name="key">Key of the object.</param>
9191 /// <param name="default">Default value of the object.</param>
92- /// <returns>The TValue object</returns>
93- public T Read < T > ( string key , T @default = default )
92+ /// <returns>The TValue object. </returns>
93+ public T ? Read < T > ( string key , T ? @default = default )
9494 {
95- if ( ! Settings . Values . TryGetValue ( key , out var valueObj ) || valueObj == null )
95+ if ( this . Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
9696 {
97- return @default ;
97+ return this . Serializer . Deserialize < T > ( valueString ) ;
9898 }
9999
100- return Serializer . Deserialize < T > ( valueObj as string ) ;
100+ return @default ;
101101 }
102102
103103 /// <inheritdoc />
104- public bool TryRead < T > ( string key , out T value )
104+ public bool TryRead < T > ( string key , out T ? value )
105105 {
106- if ( Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj != null )
106+ if ( this . Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
107107 {
108- value = Serializer . Deserialize < T > ( valueObj as string ) ;
108+ value = this . Serializer . Deserialize < T > ( valueString ) ;
109109 return true ;
110110 }
111111
@@ -116,19 +116,19 @@ public bool TryRead<T>(string key, out T value)
116116 /// <inheritdoc />
117117 public void Save < T > ( string key , T value )
118118 {
119- Settings . Values [ key ] = Serializer . Serialize ( value ) ;
119+ this . Settings . Values [ key ] = this . Serializer . Serialize ( value ) ;
120120 }
121121
122122 /// <inheritdoc />
123123 public bool TryDelete ( string key )
124124 {
125- return Settings . Values . Remove ( key ) ;
125+ return this . Settings . Values . Remove ( key ) ;
126126 }
127127
128128 /// <inheritdoc />
129129 public void Clear ( )
130130 {
131- Settings . Values . Clear ( ) ;
131+ this . Settings . Values . Clear ( ) ;
132132 }
133133
134134 /// <summary>
@@ -139,7 +139,7 @@ public void Clear()
139139 /// <returns>True if a value exists.</returns>
140140 public bool KeyExists ( string compositeKey , string key )
141141 {
142- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
142+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
143143 {
144144 return composite . ContainsKey ( key ) ;
145145 }
@@ -155,14 +155,14 @@ public bool KeyExists(string compositeKey, string key)
155155 /// <param name="key">Key of the object.</param>
156156 /// <param name="value">The value of the object retrieved.</param>
157157 /// <returns>The T object.</returns>
158- public bool TryRead < T > ( string compositeKey , string key , out T value )
158+ public bool TryRead < T > ( string compositeKey , string key , out T ? value )
159159 {
160- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
160+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
161161 {
162162 string compositeValue = ( string ) composite [ key ] ;
163163 if ( compositeValue != null )
164164 {
165- value = Serializer . Deserialize < T > ( compositeValue ) ;
165+ value = this . Serializer . Deserialize < T > ( compositeValue ) ;
166166 return true ;
167167 }
168168 }
@@ -179,13 +179,13 @@ public bool TryRead<T>(string compositeKey, string key, out T value)
179179 /// <param name="key">Key of the object.</param>
180180 /// <param name="default">Default value of the object.</param>
181181 /// <returns>The T object.</returns>
182- public T Read < T > ( string compositeKey , string key , T @default = default )
182+ public T ? Read < T > ( string compositeKey , string key , T ? @default = default )
183183 {
184- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
184+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
185185 {
186186 if ( composite . TryGetValue ( key , out object valueObj ) && valueObj is string value )
187187 {
188- return Serializer . Deserialize < T > ( value ) ;
188+ return this . Serializer . Deserialize < T > ( value ) ;
189189 }
190190 }
191191
@@ -202,17 +202,17 @@ public T Read<T>(string compositeKey, string key, T @default = default)
202202 /// <param name="values">Objects to save.</param>
203203 public void Save < T > ( string compositeKey , IDictionary < string , T > values )
204204 {
205- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
205+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
206206 {
207207 foreach ( KeyValuePair < string , T > setting in values )
208208 {
209209 if ( composite . ContainsKey ( setting . Key ) )
210210 {
211- composite [ setting . Key ] = Serializer . Serialize ( setting . Value ) ;
211+ composite [ setting . Key ] = this . Serializer . Serialize ( setting . Value ) ;
212212 }
213213 else
214214 {
215- composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
215+ composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
216216 }
217217 }
218218 }
@@ -221,10 +221,10 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
221221 composite = new ApplicationDataCompositeValue ( ) ;
222222 foreach ( KeyValuePair < string , T > setting in values )
223223 {
224- composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
224+ composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
225225 }
226226
227- Settings . Values [ compositeKey ] = composite ;
227+ this . Settings . Values [ compositeKey ] = composite ;
228228 }
229229 }
230230
@@ -236,7 +236,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
236236 /// <returns>A boolean indicator of success.</returns>
237237 public bool TryDelete ( string compositeKey , string key )
238238 {
239- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
239+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
240240 {
241241 return composite . Remove ( key ) ;
242242 }
@@ -245,33 +245,33 @@ public bool TryDelete(string compositeKey, string key)
245245 }
246246
247247 /// <inheritdoc />
248- public Task < T > ReadFileAsync < T > ( string filePath , T @default = default )
248+ public Task < T ? > ReadFileAsync < T > ( string filePath , T ? @default = default )
249249 {
250- return ReadFileAsync < T > ( Folder , filePath , @default ) ;
250+ return this . ReadFileAsync < T > ( this . Folder , filePath , @default ) ;
251251 }
252252
253253 /// <inheritdoc />
254254 public Task < IEnumerable < ( DirectoryItemType ItemType , string Name ) > > ReadFolderAsync ( string folderPath )
255255 {
256- return ReadFolderAsync ( Folder , folderPath ) ;
256+ return this . ReadFolderAsync ( this . Folder , folderPath ) ;
257257 }
258258
259259 /// <inheritdoc />
260260 public Task CreateFileAsync < T > ( string filePath , T value )
261261 {
262- return SaveFileAsync < T > ( Folder , filePath , value ) ;
262+ return this . SaveFileAsync < T > ( this . Folder , filePath , value ) ;
263263 }
264264
265265 /// <inheritdoc />
266266 public Task CreateFolderAsync ( string folderPath )
267267 {
268- return CreateFolderAsync ( Folder , folderPath ) ;
268+ return this . CreateFolderAsync ( this . Folder , folderPath ) ;
269269 }
270270
271271 /// <inheritdoc />
272272 public Task DeleteItemAsync ( string itemPath )
273273 {
274- return DeleteItemAsync ( Folder , itemPath ) ;
274+ return this . DeleteItemAsync ( this . Folder , itemPath ) ;
275275 }
276276
277277 /// <summary>
@@ -283,13 +283,13 @@ public Task DeleteItemAsync(string itemPath)
283283 /// <returns>Waiting task until completion.</returns>
284284 public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
285285 {
286- return SaveFileAsync < T > ( Folder , filePath , value ) ;
286+ return this . SaveFileAsync ( this . Folder , filePath , value ) ;
287287 }
288288
289- private async Task < T > ReadFileAsync < T > ( StorageFolder folder , string filePath , T @default = default )
289+ private async Task < T ? > ReadFileAsync < T > ( StorageFolder folder , string filePath , T ? @default = default )
290290 {
291291 string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , filePath ) ;
292- return ( value != null ) ? Serializer . Deserialize < T > ( value ) : @default ;
292+ return ( value != null ) ? this . Serializer . Deserialize < T > ( value ) : @default ;
293293 }
294294
295295 private async Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( StorageFolder folder , string folderPath )
@@ -309,7 +309,7 @@ private async Task<T> ReadFileAsync<T>(StorageFolder folder, string filePath, T
309309
310310 private Task < StorageFile > SaveFileAsync < T > ( StorageFolder folder , string filePath , T value )
311311 {
312- return StorageFileHelper . WriteTextToFileAsync ( folder , Serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
312+ return StorageFileHelper . WriteTextToFileAsync ( folder , this . Serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
313313 }
314314
315315 private async Task CreateFolderAsync ( StorageFolder folder , string folderPath )
0 commit comments