diff --git a/ProxyForge/ViewModels/GenerationItemViewModel.cs b/ProxyForge/ViewModels/GenerationItemViewModel.cs new file mode 100644 index 0000000..034826d --- /dev/null +++ b/ProxyForge/ViewModels/GenerationItemViewModel.cs @@ -0,0 +1,62 @@ +using System.ComponentModel; +using System.Windows; +using ProxyForge.Encoding; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.ViewModels; + +internal sealed class GenerationItemViewModel : Bindable +{ + public GenerationItemViewModel(ProxyGenerationItem item) + { + Item = item; + PropertyChangedEventManager.AddHandler(item, OnItemChanged, string.Empty); + } + + public ProxyGenerationItem Item { get; } + + public string FileName => Item.FileName; + + public string SourcePath => Item.SourcePath; + + public double Percentage => Item.Progress * 100d; + + public bool IsWaiting => Item.Status == ProxyGenerationStatus.Waiting; + + public string StatusText => Item.Status switch + { + ProxyGenerationStatus.Waiting => Texts.StatusWaiting, + ProxyGenerationStatus.Generating => Texts.StatusGenerating, + ProxyGenerationStatus.Completed => Texts.StatusCompleted, + ProxyGenerationStatus.Cancelled => Texts.StatusCancelled, + _ => Item.Failure is { } failure ? string.Concat(Texts.StatusFailed, ": ", DescribeFailure(failure)) : Texts.StatusFailed, + }; + + public static string DescribeFailure(ProxyEncodeFailure failure) => failure switch + { + ProxyEncodeFailure.FFmpegUnavailable => Texts.FailureFFmpegUnavailable, + ProxyEncodeFailure.SourceUnavailable => Texts.FailureSourceUnavailable, + ProxyEncodeFailure.NoFrames => Texts.FailureNoFrames, + ProxyEncodeFailure.UnusableSize => Texts.FailureUnusableSize, + ProxyEncodeFailure.Transparent => Texts.FailureTransparent, + ProxyEncodeFailure.FFmpegFailed => Texts.FailureFFmpegFailed, + ProxyEncodeFailure.NoOutput => Texts.FailureNoOutput, + ProxyEncodeFailure.SourceChanged => Texts.FailureSourceChanged, + _ => failure.ToString(), + }; + + void OnItemChanged(object? sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(ProxyGenerationItem.Progress): + OnPropertyChanged(nameof(Percentage)); + break; + case nameof(ProxyGenerationItem.Status): + case nameof(ProxyGenerationItem.Failure): + OnPropertyChanged(nameof(StatusText)); + OnPropertyChanged(nameof(IsWaiting)); + break; + } + } +} diff --git a/ProxyForge/ViewModels/GenerationProgressViewModel.cs b/ProxyForge/ViewModels/GenerationProgressViewModel.cs new file mode 100644 index 0000000..412818f --- /dev/null +++ b/ProxyForge/ViewModels/GenerationProgressViewModel.cs @@ -0,0 +1,55 @@ +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Windows; +using ProxyForge.Encoding; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.ViewModels; + +internal sealed class GenerationProgressViewModel : Bindable +{ + readonly ObservableCollection source; + + public GenerationProgressViewModel(ObservableCollection source) + { + this.source = source; + CollectionChangedEventManager.AddHandler(source, OnSourceChanged); + Rebuild(); + } + + public ObservableCollection Items { get; } = []; + + public bool HasItems => Items.Count > 0; + + void OnSourceChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + switch (e.Action) + { + case NotifyCollectionChangedAction.Add when e.NewItems is not null: + var index = e.NewStartingIndex < 0 ? Items.Count : e.NewStartingIndex; + foreach (ProxyGenerationItem item in e.NewItems) + Items.Insert(index++, new GenerationItemViewModel(item)); + break; + case NotifyCollectionChangedAction.Remove when e.OldItems is not null: + foreach (ProxyGenerationItem item in e.OldItems) + { + var existing = Items.FirstOrDefault(candidate => ReferenceEquals(candidate.Item, item)); + if (existing is not null) + Items.Remove(existing); + } + break; + default: + Rebuild(); + break; + } + + OnPropertyChanged(nameof(HasItems)); + } + + void Rebuild() + { + Items.Clear(); + foreach (var item in source) + Items.Add(new GenerationItemViewModel(item)); + } +}