4
4
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . IO ;
7
8
using System . Linq ;
8
9
using System . Threading . Tasks ;
9
10
using Microsoft . Toolkit . Helpers ;
@@ -26,19 +27,19 @@ public partial class ApplicationDataStorageHelper : IFileStorageHelper, ISetting
26
27
/// <param name="objectSerializer">Serializer for converting stored values. Defaults to <see cref="Toolkit.Helpers.SystemSerializer"/>.</param>
27
28
public ApplicationDataStorageHelper ( ApplicationData appData , Toolkit . Helpers . IObjectSerializer ? objectSerializer = null )
28
29
{
29
- this . AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
30
- this . Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
30
+ AppData = appData ?? throw new ArgumentNullException ( nameof ( appData ) ) ;
31
+ Serializer = objectSerializer ?? new Toolkit . Helpers . SystemSerializer ( ) ;
31
32
}
32
33
33
34
/// <summary>
34
35
/// Gets the settings container.
35
36
/// </summary>
36
- public ApplicationDataContainer Settings => this . AppData . LocalSettings ;
37
+ public ApplicationDataContainer Settings => AppData . LocalSettings ;
37
38
38
39
/// <summary>
39
40
/// Gets the storage folder.
40
41
/// </summary>
41
- public StorageFolder Folder => this . AppData . LocalFolder ;
42
+ public StorageFolder Folder => AppData . LocalFolder ;
42
43
43
44
/// <summary>
44
45
/// Gets the storage host.
@@ -80,7 +81,7 @@ public static async Task<ApplicationDataStorageHelper> GetForUserAsync(User user
80
81
/// <returns>True if a value exists.</returns>
81
82
public bool KeyExists ( string key )
82
83
{
83
- return this . Settings . Values . ContainsKey ( key ) ;
84
+ return Settings . Values . ContainsKey ( key ) ;
84
85
}
85
86
86
87
/// <summary>
@@ -92,9 +93,9 @@ public bool KeyExists(string key)
92
93
/// <returns>The TValue object.</returns>
93
94
public T ? Read < T > ( string key , T ? @default = default )
94
95
{
95
- if ( this . Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
96
+ if ( Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
96
97
{
97
- return this . Serializer . Deserialize < T > ( valueString ) ;
98
+ return Serializer . Deserialize < T > ( valueString ) ;
98
99
}
99
100
100
101
return @default ;
@@ -103,9 +104,9 @@ public bool KeyExists(string key)
103
104
/// <inheritdoc />
104
105
public bool TryRead < T > ( string key , out T ? value )
105
106
{
106
- if ( this . Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
107
+ if ( Settings . Values . TryGetValue ( key , out var valueObj ) && valueObj is string valueString )
107
108
{
108
- value = this . Serializer . Deserialize < T > ( valueString ) ;
109
+ value = Serializer . Deserialize < T > ( valueString ) ;
109
110
return true ;
110
111
}
111
112
@@ -116,19 +117,19 @@ public bool TryRead<T>(string key, out T? value)
116
117
/// <inheritdoc />
117
118
public void Save < T > ( string key , T value )
118
119
{
119
- this . Settings . Values [ key ] = this . Serializer . Serialize ( value ) ;
120
+ Settings . Values [ key ] = Serializer . Serialize ( value ) ;
120
121
}
121
122
122
123
/// <inheritdoc />
123
124
public bool TryDelete ( string key )
124
125
{
125
- return this . Settings . Values . Remove ( key ) ;
126
+ return Settings . Values . Remove ( key ) ;
126
127
}
127
128
128
129
/// <inheritdoc />
129
130
public void Clear ( )
130
131
{
131
- this . Settings . Values . Clear ( ) ;
132
+ Settings . Values . Clear ( ) ;
132
133
}
133
134
134
135
/// <summary>
@@ -139,7 +140,7 @@ public void Clear()
139
140
/// <returns>True if a value exists.</returns>
140
141
public bool KeyExists ( string compositeKey , string key )
141
142
{
142
- if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
143
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
143
144
{
144
145
return composite . ContainsKey ( key ) ;
145
146
}
@@ -157,12 +158,12 @@ public bool KeyExists(string compositeKey, string key)
157
158
/// <returns>The T object.</returns>
158
159
public bool TryRead < T > ( string compositeKey , string key , out T ? value )
159
160
{
160
- if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
161
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
161
162
{
162
163
string compositeValue = ( string ) composite [ key ] ;
163
164
if ( compositeValue != null )
164
165
{
165
- value = this . Serializer . Deserialize < T > ( compositeValue ) ;
166
+ value = Serializer . Deserialize < T > ( compositeValue ) ;
166
167
return true ;
167
168
}
168
169
}
@@ -181,11 +182,11 @@ public bool TryRead<T>(string compositeKey, string key, out T? value)
181
182
/// <returns>The T object.</returns>
182
183
public T ? Read < T > ( string compositeKey , string key , T ? @default = default )
183
184
{
184
- if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
185
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
185
186
{
186
187
if ( composite . TryGetValue ( key , out object valueObj ) && valueObj is string value )
187
188
{
188
- return this . Serializer . Deserialize < T > ( value ) ;
189
+ return Serializer . Deserialize < T > ( value ) ;
189
190
}
190
191
}
191
192
@@ -202,17 +203,17 @@ public bool TryRead<T>(string compositeKey, string key, out T? value)
202
203
/// <param name="values">Objects to save.</param>
203
204
public void Save < T > ( string compositeKey , IDictionary < string , T > values )
204
205
{
205
- if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
206
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
206
207
{
207
208
foreach ( KeyValuePair < string , T > setting in values )
208
209
{
209
210
if ( composite . ContainsKey ( setting . Key ) )
210
211
{
211
- composite [ setting . Key ] = this . Serializer . Serialize ( setting . Value ) ;
212
+ composite [ setting . Key ] = Serializer . Serialize ( setting . Value ) ;
212
213
}
213
214
else
214
215
{
215
- composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
216
+ composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
216
217
}
217
218
}
218
219
}
@@ -221,10 +222,10 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
221
222
composite = new ApplicationDataCompositeValue ( ) ;
222
223
foreach ( KeyValuePair < string , T > setting in values )
223
224
{
224
- composite . Add ( setting . Key , this . Serializer . Serialize ( setting . Value ) ) ;
225
+ composite . Add ( setting . Key , Serializer . Serialize ( setting . Value ) ) ;
225
226
}
226
227
227
- this . Settings . Values [ compositeKey ] = composite ;
228
+ Settings . Values [ compositeKey ] = composite ;
228
229
}
229
230
}
230
231
@@ -236,7 +237,7 @@ public void Save<T>(string compositeKey, IDictionary<string, T> values)
236
237
/// <returns>A boolean indicator of success.</returns>
237
238
public bool TryDelete ( string compositeKey , string key )
238
239
{
239
- if ( this . TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
240
+ if ( TryRead ( compositeKey , out ApplicationDataCompositeValue ? composite ) && composite != null )
240
241
{
241
242
return composite . Remove ( key ) ;
242
243
}
@@ -247,54 +248,48 @@ public bool TryDelete(string compositeKey, string key)
247
248
/// <inheritdoc />
248
249
public Task < T ? > ReadFileAsync < T > ( string filePath , T ? @default = default )
249
250
{
250
- return this . ReadFileAsync < T > ( this . Folder , filePath , @default ) ;
251
+ return ReadFileAsync < T > ( Folder , filePath , @default ) ;
251
252
}
252
253
253
254
/// <inheritdoc />
254
255
public Task < IEnumerable < ( DirectoryItemType ItemType , string Name ) > > ReadFolderAsync ( string folderPath )
255
256
{
256
- return this . ReadFolderAsync ( this . Folder , folderPath ) ;
257
+ return ReadFolderAsync ( Folder , folderPath ) ;
257
258
}
258
259
259
260
/// <inheritdoc />
260
261
public Task CreateFileAsync < T > ( string filePath , T value )
261
262
{
262
- return this . SaveFileAsync < T > ( this . Folder , filePath , value ) ;
263
+ return CreateFileAsync < T > ( Folder , filePath , value ) ;
263
264
}
264
265
265
266
/// <inheritdoc />
266
267
public Task CreateFolderAsync ( string folderPath )
267
268
{
268
- return this . CreateFolderAsync ( this . Folder , folderPath ) ;
269
+ return CreateFolderAsync ( Folder , folderPath ) ;
269
270
}
270
271
271
272
/// <inheritdoc />
272
- public Task DeleteItemAsync ( string itemPath )
273
+ public Task < bool > TryDeleteItemAsync ( string itemPath )
273
274
{
274
- return this . DeleteItemAsync ( this . Folder , itemPath ) ;
275
+ return TryDeleteItemAsync ( Folder , itemPath ) ;
275
276
}
276
277
277
- /// <summary>
278
- /// Saves an object inside a file.
279
- /// </summary>
280
- /// <typeparam name="T">Type of object saved.</typeparam>
281
- /// <param name="filePath">Path to the file that will contain the object.</param>
282
- /// <param name="value">Object to save.</param>
283
- /// <returns>Waiting task until completion.</returns>
284
- public Task < StorageFile > SaveFileAsync < T > ( string filePath , T value )
278
+ /// <inheritdoc />
279
+ public Task < bool > TryRenameItemAsync ( string itemPath , string newName )
285
280
{
286
- return this . SaveFileAsync ( this . Folder , filePath , value ) ;
281
+ return TryRenameItemAsync ( Folder , itemPath , newName ) ;
287
282
}
288
283
289
284
private async Task < T ? > ReadFileAsync < T > ( StorageFolder folder , string filePath , T ? @default = default )
290
285
{
291
- string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , filePath ) ;
292
- return ( value != null ) ? this . Serializer . Deserialize < T > ( value ) : @default ;
286
+ string value = await StorageFileHelper . ReadTextFromFileAsync ( folder , NormalizePath ( filePath ) ) ;
287
+ return ( value != null ) ? Serializer . Deserialize < T > ( value ) : @default ;
293
288
}
294
289
295
290
private async Task < IEnumerable < ( DirectoryItemType , string ) > > ReadFolderAsync ( StorageFolder folder , string folderPath )
296
291
{
297
- var targetFolder = await folder . GetFolderAsync ( folderPath ) ;
292
+ var targetFolder = await folder . GetFolderAsync ( NormalizePath ( folderPath ) ) ;
298
293
var items = await targetFolder . GetItemsAsync ( ) ;
299
294
300
295
return items . Select ( ( item ) =>
@@ -307,20 +302,47 @@ public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
307
302
} ) ;
308
303
}
309
304
310
- private Task < StorageFile > SaveFileAsync < T > ( StorageFolder folder , string filePath , T value )
305
+ private async Task < StorageFile > CreateFileAsync < T > ( StorageFolder folder , string filePath , T value )
311
306
{
312
- return StorageFileHelper . WriteTextToFileAsync ( folder , this . Serializer . Serialize ( value ) ? . ToString ( ) , filePath , CreationCollisionOption . ReplaceExisting ) ;
307
+ return await StorageFileHelper . WriteTextToFileAsync ( folder , Serializer . Serialize ( value ) ? . ToString ( ) , NormalizePath ( filePath ) , CreationCollisionOption . ReplaceExisting ) ;
313
308
}
314
309
315
310
private async Task CreateFolderAsync ( StorageFolder folder , string folderPath )
316
311
{
317
- await folder . CreateFolderAsync ( folderPath , CreationCollisionOption . OpenIfExists ) ;
312
+ await folder . CreateFolderAsync ( NormalizePath ( folderPath ) , CreationCollisionOption . OpenIfExists ) ;
313
+ }
314
+
315
+ private async Task < bool > TryDeleteItemAsync ( StorageFolder folder , string itemPath )
316
+ {
317
+ try
318
+ {
319
+ var item = await folder . GetItemAsync ( NormalizePath ( itemPath ) ) ;
320
+ await item . DeleteAsync ( ) ;
321
+ return true ;
322
+ }
323
+ catch
324
+ {
325
+ return false ;
326
+ }
327
+ }
328
+
329
+ private async Task < bool > TryRenameItemAsync ( StorageFolder folder , string itemPath , string newName )
330
+ {
331
+ try
332
+ {
333
+ var item = await folder . GetItemAsync ( NormalizePath ( itemPath ) ) ;
334
+ await item . RenameAsync ( newName , NameCollisionOption . FailIfExists ) ;
335
+ return true ;
336
+ }
337
+ catch
338
+ {
339
+ return false ;
340
+ }
318
341
}
319
342
320
- private async Task DeleteItemAsync ( StorageFolder folder , string itemPath )
343
+ private string NormalizePath ( string path )
321
344
{
322
- var item = await folder . GetItemAsync ( itemPath ) ;
323
- await item . DeleteAsync ( ) ;
345
+ return Path . Combine ( Path . GetDirectoryName ( path ) , Path . GetFileName ( path ) ) ;
324
346
}
325
347
}
326
348
}
0 commit comments