@@ -20,37 +20,25 @@ namespace Microsoft.Toolkit.Uwp.Helpers
20
20
public partial class ApplicationDataStorageHelper : IFileStorageHelper , ISettingsStorageHelper < string >
21
21
{
22
22
/// <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.
35
24
/// </summary>
36
- /// <param name="user">App data user owner .</param>
25
+ /// <param name="appData">The data store to interact with .</param>
37
26
/// <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 )
40
28
{
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 ( ) ;
43
31
}
44
32
45
33
/// <summary>
46
34
/// Gets the settings container.
47
35
/// </summary>
48
- public ApplicationDataContainer Settings => AppData . LocalSettings ;
36
+ public ApplicationDataContainer Settings => this . AppData . LocalSettings ;
49
37
50
38
/// <summary>
51
39
/// Gets the storage folder.
52
40
/// </summary>
53
- public StorageFolder Folder => AppData . LocalFolder ;
41
+ public StorageFolder Folder => this . AppData . LocalFolder ;
54
42
55
43
/// <summary>
56
44
/// Gets the storage host.
@@ -63,14 +51,26 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
63
51
protected Toolkit . Helpers . IObjectSerializer Serializer { get ; }
64
52
65
53
/// <summary>
66
- /// Initializes a new instance of the <see cref="ApplicationDataStorageHelper"/> class .
54
+ /// Get a new instance using ApplicationData.Current and the provided serializer .
67
55
/// </summary>
68
- /// <param name="appData">The data store to interact with.</param>
69
56
/// <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 )
71
71
{
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 ) ;
74
74
}
75
75
76
76
/// <summary>
@@ -80,7 +80,7 @@ public ApplicationDataStorageHelper(ApplicationData appData, Toolkit.Helpers.IOb
80
80
/// <returns>True if a value exists.</returns>
81
81
public bool KeyExists ( string key )
82
82
{
83
- return Settings . Values . ContainsKey ( key ) ;
83
+ return this . Settings . Values . ContainsKey ( key ) ;
84
84
}
85
85
86
86
/// <summary>
@@ -89,23 +89,23 @@ public bool KeyExists(string key)
89
89
/// <typeparam name="T">Type of object retrieved.</typeparam>
90
90
/// <param name="key">Key of the object.</param>
91
91
/// <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 )
94
94
{
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 )
96
96
{
97
- return @default ;
97
+ return this . Serializer . Deserialize < T > ( valueString ) ;
98
98
}
99
99
100
- return Serializer . Deserialize < T > ( valueObj as string ) ;
100
+ return @default ;
101
101
}
102
102
103
103
/// <inheritdoc />
104
- public bool TryRead < T > ( string key , out T value )
104
+ public bool TryRead < T > ( string key , out T ? value )
105
105
{
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 )
107
107
{
108
- value = Serializer . Deserialize < T > ( valueObj as string ) ;
108
+ value = this . Serializer . Deserialize < T > ( valueString ) ;
109
109
return true ;
110
110
}
111
111
@@ -116,19 +116,19 @@ public bool TryRead<T>(string key, out T value)
116
116
/// <inheritdoc />
117
117
public void Save < T > ( string key , T value )
118
118
{
119
- Settings . Values [ key ] = Serializer . Serialize ( value ) ;
119
+ this . Settings . Values [ key ] = this . Serializer . Serialize ( value ) ;
120
120
}
121
121
122
122
/// <inheritdoc />
123
123
public bool TryDelete ( string key )
124
124
{
125
- return Settings . Values . Remove ( key ) ;
125
+ return this . Settings . Values . Remove ( key ) ;
126
126
}
127
127
128
128
/// <inheritdoc />
129
129
public void Clear ( )
130
130
{
131
- Settings . Values . Clear ( ) ;
131
+ this . Settings . Values . Clear ( ) ;
132
132
}
133
133
134
134
/// <summary>
@@ -139,7 +139,7 @@ public void Clear()
139
139
/// <returns>True if a value exists.</returns>
140
140
public bool KeyExists ( string compositeKey , string key )
141
141
{
142
- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
142
+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
143
143
{
144
144
return composite . ContainsKey ( key ) ;
145
145
}
@@ -155,14 +155,14 @@ public bool KeyExists(string compositeKey, string key)
155
155
/// <param name="key">Key of the object.</param>
156
156
/// <param name="value">The value of the object retrieved.</param>
157
157
/// <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 )
159
159
{
160
- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
160
+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
161
161
{
162
162
string compositeValue = ( string ) composite [ key ] ;
163
163
if ( compositeValue != null )
164
164
{
165
- value = Serializer . Deserialize < T > ( compositeValue ) ;
165
+ value = this . Serializer . Deserialize < T > ( compositeValue ) ;
166
166
return true ;
167
167
}
168
168
}
@@ -179,13 +179,13 @@ public bool TryRead<T>(string compositeKey, string key, out T value)
179
179
/// <param name="key">Key of the object.</param>
180
180
/// <param name="default">Default value of the object.</param>
181
181
/// <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 )
183
183
{
184
- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
184
+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
185
185
{
186
186
if ( composite . TryGetValue ( key , out object valueObj ) && valueObj is string value )
187
187
{
188
- return Serializer . Deserialize < T > ( value ) ;
188
+ return this . Serializer . Deserialize < T > ( value ) ;
189
189
}
190
190
}
191
191
@@ -202,17 +202,17 @@ public T Read<T>(string compositeKey, string key, T @default = default)
202
202
/// <param name="values">Objects to save.</param>
203
203
public void Save < T > ( string compositeKey , IDictionary < string , T > values )
204
204
{
205
- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
205
+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
206
206
{
207
207
foreach ( KeyValuePair < string , T > setting in values )
208
208
{
209
209
if ( composite . ContainsKey ( setting . Key ) )
210
210
{
211
- composite [ setting . Key ] = Serializer . Serialize ( setting . Value ) ;
211
+ composite [ setting . Key ] = this . Serializer . Serialize ( setting . Value ) ;
212
212
}
213
213
else
214
214
{
215
- composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
215
+ composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
216
216
}
217
217
}
218
218
}
@@ -221,10 +221,10 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
221
221
composite = new ApplicationDataCompositeValue ( ) ;
222
222
foreach ( KeyValuePair < string , T > setting in values )
223
223
{
224
- composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
224
+ composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
225
225
}
226
226
227
- Settings . Values [ compositeKey ] = composite ;
227
+ this . Settings . Values [ compositeKey ] = composite ;
228
228
}
229
229
}
230
230
@@ -236,7 +236,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
236
236
/// <returns>A boolean indicator of success.</returns>
237
237
public bool TryDelete ( string compositeKey , string key )
238
238
{
239
- if ( TryRead ( compositeKey , out ApplicationDataCompositeValue composite ) )
239
+ if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
240
240
{
241
241
return composite . Remove ( key ) ;
242
242
}
@@ -245,33 +245,33 @@ public bool TryDelete(string compositeKey, string key)
245
245
}
246
246
247
247
/// <inheritdoc />
248
- public Task < T > ReadFileAsync < T > ( string filePath , T @default = default )
248
+ public Task < T ? > ReadFileAsync < T > ( string filePath , T ? @default = default )
249
249
{
250
- return ReadFileAsync < T > ( Folder , filePath , @default ) ;
250
+ return this . ReadFileAsync < T > ( this . Folder , filePath , @default ) ;
251
251
}
252
252
253
253
/// <inheritdoc />
254
254
public Task < IEnumerable < ( DirectoryItemType ItemType , string Name ) > > ReadFolderAsync ( string folderPath )
255
255
{
256
- return ReadFolderAsync ( Folder , folderPath ) ;
256
+ return this . ReadFolderAsync ( this . Folder , folderPath ) ;
257
257
}
258
258
259
259
/// <inheritdoc />
260
260
public Task CreateFileAsync < T > ( string filePath , T value )
261
261
{
262
- return SaveFileAsync < T > ( Folder , filePath , value ) ;
262
+ return this . SaveFileAsync < T > ( this . Folder , filePath , value ) ;
263
263
}
264
264
265
265
/// <inheritdoc />
266
266
public Task CreateFolderAsync ( string folderPath )
267
267
{
268
- return CreateFolderAsync ( Folder , folderPath ) ;
268
+ return this . CreateFolderAsync ( this . Folder , folderPath ) ;
269
269
}
270
270
271
271
/// <inheritdoc />
272
272
public Task DeleteItemAsync ( string itemPath )
273
273
{
274
- return DeleteItemAsync ( Folder , itemPath ) ;
274
+ return this . DeleteItemAsync ( this . Folder , itemPath ) ;
275
275
}
276
276
277
277
/// <summary>
@@ -283,13 +283,13 @@ public Task DeleteItemAsync(string itemPath)
283
283
/// <returns>Waiting task until completion.</returns>
284
284
public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
285
285
{
286
- return SaveFileAsync < T > ( Folder , filePath , value ) ;
286
+ return this . SaveFileAsync ( this . Folder , filePath , value ) ;
287
287
}
288
288
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 )
290
290
{
291
291
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 ;
293
293
}
294
294
295
295
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
309
309
310
310
private Task < StorageFile > SaveFileAsync < T > ( StorageFolder folder , string filePath , T value )
311
311
{
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 ) ;
313
313
}
314
314
315
315
private async Task CreateFolderAsync ( StorageFolder folder , string folderPath )
0 commit comments