[fxcop] image resizer ui (#6841)

* adjustments

* Settings fixed

* Getting resizing tests operational again

* fixed default vs loading from settings

* one small tewak
This commit is contained in:
Clint Rutkas
2020-10-01 11:33:23 -07:00
committed by GitHub
parent 19b519638f
commit c219fe0d1d
21 changed files with 141 additions and 112 deletions

View File

@@ -16,9 +16,7 @@ namespace ImageResizer.ViewModels
{
public class AdvancedViewModel : ViewModelBase
{
private static readonly IDictionary<Guid, string> _encoderMap;
static AdvancedViewModel()
private static Dictionary<Guid, string> InitEncoderMap()
{
var bmpCodec = new BmpBitmapEncoder().CodecInfo;
var gifCodec = new GifBitmapEncoder().CodecInfo;
@@ -27,7 +25,7 @@ namespace ImageResizer.ViewModels
var tiffCodec = new TiffBitmapEncoder().CodecInfo;
var wmpCodec = new WmpBitmapEncoder().CodecInfo;
_encoderMap = new Dictionary<Guid, string>
return new Dictionary<Guid, string>
{
[bmpCodec.ContainerFormat] = bmpCodec.FriendlyName,
[gifCodec.ContainerFormat] = gifCodec.FriendlyName,
@@ -45,17 +43,15 @@ namespace ImageResizer.ViewModels
Settings = settings;
}
public static IDictionary<Guid, string> EncoderMap
=> _encoderMap;
public static IDictionary<Guid, string> EncoderMap { get; } = InitEncoderMap();
public Settings Settings { get; }
public string Version
public static string Version
=> typeof(AdvancedViewModel).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
public IEnumerable<Guid> Encoders
=> _encoderMap.Keys;
public static IEnumerable<Guid> Encoders => EncoderMap.Keys;
public ICommand RemoveSizeCommand { get; }

View File

@@ -29,7 +29,10 @@ namespace ImageResizer.ViewModels
_mainView = mainView;
Settings = settings;
settings.CustomSize.PropertyChanged += (sender, e) => settings.SelectedSize = (CustomSize)sender;
if (settings != null)
{
settings.CustomSize.PropertyChanged += (sender, e) => settings.SelectedSize = (CustomSize)sender;
}
ResizeCommand = new RelayCommand(Resize);
CancelCommand = new RelayCommand(Cancel);

View File

@@ -45,7 +45,7 @@ namespace ImageResizer.ViewModels
{
if (_batch.Files.Count == 0)
{
_batch.Files.AddRange(view.OpenPictureFiles());
_batch.Files.AddRange(view?.OpenPictureFiles());
}
CurrentPage = new InputViewModel(_settings, this, view, _batch);

View File

@@ -15,7 +15,7 @@ using ImageResizer.Views;
namespace ImageResizer.ViewModels
{
public class ProgressViewModel : ViewModelBase
public class ProgressViewModel : ViewModelBase, IDisposable
{
private readonly MainViewModel _mainViewModel;
private readonly ResizeBatch _batch;
@@ -25,6 +25,7 @@ namespace ImageResizer.ViewModels
private double _progress;
private TimeSpan _timeRemaining;
private bool disposedValue;
public ProgressViewModel(
ResizeBatch batch,
@@ -56,37 +57,61 @@ namespace ImageResizer.ViewModels
public ICommand StopCommand { get; }
public void Start()
=> Task.Factory.StartNew(
() =>
{
_ = Task.Factory.StartNew(StartExecutingWork, _cancellationTokenSource.Token, TaskCreationOptions.None, TaskScheduler.Current);
}
private void StartExecutingWork()
{
_stopwatch.Restart();
var errors = _batch.Process(
(completed, total) =>
{
_stopwatch.Restart();
var errors = _batch.Process(
_cancellationTokenSource.Token,
(completed, total) =>
{
var progress = completed / total;
Progress = progress;
_mainViewModel.Progress = progress;
var progress = completed / total;
Progress = progress;
_mainViewModel.Progress = progress;
TimeRemaining = _stopwatch.Elapsed.Multiply((total - completed) / completed);
});
if (errors.Any())
{
_mainViewModel.Progress = 0;
_mainViewModel.CurrentPage = new ResultsViewModel(_mainView, errors);
}
else
{
_mainView.Close();
}
TimeRemaining = _stopwatch.Elapsed.Multiply((total - completed) / completed);
},
_cancellationTokenSource.Token);
if (errors.Any())
{
_mainViewModel.Progress = 0;
_mainViewModel.CurrentPage = new ResultsViewModel(_mainView, errors);
}
else
{
_mainView.Close();
}
}
public void Stop()
{
_cancellationTokenSource.Cancel();
_mainView.Close();
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_cancellationTokenSource.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}