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 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(ref T field, T value, [CallerMemberName] string? propertyName = null) { if (EqualityComparer.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)); } }