Skip to content

Commit

Permalink
Merge pull request #4699 from bclothier/Fix4694
Browse files Browse the repository at this point in the history
Clean up the persistence services
  • Loading branch information
retailcoder committed Feb 19, 2019
2 parents e7079f1 + e3d276f commit f584588
Show file tree
Hide file tree
Showing 28 changed files with 425 additions and 416 deletions.
21 changes: 21 additions & 0 deletions Rubberduck.Core/Settings/ExperimentalTypesProvider.cs
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Rubberduck.Settings
{
public interface IExperimentalTypesProvider
{
IReadOnlyList<Type> ExperimentalTypes { get; }
}

public class ExperimentalTypesProvider : IExperimentalTypesProvider
{
public IReadOnlyList<Type> ExperimentalTypes { get; }

public ExperimentalTypesProvider(IEnumerable<Type> experimentalTypes)
{
ExperimentalTypes = experimentalTypes.ToList().AsReadOnly();
}
}
}
Expand Up @@ -11,14 +11,17 @@

namespace Rubberduck.UI.Settings
{
public class AddRemoveReferencesUserSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
public class AddRemoveReferencesUserSettingsViewModel : SettingsViewModelBase<ReferenceSettings>, ISettingsViewModel<ReferenceSettings>
{
private readonly IConfigProvider<ReferenceSettings> _provider;
private readonly IFileSystemBrowserFactory _browserFactory;
private readonly ReferenceSettings _clean;


public AddRemoveReferencesUserSettingsViewModel(IConfigProvider<ReferenceSettings> provider, IFileSystemBrowserFactory browserFactory)
public AddRemoveReferencesUserSettingsViewModel(
IConfigProvider<ReferenceSettings> provider,
IFileSystemBrowserFactory browserFactory,
IFilePersistanceService<ReferenceSettings> service)
: base(service)
{
_provider = provider;
_browserFactory = browserFactory;
Expand All @@ -33,6 +36,7 @@ public AddRemoveReferencesUserSettingsViewModel(IConfigProvider<ReferenceSetting
}

private int _recent;

public int RecentReferencesTracked
{
get => _recent;
Expand Down Expand Up @@ -84,14 +88,18 @@ private void ExecuteRemoveSelectedPaths(object parameter)
ProjectPaths.Remove(path);
}

private void TransferSettingsToView(ReferenceSettings loading)
protected override void TransferSettingsToView(ReferenceSettings loading)
{
RecentReferencesTracked = loading.RecentReferencesTracked;
FixBrokenReferences = loading.FixBrokenReferences;
AddToRecentOnReferenceEvents = loading.AddToRecentOnReferenceEvents;
ProjectPaths = new ObservableCollection<string>(loading.ProjectPaths);
}

protected override string DialogLoadTitle { get; }

protected override string DialogSaveTitle { get; }

private void TransferViewToSettings(ReferenceSettings target)
{
target.RecentReferencesTracked = RecentReferencesTracked;
Expand Down
82 changes: 28 additions & 54 deletions Rubberduck.Core/UI/Settings/AutoCompleteSettingsViewModel.cs
@@ -1,19 +1,39 @@
using System.Windows.Input;
using NLog;
using Rubberduck.Resources;
using Rubberduck.Resources.Settings;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.UI.Command;

namespace Rubberduck.UI.Settings
{
public class AutoCompleteSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
public sealed class AutoCompleteSettingsViewModel : SettingsViewModelBase<Rubberduck.Settings.AutoCompleteSettings>, ISettingsViewModel<Rubberduck.Settings.AutoCompleteSettings>
{
public AutoCompleteSettingsViewModel(Configuration config)
public AutoCompleteSettingsViewModel(Configuration config, IFilePersistanceService<Rubberduck.Settings.AutoCompleteSettings> service)
: base(service)
{
TransferSettingsToView(config.UserSettings.AutoCompleteSettings);
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings());
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ =>
ExportSettings(new Rubberduck.Settings.AutoCompleteSettings
{
IsEnabled = IsEnabled,
BlockCompletion = new Rubberduck.Settings.AutoCompleteSettings.BlockCompletionSettings
{
CompleteOnEnter = CompleteBlockOnEnter,
CompleteOnTab = CompleteBlockOnTab,
IsEnabled = EnableBlockCompletion
},
SelfClosingPairs = new Rubberduck.Settings.AutoCompleteSettings.SelfClosingPairSettings
{
IsEnabled = EnableSelfClosingPairs
},
SmartConcat = new Rubberduck.Settings.AutoCompleteSettings.SmartConcatSettings
{
ConcatVbNewLineModifier =
ConcatVbNewLine ? ModifierKeySetting.CtrlKey : ModifierKeySetting.None,
IsEnabled = EnableSmartConcat
}
}));
ImportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ImportSettings());

IncrementMaxConcatLinesCommand = new DelegateCommand(null, ExecuteIncrementMaxConcatLines, CanExecuteIncrementMaxConcatLines);
Expand Down Expand Up @@ -50,7 +70,7 @@ public void UpdateConfig(Configuration config)
config.UserSettings.AutoCompleteSettings.BlockCompletion.CompleteOnEnter = CompleteBlockOnEnter;
}

private void TransferSettingsToView(Rubberduck.Settings.AutoCompleteSettings toLoad)
protected override void TransferSettingsToView(Rubberduck.Settings.AutoCompleteSettings toLoad)
{
IsEnabled = toLoad.IsEnabled;

Expand Down Expand Up @@ -176,6 +196,7 @@ public bool CompleteBlockOnEnter
}

private bool _completeBlockOnTab;

public bool CompleteBlockOnTab
{
get { return _completeBlockOnTab; }
Expand All @@ -194,54 +215,7 @@ public bool CompleteBlockOnTab
}
}

private void ImportSettings()
{
using (var dialog = new OpenFileDialog
{
Filter = SettingsUI.DialogMask_XmlFilesOnly,
Title = SettingsUI.DialogCaption_LoadInspectionSettings
})
{
dialog.ShowDialog();
if (string.IsNullOrEmpty(dialog.FileName)) return;
var service = new XmlPersistanceService<Rubberduck.Settings.AutoCompleteSettings> { FilePath = dialog.FileName };
var loaded = service.Load(new Rubberduck.Settings.AutoCompleteSettings());
TransferSettingsToView(loaded);
}
}

private void ExportSettings()
{
using (var dialog = new SaveFileDialog
{
Filter = SettingsUI.DialogMask_XmlFilesOnly,
Title = SettingsUI.DialogCaption_SaveAutocompletionSettings
})
{
dialog.ShowDialog();
if (string.IsNullOrEmpty(dialog.FileName)) return;
var service = new XmlPersistanceService<Rubberduck.Settings.AutoCompleteSettings> { FilePath = dialog.FileName };
service.Save(new Rubberduck.Settings.AutoCompleteSettings
{
IsEnabled = IsEnabled,
BlockCompletion = new Rubberduck.Settings.AutoCompleteSettings.BlockCompletionSettings
{
CompleteOnEnter = CompleteBlockOnEnter,
CompleteOnTab = CompleteBlockOnTab,
IsEnabled = EnableBlockCompletion
},
SelfClosingPairs = new Rubberduck.Settings.AutoCompleteSettings.SelfClosingPairSettings
{
IsEnabled = EnableSelfClosingPairs
},
SmartConcat = new Rubberduck.Settings.AutoCompleteSettings.SmartConcatSettings
{
ConcatVbNewLineModifier =
ConcatVbNewLine ? ModifierKeySetting.CtrlKey : ModifierKeySetting.None,
IsEnabled = EnableSmartConcat
}
});
}
}
protected override string DialogLoadTitle => SettingsUI.DialogCaption_LoadInspectionSettings;
protected override string DialogSaveTitle => SettingsUI.DialogCaption_SaveAutocompletionSettings;
}
}
74 changes: 46 additions & 28 deletions Rubberduck.Core/UI/Settings/GeneralSettingsViewModel.cs
Expand Up @@ -20,36 +20,48 @@ public enum DelimiterOptions
Slash = 47
}

public class GeneralSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
public sealed class GeneralSettingsViewModel : SettingsViewModelBase<Rubberduck.Settings.GeneralSettings>, ISettingsViewModel<Rubberduck.Settings.GeneralSettings>
{
private readonly IOperatingSystem _operatingSystem;
private readonly IMessageBox _messageBox;
private readonly IVbeSettings _vbeSettings;
private readonly IFilePersistanceService<HotkeySettings> _hotkeyService;

private bool _indenterPrompted;
private readonly ReadOnlyCollection<Type> _experimentalFeatureTypes;

public GeneralSettingsViewModel(Configuration config, IOperatingSystem operatingSystem, IMessageBox messageBox, IVbeSettings vbeSettings, IEnumerable<Type> experimentalFeatureTypes)
private readonly IReadOnlyList<Type> _experimentalFeatureTypes;

public GeneralSettingsViewModel(
Configuration config,
IOperatingSystem operatingSystem,
IMessageBox messageBox,
IVbeSettings vbeSettings,
IExperimentalTypesProvider experimentalTypesProvider,
IFilePersistanceService<Rubberduck.Settings.GeneralSettings> service,
IFilePersistanceService<HotkeySettings> hotkeyService)
: base(service)
{
_operatingSystem = operatingSystem;
_messageBox = messageBox;
_vbeSettings = vbeSettings;
_experimentalFeatureTypes = experimentalFeatureTypes.ToList().AsReadOnly();
_experimentalFeatureTypes = experimentalTypesProvider.ExperimentalTypes;
Languages = new ObservableCollection<DisplayLanguageSetting>(
new[]
{
new DisplayLanguageSetting("en-US"),
new DisplayLanguageSetting("fr-CA"),
new DisplayLanguageSetting("de-DE"),
new DisplayLanguageSetting("cs-CZ")
});

LogLevels = new ObservableCollection<MinimumLogLevel>(LogLevelHelper.LogLevels.Select(l => new MinimumLogLevel(l.Ordinal, l.Name)));
new[]
{
new DisplayLanguageSetting("en-US"),
new DisplayLanguageSetting("fr-CA"),
new DisplayLanguageSetting("de-DE"),
new DisplayLanguageSetting("cs-CZ")
});

LogLevels = new ObservableCollection<MinimumLogLevel>(
LogLevelHelper.LogLevels.Select(l => new MinimumLogLevel(l.Ordinal, l.Name)));
TransferSettingsToView(config.UserSettings.GeneralSettings, config.UserSettings.HotkeySettings);

ShowLogFolderCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ShowLogFolder());
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings());
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings(GetCurrentGeneralSettings()));
ImportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ImportSettings());

_hotkeyService = hotkeyService;
}

public List<ExperimentalFeatures> ExperimentalFeatures { get; set; }
Expand Down Expand Up @@ -147,7 +159,7 @@ public bool CompileBeforeParse

if (value && _vbeSettings.CompileOnDemand)
{
if(!SynchronizeVBESettings())
if(!SynchronizeVbeSettings())
{
return;
}
Expand Down Expand Up @@ -181,7 +193,7 @@ public bool SetDpiUnawareEnabled
}
}

private bool SynchronizeVBESettings()
private bool SynchronizeVbeSettings()
{
if (!_messageBox.ConfirmYesNo(RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled,
RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled_Caption, true))
Expand Down Expand Up @@ -276,10 +288,17 @@ private Rubberduck.Settings.GeneralSettings GetCurrentGeneralSettings()
};
}

protected override void TransferSettingsToView(Rubberduck.Settings.GeneralSettings toLoad)
{
TransferSettingsToView(toLoad, null);
}

private void TransferSettingsToView(IGeneralSettings general, IHotkeySettings hottkey)
{
SelectedLanguage = Languages.First(l => l.Code == general.Language.Code);
Hotkeys = new ObservableCollection<HotkeySetting>(hottkey.Settings);
Hotkeys = hottkey == null
? new ObservableCollection<HotkeySetting>()
: new ObservableCollection<HotkeySetting>(hottkey.Settings);
ShowSplashAtStartup = general.CanShowSplash;
CheckVersionAtStartup = general.CanCheckVersion;
CompileBeforeParse = general.CompileBeforeParse;
Expand All @@ -297,7 +316,10 @@ private void TransferSettingsToView(IGeneralSettings general, IHotkeySettings ho
.ToList();
}

private void ImportSettings()
protected override string DialogLoadTitle => SettingsUI.DialogCaption_LoadGeneralSettings;
protected override string DialogSaveTitle => SettingsUI.DialogCaption_SaveGeneralSettings;

protected override void ImportSettings()
{
using (var dialog = new OpenFileDialog
{
Expand All @@ -307,17 +329,15 @@ private void ImportSettings()
{
dialog.ShowDialog();
if (string.IsNullOrEmpty(dialog.FileName)) return;
var service = new XmlPersistanceService<Rubberduck.Settings.GeneralSettings> { FilePath = dialog.FileName };
var general = service.Load(new Rubberduck.Settings.GeneralSettings());
var hkService = new XmlPersistanceService<HotkeySettings> { FilePath = dialog.FileName };
var hotkey = hkService.Load(new HotkeySettings());
var general = Service.Load(new Rubberduck.Settings.GeneralSettings(), dialog.FileName);
var hotkey = _hotkeyService.Load(new HotkeySettings(), dialog.FileName);
//Always assume Smart Indenter registry import has been prompted if importing.
general.IsSmartIndenterPrompted = true;
TransferSettingsToView(general, hotkey);
}
}

private void ExportSettings()
protected override void ExportSettings(Rubberduck.Settings.GeneralSettings settings)
{
using (var dialog = new SaveFileDialog
{
Expand All @@ -327,10 +347,8 @@ private void ExportSettings()
{
dialog.ShowDialog();
if (string.IsNullOrEmpty(dialog.FileName)) return;
var service = new XmlPersistanceService<Rubberduck.Settings.GeneralSettings> { FilePath = dialog.FileName };
service.Save(GetCurrentGeneralSettings());
var hkService = new XmlPersistanceService<HotkeySettings> { FilePath = dialog.FileName };
hkService.Save(new HotkeySettings { Settings = Hotkeys.ToArray() });
Service.Save(settings, dialog.FileName);
_hotkeyService.Save(new HotkeySettings { Settings = Hotkeys.ToArray() }, dialog.FileName);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Rubberduck.Core/UI/Settings/ISettingsViewModel.cs
Expand Up @@ -2,6 +2,10 @@

namespace Rubberduck.UI.Settings
{
public interface ISettingsViewModel<out TSettings> : ISettingsViewModel
where TSettings : class, new()
{ }

public interface ISettingsViewModel
{
void UpdateConfig(Configuration config);
Expand Down

0 comments on commit f584588

Please sign in to comment.