Files
FileTime/src/Core/FileTime.Core/Command/RenameCommand.cs
2022-02-17 14:40:38 +01:00

57 lines
2.0 KiB
C#

using AsyncEvent;
using FileTime.Core.Extensions;
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Command
{
public class RenameCommand : IExecutableCommand
{
public AbsolutePath Source { get; }
public string Target { get; }
public int Progress => 100;
public int CurrentProgress => 100;
public AsyncEventHandler ProgressChanged { get; } = new();
public string DisplayLabel { get; } = "RenameCommand";
public IReadOnlyList<string> CanRunMessages { get; } = new List<string>().AsReadOnly();
public RenameCommand(AbsolutePath source, string target)
{
Source = source;
Target = target;
}
public async Task Execute(TimeRunner timeRunner)
{
var itemToRename = await Source.ResolveAsync();
if (itemToRename != null)
{
await itemToRename.Rename(Target);
if (timeRunner.RefreshContainer != null) await timeRunner.RefreshContainer.InvokeAsync(this, new AbsolutePath(itemToRename.GetParent()!));
}
}
public async Task<PointInTime> SimulateCommand(PointInTime startPoint)
{
var item = await Source.ResolveAsync();
if (item == null) throw new FileNotFoundException();
var newDifferences = new List<Difference>()
{
new Difference(item.ToDifferenceItemType(),
DifferenceActionType.Delete,
Source),
new Difference(item.ToDifferenceItemType(),
DifferenceActionType.Delete,
Source)
};
return startPoint.WithDifferences(newDifferences);
}
public async Task<CanCommandRun> CanRun(PointInTime startPoint)
{
return await Source.ResolveAsync() != null ? CanCommandRun.True : CanCommandRun.False;
}
}
}