Browse Source

Update API, samples and BCL impl

Max Katz 2 years ago
parent
commit
5968830296

+ 2 - 2
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@@ -324,9 +324,9 @@ namespace ControlCatalog.Pages
                         mappedResults.Add("+> " + FullPathOrName(selectedItem));
                         if (selectedItem is IStorageFolder folder)
                         {
-                            foreach (var innerItems in await folder.GetItemsAsync())
+                            await foreach (var innerItem in folder.GetItemsAsync())
                             {
-                                mappedResults.Add("++> " + FullPathOrName(innerItems));
+                                mappedResults.Add("++> " + FullPathOrName(innerItem));
                             }
                         }
                     }

+ 6 - 2
samples/ControlCatalog/Pages/DragAndDropPage.xaml.cs

@@ -104,8 +104,12 @@ namespace ControlCatalog.Pages
                         }
                         else if (item is IStorageFolder folder)
                         {
-                            var items = await folder.GetItemsAsync();
-                            contentStr += $"Folder {item.Name}: items {items.Count}{Environment.NewLine}{Environment.NewLine}";
+                            var childrenCount = 0;
+                            await foreach (var _ in folder.GetItemsAsync())
+                            {
+                                childrenCount++;
+                            }
+                            contentStr += $"Folder {item.Name}: items {childrenCount}{Environment.NewLine}{Environment.NewLine}";
                         }
                     }
 

+ 7 - 5
src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFolder.cs

@@ -57,14 +57,16 @@ internal class BclStorageFolder : IStorageBookmarkFolder
         return Task.FromResult<IStorageFolder?>(null);
     }
 
-    public Task<IReadOnlyList<IStorageItem>> GetItemsAsync()
+    public async IAsyncEnumerable<IStorageItem> GetItemsAsync()
     {
-         var items = DirectoryInfo.GetDirectories()
+        var items = DirectoryInfo.EnumerateDirectories()
             .Select(d => (IStorageItem)new BclStorageFolder(d))
-            .Concat(DirectoryInfo.GetFiles().Select(f => new BclStorageFile(f)))
-            .ToArray();
+            .Concat(DirectoryInfo.EnumerateFiles().Select(f => new BclStorageFile(f)));
 
-         return Task.FromResult<IReadOnlyList<IStorageItem>>(items);
+        foreach (var item in items)
+        {
+            yield return item;
+        }
     }
 
     public virtual Task<string?> SaveBookmarkAsync()

+ 1 - 1
src/Avalonia.Base/Platform/Storage/IStorageFolder.cs

@@ -16,5 +16,5 @@ public interface IStorageFolder : IStorageItem
     /// <returns>
     /// When this method completes successfully, it returns a list of the files and folders in the current folder. Each item in the list is represented by an <see cref="IStorageItem"/> implementation object.
     /// </returns>
-    Task<IReadOnlyList<IStorageItem>> GetItemsAsync();
+    IAsyncEnumerable<IStorageItem> GetItemsAsync();
 }