CommandError for Copy
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
namespace FileTime.Core.Command;
|
||||||
|
|
||||||
|
public record CommandError(string Message, Exception? Exception);
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.ObjectModel;
|
||||||
using DeclarativeProperty;
|
using DeclarativeProperty;
|
||||||
using FileTime.Core.Timeline;
|
using FileTime.Core.Timeline;
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ public interface ICommand
|
|||||||
IDeclarativeProperty<string> DisplayDetailLabel { get; }
|
IDeclarativeProperty<string> DisplayDetailLabel { get; }
|
||||||
IDeclarativeProperty<int> TotalProgress { get; }
|
IDeclarativeProperty<int> TotalProgress { get; }
|
||||||
IDeclarativeProperty<int> CurrentProgress { get; }
|
IDeclarativeProperty<int> CurrentProgress { get; }
|
||||||
|
IDeclarativeProperty<ObservableCollection<CommandError>> Errors { get; }
|
||||||
|
|
||||||
Task<CanCommandRun> CanRun(PointInTime currentTime);
|
Task<CanCommandRun> CanRun(PointInTime currentTime);
|
||||||
Task<PointInTime> SimulateCommand(PointInTime currentTime);
|
Task<PointInTime> SimulateCommand(PointInTime currentTime);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.ObjectModel;
|
||||||
using DeclarativeProperty;
|
using DeclarativeProperty;
|
||||||
using FileTime.Core.Timeline;
|
using FileTime.Core.Timeline;
|
||||||
|
|
||||||
@@ -9,11 +10,14 @@ public abstract class CommandBase : ICommand
|
|||||||
private readonly DeclarativeProperty<string> _displayDetailLabel;
|
private readonly DeclarativeProperty<string> _displayDetailLabel;
|
||||||
private readonly DeclarativeProperty<int> _totalProgress;
|
private readonly DeclarativeProperty<int> _totalProgress;
|
||||||
private readonly DeclarativeProperty<int> _currentProgress;
|
private readonly DeclarativeProperty<int> _currentProgress;
|
||||||
|
private readonly DeclarativeProperty<ObservableCollection<CommandError>> _errorsProperty;
|
||||||
|
private readonly ObservableCollection<CommandError> _errors = [];
|
||||||
|
|
||||||
public IDeclarativeProperty<string> DisplayLabel { get; }
|
public IDeclarativeProperty<string> DisplayLabel { get; }
|
||||||
public IDeclarativeProperty<string> DisplayDetailLabel { get; }
|
public IDeclarativeProperty<string> DisplayDetailLabel { get; }
|
||||||
public IDeclarativeProperty<int> TotalProgress { get; }
|
public IDeclarativeProperty<int> TotalProgress { get; }
|
||||||
public IDeclarativeProperty<int> CurrentProgress { get; }
|
public IDeclarativeProperty<int> CurrentProgress { get; }
|
||||||
|
public IDeclarativeProperty<ObservableCollection<CommandError>> Errors { get; }
|
||||||
|
|
||||||
protected CommandBase(string displayLabel = "", string displayDetailLabel = "", int totalProgress = 0, int currentProgress = 0)
|
protected CommandBase(string displayLabel = "", string displayDetailLabel = "", int totalProgress = 0, int currentProgress = 0)
|
||||||
{
|
{
|
||||||
@@ -21,11 +25,13 @@ public abstract class CommandBase : ICommand
|
|||||||
_displayDetailLabel = new(displayDetailLabel);
|
_displayDetailLabel = new(displayDetailLabel);
|
||||||
_totalProgress = new(totalProgress);
|
_totalProgress = new(totalProgress);
|
||||||
_currentProgress = new(currentProgress);
|
_currentProgress = new(currentProgress);
|
||||||
|
_errorsProperty = new(_errors);
|
||||||
|
|
||||||
DisplayLabel = _displayLabel;
|
DisplayLabel = _displayLabel;
|
||||||
DisplayDetailLabel = _displayDetailLabel;
|
DisplayDetailLabel = _displayDetailLabel;
|
||||||
TotalProgress = _totalProgress;
|
TotalProgress = _totalProgress;
|
||||||
CurrentProgress = _currentProgress;
|
CurrentProgress = _currentProgress;
|
||||||
|
Errors = _errorsProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Task<CanCommandRun> CanRun(PointInTime currentTime);
|
public abstract Task<CanCommandRun> CanRun(PointInTime currentTime);
|
||||||
@@ -50,4 +56,6 @@ public abstract class CommandBase : ICommand
|
|||||||
return Task.FromResult((int)(dataList.Sum(d => d.Progress) * 100 / total));
|
return Task.FromResult((int)(dataList.Sum(d => d.Progress) * 100 / total));
|
||||||
})
|
})
|
||||||
.Subscribe(async (p, _) => await SetTotalProgress(p));
|
.Subscribe(async (p, _) => await SetTotalProgress(p));
|
||||||
|
|
||||||
|
protected void AddError(CommandError error) => _errors.Add(error);
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ public class CopyCommand : CommandBase, ITransportationCommand
|
|||||||
return p?.Progress.Map(currentProgress =>
|
return p?.Progress.Map(currentProgress =>
|
||||||
p.TotalCount == 0
|
p.TotalCount == 0
|
||||||
? 0
|
? 0
|
||||||
: (int) (currentProgress * 100 / p.TotalCount)
|
: (int)(currentProgress * 100 / p.TotalCount)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.Switch()
|
.Switch()
|
||||||
@@ -174,7 +174,7 @@ public class CopyCommand : CommandBase, ITransportationCommand
|
|||||||
{
|
{
|
||||||
if (_cancellationTokenSource.IsCancellationRequested) return;
|
if (_cancellationTokenSource.IsCancellationRequested) return;
|
||||||
|
|
||||||
var resolvedTarget = (IContainer) await target.ResolveAsync() ?? throw new Exception();
|
var resolvedTarget = (IContainer)await target.ResolveAsync() ?? throw new Exception();
|
||||||
var item = await _timelessContentProvider.GetItemByFullNameAsync(source, currentTime);
|
var item = await _timelessContentProvider.GetItemByFullNameAsync(source, currentTime);
|
||||||
|
|
||||||
if (item is IContainer container)
|
if (item is IContainer container)
|
||||||
@@ -183,7 +183,7 @@ public class CopyCommand : CommandBase, ITransportationCommand
|
|||||||
{
|
{
|
||||||
await container.WaitForLoaded(_cancellationTokenSource.Token);
|
await container.WaitForLoaded(_cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
catch(OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -208,8 +208,16 @@ public class CopyCommand : CommandBase, ITransportationCommand
|
|||||||
var currentProgress = _operationProgresses.Find(o => o.Key == element.FullName!.Path);
|
var currentProgress = _operationProgresses.Find(o => o.Key == element.FullName!.Path);
|
||||||
await _currentOperationProgress.SetValue(currentProgress);
|
await _currentOperationProgress.SetValue(currentProgress);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await copyOperation.CopyAsync(new AbsolutePath(_timelessContentProvider, element), newElementPath, new CopyCommandContext(UpdateProgress, currentProgress, _cancellationTokenSource.Token));
|
await copyOperation.CopyAsync(new AbsolutePath(_timelessContentProvider, element), newElementPath, new CopyCommandContext(UpdateProgress, currentProgress, _cancellationTokenSource.Token));
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error while copying file: {Path}, {Message}", element.FullName!.Path, e.Message);
|
||||||
|
AddError(new CommandError("Error while copying file: " + element.FullName!.Path, e));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user