/ src / modules / imageresizer / ui / ViewModels / AdvancedViewModel.cs
AdvancedViewModel.cs
 1  #pragma warning disable IDE0073
 2  // Copyright (c) Brice Lambson
 3  // The Brice Lambson licenses this file to you under the MIT license.
 4  // See the LICENSE file in the project root for more information.  Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
 5  #pragma warning restore IDE0073
 6  
 7  using System;
 8  using System.Collections.Generic;
 9  using System.Reflection;
10  using System.Windows.Input;
11  using System.Windows.Media.Imaging;
12  
13  using ImageResizer.Helpers;
14  using ImageResizer.Models;
15  using ImageResizer.Properties;
16  
17  namespace ImageResizer.ViewModels
18  {
19      public class AdvancedViewModel : Observable
20      {
21          private static Dictionary<Guid, string> InitEncoderMap()
22          {
23              var bmpCodec = new BmpBitmapEncoder().CodecInfo;
24              var gifCodec = new GifBitmapEncoder().CodecInfo;
25              var jpegCodec = new JpegBitmapEncoder().CodecInfo;
26              var pngCodec = new PngBitmapEncoder().CodecInfo;
27              var tiffCodec = new TiffBitmapEncoder().CodecInfo;
28              var wmpCodec = new WmpBitmapEncoder().CodecInfo;
29  
30              return new Dictionary<Guid, string>
31              {
32                  [bmpCodec.ContainerFormat] = bmpCodec.FriendlyName,
33                  [gifCodec.ContainerFormat] = gifCodec.FriendlyName,
34                  [jpegCodec.ContainerFormat] = jpegCodec.FriendlyName,
35                  [pngCodec.ContainerFormat] = pngCodec.FriendlyName,
36                  [tiffCodec.ContainerFormat] = tiffCodec.FriendlyName,
37                  [wmpCodec.ContainerFormat] = wmpCodec.FriendlyName,
38              };
39          }
40  
41          public AdvancedViewModel(Settings settings)
42          {
43              RemoveSizeCommand = new RelayCommand<ResizeSize>(RemoveSize);
44              AddSizeCommand = new RelayCommand(AddSize);
45              Settings = settings;
46          }
47  
48          public static IDictionary<Guid, string> EncoderMap { get; } = InitEncoderMap();
49  
50          public Settings Settings { get; }
51  
52          public static string Version
53              => typeof(AdvancedViewModel).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
54                  ?.InformationalVersion;
55  
56          public static IEnumerable<Guid> Encoders => EncoderMap.Keys;
57  
58          public ICommand RemoveSizeCommand { get; }
59  
60          public ICommand AddSizeCommand { get; }
61  
62          public void RemoveSize(ResizeSize size)
63              => Settings.Sizes.Remove(size);
64  
65          public void AddSize()
66              => Settings.Sizes.Add(new ResizeSize());
67  
68          public void Close(bool accepted)
69          {
70              if (accepted)
71              {
72                  Settings.Save();
73  
74                  return;
75              }
76  
77              var selectedSizeIndex = Settings.SelectedSizeIndex;
78              var shrinkOnly = Settings.ShrinkOnly;
79              var replace = Settings.Replace;
80              var ignoreOrientation = Settings.IgnoreOrientation;
81  
82              Settings.Reload();
83              Settings.SelectedSizeIndex = selectedSizeIndex;
84              Settings.ShrinkOnly = shrinkOnly;
85              Settings.Replace = replace;
86              Settings.IgnoreOrientation = ignoreOrientation;
87          }
88      }
89  }