Skip to content

Commit 5564df3

Browse files
committed
Added TryRenameItemAsync and TryDeleteItemAsync to IFileStorageHelper
1 parent ee93385 commit 5564df3

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.CacheFolder.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public partial class ApplicationDataStorageHelper
2929
/// <returns>Waiting task until completion with the object in the file.</returns>
3030
public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
3131
{
32-
return ReadFileAsync<T>(CacheFolder, filePath, @default);
32+
return ReadFileAsync<T>(this.CacheFolder, filePath, @default);
3333
}
3434

3535
/// <summary>
@@ -39,7 +39,7 @@ public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
3939
/// <returns>A list of file types and names in the target folder.</returns>
4040
public Task<IEnumerable<(DirectoryItemType ItemType, string Name)>> ReadCacheFolderAsync(string folderPath)
4141
{
42-
return ReadFolderAsync(CacheFolder, folderPath);
42+
return ReadFolderAsync(this.CacheFolder, folderPath);
4343
}
4444

4545
/// <summary>
@@ -51,7 +51,7 @@ public Task<T> ReadCacheFileAsync<T>(string filePath, T @default = default)
5151
/// <returns>Waiting task until completion.</returns>
5252
public Task CreateCacheFileAsync<T>(string filePath, T value)
5353
{
54-
return SaveFileAsync<T>(CacheFolder, filePath, value);
54+
return CreateFileAsync<T>(this.CacheFolder, filePath, value);
5555
}
5656

5757
/// <summary>
@@ -61,17 +61,28 @@ public Task CreateCacheFileAsync<T>(string filePath, T value)
6161
/// <returns>Waiting task until completion.</returns>
6262
public Task CreateCacheFolderAsync(string folderPath)
6363
{
64-
return CreateFolderAsync(CacheFolder, folderPath);
64+
return CreateFolderAsync(this.CacheFolder, folderPath);
6565
}
6666

6767
/// <summary>
6868
/// Deletes a file or folder item in the LocalCacheFolder.
6969
/// </summary>
7070
/// <param name="itemPath">The path to the item for deletion.</param>
7171
/// <returns>Waiting task until completion.</returns>
72-
public Task DeleteCacheItemAsync(string itemPath)
72+
public Task<bool> TryDeleteCacheItemAsync(string itemPath)
7373
{
74-
return DeleteItemAsync(CacheFolder, itemPath);
74+
return TryDeleteItemAsync(CacheFolder, itemPath);
75+
}
76+
77+
/// <summary>
78+
/// Rename an item in the LocalCacheFolder.
79+
/// </summary>
80+
/// <param name="itemPath">The path to the target item.</param>
81+
/// <param name="newName">The new nam for the target item.</param>
82+
/// <returns>Waiting task until completion.</returns>
83+
public Task<bool> TryRenameCacheItemAsync(string itemPath, string newName)
84+
{
85+
return TryRenameItemAsync(this.CacheFolder, itemPath, newName);
7586
}
7687
}
7788
}

Microsoft.Toolkit.Uwp/Helpers/ObjectStorage/ApplicationDataStorageHelper.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public bool TryDelete(string compositeKey, string key)
259259
/// <inheritdoc />
260260
public Task CreateFileAsync<T>(string filePath, T value)
261261
{
262-
return this.SaveFileAsync<T>(this.Folder, filePath, value);
262+
return this.CreateFileAsync<T>(this.Folder, filePath, value);
263263
}
264264

265265
/// <inheritdoc />
@@ -269,21 +269,15 @@ public Task CreateFolderAsync(string folderPath)
269269
}
270270

271271
/// <inheritdoc />
272-
public Task DeleteItemAsync(string itemPath)
272+
public Task<bool> TryDeleteItemAsync(string itemPath)
273273
{
274-
return this.DeleteItemAsync(this.Folder, itemPath);
274+
return TryDeleteItemAsync(this.Folder, itemPath);
275275
}
276276

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)
277+
/// <inheritdoc />
278+
public Task<bool> TryRenameItemAsync(string itemPath, string newName)
285279
{
286-
return this.SaveFileAsync(this.Folder, filePath, value);
280+
return TryRenameItemAsync(this.Folder, itemPath, newName);
287281
}
288282

289283
private async Task<T?> ReadFileAsync<T>(StorageFolder folder, string filePath, T? @default = default)
@@ -307,7 +301,7 @@ public Task<StorageFile> SaveFileAsync<T>(string filePath, T value)
307301
});
308302
}
309303

310-
private Task<StorageFile> SaveFileAsync<T>(StorageFolder folder, string filePath, T value)
304+
private Task<StorageFile> CreateFileAsync<T>(StorageFolder folder, string filePath, T value)
311305
{
312306
return StorageFileHelper.WriteTextToFileAsync(folder, this.Serializer.Serialize(value)?.ToString(), filePath, CreationCollisionOption.ReplaceExisting);
313307
}
@@ -317,10 +311,32 @@ private async Task CreateFolderAsync(StorageFolder folder, string folderPath)
317311
await folder.CreateFolderAsync(folderPath, CreationCollisionOption.OpenIfExists);
318312
}
319313

320-
private async Task DeleteItemAsync(StorageFolder folder, string itemPath)
314+
private async Task<bool> TryDeleteItemAsync(StorageFolder folder, string itemPath)
321315
{
322-
var item = await folder.GetItemAsync(itemPath);
323-
await item.DeleteAsync();
316+
try
317+
{
318+
var item = await folder.GetItemAsync(itemPath);
319+
await item.DeleteAsync();
320+
return true;
321+
}
322+
catch
323+
{
324+
return false;
325+
}
326+
}
327+
328+
private async Task<bool> TryRenameItemAsync(StorageFolder folder, string itemPath, string newName)
329+
{
330+
try
331+
{
332+
var item = await folder.GetItemAsync(itemPath);
333+
await item.RenameAsync(newName, NameCollisionOption.FailIfExists);
334+
return true;
335+
}
336+
catch
337+
{
338+
return false;
339+
}
324340
}
325341
}
326342
}

Microsoft.Toolkit/Helpers/ObjectStorage/IFileStorageHelper.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public interface IFileStorageHelper
5555
/// </summary>
5656
/// <param name="itemPath">The path to the item for deletion.</param>
5757
/// <returns>Waiting task until completion.</returns>
58-
Task DeleteItemAsync(string itemPath);
58+
Task<bool> TryDeleteItemAsync(string itemPath);
59+
60+
/// <summary>
61+
/// Rename an item.
62+
/// </summary>
63+
/// <param name="itemPath">The path to the target item.</param>
64+
/// <param name="newName">The new nam for the target item.</param>
65+
/// <returns>Waiting task until completion.</returns>
66+
Task<bool> TryRenameItemAsync(string itemPath, string newName);
5967
}
6068
}

0 commit comments

Comments
 (0)