Crunchy-Downloader/CRD/ViewModels/Utils/ContentDialogEncodingPresetViewModel.cs
Elwador d9813191ad - Added **Global Pause button** for the download queue [#418](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/418)
- Added **fallback for sync failures** [#407](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/407)
- Added **history setting** to remove non-existent series/episodes on refresh [#420](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/420)
- Added **movies to history**
- Added **queue persistence**
- Changed **download item state handling**
- Changed **download item removal processing**
- Made small changes to **font detection**
- Changed **rate limit error handling**
- Fixed issue where **files were not always cleaned up** for removed downloads
- Fixed **crash when the queue list was modified**
- Fixed **changelog parsing** not handling versions like `vX.X.X.X`, which caused changes to be re-added on every restart
2026-04-20 15:40:58 +02:00

194 lines
7.3 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CRD.Utils.Ffmpeg_Encoding;
using CRD.Utils.Files;
using CRD.Utils.Structs;
using CRD.Views;
using DynamicData;
using FluentAvalonia.UI.Controls;
using ReactiveUI;
namespace CRD.ViewModels.Utils;
public partial class ContentDialogEncodingPresetViewModel : ViewModelBase{
private readonly ContentDialog dialog;
[ObservableProperty]
private bool editMode;
[ObservableProperty]
private string presetName;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(HasCodec))]
private string codec;
[ObservableProperty]
private ComboBoxItem selectedResolution = new();
[ObservableProperty]
private double? crf = 23;
[ObservableProperty]
private string frameRate = "";
[ObservableProperty]
private string additionalParametersString = "";
[ObservableProperty]
private ObservableCollection<StringItem> additionalParameters = new();
[ObservableProperty]
private VideoPreset? selectedCustomPreset;
[ObservableProperty]
private bool fileExists;
public bool HasCodec => !string.IsNullOrWhiteSpace(Codec);
public ObservableCollection<VideoPreset> CustomPresetsList{ get; } = new(){ };
public ObservableCollection<ComboBoxItem> ResolutionList{ get; } = new(){
new ComboBoxItem(){ Content = "3840:2160" }, // 4K UHD
new ComboBoxItem(){ Content = "3440:1440" }, // Ultra-Wide Quad HD
new ComboBoxItem(){ Content = "2560:1440" }, // 1440p
new ComboBoxItem(){ Content = "2560:1080" }, // Ultra-Wide Full HD
new ComboBoxItem(){ Content = "2160:1080" }, // 2:1 Aspect Ratio
new ComboBoxItem(){ Content = "1920:1080" }, // 1080p Full HD
new ComboBoxItem(){ Content = "1920:800" }, // Cinematic 2.40:1
new ComboBoxItem(){ Content = "1600:900" }, // 900p
new ComboBoxItem(){ Content = "1366:768" }, // 768p
new ComboBoxItem(){ Content = "1280:960" }, // SXGA 4:3
new ComboBoxItem(){ Content = "1280:720" }, // 720p HD
new ComboBoxItem(){ Content = "1024:576" }, // 576p
new ComboBoxItem(){ Content = "960:540" }, // 540p qHD
new ComboBoxItem(){ Content = "854:480" }, // 480p
new ComboBoxItem(){ Content = "800:600" }, // SVGA
new ComboBoxItem(){ Content = "768:432" }, // 432p
new ComboBoxItem(){ Content = "720:480" }, // NTSC SD
new ComboBoxItem(){ Content = "704:576" }, // PAL SD
new ComboBoxItem(){ Content = "640:360" }, // 360p
new ComboBoxItem(){ Content = "426:240" }, // 240p
new ComboBoxItem(){ Content = "320:240" }, // QVGA
new ComboBoxItem(){ Content = "320:180" }, // 180p
new ComboBoxItem(){ Content = "256:144" }, // 144p
};
public ContentDialogEncodingPresetViewModel(ContentDialog dialog, bool editMode){
this.dialog = dialog;
if (dialog is null){
throw new ArgumentNullException(nameof(dialog));
}
AdditionalParameters.Add(new StringItem(){ stringValue = "-map 0" });
if (editMode){
EditMode = true;
CustomPresetsList.AddRange(FfmpegEncoding.presets.Skip(15));
this.dialog.Title = "Edit Encoding Preset";
if (CustomPresetsList.Count == 0){
MessageBus.Current.SendMessage(new ToastMessage($"There are no presets to be edited", ToastType.Warning, 5));
EditMode = false;
} else{
SelectedCustomPreset = CustomPresetsList.First();
}
}
dialog.Closed += DialogOnClosed;
dialog.PrimaryButtonClick += SaveButton;
}
partial void OnSelectedCustomPresetChanged(VideoPreset value){
PresetName = value.PresetName ?? "";
Codec = value.Codec ?? "";
Crf = value.Crf;
FrameRate = value.FrameRate ?? "24000/1001";
SelectedResolution = ResolutionList.FirstOrDefault(e => e.Content?.ToString() == value.Resolution) ?? ResolutionList.First();
AdditionalParameters.Clear();
foreach (var valueAdditionalParameter in value.AdditionalParameters){
AdditionalParameters.Add(new StringItem(){ stringValue = valueAdditionalParameter });
}
AdditionalParametersString = "";
}
partial void OnPresetNameChanged(string value){
var path = Path.Combine(CfgManager.PathENCODING_PRESETS_DIR, value + ".json");
var fileExists = File.Exists(path);
dialog.IsPrimaryButtonEnabled = !fileExists || EditMode && value == SelectedCustomPreset?.PresetName;
FileExists = !dialog.IsPrimaryButtonEnabled;
}
[RelayCommand]
public void AddAdditionalParam(){
AdditionalParameters.Add(new StringItem(){ stringValue = AdditionalParametersString });
AdditionalParametersString = "";
RaisePropertyChanged(nameof(AdditionalParametersString));
}
[RelayCommand]
public void RemoveAdditionalParam(StringItem param){
AdditionalParameters.Remove(param);
RaisePropertyChanged(nameof(AdditionalParameters));
}
private void SaveButton(ContentDialog sender, ContentDialogButtonClickEventArgs args){
dialog.PrimaryButtonClick -= SaveButton;
if (EditMode){
if (SelectedCustomPreset != null){
var oldName = SelectedCustomPreset.PresetName;
SelectedCustomPreset.PresetName = PresetName;
SelectedCustomPreset.Codec = Codec;
SelectedCustomPreset.FrameRate = FrameRate;
SelectedCustomPreset.Crf = Math.Clamp((int)(Crf ?? 0), 0, 51);
SelectedCustomPreset.Resolution = SelectedResolution.Content?.ToString() ?? "1920:1080";
SelectedCustomPreset.AdditionalParameters = AdditionalParameters.Select(additionalParameter => additionalParameter.stringValue).ToList();
try{
var oldPath = Path.Combine(CfgManager.PathENCODING_PRESETS_DIR, oldName + ".json");
var path = Path.Combine(CfgManager.PathENCODING_PRESETS_DIR, SelectedCustomPreset.PresetName + ".json");
if (File.Exists(oldPath)){
File.Delete(oldPath);
}
CfgManager.WriteJsonToFile(path, SelectedCustomPreset);
} catch (Exception e){
Console.Error.WriteLine("Error saving preset: " + e);
}
}
} else{
VideoPreset newPreset = new VideoPreset(){
PresetName = PresetName,
Codec = Codec,
FrameRate = FrameRate,
Crf = Math.Clamp((int)(Crf ?? 0), 0, 51),
Resolution = SelectedResolution.Content?.ToString() ?? "1920:1080",
AdditionalParameters = AdditionalParameters.Select(additionalParameter => additionalParameter.stringValue).ToList()
};
CfgManager.WriteJsonToFile(Path.Combine(CfgManager.PathENCODING_PRESETS_DIR, newPreset.PresetName + ".json"), newPreset);
FfmpegEncoding.AddPreset(newPreset);
}
}
private void DialogOnClosed(ContentDialog sender, ContentDialogClosedEventArgs args){
dialog.Closed -= DialogOnClosed;
}
}