From 9d499682ba9c11d5e55f29d6405dd1c1f23f424d Mon Sep 17 00:00:00 2001 From: Elwador <75888166+Elwador@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:11:13 +0200 Subject: [PATCH] Chg - Save Auto Download & Remove Finished in settings Fix - Search popup didn't close when dismissed --- CRD/Downloader/CalendarManager.cs | 2 - .../Crunchyroll/CrunchyrollManager.cs | 77 +++++++++++-------- CRD/Utils/Files/CfgManager.cs | 4 +- CRD/Utils/Structs/CrDownloadOptions.cs | 4 +- CRD/ViewModels/AddDownloadPageViewModel.cs | 1 - CRD/ViewModels/DownloadsPageViewModel.cs | 9 ++- CRD/ViewModels/MainWindowViewModel.cs | 18 ++--- CRD/Views/AddDownloadPageView.axaml | 5 +- CRD/Views/AddDownloadPageView.axaml.cs | 10 ++- 9 files changed, 74 insertions(+), 56 deletions(-) diff --git a/CRD/Downloader/CalendarManager.cs b/CRD/Downloader/CalendarManager.cs index ce83426..f687318 100644 --- a/CRD/Downloader/CalendarManager.cs +++ b/CRD/Downloader/CalendarManager.cs @@ -148,8 +148,6 @@ public class CalendarManager{ public async Task BuildCustomCalendar(bool forceUpdate){ - Console.WriteLine("C" + DateTime.Now.ToString("yyyy-MM-dd")); - if (!forceUpdate && calendar.TryGetValue("C" + DateTime.Now.ToString("yyyy-MM-dd"), out var forDate)){ return forDate; } diff --git a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs index 1278a8e..2f91ddc 100644 --- a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs +++ b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net.Http; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Xml; using Avalonia.Media; @@ -18,6 +19,7 @@ using CRD.Utils.Muxing; using CRD.Utils.Sonarr; using CRD.Utils.Structs; using CRD.Utils.Structs.History; +using CRD.ViewModels; using CRD.Views; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -30,7 +32,8 @@ public class CrunchyrollManager{ public CrCmsToken? CmsToken; public CrProfile Profile = new(); - public CrDownloadOptions CrunOptions; + private readonly Lazy _optionsLazy; + public CrDownloadOptions CrunOptions => _optionsLazy.Value; #region History Variables @@ -55,7 +58,7 @@ public class CrunchyrollManager{ public CrEpisode CrEpisode; public CrSeries CrSeries; public History History; - + #region Singelton private static CrunchyrollManager? _instance; @@ -78,42 +81,50 @@ public class CrunchyrollManager{ #endregion public CrunchyrollManager(){ - CrunOptions = new CrDownloadOptions(); + _optionsLazy = new Lazy(InitDownloadOptions, LazyThreadSafetyMode.ExecutionAndPublication); } - public void InitOptions(){ - CrunOptions.AutoDownload = false; - CrunOptions.RemoveFinishedDownload = false; - CrunOptions.Chapters = true; - CrunOptions.Hslang = "none"; - CrunOptions.Force = "Y"; - CrunOptions.FileName = "${seriesTitle} - S${season}E${episode} [${height}p]"; - CrunOptions.Partsize = 10; - CrunOptions.DlSubs = new List{ "de-DE" }; - CrunOptions.Skipmux = false; - CrunOptions.MkvmergeOptions = new List{ "--no-date", "--disable-track-statistics-tags", "--engage no_variable_data" }; - CrunOptions.FfmpegOptions = new(); - CrunOptions.DefaultAudio = "ja-JP"; - CrunOptions.DefaultSub = "de-DE"; - CrunOptions.CcTag = "cc"; - CrunOptions.FsRetryTime = 5; - CrunOptions.Numbers = 2; - CrunOptions.Timeout = 15000; - CrunOptions.DubLang = new List(){ "ja-JP" }; - CrunOptions.SimultaneousDownloads = 2; - CrunOptions.AccentColor = Colors.SlateBlue.ToString(); - CrunOptions.Theme = "System"; - CrunOptions.SelectedCalendarLanguage = "en-us"; - CrunOptions.CalendarDubFilter = "none"; - CrunOptions.DlVideoOnce = true; - CrunOptions.StreamEndpoint = "web/firefox"; - CrunOptions.SubsAddScaledBorder = ScaledBorderAndShadowSelection.ScaledBorderAndShadowYes; - CrunOptions.HistoryLang = DefaultLocale; - CrunOptions.History = true; + private CrDownloadOptions InitDownloadOptions(){ - CfgManager.UpdateSettingsFromFile(); + var options = new CrDownloadOptions(); + options.AutoDownload = false; + options.RemoveFinishedDownload = false; + options.Chapters = true; + options.Hslang = "none"; + options.Force = "Y"; + options.FileName = "${seriesTitle} - S${season}E${episode} [${height}p]"; + options.Partsize = 10; + options.DlSubs = new List{ "de-DE" }; + options.Skipmux = false; + options.MkvmergeOptions = new List{ "--no-date", "--disable-track-statistics-tags", "--engage no_variable_data" }; + options.FfmpegOptions = new(); + options.DefaultAudio = "ja-JP"; + options.DefaultSub = "de-DE"; + options.CcTag = "cc"; + options.FsRetryTime = 5; + options.Numbers = 2; + options.Timeout = 15000; + options.DubLang = new List(){ "ja-JP" }; + options.SimultaneousDownloads = 2; + options.AccentColor = Colors.SlateBlue.ToString(); + options.Theme = "System"; + options.SelectedCalendarLanguage = "en-us"; + options.CalendarDubFilter = "none"; + options.DlVideoOnce = true; + options.StreamEndpoint = "web/firefox"; + options.SubsAddScaledBorder = ScaledBorderAndShadowSelection.ScaledBorderAndShadowYes; + options.HistoryLang = DefaultLocale; + + options.History = true; + + CfgManager.UpdateSettingsFromFile(options); + + return options; + } + + public void InitOptions(){ _widevine = Widevine.Instance; CrAuth = new CrAuth(); diff --git a/CRD/Utils/Files/CfgManager.cs b/CRD/Utils/Files/CfgManager.cs index 12dd040..84cd932 100644 --- a/CRD/Utils/Files/CfgManager.cs +++ b/CRD/Utils/Files/CfgManager.cs @@ -147,7 +147,7 @@ public class CfgManager{ } - public static void UpdateSettingsFromFile(){ + public static void UpdateSettingsFromFile(CrDownloadOptions options){ string dirPath = Path.GetDirectoryName(PathCrDownloadOptions) ?? string.Empty; if (!Directory.Exists(dirPath)){ @@ -174,7 +174,7 @@ public class CfgManager{ var propertiesPresentInYaml = GetTopLevelPropertiesInYaml(input); var loadedOptions = deserializer.Deserialize(new StringReader(input)); - var instanceOptions = CrunchyrollManager.Instance.CrunOptions; + var instanceOptions = options; foreach (PropertyInfo property in typeof(CrDownloadOptions).GetProperties()){ var yamlMemberAttribute = property.GetCustomAttribute(); diff --git a/CRD/Utils/Structs/CrDownloadOptions.cs b/CRD/Utils/Structs/CrDownloadOptions.cs index 0deeb80..c48384a 100644 --- a/CRD/Utils/Structs/CrDownloadOptions.cs +++ b/CRD/Utils/Structs/CrDownloadOptions.cs @@ -7,11 +7,11 @@ namespace CRD.Utils.Structs; public class CrDownloadOptions{ - [YamlIgnore] + [YamlMember(Alias = "auto_download", ApplyNamingConventions = false)] public bool AutoDownload{ get; set; } - [YamlIgnore] + [YamlMember(Alias = "remove_finished_downloads", ApplyNamingConventions = false)] public bool RemoveFinishedDownload{ get; set; } diff --git a/CRD/ViewModels/AddDownloadPageViewModel.cs b/CRD/ViewModels/AddDownloadPageViewModel.cs index 1cb5f2a..14b1f4d 100644 --- a/CRD/ViewModels/AddDownloadPageViewModel.cs +++ b/CRD/ViewModels/AddDownloadPageViewModel.cs @@ -93,7 +93,6 @@ public partial class AddDownloadPageViewModel : ViewModelBase{ SearchItems.Add(episode); } - SearchPopupVisible = true; RaisePropertyChanged(nameof(SearchItems)); RaisePropertyChanged(nameof(SearchVisible)); diff --git a/CRD/ViewModels/DownloadsPageViewModel.cs b/CRD/ViewModels/DownloadsPageViewModel.cs index fb46262..538a556 100644 --- a/CRD/ViewModels/DownloadsPageViewModel.cs +++ b/CRD/ViewModels/DownloadsPageViewModel.cs @@ -9,6 +9,7 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CRD.Downloader; using CRD.Downloader.Crunchyroll; +using CRD.Utils; using CRD.Utils.Structs; namespace CRD.ViewModels; @@ -17,10 +18,10 @@ public partial class DownloadsPageViewModel : ViewModelBase{ public ObservableCollection Items{ get; } [ObservableProperty] - public bool _autoDownload; + private bool _autoDownload; [ObservableProperty] - public bool _removeFinished; + private bool _removeFinished; public DownloadsPageViewModel(){ QueueManager.Instance.UpdateDownloadListItems(); @@ -28,16 +29,18 @@ public partial class DownloadsPageViewModel : ViewModelBase{ AutoDownload = CrunchyrollManager.Instance.CrunOptions.AutoDownload; RemoveFinished = CrunchyrollManager.Instance.CrunOptions.RemoveFinishedDownload; } - + partial void OnAutoDownloadChanged(bool value){ CrunchyrollManager.Instance.CrunOptions.AutoDownload = value; if (value){ QueueManager.Instance.UpdateDownloadListItems(); } + CfgManager.WriteSettingsToFile(); } partial void OnRemoveFinishedChanged(bool value){ CrunchyrollManager.Instance.CrunOptions.RemoveFinishedDownload = value; + CfgManager.WriteSettingsToFile(); } } diff --git a/CRD/ViewModels/MainWindowViewModel.cs b/CRD/ViewModels/MainWindowViewModel.cs index cc2e3d5..0cddf7a 100644 --- a/CRD/ViewModels/MainWindowViewModel.cs +++ b/CRD/ViewModels/MainWindowViewModel.cs @@ -20,27 +20,26 @@ public partial class MainWindowViewModel : ViewModelBase{ [ObservableProperty] private bool _updateAvailable = true; - + public MainWindowViewModel(){ _faTheme = App.Current.Styles[0] as FluentAvaloniaTheme; Init(); CleanUpOldUpdater(); - } - private void CleanUpOldUpdater() { + private void CleanUpOldUpdater(){ string backupFilePath = Path.Combine(Directory.GetCurrentDirectory(), "Updater.exe.bak"); - if (File.Exists(backupFilePath)) { - try { + if (File.Exists(backupFilePath)){ + try{ File.Delete(backupFilePath); Console.WriteLine($"Deleted old updater file: {backupFilePath}"); - } catch (Exception ex) { + } catch (Exception ex){ Console.Error.WriteLine($"Failed to delete old updater file: {ex.Message}"); } - } else { + } else{ Console.WriteLine("No old updater file found to delete."); } } @@ -48,7 +47,7 @@ public partial class MainWindowViewModel : ViewModelBase{ public async void Init(){ UpdateAvailable = await Updater.Instance.CheckForUpdatesAsync(); - + CrunchyrollManager.Instance.InitOptions(); if (CrunchyrollManager.Instance.CrunOptions.AccentColor != null){ @@ -64,8 +63,7 @@ public partial class MainWindowViewModel : ViewModelBase{ _faTheme.PreferSystemTheme = false; Application.Current.RequestedThemeVariant = ThemeVariant.Light; } - + await CrunchyrollManager.Instance.Init(); - } } \ No newline at end of file diff --git a/CRD/Views/AddDownloadPageView.axaml b/CRD/Views/AddDownloadPageView.axaml index f719e9d..4f7afd1 100644 --- a/CRD/Views/AddDownloadPageView.axaml +++ b/CRD/Views/AddDownloadPageView.axaml @@ -25,12 +25,13 @@ - + PlacementTarget="{Binding ElementName=SearchBar}" + Closed="Popup_Closed">