Crunchy-Downloader/CRD/ViewModels/SeriesPageViewModel.cs
Elwador 4f6d0f2257 Add - Added --historyRefreshActive parameter
Add - Added **hardware acceleration options** for sync timings
Add - Added an **icon for episodes removed from Crunchyroll** or moved to a different season
Add - Added **highlighting for selected dubs/subs** in history
Add - Added **highlighting of episode titles** when all selected dubs/subs are available
Add - Added option to **set history series to inactive**
Add - Added **filter for Active and Inactive** series in history
Add - Added **download retry options** to settings, making retry variables editable
Chg - Changed **Sonarr missing filter** to respect the "Skip Unmonitored" setting
Chg - Changed to **show an error** if an episode wasn't added to the queue
Chg - Changed to show **dates for episodes** in the history
Chg - Changed **sync timings algorithm** to improve overall performance
Chg - Changed **sync timings algorithm** to more accurately synchronize dubs
Chg - Changed **series/season refresh messages** to indicate failure or success more clearly
Chg - Changed **error display frequency** to reduce repeated popups for the same message
Fix - Fixed **movie detection** to properly verify if the ID corresponds to a movie
Fix - Fixed **long option hiding remove buttons** for FFmpeg and MKVMerge additional options
Fix - Fixed **toast message timer** not updating correctly
Fix - Fixed **path length error**
Fix - Fixed a **rare crash when navigating history**
2025-04-23 22:36:05 +02:00

227 lines
No EOL
7.6 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CRD.Downloader;
using CRD.Downloader.Crunchyroll;
using CRD.Utils;
using CRD.Utils.Files;
using CRD.Utils.Structs;
using CRD.Utils.Structs.History;
using CRD.ViewModels.Utils;
using CRD.Views;
using CRD.Views.Utils;
using FluentAvalonia.UI.Controls;
using ReactiveUI;
namespace CRD.ViewModels;
public partial class SeriesPageViewModel : ViewModelBase{
[ObservableProperty]
public HistorySeries _selectedSeries;
[ObservableProperty]
public static bool _editMode;
[ObservableProperty]
public static bool _sonarrAvailable;
[ObservableProperty]
public static bool _showMonitoredBookmark;
[ObservableProperty]
public static bool _sonarrConnected;
private IStorageProvider? _storageProvider;
public SeriesPageViewModel(){
_storageProvider = ProgramManager.Instance.StorageProvider ?? throw new ArgumentNullException(nameof(ProgramManager.Instance.StorageProvider));
_selectedSeries = CrunchyrollManager.Instance.SelectedSeries;
if (_selectedSeries.ThumbnailImage == null){
_ = _selectedSeries.LoadImage();
}
if (CrunchyrollManager.Instance.CrunOptions.SonarrProperties != null){
SonarrConnected = CrunchyrollManager.Instance.CrunOptions.SonarrProperties.SonarrEnabled;
if (!string.IsNullOrEmpty(SelectedSeries.SonarrSeriesId)){
SonarrAvailable = SelectedSeries.SonarrSeriesId.Length > 0 && SonarrConnected;
if (SonarrAvailable){
ShowMonitoredBookmark = CrunchyrollManager.Instance.CrunOptions.HistorySkipUnmonitored;
}
} else{
SonarrAvailable = false;
}
} else{
SonarrConnected = SonarrAvailable = false;
}
SelectedSeries.UpdateSeriesFolderPath();
}
[RelayCommand]
public async Task OpenFolderDialogAsync(HistorySeason? season){
if (_storageProvider == null){
Console.Error.WriteLine("StorageProvider must be set before using the dialog.");
throw new InvalidOperationException("StorageProvider must be set before using the dialog.");
}
var result = await _storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions{
Title = "Select Folder"
});
if (result.Count > 0){
var selectedFolder = result[0];
var folderPath = selectedFolder.Path.IsAbsoluteUri ? selectedFolder.Path.LocalPath : selectedFolder.Path.ToString();
Console.WriteLine($"Selected folder: {folderPath}");
if (season != null){
season.SeasonDownloadPath = folderPath;
CfgManager.UpdateHistoryFile();
} else{
SelectedSeries.SeriesDownloadPath = folderPath;
CfgManager.UpdateHistoryFile();
}
}
SelectedSeries.UpdateSeriesFolderPath();
}
[RelayCommand]
public async Task MatchSonarrSeries_Button(){
var dialog = new ContentDialog(){
Title = "Sonarr Matching",
PrimaryButtonText = "Save",
CloseButtonText = "Close",
FullSizeDesired = true
};
var viewModel = new ContentDialogSonarrMatchViewModel(dialog, SelectedSeries.SonarrSeriesId, SelectedSeries.SeriesTitle);
dialog.Content = new ContentDialogSonarrMatchView(){
DataContext = viewModel
};
var dialogResult = await dialog.ShowAsync();
if (dialogResult == ContentDialogResult.Primary){
SelectedSeries.SonarrSeriesId = viewModel.CurrentSonarrSeries.Id.ToString();
SelectedSeries.SonarrTvDbId = viewModel.CurrentSonarrSeries.TvdbId.ToString();
SelectedSeries.SonarrSlugTitle = viewModel.CurrentSonarrSeries.TitleSlug;
if (CrunchyrollManager.Instance.CrunOptions.SonarrProperties != null){
SonarrConnected = CrunchyrollManager.Instance.CrunOptions.SonarrProperties.SonarrEnabled;
if (!string.IsNullOrEmpty(SelectedSeries.SonarrSeriesId)){
SonarrAvailable = SelectedSeries.SonarrSeriesId.Length > 0 && SonarrConnected;
} else{
SonarrAvailable = false;
}
} else{
SonarrConnected = SonarrAvailable = false;
}
_ = UpdateData("");
}
}
[RelayCommand]
public async Task DownloadSeasonAll(HistorySeason season){
var downloadTasks = season.EpisodesList
.Select(episode => episode.DownloadEpisode());
await Task.WhenAll(downloadTasks);
}
[RelayCommand]
public async Task DownloadSeasonMissing(HistorySeason season){
var downloadTasks = season.EpisodesList
.Where(episode => !episode.WasDownloaded)
.Select(episode => episode.DownloadEpisode()).ToList();
if (downloadTasks.Count == 0){
MessageBus.Current.SendMessage(new ToastMessage($"There are no missing episodes", ToastType.Error, 3));
} else{
await Task.WhenAll(downloadTasks);
}
}
[RelayCommand]
public async Task DownloadSeasonMissingSonarr(HistorySeason season){
var downloadTasks = season.EpisodesList
.Where(episode => !episode.SonarrHasFile)
.Select(episode => episode.DownloadEpisode());
await Task.WhenAll(downloadTasks);
}
[RelayCommand]
public void ToggleDownloadedMark(HistorySeason season){
bool allDownloaded = season.EpisodesList.All(ep => ep.WasDownloaded);
foreach (var historyEpisode in season.EpisodesList){
if (historyEpisode.WasDownloaded == allDownloaded){
season.UpdateDownloaded(historyEpisode.EpisodeId);
}
}
}
[RelayCommand]
public async Task UpdateData(string? season){
var result = await SelectedSeries.FetchData(season);
MessageBus.Current.SendMessage(result
? new ToastMessage(string.IsNullOrEmpty(season) ? $"Series Refreshed" : $"Season Refreshed", ToastType.Information, 2)
: new ToastMessage(string.IsNullOrEmpty(season) ? $"Series Refresh Failed" : $"Season Refresh Failed", ToastType.Error, 2));
SelectedSeries.Seasons.Refresh();
// MessageBus.Current.SendMessage(new NavigationMessage(typeof(SeriesPageViewModel), false, true));
}
[RelayCommand]
public void RemoveSeason(string? season){
HistorySeason? objectToRemove = SelectedSeries.Seasons.FirstOrDefault(se => se.SeasonId == season) ?? null;
if (objectToRemove != null){
SelectedSeries.Seasons.Remove(objectToRemove);
CfgManager.UpdateHistoryFile();
}
MessageBus.Current.SendMessage(new NavigationMessage(typeof(SeriesPageViewModel), false, true));
}
[RelayCommand]
public void ToggleInactive(){
CfgManager.UpdateHistoryFile();
}
[RelayCommand]
public void NavBack(){
SelectedSeries.UpdateNewEpisodes();
MessageBus.Current.SendMessage(new NavigationMessage(null, true, false));
}
[RelayCommand]
public void OpenFolderPath(){
try{
Process.Start(new ProcessStartInfo{
FileName = SelectedSeries.SeriesFolderPath,
UseShellExecute = true,
Verb = "open"
});
} catch (Exception ex){
Console.Error.WriteLine($"An error occurred while opening the folder: {ex.Message}");
}
}
}