110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using System.ComponentModel;
|
||
using System.Collections.ObjectModel;
|
||
using System.Runtime.CompilerServices;
|
||
using EmbyToolbox.Models;
|
||
|
||
namespace EmbyToolbox.ViewModels;
|
||
|
||
public sealed class AddFilesOptionsViewModel : INotifyPropertyChanged
|
||
{
|
||
private readonly Action<AddFilesOptions> _onAdd;
|
||
private readonly Action _onCancel;
|
||
|
||
private bool _removeForeignAudioAndSubtitles;
|
||
private ConversionProfilePresetRow? _selectedProfile;
|
||
|
||
public AddFilesOptionsViewModel(
|
||
IReadOnlyList<ConversionProfilePresetRow> profiles,
|
||
string selectedProfileName,
|
||
Action<AddFilesOptions> onAdd,
|
||
Action onCancel)
|
||
{
|
||
_onAdd = onAdd;
|
||
_onCancel = onCancel;
|
||
Profiles = new ObservableCollection<ConversionProfilePresetRow>(profiles);
|
||
_selectedProfile = Profiles.FirstOrDefault(p => p.Profile.Equals(selectedProfileName, StringComparison.OrdinalIgnoreCase))
|
||
?? Profiles.FirstOrDefault(p => p.Profile.Equals("Emby", StringComparison.OrdinalIgnoreCase))
|
||
?? Profiles.FirstOrDefault();
|
||
AddCommand = new RelayCommand(ExecuteAdd);
|
||
CancelCommand = new RelayCommand(() => _onCancel());
|
||
}
|
||
|
||
/// <summary>Взаимоисключающие опции: два свойства, чтобы не использовать TwoWay на одно поле с конвертером (переполнение стека в WPF).</summary>
|
||
public bool OptionKeepAllTracks
|
||
{
|
||
get => !_removeForeignAudioAndSubtitles;
|
||
set
|
||
{
|
||
if (!value)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (!_removeForeignAudioAndSubtitles)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_removeForeignAudioAndSubtitles = false;
|
||
OnPropertyChanged();
|
||
OnPropertyChanged(nameof(OptionRemoveForeignTracks));
|
||
}
|
||
}
|
||
|
||
public bool OptionRemoveForeignTracks
|
||
{
|
||
get => _removeForeignAudioAndSubtitles;
|
||
set
|
||
{
|
||
if (!value)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (_removeForeignAudioAndSubtitles)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_removeForeignAudioAndSubtitles = true;
|
||
OnPropertyChanged();
|
||
OnPropertyChanged(nameof(OptionKeepAllTracks));
|
||
}
|
||
}
|
||
|
||
public ObservableCollection<ConversionProfilePresetRow> Profiles { get; }
|
||
|
||
public ConversionProfilePresetRow? SelectedProfile
|
||
{
|
||
get => _selectedProfile;
|
||
set
|
||
{
|
||
if (ReferenceEquals(_selectedProfile, value))
|
||
{
|
||
return;
|
||
}
|
||
|
||
_selectedProfile = value;
|
||
OnPropertyChanged();
|
||
}
|
||
}
|
||
|
||
public RelayCommand AddCommand { get; }
|
||
public RelayCommand CancelCommand { get; }
|
||
|
||
private void ExecuteAdd()
|
||
{
|
||
_onAdd(
|
||
new AddFilesOptions
|
||
{
|
||
Profile = string.IsNullOrWhiteSpace(_selectedProfile?.Profile) ? "Emby" : _selectedProfile.Profile,
|
||
RemoveForeignAudioAndSubtitles = _removeForeignAudioAndSubtitles
|
||
});
|
||
}
|
||
|
||
public event PropertyChangedEventHandler? PropertyChanged;
|
||
|
||
private void OnPropertyChanged([CallerMemberName] string? name = null) =>
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||
}
|