From 287492bb8ad97c88b3c705ac7dde1b1c114bd528 Mon Sep 17 00:00:00 2001 From: Daniel Paulino Date: Fri, 8 Mar 2024 19:53:10 -0800 Subject: [PATCH] Added timeout settings --- .../Client/NightingaleClient.cs | 31 +- .../Settings/SettingsConstants.cs | 6 +- .../UserControls/SettingsControl.xaml | 22 + .../ViewModels/SettingsViewModel.cs | 520 ++++++++++-------- 4 files changed, 328 insertions(+), 251 deletions(-) diff --git a/src/Nightingale.Core/Client/NightingaleClient.cs b/src/Nightingale.Core/Client/NightingaleClient.cs index 15ad955..c18bb6b 100644 --- a/src/Nightingale.Core/Client/NightingaleClient.cs +++ b/src/Nightingale.Core/Client/NightingaleClient.cs @@ -8,6 +8,7 @@ using Nightingale.Core.Interfaces; using Nightingale.Core.Logging; using Nightingale.Core.Models; +using Nightingale.Core.Settings; using Nightingale.Core.Workspaces.Extensions; using Nightingale.Core.Workspaces.Models; using System; @@ -41,7 +42,8 @@ public NightingaleClient( ICookieJar cookieJar, ISslValidator validator, IBodyBuilder bodyBuilder, - IHeaderBuilder headerBuilder) + IHeaderBuilder headerBuilder, + IUserSettings userSettings) { var handler = new HttpClientHandler { @@ -57,17 +59,28 @@ public NightingaleClient( }; _client = new HttpClient(handler); - _varRes = variableResolver - ?? throw new ArgumentNullException(nameof(variableResolver)); - _cookieJar = cookieJar - ?? throw new ArgumentNullException(nameof(cookieJar)); - _bodyBuilder = bodyBuilder - ?? throw new ArgumentNullException(nameof(bodyBuilder)); - _headerBuilder = headerBuilder - ?? throw new ArgumentNullException(nameof(headerBuilder)); + ApplyTimeoutSettings(userSettings, _client); + _varRes = variableResolver; + _cookieJar = cookieJar; + _bodyBuilder = bodyBuilder; + _headerBuilder = headerBuilder; _digestAuthenticator = new DigestAuthenticator(); } + private static void ApplyTimeoutSettings(IUserSettings userSettings, HttpClient client) + { + if (userSettings.Get(SettingsConstants.InfiniteTimeoutKey) is true) + { + client.Timeout = Timeout.InfiniteTimeSpan; + } + else if (userSettings.Get(SettingsConstants.TimeoutSecondsKey) is double timeoutSeconds && + timeoutSeconds > 0 && + timeoutSeconds != 100d) + { + client.Timeout = TimeSpan.FromSeconds(timeoutSeconds); + } + } + /// public async Task SendAsync( Item request, diff --git a/src/Nightingale.Core/Settings/SettingsConstants.cs b/src/Nightingale.Core/Settings/SettingsConstants.cs index 33b1524..c14c81a 100644 --- a/src/Nightingale.Core/Settings/SettingsConstants.cs +++ b/src/Nightingale.Core/Settings/SettingsConstants.cs @@ -37,6 +37,8 @@ public sealed class SettingsConstants public const string BackgroundImage = "BackgroundImage"; public const string AlwaysWrapURL = "AlwaysWrapURL"; public const string WordWrapEditor = "WordWrapEditor"; + public const string TimeoutSecondsKey = "TimeOutSeconds"; + public const string InfiniteTimeoutKey = "InfiniteTimeout"; /// /// Settings defaults. @@ -69,7 +71,9 @@ public sealed class SettingsConstants { AlwaysWrapURL, false }, { WordWrapEditor, false }, { BackgroundImage, "" }, - { AutoSaveInterval, true } + { AutoSaveInterval, true }, + { TimeoutSecondsKey, 100d }, + { InfiniteTimeoutKey, false }, }; } } diff --git a/src/Nightingale/UserControls/SettingsControl.xaml b/src/Nightingale/UserControls/SettingsControl.xaml index 9c008ff..6f68178 100644 --- a/src/Nightingale/UserControls/SettingsControl.xaml +++ b/src/Nightingale/UserControls/SettingsControl.xaml @@ -141,6 +141,28 @@ FontFamily="Segoe MDL2 Assets" Text="" /> + + + + + + + + + + diff --git a/src/Nightingale/ViewModels/SettingsViewModel.cs b/src/Nightingale/ViewModels/SettingsViewModel.cs index 4d0fb59..91faaae 100644 --- a/src/Nightingale/ViewModels/SettingsViewModel.cs +++ b/src/Nightingale/ViewModels/SettingsViewModel.cs @@ -1,4 +1,5 @@ -using Microsoft.AppCenter.Analytics; +using CommunityToolkit.Mvvm.ComponentModel; +using Microsoft.AppCenter.Analytics; using Microsoft.Toolkit.Uwp.Helpers; using Nightingale.Core.Helpers.Interfaces; using Nightingale.Core.Settings; @@ -7,342 +8,379 @@ using System; using System.Collections.Generic; -namespace Nightingale.ViewModels +namespace Nightingale.ViewModels; + +public partial class SettingsViewModel : ObservableObject { - public class SettingsViewModel : ViewModelBase + private readonly IRecentUrlCache _recentUrlCache; + private readonly IUserSettings _userSettings; + + public SettingsViewModel(IUserSettings userSettings) { - private readonly IRecentUrlCache _recentUrlCache; + _userSettings = userSettings; + _timeoutText = _userSettings.Get(SettingsConstants.TimeoutSecondsKey).ToString(); + } - private bool _loading; - private int _pivotIndex; - private bool _deletingPasswords; - private bool _clearingRecentUrls; - private bool _clearRecentUrlSuccessful; - private bool _canClearUrls; + private bool _loading; + private int _pivotIndex; + private bool _deletingPasswords; + private bool _clearingRecentUrls; + private bool _clearRecentUrlSuccessful; + private bool _canClearUrls; - public event EventHandler SyncStatusUpdated; + public event EventHandler SyncStatusUpdated; - public bool ConfirmDeleteOn + public bool ConfirmDeleteOn + { + get => _userSettings.Get(SettingsConstants.ConfirmDeletion); + set { - get => UserSettings.Get(SettingsConstants.ConfirmDeletion); - set + if (value == ConfirmDeleteOn) { - if (value == ConfirmDeleteOn) - { - return; - } - - UserSettings.Set(SettingsConstants.ConfirmDeletion, value); - Analytics.TrackEvent("Settings changed: confirm deletion", new Dictionary - { - { "Value", value ? "true" : "false" } - }); + return; } + + _userSettings.Set(SettingsConstants.ConfirmDeletion, value); + Analytics.TrackEvent("Settings changed: confirm deletion", new Dictionary + { + { "Value", value ? "true" : "false" } + }); } + } - public bool EnvQuickEditOn + public bool EnvQuickEditOn + { + get => _userSettings.Get(SettingsConstants.EnableEnvQuickEdit); + set { - get => UserSettings.Get(SettingsConstants.EnableEnvQuickEdit); - set + if (value == EnvQuickEditOn) { - if (value == EnvQuickEditOn) - { - return; - } - - UserSettings.Set(SettingsConstants.EnableEnvQuickEdit, value); - Analytics.TrackEvent("Settings changed: EnableEnvQuickEdit", new Dictionary - { - { "Value", value ? "true" : "false" }, - { "Location", "settings" } - }); + return; } + + _userSettings.Set(SettingsConstants.EnableEnvQuickEdit, value); + Analytics.TrackEvent("Settings changed: EnableEnvQuickEdit", new Dictionary + { + { "Value", value ? "true" : "false" }, + { "Location", "settings" } + }); } + } - public bool MvpBadgeEnabled + public bool MvpBadgeEnabled + { + get => _userSettings.Get(SettingsConstants.ShowMvpBadge); + set { - get => UserSettings.Get(SettingsConstants.ShowMvpBadge); - set + if (value == MvpBadgeEnabled) { - if (value == MvpBadgeEnabled) - { - return; - } - - UserSettings.Set(SettingsConstants.ShowMvpBadge, value); - Analytics.TrackEvent("Settings changed: ShowMvpBadge", new Dictionary - { - { "Value", value ? "true" : "false" }, - }); + return; } + + _userSettings.Set(SettingsConstants.ShowMvpBadge, value); + Analytics.TrackEvent("Settings changed: ShowMvpBadge", new Dictionary + { + { "Value", value ? "true" : "false" }, + }); } + } - public bool AutoSaveIntervalEnabled + public bool AutoSaveIntervalEnabled + { + get => _userSettings.Get(SettingsConstants.AutoSaveInterval); + set { - get => UserSettings.Get(SettingsConstants.AutoSaveInterval); - set + if (value == AutoSaveIntervalEnabled) { - if (value == AutoSaveIntervalEnabled) - { - return; - } - - UserSettings.Set(SettingsConstants.AutoSaveInterval, value); - Analytics.TrackEvent("Settings changed: AutoSaveInterval", new Dictionary - { - { "Value", value ? "true" : "false" }, - }); + return; } + + _userSettings.Set(SettingsConstants.AutoSaveInterval, value); + Analytics.TrackEvent("Settings changed: AutoSaveInterval", new Dictionary + { + { "Value", value ? "true" : "false" }, + }); } + } - public bool HistoryEnabled + public bool HistoryEnabled + { + get => _userSettings.Get(SettingsConstants.HistoryEnabled); + set { - get => UserSettings.Get(SettingsConstants.HistoryEnabled); - set + if (value == HistoryEnabled) { - if (value == HistoryEnabled) - { - return; - } - - UserSettings.Set(SettingsConstants.HistoryEnabled, value); - Analytics.TrackEvent("Settings changed: history enabled", new Dictionary - { - { "Value", value ? "true" : "false" } - }); + return; } + + _userSettings.Set(SettingsConstants.HistoryEnabled, value); + Analytics.TrackEvent("Settings changed: history enabled", new Dictionary + { + { "Value", value ? "true" : "false" } + }); } + } - public bool TelemetryOn + public bool TelemetryOn + { + get => _userSettings.Get(SettingsConstants.TelemetryEnabledKey); + set { - get => UserSettings.Get(SettingsConstants.TelemetryEnabledKey); - set - { - if (value == TelemetryOn) - return; + if (value == TelemetryOn) + return; - Analytics.TrackEvent("Settings changed: telemetry", new Dictionary - { - { "Value", value.ToString() } - }); - UserSettings.Set(SettingsConstants.TelemetryEnabledKey, value); + Analytics.TrackEvent("Settings changed: telemetry", new Dictionary + { + { "Value", value.ToString() } + }); + _userSettings.Set(SettingsConstants.TelemetryEnabledKey, value); - ChangeTelemetry(value); - } + ChangeTelemetry(value); } + } - public bool AutoSaveOn + public bool AutoSaveOn + { + get => _userSettings.Get(SettingsConstants.AutoSaveEnabled); + set { - get => UserSettings.Get(SettingsConstants.AutoSaveEnabled); - set + if (value == AutoSaveOn) { - if (value == AutoSaveOn) - { - return; - } - - UserSettings.Set(SettingsConstants.AutoSaveEnabled, value); - RaisePropertyChanged(nameof(AutoSaveOn)); - - Analytics.TrackEvent("Settings changed: Auto save", new Dictionary - { - { "Value", value.ToString() } - }); + return; } - } - public bool AlwaysWrapUrlOn - { - get => UserSettings.Get(SettingsConstants.AlwaysWrapURL); - set + _userSettings.Set(SettingsConstants.AutoSaveEnabled, value); + OnPropertyChanged(nameof(AutoSaveOn)); + + Analytics.TrackEvent("Settings changed: Auto save", new Dictionary { - if (value == AlwaysWrapUrlOn) - { - return; - } - - UserSettings.Set(SettingsConstants.AlwaysWrapURL, value); - RaisePropertyChanged(nameof(AlwaysWrapUrlOn)); - - Analytics.TrackEvent("Settings changed: always wrap", new Dictionary - { - { "Value", value.ToString() } - }); - } + { "Value", value.ToString() } + }); } + } - public bool WordWrapEnabled + public bool AlwaysWrapUrlOn + { + get => _userSettings.Get(SettingsConstants.AlwaysWrapURL); + set { - get => UserSettings.Get(SettingsConstants.WordWrapEditor); - set + if (value == AlwaysWrapUrlOn) { - if (value == WordWrapEnabled) - { - return; - } - - UserSettings.Set(SettingsConstants.WordWrapEditor, value); - RaisePropertyChanged(nameof(WordWrapEnabled)); - - Analytics.TrackEvent("Settings changed: word wrap editor", new Dictionary - { - { "Value", value.ToString() } - }); + return; } + + _userSettings.Set(SettingsConstants.AlwaysWrapURL, value); + OnPropertyChanged(nameof(AlwaysWrapUrlOn)); + + Analytics.TrackEvent("Settings changed: always wrap", new Dictionary + { + { "Value", value.ToString() } + }); } + } - public bool SslValidationOn + public bool WordWrapEnabled + { + get => _userSettings.Get(SettingsConstants.WordWrapEditor); + set { - get => UserSettings.Get(SettingsConstants.SslValidationKey); - set + if (value == WordWrapEnabled) { - if (value == SslValidationOn) - return; - - UserSettings.Set(SettingsConstants.SslValidationKey, value); - Analytics.TrackEvent("Settings changed: SSL validation", new Dictionary - { - { "Value", value.ToString() } - }); - RaisePropertyChanged("SslValidation"); + return; } + + _userSettings.Set(SettingsConstants.WordWrapEditor, value); + OnPropertyChanged(nameof(WordWrapEnabled)); + + Analytics.TrackEvent("Settings changed: word wrap editor", new Dictionary + { + { "Value", value.ToString() } + }); } + } - public bool DeletingPasswords + public bool SslValidationOn + { + get => _userSettings.Get(SettingsConstants.SslValidationKey); + set { - get => _deletingPasswords; - set + if (value == SslValidationOn) + return; + + _userSettings.Set(SettingsConstants.SslValidationKey, value); + Analytics.TrackEvent("Settings changed: SSL validation", new Dictionary { - _deletingPasswords = value; - RaisePropertyChanged("DeletingPasswords"); - RaisePropertyChanged("IsDeletePasswordEnabled"); - } + { "Value", value.ToString() } + }); + OnPropertyChanged("SslValidation"); } + } - public bool IsDeletePasswordEnabled + public bool DeletingPasswords + { + get => _deletingPasswords; + set { - get => !DeletingPasswords; + _deletingPasswords = value; + OnPropertyChanged("DeletingPasswords"); + OnPropertyChanged("IsDeletePasswordEnabled"); } + } + + public bool IsDeletePasswordEnabled + { + get => !DeletingPasswords; + } - public int SelectedThemeIndex + public int SelectedThemeIndex + { + get => (int)UserSettings.GetTheme(); + set { - get => (int)UserSettings.GetTheme(); - set + ThemeController.ChangeTheme((SelectedTheme)value); + Analytics.TrackEvent("Theme changed", new Dictionary { - ThemeController.ChangeTheme((SelectedTheme)value); - Analytics.TrackEvent("Theme changed", new Dictionary - { - { "Theme", ((SelectedTheme)value).ToString() } - }); - } + { "Theme", ((SelectedTheme)value).ToString() } + }); } + } - public int PivotIndex + public int PivotIndex + { + get => _pivotIndex; + set { - get => _pivotIndex; - set - { - _pivotIndex = value; - RaisePropertyChanged("PivotIndex"); - } + _pivotIndex = value; + OnPropertyChanged("PivotIndex"); } + } - //public bool SyncToggle - //{ - // get => _syncToggle; - // set => SetToggle(value); - //} + //public bool SyncToggle + //{ + // get => _syncToggle; + // set => SetToggle(value); + //} - public bool Loading + public bool Loading + { + get => _loading; + set { - get => _loading; - set - { - _loading = value; - RaisePropertyChanged("Loading"); - SyncStatusUpdated?.Invoke(this, new EventArgs()); - } + _loading = value; + OnPropertyChanged("Loading"); + SyncStatusUpdated?.Invoke(this, new EventArgs()); } - public string AppVersion + } + public string AppVersion + { + get { - get - { - var version = SystemInformation.ApplicationVersion; - return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; - } + var version = SystemInformation.ApplicationVersion; + return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; } + } - public SettingsViewModel( - IRecentUrlCache recentUrlCache, - BackgroundSettingsViewModel backgroundSettingsViewModel) - { - BackgroundSettingsViewModel = backgroundSettingsViewModel; - _recentUrlCache = recentUrlCache ?? throw new ArgumentNullException(nameof(recentUrlCache)); - DeletingPasswords = false; - CanClearUrls = true; - } + public SettingsViewModel( + IRecentUrlCache recentUrlCache, + BackgroundSettingsViewModel backgroundSettingsViewModel) + { + BackgroundSettingsViewModel = backgroundSettingsViewModel; + _recentUrlCache = recentUrlCache ?? throw new ArgumentNullException(nameof(recentUrlCache)); + DeletingPasswords = false; + CanClearUrls = true; + } - public BackgroundSettingsViewModel BackgroundSettingsViewModel { get; set; } + public BackgroundSettingsViewModel BackgroundSettingsViewModel { get; set; } - public bool ClearingRecentUrls + public bool ClearingRecentUrls + { + get => _clearingRecentUrls; + set { - get => _clearingRecentUrls; - set + if (_clearingRecentUrls != value) { - if (_clearingRecentUrls != value) - { - _clearingRecentUrls = value; - RaisePropertyChanged("ClearingRecentUrls"); - } + _clearingRecentUrls = value; + OnPropertyChanged("ClearingRecentUrls"); } } + } - public bool ClearRecentUrlSuccessful + public bool ClearRecentUrlSuccessful + { + get => _clearRecentUrlSuccessful; + set { - get => _clearRecentUrlSuccessful; - set + if (_clearRecentUrlSuccessful != value) { - if (_clearRecentUrlSuccessful != value) - { - _clearRecentUrlSuccessful = value; - RaisePropertyChanged("ClearRecentUrlSuccessful"); - } + _clearRecentUrlSuccessful = value; + OnPropertyChanged("ClearRecentUrlSuccessful"); } } + } - public bool CanClearUrls + public bool CanClearUrls + { + get => _canClearUrls; + set { - get => _canClearUrls; - set + if (_canClearUrls != value) { - if (_canClearUrls != value) - { - _canClearUrls = value; - RaisePropertyChanged("CanClearUrls"); - } + _canClearUrls = value; + OnPropertyChanged("CanClearUrls"); } } + } - public async void ClearRecentUrlsAsync() + public bool InfiniteTimeoutEnabled + { + get => _userSettings.Get(SettingsConstants.InfiniteTimeoutKey); + set { - CanClearUrls = false; - ClearingRecentUrls = true; + _userSettings.Set(SettingsConstants.InfiniteTimeoutKey, value); + OnPropertyChanged(nameof(TimeoutTextEnabled)); + } + } - await _recentUrlCache.InitializeAsync(); - await _recentUrlCache.ClearAllUrlsAsync(); + public bool TimeoutTextEnabled => !InfiniteTimeoutEnabled; - ClearingRecentUrls = false; - ClearRecentUrlSuccessful = true; - } + [ObservableProperty] + private string _timeoutText; + + partial void OnTimeoutTextChanged(string value) + { + var currentValue = _userSettings.Get(SettingsConstants.TimeoutSecondsKey); - public async void RateAndReviewAsync() + if (double.TryParse(value.Trim(), out double result) && + result > 0 && + result != currentValue) { - await StoreHandler.ShowRatingReviewDialog(); + _userSettings.Set(SettingsConstants.TimeoutSecondsKey, result); } - - private async void ChangeTelemetry(bool value) + else { - await Analytics.SetEnabledAsync(value); + TimeoutText = currentValue.ToString(); } } + + public async void ClearRecentUrlsAsync() + { + CanClearUrls = false; + ClearingRecentUrls = true; + + await _recentUrlCache.InitializeAsync(); + await _recentUrlCache.ClearAllUrlsAsync(); + + ClearingRecentUrls = false; + ClearRecentUrlSuccessful = true; + } + + public async void RateAndReviewAsync() + { + await StoreHandler.ShowRatingReviewDialog(); + } + + private async void ChangeTelemetry(bool value) + { + await Analytics.SetEnabledAsync(value); + } }