Timeless refactor

This commit is contained in:
2022-05-21 14:30:24 +02:00
parent ced0c88a10
commit 6ee5afa632
47 changed files with 571 additions and 181 deletions

View File

@@ -0,0 +1,8 @@
namespace FileTime.Core.Command;
public enum CanCommandRun
{
True,
False,
Forcable
}

View File

@@ -1,6 +1,9 @@
using FileTime.Core.Timeline;
namespace FileTime.Core.Command;
public interface ICommand
{
Task<CanCommandRun> CanRun(PointInTime currentTime);
Task<PointInTime?> SimulateCommand(PointInTime? currentTime);
}

View File

@@ -0,0 +1,8 @@
using FileTime.Core.Timeline;
namespace FileTime.Core.Command;
public interface IExecutableCommand : ICommand
{
Task Execute(ICommandScheduler commandScheduler);
}

View File

@@ -0,0 +1,53 @@
using FileTime.Core.Enums;
using FileTime.Core.Timeline;
namespace FileTime.Core.Models;
public class AbsolutePath
{
public ITimelessContentProvider TimelessProvider { get; }
public PointInTime PointInTime { get; }
public FullName Path { get; }
public AbsolutePathType Type { get; }
public AbsolutePath(
ITimelessContentProvider timelessProvider,
PointInTime pointInTime,
FullName path,
AbsolutePathType type)
{
TimelessProvider = timelessProvider;
Path = path;
Type = type;
PointInTime = pointInTime;
}
public AbsolutePath(ITimelessContentProvider timelessProvider, IItem item)
{
TimelessProvider = timelessProvider;
PointInTime = item.PointInTime;
Path = item.FullName ?? throw new ArgumentException($"{nameof(item.FullName)} can not be null.", nameof(item));
Type = item.Type;
}
public async Task<IItem> ResolveAsync(bool forceResolve = false,
ItemInitializationSettings itemInitializationSettings = default)
{
return await TimelessProvider.GetItemByFullNameAsync(Path, PointInTime, forceResolve, Type,
itemInitializationSettings);
}
public async Task<IItem?> ResolveAsyncSafe(bool forceResolve = false,
ItemInitializationSettings itemInitializationSettings = default)
{
try
{
return await ResolveAsync(forceResolve, itemInitializationSettings);
}
catch
{
return null;
}
}
}

View File

@@ -1,15 +0,0 @@
using FileTime.Core.Enums;
using FileTime.Core.Services;
namespace FileTime.Core.Models;
public interface IAbsolutePath
{
IContentProvider ContentProvider { get; }
IContentProvider? VirtualContentProvider { get; }
FullName Path { get; }
AbsolutePathType Type { get; }
Task<IItem> ResolveAsync(bool forceResolve = false, ItemInitializationSettings itemInitializationSettings = default);
Task<IItem?> ResolveAsyncSafe(bool forceResolve = false, ItemInitializationSettings itemInitializationSettings = default);
}

View File

@@ -4,6 +4,6 @@ namespace FileTime.Core.Models;
public interface IContainer : IItem
{
IObservable<IObservable<IChangeSet<IAbsolutePath>>?> Items { get; }
IObservable<IObservable<IChangeSet<AbsolutePath>>?> Items { get; }
IObservable<bool> IsLoading { get; }
}

View File

@@ -1,6 +1,7 @@
using System.Reactive.Linq;
using FileTime.Core.Enums;
using FileTime.Core.Services;
using FileTime.Core.Timeline;
namespace FileTime.Core.Models;
@@ -10,7 +11,7 @@ public interface IItem
string DisplayName { get; }
FullName? FullName { get; }
NativePath? NativePath { get; }
IAbsolutePath? Parent { get; }
AbsolutePath? Parent { get; }
bool IsHidden { get; }
bool IsExists { get; }
DateTime? CreatedAt { get; }
@@ -19,6 +20,7 @@ public interface IItem
IContentProvider Provider { get; }
string? Attributes { get; }
AbsolutePathType Type { get; }
PointInTime PointInTime { get; }
IObservable<IEnumerable<Exception>> Exceptions { get; }
ReadOnlyExtensionCollection Extensions { get; }

View File

@@ -1,6 +1,7 @@
using FileTime.Core.Behaviors;
using FileTime.Core.Enums;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Services;
@@ -8,17 +9,18 @@ public interface IContentProvider : IContainer, IOnContainerEnter
{
Task<IItem> GetItemByFullNameAsync(
FullName fullName,
PointInTime pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default);
Task<IItem> GetItemByNativePathAsync(
NativePath nativePath,
Task<IItem> GetItemByNativePathAsync(NativePath nativePath,
PointInTime pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default);
Task<List<IAbsolutePath>> GetItemsByContainerAsync(FullName fullName);
Task<List<AbsolutePath>> GetItemsByContainerAsync(FullName fullName, PointInTime pointInTime);
NativePath GetNativePath(FullName fullName);
Task<byte[]?> GetContentAsync(IElement element, int? maxLength = null, CancellationToken cancellationToken = default);

View File

@@ -7,12 +7,12 @@ namespace FileTime.Core.Services;
public interface ITab : IInitable<IContainer>
{
IObservable<IContainer?> CurrentLocation { get; }
IObservable<IAbsolutePath?> CurrentSelectedItem { get; }
IObservable<AbsolutePath?> CurrentSelectedItem { get; }
IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
void SetCurrentLocation(IContainer newLocation);
void AddItemFilter(ItemFilter filter);
void RemoveItemFilter(ItemFilter filter);
void RemoveItemFilter(string name);
void SetSelectedItem(IAbsolutePath newSelectedItem);
void SetSelectedItem(AbsolutePath newSelectedItem);
}

View File

@@ -0,0 +1,21 @@
using FileTime.Core.Command;
namespace FileTime.Core.Timeline;
public class CommandTimeState
{
public ICommand Command { get; }
public CanCommandRun CanRun { get; private set; } = CanCommandRun.False;
public bool ForceRun { get; set; }
public CommandTimeState(ICommand command, PointInTime? startTime)
{
Command = command;
Task.Run(async () => await UpdateState(startTime)).Wait();
}
public async Task UpdateState(PointInTime? startPoint)
{
CanRun = startPoint == null ? CanCommandRun.False : await Command.CanRun(startPoint);
}
}

View File

@@ -0,0 +1,16 @@
using FileTime.Core.Models;
using FileTime.Core.Services;
namespace FileTime.Core.Timeline;
public class Difference
{
public AbsolutePath AbsolutePath { get; }
public DifferenceActionType Action { get; }
public Difference(DifferenceActionType action, AbsolutePath absolutePath)
{
AbsolutePath = absolutePath;
Action = action;
}
}

View File

@@ -0,0 +1,7 @@
namespace FileTime.Core.Timeline;
public enum DifferenceActionType
{
Create,
Delete
}

View File

@@ -0,0 +1,6 @@
namespace FileTime.Core.Timeline;
public interface ICommandScheduler
{
}

View File

@@ -0,0 +1,16 @@
using System.Reactive.Subjects;
using FileTime.Core.Enums;
using FileTime.Core.Models;
namespace FileTime.Core.Timeline;
public interface ITimelessContentProvider
{
BehaviorSubject<PointInTime> CurrentPointInTime { get; }
Task<IItem> GetItemByFullNameAsync(FullName fullName,
PointInTime? pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default);
}

View File

@@ -0,0 +1,90 @@
using FileTime.Core.Command;
namespace FileTime.Core.Timeline;
public class ParallelCommands
{
private static ushort _idCounter;
public List<CommandTimeState> _commands;
public ushort Id { get; }
public IReadOnlyList<CommandTimeState> Commands { get; }
public PointInTime? Result { get; private set; }
public ParallelCommands(PointInTime? result)
: this(new List<CommandTimeState>(), result)
{
}
private ParallelCommands(List<CommandTimeState> commands, PointInTime? result)
{
Id = _idCounter++;
_commands = commands;
Commands = _commands.AsReadOnly();
Result = result;
}
public static async Task<ParallelCommands> Create(PointInTime? startTime, IEnumerable<ICommand> commands)
{
var commandStates = new List<CommandTimeState>();
var currentTime = startTime;
foreach (var command in commands)
{
CommandTimeState commandTimeState = new(command, currentTime);
if (currentTime != null)
{
var canRun = await command.CanRun(currentTime);
if (canRun == CanCommandRun.True)
{
currentTime = await command.SimulateCommand(currentTime);
}
else
{
currentTime = null;
}
}
commandStates.Add(commandTimeState);
}
return new ParallelCommands(commandStates, currentTime);
}
public async Task AddCommand(ICommand command)
{
_commands.Add(new CommandTimeState(command, Result));
if (Result != null)
{
Result = await command.SimulateCommand(Result);
}
}
public async Task<PointInTime?> RefreshResult(PointInTime? startPoint)
{
var result = startPoint;
foreach (var command in _commands)
{
await command.UpdateState(result);
if (result != null)
{
var canRun = await command.Command.CanRun(result);
if (canRun == CanCommandRun.True || (canRun == CanCommandRun.Forcable && command.ForceRun))
{
result = await command.Command.SimulateCommand(result);
}
else
{
result = null;
}
}
}
Result = result;
return Result;
}
public void RemoveAt(int number) => _commands.RemoveAt(number);
internal void Remove(CommandTimeState command) => _commands.Remove(command);
}

View File

@@ -0,0 +1,41 @@
namespace FileTime.Core.Timeline;
public class PointInTime
{
private readonly List<Difference> _differences;
public static readonly PointInTime Eternal = new PointInTime();
public static readonly PointInTime Present = new PointInTime();
public IReadOnlyList<Difference> Differences { get; }
private PointInTime() : this(new List<Difference>())
{
}
private PointInTime(IEnumerable<Difference> differences)
{
_differences = new List<Difference>(differences);
Differences = _differences.AsReadOnly();
}
private PointInTime(PointInTime previous, IEnumerable<Difference> differences)
: this(MergeDifferences(previous.Differences, differences))
{
}
public PointInTime WithDifferences(IEnumerable<Difference> differences) =>
new(this, differences);
private static List<Difference> MergeDifferences(IEnumerable<Difference> previouses,
IEnumerable<Difference> differences)
{
var merged = new List<Difference>();
merged.AddRange(previouses);
merged.AddRange(differences);
return merged;
}
public static PointInTime CreateEmpty() => new PointInTime();
}

View File

@@ -1,6 +1,16 @@
using FileTime.Core.Timeline;
namespace FileTime.Core.Command.Copy;
public class CopyCommand : ITransportationCommand
{
public Task<CanCommandRun> CanRun(PointInTime currentTime)
{
throw new NotImplementedException();
}
public Task<PointInTime?> SimulateCommand(PointInTime? currentTime)
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,21 @@
using FileTime.Core.Timeline;
namespace FileTime.Core.Command.CreateContainer;
public class CreateContainerCommand : IExecutableCommand
{
public Task<CanCommandRun> CanRun(PointInTime currentTime)
{
throw new NotImplementedException();
}
public Task<PointInTime?> SimulateCommand(PointInTime? currentTime)
{
throw new NotImplementedException();
}
public Task Execute(ICommandScheduler commandScheduler)
{
throw new NotImplementedException();
}
}

View File

@@ -1,44 +0,0 @@
using FileTime.Core.Enums;
using FileTime.Core.Services;
namespace FileTime.Core.Models;
public class AbsolutePath : IAbsolutePath
{
public IContentProvider ContentProvider { get; }
public IContentProvider? VirtualContentProvider { get; }
public FullName Path { get; }
public AbsolutePathType Type { get; }
public AbsolutePath(IContentProvider contentProvider, FullName path, AbsolutePathType type, IContentProvider? virtualContentProvider = null)
{
ContentProvider = contentProvider;
Path = path;
VirtualContentProvider = virtualContentProvider;
Type = type;
}
public AbsolutePath(IItem item, IContentProvider? virtualContentProvider = null)
{
ContentProvider = item.Provider;
Path = item.FullName ?? throw new ArgumentException($"{nameof(item.FullName)} can not be null.", nameof(item));
VirtualContentProvider = virtualContentProvider;
Type = item.Type;
}
public async Task<IItem> ResolveAsync(bool forceResolve = false, ItemInitializationSettings itemInitializationSettings = default)
{
var provider = VirtualContentProvider ?? ContentProvider;
return await provider.GetItemByFullNameAsync(Path, forceResolve, Type, itemInitializationSettings);
}
public async Task<IItem?> ResolveAsyncSafe(bool forceResolve = false, ItemInitializationSettings itemInitializationSettings = default)
{
try
{
return await ResolveAsync(forceResolve, itemInitializationSettings);
}
catch { return null; }
}
}

View File

@@ -4,6 +4,7 @@ using System.Reactive.Subjects;
using DynamicData;
using FileTime.Core.Enums;
using FileTime.Core.Services;
using FileTime.Core.Timeline;
namespace FileTime.Core.Models;
@@ -12,7 +13,7 @@ public record Container(
string DisplayName,
FullName FullName,
NativePath NativePath,
IAbsolutePath? Parent,
AbsolutePath? Parent,
bool IsHidden,
bool IsExists,
DateTime? CreatedAt,
@@ -20,9 +21,10 @@ public record Container(
bool CanRename,
string? Attributes,
IContentProvider Provider,
PointInTime PointInTime,
IObservable<IEnumerable<Exception>> Exceptions,
ReadOnlyExtensionCollection Extensions,
IObservable<IObservable<IChangeSet<IAbsolutePath>>?> Items) : IContainer
IObservable<IObservable<IChangeSet<AbsolutePath>>?> Items) : IContainer
{
BehaviorSubject<bool> IsLoading { get; } = new BehaviorSubject<bool>(false);
IObservable<bool> IContainer.IsLoading => IsLoading.AsObservable();

View File

@@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using FileTime.Core.Enums;
using FileTime.Core.Services;
using FileTime.Core.Timeline;
namespace FileTime.Core.Models;
@@ -9,7 +10,7 @@ public record Element(
string DisplayName,
FullName FullName,
NativePath NativePath,
IAbsolutePath? Parent,
AbsolutePath? Parent,
bool IsHidden,
bool IsExists,
DateTime? CreatedAt,
@@ -17,6 +18,7 @@ public record Element(
bool CanRename,
string? Attributes,
IContentProvider Provider,
PointInTime PointInTime,
IObservable<IEnumerable<Exception>> Exceptions,
ReadOnlyExtensionCollection Extensions) : IElement
{

View File

@@ -4,6 +4,7 @@ using System.Reactive.Subjects;
using DynamicData;
using FileTime.Core.Enums;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Services;
@@ -11,10 +12,10 @@ public abstract class ContentProviderBase : IContentProvider
{
private readonly ReadOnlyExtensionCollection _extensions;
protected BehaviorSubject<IObservable<IChangeSet<IAbsolutePath>>?> Items { get; } = new(null);
protected BehaviorSubject<IObservable<IChangeSet<AbsolutePath>>?> Items { get; } = new(null);
protected ExtensionCollection Extensions { get; }
IObservable<IObservable<IChangeSet<IAbsolutePath>>?> IContainer.Items => Items;
IObservable<IObservable<IChangeSet<AbsolutePath>>?> IContainer.Items => Items;
public string Name { get; }
@@ -34,7 +35,7 @@ public abstract class ContentProviderBase : IContentProvider
public IContentProvider Provider => this;
public IAbsolutePath? Parent => null;
public AbsolutePath? Parent => null;
public DateTime? CreatedAt => null;
@@ -45,6 +46,7 @@ public abstract class ContentProviderBase : IContentProvider
IObservable<bool> IContainer.IsLoading => IsLoading.AsObservable();
public AbsolutePathType Type => AbsolutePathType.Container;
public PointInTime PointInTime { get; } = PointInTime.Eternal;
public IObservable<IEnumerable<Exception>> Exceptions => Observable.Return(Enumerable.Empty<Exception>());
@@ -60,21 +62,21 @@ public abstract class ContentProviderBase : IContentProvider
public virtual Task OnEnter() => Task.CompletedTask;
public virtual async Task<IItem> GetItemByFullNameAsync(
FullName fullName,
public virtual async Task<IItem> GetItemByFullNameAsync(FullName fullName,
PointInTime pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default)
=> await GetItemByNativePathAsync(GetNativePath(fullName), forceResolve, forceResolvePathType,
=> await GetItemByNativePathAsync(GetNativePath(fullName), pointInTime, forceResolve, forceResolvePathType,
itemInitializationSettings);
public abstract Task<IItem> GetItemByNativePathAsync(
NativePath nativePath,
public abstract Task<IItem> GetItemByNativePathAsync(NativePath nativePath,
PointInTime pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default);
public abstract Task<List<IAbsolutePath>> GetItemsByContainerAsync(FullName fullName);
public abstract Task<List<AbsolutePath>> GetItemsByContainerAsync(FullName fullName, PointInTime pointInTime);
public abstract NativePath GetNativePath(FullName fullName);
public abstract Task<byte[]?> GetContentAsync(IElement element,

View File

@@ -3,22 +3,30 @@ using System.Reactive.Subjects;
using DynamicData;
using DynamicData.Alias;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Services;
public class Tab : ITab
{
private readonly ITimelessContentProvider _timelessContentProvider;
private readonly BehaviorSubject<IContainer?> _currentLocation = new(null);
private readonly BehaviorSubject<IAbsolutePath?> _currentSelectedItem = new(null);
private readonly BehaviorSubject<AbsolutePath?> _currentSelectedItem = new(null);
private readonly SourceList<ItemFilter> _itemFilters = new();
private IAbsolutePath? _currentSelectedItemCached;
private AbsolutePath? _currentSelectedItemCached;
private PointInTime _currentPointInTime;
public IObservable<IContainer?> CurrentLocation { get; }
public IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
public IObservable<IAbsolutePath?> CurrentSelectedItem { get; }
public IObservable<AbsolutePath?> CurrentSelectedItem { get; }
public Tab()
public Tab(ITimelessContentProvider timelessContentProvider)
{
_timelessContentProvider = timelessContentProvider;
_currentPointInTime = null!;
_timelessContentProvider.CurrentPointInTime.Subscribe(p => _currentPointInTime = p);
CurrentLocation = _currentLocation.DistinctUntilChanged().Publish(null).RefCount();
CurrentItems =
Observable.Merge(
@@ -32,9 +40,9 @@ public class Tab : ITab
(items, filters) => items?.Where(i => filters.All(f => f.Filter(i)))),
CurrentLocation
.Where(c => c is null)
.Select(_ => (IObservable<IChangeSet<IItem>>?) null)
.Select(_ => (IObservable<IChangeSet<IItem>>?)null)
)
.Publish((IObservable<IChangeSet<IItem>>?) null)
.Publish((IObservable<IChangeSet<IItem>>?)null)
.RefCount();
CurrentSelectedItem =
@@ -66,22 +74,22 @@ public class Tab : ITab
});
}
private async Task<IItem> MapItem(IAbsolutePath item) => await item.ResolveAsync(true);
private async Task<IItem> MapItem(AbsolutePath item) => await item.ResolveAsync(true);
public void Init(IContainer currentLocation)
{
_currentLocation.OnNext(currentLocation);
}
private static IAbsolutePath? GetSelectedItemByItems(IEnumerable<IItem> items)
private AbsolutePath? GetSelectedItemByItems(IEnumerable<IItem> items)
{
//TODO:
return new AbsolutePath(items.First());
return new AbsolutePath(_timelessContentProvider, items.First());
}
public void SetCurrentLocation(IContainer newLocation) => _currentLocation.OnNext(newLocation);
public void SetSelectedItem(IAbsolutePath newSelectedItem) => _currentSelectedItem.OnNext(newSelectedItem);
public void SetSelectedItem(AbsolutePath newSelectedItem) => _currentSelectedItem.OnNext(newSelectedItem);
public void AddItemFilter(ItemFilter filter) => _itemFilters.Add(filter);
public void RemoveItemFilter(ItemFilter filter) => _itemFilters.Remove(filter);
@@ -95,7 +103,8 @@ public class Tab : ITab
public async Task OpenSelected()
{
if (_currentSelectedItemCached == null) return;
var resolvedSelectedItem = await _currentSelectedItemCached.ContentProvider.GetItemByFullNameAsync(_currentSelectedItemCached.Path);
var resolvedSelectedItem =
await _currentSelectedItemCached.TimelessProvider.GetItemByFullNameAsync(_currentSelectedItemCached.Path, _currentPointInTime);
if (resolvedSelectedItem is not IContainer resolvedContainer) return;
SetCurrentLocation(resolvedContainer);

View File

@@ -0,0 +1,6 @@
namespace FileTime.Core.Timeline;
public class CommandScheduler : ICommandScheduler
{
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FileTime.Core.Abstraction\FileTime.Core.Abstraction.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,38 @@
using System.Reactive.Subjects;
using FileTime.Core.Enums;
using FileTime.Core.Models;
using FileTime.Core.Services;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.Core.Timeline;
public class TimelessContentProvider : ITimelessContentProvider
{
private readonly Lazy<List<IContentProvider>> _contentProviders;
public BehaviorSubject<PointInTime> CurrentPointInTime { get; } =
new BehaviorSubject<PointInTime>(PointInTime.Present);
public TimelessContentProvider(IServiceProvider serviceProvider)
{
_contentProviders =
new Lazy<List<IContentProvider>>(() => serviceProvider.GetServices<IContentProvider>().ToList());
}
public async Task<IItem> GetItemByFullNameAsync(FullName fullName, PointInTime? pointInTime,
bool forceResolve = false,
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default)
{
//TODO time modifications
var contentProviderName = fullName.Path.Split(Constants.SeparatorChar).FirstOrDefault();
var contentProvider = _contentProviders.Value.FirstOrDefault(p => p.Name == contentProviderName);
if (contentProvider is null)
throw new Exception($"No content provider is found for name '{contentProviderName}'");
return await contentProvider.GetItemByFullNameAsync(fullName, pointInTime ?? PointInTime.Present,
forceResolve, forceResolvePathType,
itemInitializationSettings);
}
}