Skip to content

Commit e36c955

Browse files
committed
PR updates
1 parent d9e43fb commit e36c955

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ public void Delete(string compositeKey, string key)
164164
}
165165

166166
/// <inheritdoc />
167-
public Task<bool> FileExistsAsync(string filePath)
167+
public Task<bool> ItemExistsAsync(string itemName)
168168
{
169-
return FileExistsAsync(DefaultFolder, filePath);
169+
return ItemExistsAsync(DefaultFolder, itemName);
170170
}
171171

172172
/// <inheritdoc />
@@ -176,7 +176,7 @@ public Task<T> ReadFileAsync<T>(string filePath, T @default = default)
176176
}
177177

178178
/// <inheritdoc />
179-
public Task<IList<string>> ReadFolderAsync(string folderPath)
179+
public Task<IList<Tuple<DirectoryItemType, string>>> ReadFolderAsync(string folderPath)
180180
{
181181
return ReadFolderAsync(DefaultFolder, folderPath);
182182
}
@@ -199,9 +199,27 @@ public Task DeleteItemAsync(string itemPath)
199199
return DeleteItemAsync(DefaultFolder, itemPath);
200200
}
201201

202-
private Task<bool> FileExistsAsync(StorageFolder folder, string filePath)
202+
/// <summary>
203+
/// Determine the existance of a file at the specified path.
204+
/// To check for folders, use <see cref="ItemExistsAsync(string)" />.
205+
/// </summary>
206+
/// <param name="fileName">The name of the file.</param>
207+
/// <param name="isRecursive">Whether the file should be searched for recursively.</param>
208+
/// <returns>A task with the result of the file query.</returns>
209+
public Task<bool> FileExistsAsync(string fileName, bool isRecursive = false)
210+
{
211+
return FileExistsAsync(DefaultFolder, fileName, isRecursive);
212+
}
213+
214+
private async Task<bool> ItemExistsAsync(StorageFolder folder, string itemName)
203215
{
204-
return folder.FileExistsAsync(filePath);
216+
var item = await folder.TryGetItemAsync(itemName);
217+
return item != null;
218+
}
219+
220+
private Task<bool> FileExistsAsync(StorageFolder folder, string fileName, bool isRecursive)
221+
{
222+
return folder.FileExistsAsync(fileName, isRecursive);
205223
}
206224

207225
private async Task<T> ReadFileAsync<T>(StorageFolder folder, string filePath, T @default = default)
@@ -210,11 +228,19 @@ private async Task<T> ReadFileAsync<T>(StorageFolder folder, string filePath, T
210228
return (value != null) ? _serializer.Deserialize<T>(value) : @default;
211229
}
212230

213-
private async Task<IList<string>> ReadFolderAsync(StorageFolder folder, string folderPath)
231+
private async Task<IList<Tuple<DirectoryItemType, string>>> ReadFolderAsync(StorageFolder folder, string folderPath)
214232
{
215233
var targetFolder = await folder.GetFolderAsync(folderPath);
216-
var files = await targetFolder.GetFilesAsync();
217-
return files.Select((f) => f.Path + f.Name).ToList();
234+
var items = await targetFolder.GetItemsAsync();
235+
236+
return items.Select((item) =>
237+
{
238+
var itemType = item.IsOfType(StorageItemTypes.File) ? DirectoryItemType.File
239+
: item.IsOfType(StorageItemTypes.Folder) ? DirectoryItemType.Folder
240+
: DirectoryItemType.None;
241+
242+
return new Tuple<DirectoryItemType, string>(itemType, item.Name);
243+
}).ToList();
218244
}
219245

220246
private Task SaveFileAsync<T>(StorageFolder folder, string filePath, T value)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Toolkit.Uwp.Helpers
1010
/// <summary>
1111
/// Store data in the Local environment (only on the current device).
1212
/// </summary>
13-
[Obsolete("LocalObjectStorageHelper is deprecated and has been superceded by the ApplicationDataStorageHelper.")]
13+
[Obsolete("LocalObjectStorageHelper is deprecated and has been superceded by the ApplicationDataStorageHelper. To upgrade, simply swap any LocalObjectStorageHelper instances with ApplicationDataStorageHelper.GetCurrent(serializer). The underlying interfaces are nearly identical but now with even more features available, such as deletion and access to user specific data stores!")]
1414
public class LocalObjectStorageHelper : BaseObjectStorageHelper
1515
{
1616
/// <summary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.Toolkit.Helpers
6+
{
7+
/// <summary>
8+
/// Represents the types of items available in a directory.
9+
/// </summary>
10+
public enum DirectoryItemType
11+
{
12+
/// <summary>
13+
/// The item is neither a file or a folder.
14+
/// </summary>
15+
None,
16+
17+
/// <summary>
18+
/// Represents a file type item.
19+
/// </summary>
20+
File,
21+
22+
/// <summary>
23+
/// Represents a folder type item.
24+
/// </summary>
25+
Folder
26+
}
27+
}

Microsoft.Toolkit/Helpers/ObjectStorage/IFileStorageHelper.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Threading.Tasks;
78

89
namespace Microsoft.Toolkit.Helpers
910
{
1011
/// <summary>
11-
/// Service interface used to store data in files and folders.
12+
/// Service interface used to store data in a directory/file-system via files and folders.
13+
///
14+
/// This interface is meant to help abstract file storage operations across platforms in a library,
15+
/// but the actual behavior will be up to the implementer. Such as, we don't provide a sense of a current directory,
16+
/// so an implementor should consider using full paths to support any file operations. Otherwise, a "directory aware"
17+
/// implementation could be achieved with a current directory field and traversal functions, in which case relative paths would be applicable.
1218
/// </summary>
1319
public interface IFileStorageHelper
1420
{
1521
/// <summary>
16-
/// Determines whether a file already exists.
22+
/// Determines if a directory item already exists.
1723
/// </summary>
18-
/// <param name="filePath">Key of the file (that contains object).</param>
19-
/// <returns>True if a value exists.</returns>
20-
Task<bool> FileExistsAsync(string filePath);
24+
/// <param name="itemName">Key of the file.</param>
25+
/// <returns>True if an item exists.</returns>
26+
Task<bool> ItemExistsAsync(string itemName);
2127

2228
/// <summary>
2329
/// Retrieves an object from a file.
@@ -29,11 +35,11 @@ public interface IFileStorageHelper
2935
Task<T> ReadFileAsync<T>(string filePath, T? @default = default);
3036

3137
/// <summary>
32-
/// Retrieves all file listings for a folder.
38+
/// Retrieves the listings for a folder and the item types.
3339
/// </summary>
3440
/// <param name="folderPath">The path to the target folder.</param>
35-
/// <returns>A list of file names in the target folder.</returns>
36-
Task<IList<string>> ReadFolderAsync(string folderPath);
41+
/// <returns>A list of file types and names in the target folder.</returns>
42+
Task<IList<Tuple<DirectoryItemType, string>>> ReadFolderAsync(string folderPath);
3743

3844
/// <summary>
3945
/// Saves an object inside a file.
@@ -45,7 +51,7 @@ public interface IFileStorageHelper
4551
Task SaveFileAsync<T>(string filePath, T value);
4652

4753
/// <summary>
48-
/// Saves a folder.
54+
/// Ensure a folder exists at the folder path specified.
4955
/// </summary>
5056
/// <param name="folderPath">The path and name of the target folder.</param>
5157
/// <returns>Waiting task until completion.</returns>

0 commit comments

Comments
 (0)