Async core refactor
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -8,15 +9,19 @@ namespace FileTime.Providers.Local
|
||||
public class LocalContentProvider : IContentProvider
|
||||
{
|
||||
private readonly ILogger<LocalContentProvider> _logger;
|
||||
private IContainer _parent = null;
|
||||
private IContainer? _parent;
|
||||
|
||||
public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
private readonly IReadOnlyList<IContainer> _rootContainers;
|
||||
private readonly IReadOnlyList<IItem>? _items;
|
||||
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
|
||||
|
||||
/* public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
|
||||
public IReadOnlyList<IItem> Items => RootContainers;
|
||||
|
||||
public IReadOnlyList<IContainer> Containers => RootContainers;
|
||||
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>();
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>(); */
|
||||
|
||||
public string Name { get; } = "local";
|
||||
|
||||
@@ -25,7 +30,7 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public IContentProvider Provider => this;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public bool IsCaseInsensitive { get; }
|
||||
|
||||
@@ -41,13 +46,14 @@ namespace FileTime.Providers.Local
|
||||
|
||||
FullName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "" : null;
|
||||
|
||||
RootContainers = rootDirectories.Select(d => new LocalFolder(d, this, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
_rootContainers = rootDirectories.Select(d => new LocalFolder(d, this, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
_items = _rootContainers.Cast<IItem>().ToList().AsReadOnly();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var pathParts = (IsCaseInsensitive ? path.ToLower() : path).TrimStart(Constants.SeparatorChar).Split(Constants.SeparatorChar);
|
||||
var rootContainer = RootContainers.FirstOrDefault(c => NormalizePath(c.Name) == NormalizePath(pathParts[0]));
|
||||
var rootContainer = _rootContainers.FirstOrDefault(c => NormalizePath(c.Name) == NormalizePath(pathParts[0]));
|
||||
|
||||
if (rootContainer == null)
|
||||
{
|
||||
@@ -55,27 +61,43 @@ namespace FileTime.Providers.Local
|
||||
return null;
|
||||
}
|
||||
|
||||
return rootContainer.GetByPath(string.Join(Constants.SeparatorChar, pathParts.Skip(1)));
|
||||
return await rootContainer.GetByPath(string.Join(Constants.SeparatorChar, pathParts.Skip(1)));
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
}
|
||||
public Task Refresh() => Task.CompletedTask;
|
||||
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
public IContainer CreateContainer(string name) => throw new NotSupportedException();
|
||||
public IElement CreateElement(string name) => throw new NotSupportedException();
|
||||
public bool IsExists(string name) => Items.Any(i => i.Name == name);
|
||||
public Task<IContainer> CreateContainer(string name) => throw new NotSupportedException();
|
||||
public Task<IElement> CreateElement(string name) => throw new NotSupportedException();
|
||||
public Task<bool> IsExists(string name) => Task.FromResult(_rootContainers.Any(i => i.Name == name));
|
||||
|
||||
public void Delete() => throw new NotSupportedException();
|
||||
public Task Delete() => throw new NotSupportedException();
|
||||
|
||||
internal string NormalizePath(string path) => IsCaseInsensitive ? path.ToLower() : path;
|
||||
|
||||
public bool CanHandlePath(string path) => RootContainers.Any(r => path.StartsWith(r.Name));
|
||||
public bool CanHandlePath(string path) => _rootContainers.Any(r => path.StartsWith(r.Name));
|
||||
|
||||
public void SetParent(IContainer container)
|
||||
{
|
||||
_parent = container;
|
||||
}
|
||||
public Task<IReadOnlyList<IContainer>?> GetRootContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainers);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_items);
|
||||
}
|
||||
public Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainers);
|
||||
}
|
||||
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_elements);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,10 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public string GetPrimaryAttributeText() => _file.Length.ToSizeString();
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
_file.Delete();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
|
||||
@@ -15,44 +16,11 @@ namespace FileTime.Providers.Local
|
||||
public LocalContentProvider Provider { get; }
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string FullName { get; }
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public LocalFolder(DirectoryInfo directory, LocalContentProvider contentProvider, IContainer? parent)
|
||||
{
|
||||
@@ -66,7 +34,7 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public void Refresh()
|
||||
public Task Refresh()
|
||||
{
|
||||
_containers = new List<IContainer>();
|
||||
_elements = new List<IElement>();
|
||||
@@ -79,14 +47,32 @@ namespace FileTime.Providers.Local
|
||||
catch { }
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_items == null) await Refresh();
|
||||
return _items;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_containers == null) await Refresh();
|
||||
return _containers;
|
||||
}
|
||||
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
if (_elements == null) await Refresh();
|
||||
return _elements;
|
||||
}
|
||||
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(paths[0]));
|
||||
var item = (await GetItems())!.FirstOrDefault(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(paths[0]));
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
@@ -95,29 +81,33 @@ namespace FileTime.Providers.Local
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
return await container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public IContainer CreateContainer(string name)
|
||||
public async Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
Directory.CreateSubdirectory(name);
|
||||
Refresh();
|
||||
await Refresh();
|
||||
|
||||
return _containers!.FirstOrDefault(c => Provider.NormalizePath(c.Name) == Provider.NormalizePath(name))!;
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public async Task<IElement> CreateElement(string name)
|
||||
{
|
||||
using (File.Create(Path.Combine(Directory.FullName, name))) { }
|
||||
Refresh();
|
||||
await Refresh();
|
||||
|
||||
return _elements!.FirstOrDefault(e => Provider.NormalizePath(e.Name) == Provider.NormalizePath(name))!;
|
||||
}
|
||||
|
||||
public bool IsExists(string name) => Items.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name));
|
||||
public async Task<bool> IsExists(string name) => (await GetItems())?.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name)) ?? false;
|
||||
|
||||
public void Delete() => Directory.Delete(true);
|
||||
public Task Delete()
|
||||
{
|
||||
Directory.Delete(true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user