Files
gif-resizer/GifResizer/ViewModels/MainViewModel.cs

74 lines
1.9 KiB
C#

using System.ComponentModel;
using System.Runtime.CompilerServices;
using GifResizer.Models;
using GifResizer.Service;
namespace GifResizer.ViewModels;
public sealed class MainViewModel : INotifyPropertyChanged
{
private readonly GifService _gifService;
public event PropertyChangedEventHandler? PropertyChanged;
public MainViewModel(GifService service)
{
_gifService = service;
CommonFormats = [
new Format(112, 112),
new Format(500, 500),
new Format(1000, 1000)
];
FilePath = null;
}
public string? FilePath
{
get;
set => SetField(ref field, value);
}
public int? GifWidth
{
get;
set => SetField(ref field, value);
}
public int? GifHeight
{
get;
set => SetField(ref field, value);
}
public List<Format> CommonFormats { get; }
public void LoadGifData(string filePath)
{
GifData data = _gifService.GetData(filePath);
FilePath = filePath;
GifWidth = data.Width;
GifHeight = data.Height;
}
public void ResizeGif()
{
if(FilePath == null && GifWidth == null && GifHeight == null) return;
_gifService.ResizeGif(FilePath!, GifWidth!.Value, GifHeight!.Value);
CommonFormats
.Where(f => f.Checked)
.ToList()
.ForEach(f => _gifService.ResizeGif(FilePath!, f.Width, f.Height));
}
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}