Remove Type from Differece, CopyCommand refactor

This commit is contained in:
2022-02-18 17:15:11 +01:00
parent 83b86f2b50
commit 502f80313a
10 changed files with 61 additions and 89 deletions

View File

@@ -64,33 +64,25 @@ namespace FileTime.Core.Command
var newDiffs = new List<Difference>(); var newDiffs = new List<Difference>();
_copyOperation = async (_, to, _, _) => _copyOperation = (_, to, _, _) =>
{ {
var target = await to.GetParent().ResolveAsync(); newDiffs.Add(new Difference(DifferenceActionType.Create, to));
newDiffs.Add(new Difference( return Task.CompletedTask;
target is IElement
? DifferenceItemType.Element
: DifferenceItemType.Container,
DifferenceActionType.Create,
to
));
}; };
_createContainer = async (IContainer target, string name) => _createContainer = (IContainer target, string name) =>
{ {
var newContainerDiff = new Difference( var newContainerDiff = new Difference(
DifferenceItemType.Container,
DifferenceActionType.Create, DifferenceActionType.Create,
AbsolutePath.FromParentAndChildName(target, name, AbsolutePathType.Container) AbsolutePath.FromParentAndChildName(target, name, AbsolutePathType.Container)
); );
newDiffs.Add(newContainerDiff); newDiffs.Add(newContainerDiff);
return (IContainer)(await newContainerDiff.AbsolutePath.ResolveAsync())!; return Task.FromResult((IContainer)null!);
}; };
var resolvedTarget = (IContainer)(await Target.ResolveAsync())!; await TraverseTree(Sources, Target, TransportMode.Value);
await TraverseTree(Sources, resolvedTarget, TransportMode.Value);
return startPoint.WithDifferences(newDiffs); return startPoint.WithDifferences(newDiffs);
} }
@@ -127,8 +119,7 @@ namespace FileTime.Core.Command
} }
}; };
var resolvedTarget = (IContainer)(await Target.ResolveAsync())!; await TraverseTree(Sources, Target, TransportMode.Value);
await TraverseTree(Sources, resolvedTarget, TransportMode.Value);
} }
private async Task CalculateProgress() private async Task CalculateProgress()
@@ -156,26 +147,33 @@ namespace FileTime.Core.Command
} }
}; };
var resolvedTarget = (IContainer)(await Target.ResolveAsync())!; await TraverseTree(Sources, Target, TransportMode.Value);
await TraverseTree(Sources, resolvedTarget, TransportMode.Value);
_operationStatuses = operationStatuses; _operationStatuses = operationStatuses;
} }
private async Task TraverseTree( private async Task TraverseTree(
IEnumerable<AbsolutePath> sources, IEnumerable<AbsolutePath> sources,
IContainer target, AbsolutePath target,
TransportMode transportMode) TransportMode transportMode)
{ {
if (_copyOperation == null) throw new ArgumentException("No copy operation were given."); if (_copyOperation == null) throw new ArgumentException("No copy operation were given.");
if (_createContainer == null) throw new ArgumentException("No container creation function were given."); if (_createContainer == null) throw new ArgumentException("No container creation function were given.");
var resolvedTarget = (IContainer?)await target.ResolveAsync();
foreach (var source in sources) foreach (var source in sources)
{ {
var item = await source.ResolveAsync(); var item = await source.ResolveAsync();
if (item is IContainer container) if (item is IContainer container)
{ {
var targetContainer = (await target.GetContainers())?.FirstOrDefault(d => d.Name == container.Name) ?? (await _createContainer?.Invoke(target, container.Name)!); var targetContainer = target.GetChild(item.Name, AbsolutePathType.Container);
if (_createContainer != null
&& resolvedTarget != null
&& !await resolvedTarget.IsExistsAsync(item.Name))
{
await _createContainer.Invoke(resolvedTarget, container.Name);
}
var childDirectories = (await container.GetContainers())!.Select(d => new AbsolutePath(d)); var childDirectories = (await container.GetContainers())!.Select(d => new AbsolutePath(d));
var childFiles = (await container.GetElements())!.Select(f => new AbsolutePath(f)); var childFiles = (await container.GetElements())!.Select(f => new AbsolutePath(f));
@@ -185,32 +183,12 @@ namespace FileTime.Core.Command
} }
else if (item is IElement element) else if (item is IElement element)
{ {
var targetName = element.Name; var targetName = await Helper.CommandHelper.GetNewNameAsync(resolvedTarget, element.Name, transportMode);
if (targetName == null) continue;
var targetNameExists = await target.IsExistsAsync(targetName);
if (transportMode == Command.TransportMode.Merge)
{
for (var i = 0; targetNameExists; i++)
{
targetName = element.Name + (i == 0 ? "_" : $"_{i}");
targetNameExists = await target.IsExistsAsync(targetName);
}
}
else if (transportMode == Command.TransportMode.Skip && targetNameExists)
{
continue;
}
OperationProgress? operation = null; OperationProgress? operation = null;
var targetFolderPath = new AbsolutePath(target); var targetFolderPath = new AbsolutePath(target);
var targetElementPath = AbsolutePath.FromParentAndChildName(target, targetName, AbsolutePathType.Element); var targetElementPath = target.GetChild(targetName, AbsolutePathType.Element);
foreach (var asd in _operationStatuses.Keys)
{
var hash1 = asd.GetHashCode();
var hash2 = targetFolderPath.GetHashCode();
var eq = asd == targetFolderPath;
}
if (_operationStatuses.TryGetValue(targetFolderPath, out var targetPathOperations)) if (_operationStatuses.TryGetValue(targetFolderPath, out var targetPathOperations))
{ {

View File

@@ -39,7 +39,7 @@ namespace FileTime.Core.Command
{ {
var newDifferences = new List<Difference>() var newDifferences = new List<Difference>()
{ {
new Difference(DifferenceItemType.Container, DifferenceActionType.Create, Container.GetChild(NewContainerName, AbsolutePathType.Container)) new Difference(DifferenceActionType.Create, Container.GetChild(NewContainerName, AbsolutePathType.Container))
}; };
return Task.FromResult(startPoint.WithDifferences(newDifferences)); return Task.FromResult(startPoint.WithDifferences(newDifferences));
} }

View File

@@ -36,7 +36,7 @@ namespace FileTime.Core.Command
{ {
var newDifferences = new List<Difference>() var newDifferences = new List<Difference>()
{ {
new Difference(DifferenceItemType.Element, DifferenceActionType.Create, Container.GetChild(NewElementName, AbsolutePathType.Element)) new Difference(DifferenceActionType.Create, Container.GetChild(NewElementName, AbsolutePathType.Element))
}; };
return Task.FromResult(startPoint.WithDifferences(newDifferences)); return Task.FromResult(startPoint.WithDifferences(newDifferences));
} }

View File

@@ -27,7 +27,6 @@ namespace FileTime.Core.Command
_deleteContainer = (c) => _deleteContainer = (c) =>
{ {
newDifferences.Add(new Difference( newDifferences.Add(new Difference(
DifferenceItemType.Container,
DifferenceActionType.Delete, DifferenceActionType.Delete,
new AbsolutePath(c) new AbsolutePath(c)
)); ));
@@ -37,7 +36,6 @@ namespace FileTime.Core.Command
_deleteElement = (e) => _deleteElement = (e) =>
{ {
newDifferences.Add(new Difference( newDifferences.Add(new Difference(
DifferenceItemType.Element,
DifferenceActionType.Delete, DifferenceActionType.Delete,
new AbsolutePath(e) new AbsolutePath(e)
)); ));

View File

@@ -1,6 +1,4 @@
using AsyncEvent; using AsyncEvent;
using FileTime.Core.Extensions;
using FileTime.Core.Interactions;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Timeline; using FileTime.Core.Timeline;
@@ -33,20 +31,18 @@ namespace FileTime.Core.Command
} }
} }
public async Task<PointInTime> SimulateCommand(PointInTime startPoint) public Task<PointInTime> SimulateCommand(PointInTime startPoint)
{ {
var item = await Source.ResolveAsync();
if (item == null) throw new FileNotFoundException();
var newDifferences = new List<Difference>() var newDifferences = new List<Difference>()
{ {
new Difference(item.ToDifferenceItemType(), new Difference(
DifferenceActionType.Delete, DifferenceActionType.Delete,
Source), Source),
new Difference(item.ToDifferenceItemType(), new Difference(
DifferenceActionType.Delete, DifferenceActionType.Create,
Source) Source.GetParent().GetChild(Target, Source.Type))
}; };
return startPoint.WithDifferences(newDifferences); return Task.FromResult(startPoint.WithDifferences(newDifferences));
} }
public async Task<CanCommandRun> CanRun(PointInTime startPoint) public async Task<CanCommandRun> CanRun(PointInTime startPoint)

View File

@@ -1,15 +0,0 @@
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Extensions
{
public static class TimelineExtensions
{
public static DifferenceItemType ToDifferenceItemType(this IItem? item)
{
if (item is IContainer) return DifferenceItemType.Container;
else if (item is IElement) return DifferenceItemType.Element;
else return DifferenceItemType.Unknown;
}
}
}

View File

@@ -0,0 +1,27 @@
using FileTime.Core.Models;
namespace FileTime.Core.Helper
{
public static class CommandHelper
{
public static async Task<string?> GetNewNameAsync(IContainer? resolvedTarget, string name, Command.TransportMode transportMode)
{
var newName = name;
var targetNameExists = resolvedTarget != null && await resolvedTarget.IsExistsAsync(newName);
if (transportMode == Command.TransportMode.Merge && resolvedTarget != null)
{
for (var i = 0; targetNameExists; i++)
{
newName = name + (i == 0 ? "_" : $"_{i}");
targetNameExists = await resolvedTarget.IsExistsAsync(newName);
}
}
else if (transportMode == Command.TransportMode.Skip && targetNameExists)
{
return null;
}
return newName;
}
}
}

View File

@@ -5,14 +5,12 @@ namespace FileTime.Core.Timeline
{ {
public class Difference public class Difference
{ {
public DifferenceItemType Type { get; }
public string Name { get; } public string Name { get; }
public AbsolutePath AbsolutePath { get; } public AbsolutePath AbsolutePath { get; }
public DifferenceActionType Action { get; } public DifferenceActionType Action { get; }
public Difference(DifferenceItemType type, DifferenceActionType action, AbsolutePath absolutePath) public Difference(DifferenceActionType action, AbsolutePath absolutePath)
{ {
Type = type;
AbsolutePath = absolutePath; AbsolutePath = absolutePath;
Action = action; Action = action;
@@ -22,7 +20,6 @@ namespace FileTime.Core.Timeline
public Difference WithVirtualContentProvider(IContentProvider? virtualContentProvider) public Difference WithVirtualContentProvider(IContentProvider? virtualContentProvider)
{ {
return new Difference( return new Difference(
Type,
Action, Action,
new AbsolutePath(AbsolutePath.ContentProvider, AbsolutePath.Path, AbsolutePath.Type, virtualContentProvider) new AbsolutePath(AbsolutePath.ContentProvider, AbsolutePath.Path, AbsolutePath.Type, virtualContentProvider)
); );

View File

@@ -1,9 +0,0 @@
namespace FileTime.Core.Timeline
{
public enum DifferenceItemType
{
Container,
Element,
Unknown
}
}

View File

@@ -77,7 +77,7 @@ namespace FileTime.Core.Timeline
(IReadOnlyList<IContainer>?)_pointInTime (IReadOnlyList<IContainer>?)_pointInTime
.Differences .Differences
.Where(d => .Where(d =>
d.Type == DifferenceItemType.Container d.AbsolutePath.Type == AbsolutePathType.Container
&& GetParentPath(d.AbsolutePath.Path) == FullName) && GetParentPath(d.AbsolutePath.Path) == FullName)
.Select(MapContainer) .Select(MapContainer)
.ToList() .ToList()
@@ -89,7 +89,7 @@ namespace FileTime.Core.Timeline
(IReadOnlyList<IElement>?)_pointInTime (IReadOnlyList<IElement>?)_pointInTime
.Differences .Differences
.Where(d => .Where(d =>
d.Type == DifferenceItemType.Element d.AbsolutePath.Type == AbsolutePathType.Element
&& GetParentPath(d.AbsolutePath.Path) == FullName) && GetParentPath(d.AbsolutePath.Path) == FullName)
.Select(MapElement) .Select(MapElement)
.ToList() .ToList()
@@ -116,13 +116,13 @@ namespace FileTime.Core.Timeline
private IContainer MapContainer(Difference containerDiff) private IContainer MapContainer(Difference containerDiff)
{ {
if (containerDiff.Type != DifferenceItemType.Container) throw new ArgumentException($"{nameof(containerDiff)}'s {nameof(Difference.Type)} property is not {DifferenceItemType.Container}."); if (containerDiff.AbsolutePath.Type != AbsolutePathType.Container) throw new ArgumentException($"{nameof(containerDiff)}'s {nameof(AbsolutePath.Type)} property is not {AbsolutePathType.Container}.");
return new TimeContainer(containerDiff.Name, this, Provider, containerDiff.AbsolutePath.VirtualContentProvider ?? containerDiff.AbsolutePath.ContentProvider, _pointInTime); return new TimeContainer(containerDiff.Name, this, Provider, containerDiff.AbsolutePath.VirtualContentProvider ?? containerDiff.AbsolutePath.ContentProvider, _pointInTime);
} }
private IElement MapElement(Difference elementDiff) private IElement MapElement(Difference elementDiff)
{ {
if (elementDiff.Type != DifferenceItemType.Container) throw new ArgumentException($"{elementDiff}'s {nameof(Difference.Type)} property is not {DifferenceItemType.Element}."); if (elementDiff.AbsolutePath.Type != AbsolutePathType.Element) throw new ArgumentException($"{elementDiff}'s {nameof(AbsolutePath.Type)} property is not {AbsolutePathType.Element}.");
return new TimeElement(elementDiff.Name, this, Provider, elementDiff.AbsolutePath.VirtualContentProvider ?? elementDiff.AbsolutePath.ContentProvider); return new TimeElement(elementDiff.Name, this, Provider, elementDiff.AbsolutePath.VirtualContentProvider ?? elementDiff.AbsolutePath.ContentProvider);
} }
public Task<bool> CanOpenAsync() => Task.FromResult(true); public Task<bool> CanOpenAsync() => Task.FromResult(true);