Skip to content

Commit

Permalink
Moved static method to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulino committed Mar 9, 2024
1 parent c20ba55 commit 61be286
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 111 deletions.
11 changes: 6 additions & 5 deletions src/Nightingale/Handlers/StoreHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Toolkit.Uwp.Connectivity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Uwp.Connectivity;
using Nightingale.Core.Settings;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -41,12 +42,12 @@ public static async Task<IReadOnlyDictionary<string, StoreProduct>> GetLicensesA
/// <returns>True if user is subscribed or has purchased premium, false otherwise.</returns>
public static async Task<bool> IsUserSubscribed()
{
long userSubDateTicks = UserSettings.Get<long>(SettingsConstants.PremiumDateUnlocked);
long userSubDateTicks = App.Services.GetRequiredService<IUserSettings>().Get<long>(SettingsConstants.PremiumDateUnlocked);
DateTime userSubDate = new DateTime(userSubDateTicks);

if (userSubDate > DateTime.MinValue)
{
string premiumId = UserSettings.Get<string>(SettingsConstants.PremiumIapId);
string premiumId = App.Services.GetRequiredService<IUserSettings>().Get<string>(SettingsConstants.PremiumIapId);

if (premiumId == PremiumDurable)
{
Expand Down Expand Up @@ -129,8 +130,8 @@ public static async Task<bool> IsUserSubscribed()

private static void UpdateLocalSettings(string iapId, long ticks)
{
UserSettings.Set<long>(SettingsConstants.PremiumDateUnlocked, ticks);
UserSettings.Set<string>(SettingsConstants.PremiumIapId, iapId.ToUpper());
App.Services.GetRequiredService<IUserSettings>().Set<long>(SettingsConstants.PremiumDateUnlocked, ticks);
App.Services.GetRequiredService<IUserSettings>().Set<string>(SettingsConstants.PremiumIapId, iapId.ToUpper());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Nightingale/Handlers/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Nightingale.Handlers
/// </summary>
public class UserSettings : IUserSettings
{
public static void Set<T>(string settingKey, object value)
public void Set<T>(string settingKey, object value)
{
ApplicationData.Current.LocalSettings.Values[settingKey] = (T)value;
}

public static T Get<T>(string settingKey)
public T Get<T>(string settingKey)
{
object result = ApplicationData.Current.LocalSettings.Values[settingKey];
return result == null ? (T)SettingsConstants.Defaults[settingKey] : (T)result;
Expand Down
3 changes: 2 additions & 1 deletion src/Nightingale/UserControls/EditorControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Linq;
using Nightingale.Handlers;
using Nightingale.Core.Settings;
using Microsoft.Extensions.DependencyInjection;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

Expand Down Expand Up @@ -54,7 +55,7 @@ public EditorControl()
Editor.InputBindings.Remove(binding);
}

Editor.IsWordWrapEnabled = UserSettings.Get<bool>(SettingsConstants.WordWrapEditor);
Editor.IsWordWrapEnabled = App.Services.GetRequiredService<IUserSettings>().Get<bool>(SettingsConstants.WordWrapEditor);
}

private void ThemeController_ThemeChanged(object sender, EventArgs e)
Expand Down
7 changes: 4 additions & 3 deletions src/Nightingale/UserControls/UrlBarControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nightingale.Core.Settings;
using Microsoft.Extensions.DependencyInjection;
using Nightingale.Core.Settings;
using Nightingale.CustomEventArgs;
using Nightingale.Handlers;
using Nightingale.Utilities;
Expand All @@ -25,7 +26,7 @@ public UrlBarControl()
{
this.InitializeComponent();

if (UserSettings.Get<bool>(SettingsConstants.AlwaysWrapURL))
if (App.Services.GetRequiredService<IUserSettings>().Get<bool>(SettingsConstants.AlwaysWrapURL))
{
VisualStateManager.GoToState(this, "Focused", false);
}
Expand Down Expand Up @@ -72,7 +73,7 @@ private void UrlLostFocus()
UrlTextBoxLostFocus?.Invoke(this, new EventArgs());

// Only revert to normal if alwaysWrapUrl = true.
if (!UserSettings.Get<bool>(SettingsConstants.AlwaysWrapURL))
if (!App.Services.GetRequiredService<IUserSettings>().Get<bool>(SettingsConstants.AlwaysWrapURL))
{
VisualStateManager.GoToState(this, "Normal", false);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Nightingale/Utilities/ThemeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nightingale.Core.Settings;
using Microsoft.Extensions.DependencyInjection;
using Nightingale.Core.Settings;
using Nightingale.CustomEventArgs;
using Nightingale.Handlers;
using System;
Expand All @@ -25,7 +26,7 @@ public static void ChangeTheme(SelectedTheme selectedTheme)

public static void ChangeBackgroundImage(string imageName)
{
UserSettings.Set<string>(SettingsConstants.BackgroundImage, imageName);
App.Services.GetRequiredService<IUserSettings>().Set<string>(SettingsConstants.BackgroundImage, imageName);
BackgroundImageChanged?.Invoke(new ThemeController(), new AddedItemArgs<string>(imageName));
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/Nightingale/ViewModels/EnvironmentsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,33 @@ public class EnvironmentsViewModel : ViewModelBase
{
private readonly IEnvironmentListModifier _envListModifier;
private readonly IParameterStorageAccessor _parameterStorageAccessor;
private readonly IUserSettings _userSettings;
private string _newEnvName;
private bool _premiumMessageVisible;
private bool _addButtonEnabled = true;
private ObservableCollection<Core.Models.Environment> _envList;

public EnvironmentsViewModel(
IEnvironmentListModifier listModifier,
IParameterStorageAccessor parameterStorageAccessor)
IParameterStorageAccessor parameterStorageAccessor,
IUserSettings userSettings)
{
_envListModifier = listModifier ?? throw new ArgumentNullException(nameof(listModifier));
_parameterStorageAccessor = parameterStorageAccessor ?? throw new ArgumentNullException(nameof(parameterStorageAccessor));
_envListModifier = listModifier;
_parameterStorageAccessor = parameterStorageAccessor;
_userSettings = userSettings;
}

public bool EnvQuickEditOn
{
get => UserSettings.Get<bool>(SettingsConstants.EnableEnvQuickEdit);
get => _userSettings.Get<bool>(SettingsConstants.EnableEnvQuickEdit);
set
{
if (value == EnvQuickEditOn)
{
return;
}

UserSettings.Set<bool>(SettingsConstants.EnableEnvQuickEdit, value);
_userSettings.Set<bool>(SettingsConstants.EnableEnvQuickEdit, value);
Analytics.TrackEvent("Settings changed: EnableEnvQuickEdit", new Dictionary<string, string>
{
{ "Value", value ? "true" : "false" },
Expand Down
22 changes: 12 additions & 10 deletions src/Nightingale/ViewModels/ImportPostmanViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ImportPostmanViewModel : ObservableObject
private readonly INcfImporter _ncfImporter;
private readonly ICurlConverter _curlConverter;
private readonly IODataImporter _odataImporter;
private readonly IUserSettings _userSettings;
private string _message = "";

public ImportPostmanViewModel(
Expand All @@ -49,22 +50,23 @@ public ImportPostmanViewModel(
INcfImporter ncfImporter,
ISwaggerImporter swaggerImporter,
IODataImporter odataImporter,
ICurlConverter curlConverter)
ICurlConverter curlConverter,
IUserSettings userSettings)
{
_odataImporter = odataImporter ?? throw new ArgumentNullException(nameof(odataImporter));
_filePicker = filePicker ?? throw new ArgumentNullException(nameof(filePicker));
_postmanImporter = postmanImporter ?? throw new ArgumentNullException(nameof(postmanImporter));
_swaggerImporter = swaggerImporter ?? throw new ArgumentNullException(nameof(swaggerImporter));
_insomniaImporter = insomniaImporter ?? throw new ArgumentNullException(nameof(insomniaImporter));
_curlConverter = curlConverter ?? throw new ArgumentNullException(nameof(curlConverter));
_ncfImporter = ncfImporter ?? throw new ArgumentNullException(nameof(ncfImporter));
_odataImporter = odataImporter;
_filePicker = filePicker;
_postmanImporter = postmanImporter;
_swaggerImporter = swaggerImporter;
_insomniaImporter = insomniaImporter;
_curlConverter = curlConverter;
_ncfImporter = ncfImporter;
}

public int ImportTypeSelected
{
get
{
int lastTypeUsed = UserSettings.Get<int>(SettingsConstants.LastImportTypeUsed);
int lastTypeUsed = _userSettings.Get<int>(SettingsConstants.LastImportTypeUsed);

return Enum.IsDefined(typeof(ImportType), lastTypeUsed)
? lastTypeUsed
Expand All @@ -74,7 +76,7 @@ public int ImportTypeSelected
{
if (ImportTypeSelected != value && Enum.IsDefined(typeof(ImportType), value))
{
UserSettings.Set<int>(SettingsConstants.LastImportTypeUsed, value);
_userSettings.Set<int>(SettingsConstants.LastImportTypeUsed, value);
OnPropertyChanged(nameof(CurlBoxVisible));
OnPropertyChanged(nameof(DragDropVislble));
}
Expand Down
49 changes: 26 additions & 23 deletions src/Nightingale/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class MainPageViewModel : ViewModelBase
private readonly IDeployService _deployService;
private readonly IStorage _storage;
private readonly IExportService _exportService;
private readonly IUserSettings _userSettings;
private readonly string _workspaceRootId = "root";
private Workspace _selectedWorkspace;
private bool _saving;
Expand Down Expand Up @@ -81,26 +82,28 @@ public MainPageViewModel(
IDeployService deployService,
MvpViewModel mvpViewModel,
IStorage storage,
IExportService exportService)
{
MvpViewModel = mvpViewModel ?? throw new ArgumentNullException(nameof(mvpViewModel));
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
_workspaceStorageAccessor = workspaceStorageAccessor ?? throw new ArgumentNullException(nameof(workspaceStorageAccessor));
_workspaceListModifier = workspaceListModifier ?? throw new ArgumentNullException(nameof(workspaceListModifier));
_workspaceContainer = workspaceContainer ?? throw new ArgumentNullException(nameof(workspaceContainer));
_environmentContainer = environmentContainer ?? throw new ArgumentNullException(nameof(environmentContainer));
_cookieJar = cookieJar ?? throw new ArgumentNullException(nameof(cookieJar));
_workspaceNavigationService = workspaceNavigationService ?? throw new ArgumentNullException(nameof(workspaceNavigationService));
_cookieDialogService = cookieDialogService ?? throw new ArgumentNullException(nameof(cookieDialogService));
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
_messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus));
_methodsContainer = methodsContainer ?? throw new ArgumentNullException(nameof(methodsContainer));
_workspaceTreeModifier = workspaceTreeModifier ?? throw new ArgumentNullException(nameof(workspaceTreeModifier));
_visualStatePublisher = visualStatePublisher ?? throw new ArgumentNullException(nameof(visualStatePublisher));
_environmentDialogService = environmentDialogService ?? throw new ArgumentNullException(nameof(environmentDialogService));
_tabContainer = tabContainer ?? throw new ArgumentNullException(nameof(tabContainer));
_deployService = deployService ?? throw new ArgumentNullException(nameof(deployService));
_exportService = exportService ?? throw new ArgumentNullException(nameof(exportService));
IExportService exportService,
IUserSettings userSettings)
{
MvpViewModel = mvpViewModel;
_storage = storage;
_workspaceStorageAccessor = workspaceStorageAccessor;
_workspaceListModifier = workspaceListModifier;
_workspaceContainer = workspaceContainer ;
_environmentContainer = environmentContainer;
_cookieJar = cookieJar;
_workspaceNavigationService = workspaceNavigationService;
_cookieDialogService = cookieDialogService;
_dialogService = dialogService;
_messageBus = messageBus;
_methodsContainer = methodsContainer;
_workspaceTreeModifier = workspaceTreeModifier;
_visualStatePublisher = visualStatePublisher;
_environmentDialogService = environmentDialogService;
_tabContainer = tabContainer;
_deployService = deployService;
_exportService = exportService;
_userSettings = userSettings;

this._workspaceItemNavigationService = workspaceItemNavigationService ?? throw new ArgumentNullException(nameof(workspaceItemNavigationService));
UpdateRateButtonVisibility();
Expand All @@ -116,7 +119,7 @@ public MainPageViewModel(

_visualStatePublisher.PaneLayoutToggled += PaneLayoutChanged;

if (UserSettings.Get<bool>(SettingsConstants.AutoSaveInterval))
if (_userSettings.Get<bool>(SettingsConstants.AutoSaveInterval))
{
_saveTimer = new Timer(TimerCallback, null, 300000 /* 5 minutes */, 300000);
}
Expand Down Expand Up @@ -174,7 +177,7 @@ private async void MainPageViewModel_CloseRequested(object sender, Windows.UI.Co
{
var deferral = e.GetDeferral();

if (UserSettings.Get<bool>(SettingsConstants.AutoSaveEnabled))
if (_userSettings.Get<bool>(SettingsConstants.AutoSaveEnabled))
{
Saving = true;
await _workspaceStorageAccessor.SaveWorkspacesAsync(Workspaces, _workspaceRootId, SelectedWorkspace?.Id);
Expand Down Expand Up @@ -639,7 +642,7 @@ private async void UpdateRateButtonVisibility()
{
bool displayRateButton = await Task.Run(() =>
{
bool IsAlreadyRated = UserSettings.Get<bool>(SettingsConstants.IsAppRated);
bool IsAlreadyRated = _userSettings.Get<bool>(SettingsConstants.IsAppRated);
return IsAlreadyRated ? false : new Random().Next(2) == 0;
});

Expand Down
23 changes: 13 additions & 10 deletions src/Nightingale/ViewModels/RequestControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class RequestControlViewModel : ViewModelBase
private readonly IVisualStatePublisher _visualStatePublisher;
private readonly IMessageBus _messageBus;
private readonly ICurlConverter _curlConverter;
private readonly IUserSettings _userSettings;
private readonly ResourceLoader _resourceLoader;

private Item _request;
Expand All @@ -53,17 +54,19 @@ public RequestControlViewModel(
IVariableResolver variableResolver,
IVisualStatePublisher visualStatePublisher,
IMessageBus messageBus,
ICurlConverter curlConverter)
ICurlConverter curlConverter,
IUserSettings userSettings)
{
_cts = new CancellationTokenSource();
_requestSender = sender ?? throw new ArgumentNullException(nameof(sender));
_curlConverter = curlConverter ?? throw new ArgumentNullException(nameof(curlConverter));
_fileWriter = fileWriter ?? throw new ArgumentNullException(nameof(fileWriter));
_responseValueExtractor = responseValueExtractor ?? throw new ArgumentNullException(nameof(responseValueExtractor));
EnvironmentContainer = environmentContainer ?? throw new ArgumentNullException(nameof(environmentContainer));
_variableResolver = variableResolver ?? throw new ArgumentNullException(nameof(variableResolver));
_visualStatePublisher = visualStatePublisher ?? throw new ArgumentNullException(nameof(visualStatePublisher));
_messageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus));
_requestSender = sender;
_curlConverter = curlConverter;
_fileWriter = fileWriter;
_responseValueExtractor = responseValueExtractor;
EnvironmentContainer = environmentContainer;
_variableResolver = variableResolver;
_visualStatePublisher = visualStatePublisher;
_messageBus = messageBus;
_userSettings = userSettings;
_resourceLoader = ResourceLoader.GetForCurrentView();

_visualStatePublisher.PaneLayoutToggled += VisualStatePublisher_PaneLayoutToggled;
Expand Down Expand Up @@ -465,7 +468,7 @@ private async Task SendRequest(bool downloadResponse)
WorkspaceResponse response = await _requestSender.SendRequestAsync(
Request,
_cts.Token,
UserSettings.Get<bool>(SettingsConstants.HistoryEnabled));
_userSettings.Get<bool>(SettingsConstants.HistoryEnabled));

if (Request is HistoryItem)
{
Expand Down
Loading

0 comments on commit 61be286

Please sign in to comment.