Skip to content

Commit f584588

Browse files
authored
Merge pull request #4699 from bclothier/Fix4694
Clean up the persistence services
2 parents e7079f1 + e3d276f commit f584588

28 files changed

+425
-416
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Rubberduck.Settings
6+
{
7+
public interface IExperimentalTypesProvider
8+
{
9+
IReadOnlyList<Type> ExperimentalTypes { get; }
10+
}
11+
12+
public class ExperimentalTypesProvider : IExperimentalTypesProvider
13+
{
14+
public IReadOnlyList<Type> ExperimentalTypes { get; }
15+
16+
public ExperimentalTypesProvider(IEnumerable<Type> experimentalTypes)
17+
{
18+
ExperimentalTypes = experimentalTypes.ToList().AsReadOnly();
19+
}
20+
}
21+
}

Rubberduck.Core/UI/Settings/AddRemoveReferencesUserSettingsViewModel.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111

1212
namespace Rubberduck.UI.Settings
1313
{
14-
public class AddRemoveReferencesUserSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
14+
public class AddRemoveReferencesUserSettingsViewModel : SettingsViewModelBase<ReferenceSettings>, ISettingsViewModel<ReferenceSettings>
1515
{
1616
private readonly IConfigProvider<ReferenceSettings> _provider;
1717
private readonly IFileSystemBrowserFactory _browserFactory;
1818
private readonly ReferenceSettings _clean;
1919

20-
21-
public AddRemoveReferencesUserSettingsViewModel(IConfigProvider<ReferenceSettings> provider, IFileSystemBrowserFactory browserFactory)
20+
public AddRemoveReferencesUserSettingsViewModel(
21+
IConfigProvider<ReferenceSettings> provider,
22+
IFileSystemBrowserFactory browserFactory,
23+
IFilePersistanceService<ReferenceSettings> service)
24+
: base(service)
2225
{
2326
_provider = provider;
2427
_browserFactory = browserFactory;
@@ -33,6 +36,7 @@ public AddRemoveReferencesUserSettingsViewModel(IConfigProvider<ReferenceSetting
3336
}
3437

3538
private int _recent;
39+
3640
public int RecentReferencesTracked
3741
{
3842
get => _recent;
@@ -84,14 +88,18 @@ private void ExecuteRemoveSelectedPaths(object parameter)
8488
ProjectPaths.Remove(path);
8589
}
8690

87-
private void TransferSettingsToView(ReferenceSettings loading)
91+
protected override void TransferSettingsToView(ReferenceSettings loading)
8892
{
8993
RecentReferencesTracked = loading.RecentReferencesTracked;
9094
FixBrokenReferences = loading.FixBrokenReferences;
9195
AddToRecentOnReferenceEvents = loading.AddToRecentOnReferenceEvents;
9296
ProjectPaths = new ObservableCollection<string>(loading.ProjectPaths);
9397
}
9498

99+
protected override string DialogLoadTitle { get; }
100+
101+
protected override string DialogSaveTitle { get; }
102+
95103
private void TransferViewToSettings(ReferenceSettings target)
96104
{
97105
target.RecentReferencesTracked = RecentReferencesTracked;

Rubberduck.Core/UI/Settings/AutoCompleteSettingsViewModel.cs

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
using System.Windows.Input;
22
using NLog;
3-
using Rubberduck.Resources;
43
using Rubberduck.Resources.Settings;
54
using Rubberduck.Settings;
65
using Rubberduck.SettingsProvider;
76
using Rubberduck.UI.Command;
87

98
namespace Rubberduck.UI.Settings
109
{
11-
public class AutoCompleteSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
10+
public sealed class AutoCompleteSettingsViewModel : SettingsViewModelBase<Rubberduck.Settings.AutoCompleteSettings>, ISettingsViewModel<Rubberduck.Settings.AutoCompleteSettings>
1211
{
13-
public AutoCompleteSettingsViewModel(Configuration config)
12+
public AutoCompleteSettingsViewModel(Configuration config, IFilePersistanceService<Rubberduck.Settings.AutoCompleteSettings> service)
13+
: base(service)
1414
{
1515
TransferSettingsToView(config.UserSettings.AutoCompleteSettings);
16-
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings());
16+
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ =>
17+
ExportSettings(new Rubberduck.Settings.AutoCompleteSettings
18+
{
19+
IsEnabled = IsEnabled,
20+
BlockCompletion = new Rubberduck.Settings.AutoCompleteSettings.BlockCompletionSettings
21+
{
22+
CompleteOnEnter = CompleteBlockOnEnter,
23+
CompleteOnTab = CompleteBlockOnTab,
24+
IsEnabled = EnableBlockCompletion
25+
},
26+
SelfClosingPairs = new Rubberduck.Settings.AutoCompleteSettings.SelfClosingPairSettings
27+
{
28+
IsEnabled = EnableSelfClosingPairs
29+
},
30+
SmartConcat = new Rubberduck.Settings.AutoCompleteSettings.SmartConcatSettings
31+
{
32+
ConcatVbNewLineModifier =
33+
ConcatVbNewLine ? ModifierKeySetting.CtrlKey : ModifierKeySetting.None,
34+
IsEnabled = EnableSmartConcat
35+
}
36+
}));
1737
ImportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ImportSettings());
1838

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

53-
private void TransferSettingsToView(Rubberduck.Settings.AutoCompleteSettings toLoad)
73+
protected override void TransferSettingsToView(Rubberduck.Settings.AutoCompleteSettings toLoad)
5474
{
5575
IsEnabled = toLoad.IsEnabled;
5676

@@ -176,6 +196,7 @@ public bool CompleteBlockOnEnter
176196
}
177197

178198
private bool _completeBlockOnTab;
199+
179200
public bool CompleteBlockOnTab
180201
{
181202
get { return _completeBlockOnTab; }
@@ -194,54 +215,7 @@ public bool CompleteBlockOnTab
194215
}
195216
}
196217

197-
private void ImportSettings()
198-
{
199-
using (var dialog = new OpenFileDialog
200-
{
201-
Filter = SettingsUI.DialogMask_XmlFilesOnly,
202-
Title = SettingsUI.DialogCaption_LoadInspectionSettings
203-
})
204-
{
205-
dialog.ShowDialog();
206-
if (string.IsNullOrEmpty(dialog.FileName)) return;
207-
var service = new XmlPersistanceService<Rubberduck.Settings.AutoCompleteSettings> { FilePath = dialog.FileName };
208-
var loaded = service.Load(new Rubberduck.Settings.AutoCompleteSettings());
209-
TransferSettingsToView(loaded);
210-
}
211-
}
212-
213-
private void ExportSettings()
214-
{
215-
using (var dialog = new SaveFileDialog
216-
{
217-
Filter = SettingsUI.DialogMask_XmlFilesOnly,
218-
Title = SettingsUI.DialogCaption_SaveAutocompletionSettings
219-
})
220-
{
221-
dialog.ShowDialog();
222-
if (string.IsNullOrEmpty(dialog.FileName)) return;
223-
var service = new XmlPersistanceService<Rubberduck.Settings.AutoCompleteSettings> { FilePath = dialog.FileName };
224-
service.Save(new Rubberduck.Settings.AutoCompleteSettings
225-
{
226-
IsEnabled = IsEnabled,
227-
BlockCompletion = new Rubberduck.Settings.AutoCompleteSettings.BlockCompletionSettings
228-
{
229-
CompleteOnEnter = CompleteBlockOnEnter,
230-
CompleteOnTab = CompleteBlockOnTab,
231-
IsEnabled = EnableBlockCompletion
232-
},
233-
SelfClosingPairs = new Rubberduck.Settings.AutoCompleteSettings.SelfClosingPairSettings
234-
{
235-
IsEnabled = EnableSelfClosingPairs
236-
},
237-
SmartConcat = new Rubberduck.Settings.AutoCompleteSettings.SmartConcatSettings
238-
{
239-
ConcatVbNewLineModifier =
240-
ConcatVbNewLine ? ModifierKeySetting.CtrlKey : ModifierKeySetting.None,
241-
IsEnabled = EnableSmartConcat
242-
}
243-
});
244-
}
245-
}
218+
protected override string DialogLoadTitle => SettingsUI.DialogCaption_LoadInspectionSettings;
219+
protected override string DialogSaveTitle => SettingsUI.DialogCaption_SaveAutocompletionSettings;
246220
}
247221
}

Rubberduck.Core/UI/Settings/GeneralSettingsViewModel.cs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,48 @@ public enum DelimiterOptions
2020
Slash = 47
2121
}
2222

23-
public class GeneralSettingsViewModel : SettingsViewModelBase, ISettingsViewModel
23+
public sealed class GeneralSettingsViewModel : SettingsViewModelBase<Rubberduck.Settings.GeneralSettings>, ISettingsViewModel<Rubberduck.Settings.GeneralSettings>
2424
{
2525
private readonly IOperatingSystem _operatingSystem;
2626
private readonly IMessageBox _messageBox;
2727
private readonly IVbeSettings _vbeSettings;
28+
private readonly IFilePersistanceService<HotkeySettings> _hotkeyService;
2829

2930
private bool _indenterPrompted;
30-
private readonly ReadOnlyCollection<Type> _experimentalFeatureTypes;
31-
32-
public GeneralSettingsViewModel(Configuration config, IOperatingSystem operatingSystem, IMessageBox messageBox, IVbeSettings vbeSettings, IEnumerable<Type> experimentalFeatureTypes)
31+
private readonly IReadOnlyList<Type> _experimentalFeatureTypes;
32+
33+
public GeneralSettingsViewModel(
34+
Configuration config,
35+
IOperatingSystem operatingSystem,
36+
IMessageBox messageBox,
37+
IVbeSettings vbeSettings,
38+
IExperimentalTypesProvider experimentalTypesProvider,
39+
IFilePersistanceService<Rubberduck.Settings.GeneralSettings> service,
40+
IFilePersistanceService<HotkeySettings> hotkeyService)
41+
: base(service)
3342
{
3443
_operatingSystem = operatingSystem;
3544
_messageBox = messageBox;
3645
_vbeSettings = vbeSettings;
37-
_experimentalFeatureTypes = experimentalFeatureTypes.ToList().AsReadOnly();
46+
_experimentalFeatureTypes = experimentalTypesProvider.ExperimentalTypes;
3847
Languages = new ObservableCollection<DisplayLanguageSetting>(
39-
new[]
40-
{
41-
new DisplayLanguageSetting("en-US"),
42-
new DisplayLanguageSetting("fr-CA"),
43-
new DisplayLanguageSetting("de-DE"),
44-
new DisplayLanguageSetting("cs-CZ")
45-
});
46-
47-
LogLevels = new ObservableCollection<MinimumLogLevel>(LogLevelHelper.LogLevels.Select(l => new MinimumLogLevel(l.Ordinal, l.Name)));
48+
new[]
49+
{
50+
new DisplayLanguageSetting("en-US"),
51+
new DisplayLanguageSetting("fr-CA"),
52+
new DisplayLanguageSetting("de-DE"),
53+
new DisplayLanguageSetting("cs-CZ")
54+
});
55+
56+
LogLevels = new ObservableCollection<MinimumLogLevel>(
57+
LogLevelHelper.LogLevels.Select(l => new MinimumLogLevel(l.Ordinal, l.Name)));
4858
TransferSettingsToView(config.UserSettings.GeneralSettings, config.UserSettings.HotkeySettings);
4959

5060
ShowLogFolderCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ShowLogFolder());
51-
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings());
61+
ExportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ExportSettings(GetCurrentGeneralSettings()));
5262
ImportButtonCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), _ => ImportSettings());
63+
64+
_hotkeyService = hotkeyService;
5365
}
5466

5567
public List<ExperimentalFeatures> ExperimentalFeatures { get; set; }
@@ -147,7 +159,7 @@ public bool CompileBeforeParse
147159

148160
if (value && _vbeSettings.CompileOnDemand)
149161
{
150-
if(!SynchronizeVBESettings())
162+
if(!SynchronizeVbeSettings())
151163
{
152164
return;
153165
}
@@ -181,7 +193,7 @@ public bool SetDpiUnawareEnabled
181193
}
182194
}
183195

184-
private bool SynchronizeVBESettings()
196+
private bool SynchronizeVbeSettings()
185197
{
186198
if (!_messageBox.ConfirmYesNo(RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled,
187199
RubberduckUI.GeneralSettings_CompileBeforeParse_WarnCompileOnDemandEnabled_Caption, true))
@@ -276,10 +288,17 @@ private Rubberduck.Settings.GeneralSettings GetCurrentGeneralSettings()
276288
};
277289
}
278290

291+
protected override void TransferSettingsToView(Rubberduck.Settings.GeneralSettings toLoad)
292+
{
293+
TransferSettingsToView(toLoad, null);
294+
}
295+
279296
private void TransferSettingsToView(IGeneralSettings general, IHotkeySettings hottkey)
280297
{
281298
SelectedLanguage = Languages.First(l => l.Code == general.Language.Code);
282-
Hotkeys = new ObservableCollection<HotkeySetting>(hottkey.Settings);
299+
Hotkeys = hottkey == null
300+
? new ObservableCollection<HotkeySetting>()
301+
: new ObservableCollection<HotkeySetting>(hottkey.Settings);
283302
ShowSplashAtStartup = general.CanShowSplash;
284303
CheckVersionAtStartup = general.CanCheckVersion;
285304
CompileBeforeParse = general.CompileBeforeParse;
@@ -297,7 +316,10 @@ private void TransferSettingsToView(IGeneralSettings general, IHotkeySettings ho
297316
.ToList();
298317
}
299318

300-
private void ImportSettings()
319+
protected override string DialogLoadTitle => SettingsUI.DialogCaption_LoadGeneralSettings;
320+
protected override string DialogSaveTitle => SettingsUI.DialogCaption_SaveGeneralSettings;
321+
322+
protected override void ImportSettings()
301323
{
302324
using (var dialog = new OpenFileDialog
303325
{
@@ -307,17 +329,15 @@ private void ImportSettings()
307329
{
308330
dialog.ShowDialog();
309331
if (string.IsNullOrEmpty(dialog.FileName)) return;
310-
var service = new XmlPersistanceService<Rubberduck.Settings.GeneralSettings> { FilePath = dialog.FileName };
311-
var general = service.Load(new Rubberduck.Settings.GeneralSettings());
312-
var hkService = new XmlPersistanceService<HotkeySettings> { FilePath = dialog.FileName };
313-
var hotkey = hkService.Load(new HotkeySettings());
332+
var general = Service.Load(new Rubberduck.Settings.GeneralSettings(), dialog.FileName);
333+
var hotkey = _hotkeyService.Load(new HotkeySettings(), dialog.FileName);
314334
//Always assume Smart Indenter registry import has been prompted if importing.
315335
general.IsSmartIndenterPrompted = true;
316336
TransferSettingsToView(general, hotkey);
317337
}
318338
}
319339

320-
private void ExportSettings()
340+
protected override void ExportSettings(Rubberduck.Settings.GeneralSettings settings)
321341
{
322342
using (var dialog = new SaveFileDialog
323343
{
@@ -327,10 +347,8 @@ private void ExportSettings()
327347
{
328348
dialog.ShowDialog();
329349
if (string.IsNullOrEmpty(dialog.FileName)) return;
330-
var service = new XmlPersistanceService<Rubberduck.Settings.GeneralSettings> { FilePath = dialog.FileName };
331-
service.Save(GetCurrentGeneralSettings());
332-
var hkService = new XmlPersistanceService<HotkeySettings> { FilePath = dialog.FileName };
333-
hkService.Save(new HotkeySettings { Settings = Hotkeys.ToArray() });
350+
Service.Save(settings, dialog.FileName);
351+
_hotkeyService.Save(new HotkeySettings { Settings = Hotkeys.ToArray() }, dialog.FileName);
334352
}
335353
}
336354
}

Rubberduck.Core/UI/Settings/ISettingsViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Rubberduck.UI.Settings
44
{
5+
public interface ISettingsViewModel<out TSettings> : ISettingsViewModel
6+
where TSettings : class, new()
7+
{ }
8+
59
public interface ISettingsViewModel
610
{
711
void UpdateConfig(Configuration config);

0 commit comments

Comments
 (0)