Skip to content

Commit aa72312

Browse files
committed
Formatting AppSettings
1 parent 3035308 commit aa72312

1 file changed

Lines changed: 44 additions & 77 deletions

File tree

Helpers/AppSettings.cs

Lines changed: 44 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.Collections.Specialized;
45
using System.Configuration;
56
using System.Linq;
67

7-
namespace Caffeinated.Helpers
8-
{
9-
public class AppSettings
10-
{
8+
namespace Caffeinated.Helpers {
9+
public class AppSettings {
1110
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
1211
KeyValueConfigurationCollection appSettings;
1312

1413
private bool _activateOnLaunch;
1514

16-
public bool ActivateOnLaunch
17-
{
15+
public bool ActivateOnLaunch {
1816
get { return _activateOnLaunch; }
19-
set
20-
{
17+
set {
2118
_activateOnLaunch = value;
2219
appSettings[nameof(ActivateOnLaunch)].Value = _activateOnLaunch.ToString();
2320
configFile.Save(ConfigurationSaveMode.Modified);
@@ -26,132 +23,105 @@ public bool ActivateOnLaunch
2623

2724
private bool _automaticallyLaunchWithWindows;
2825

29-
public bool AutomaticallyLaunchWithWindows
30-
{
26+
public bool AutomaticallyLaunchWithWindows {
3127
get { return _automaticallyLaunchWithWindows; }
32-
set
33-
{
28+
set {
3429
_automaticallyLaunchWithWindows = value;
3530
appSettings[nameof(AutomaticallyLaunchWithWindows)].Value = _automaticallyLaunchWithWindows.ToString();
3631
configFile.Save(ConfigurationSaveMode.Modified);
3732
}
3833
}
3934

40-
4135
private bool _showMessageOnLaunch;
4236

43-
public bool ShowMessageOnLaunch
44-
{
37+
public bool ShowMessageOnLaunch {
4538
get { return _showMessageOnLaunch; }
46-
set
47-
{
39+
set {
4840
_showMessageOnLaunch = value;
4941
appSettings[nameof(ShowMessageOnLaunch)].Value = _showMessageOnLaunch.ToString();
5042
configFile.Save(ConfigurationSaveMode.Modified);
5143
}
5244
}
5345

54-
5546
private int _defaultDuration;
5647

57-
public int DefaultDuration
58-
{
48+
public int DefaultDuration {
5949
get { return _defaultDuration; }
60-
set
61-
{
50+
set {
6251
_defaultDuration = value;
6352
appSettings[nameof(DefaultDuration)].Value = _defaultDuration.ToString();
6453
configFile.Save(ConfigurationSaveMode.Modified);
6554
}
6655
}
6756

68-
6957
private TrayIcon _icon;
7058

71-
public TrayIcon Icon
72-
{
59+
public TrayIcon Icon {
7360
get { return _icon; }
74-
set
75-
{
61+
set {
7662
_icon = value;
7763
appSettings[nameof(Icon)].Value = _icon.ToString();
7864
configFile.Save(ConfigurationSaveMode.Modified);
7965
}
8066
}
8167

68+
private ObservableCollection<int> _durations;
8269

83-
private List<int> _durations;
84-
85-
public List<int> Durations
86-
{
70+
public ObservableCollection<int> Durations {
8771
get { return _durations; }
88-
set
89-
{
72+
set {
9073
_durations = value;
9174
appSettings[nameof(Durations)].Value = string.Join(',',Durations.ToArray());
9275
configFile.Save(ConfigurationSaveMode.Modified);
9376
}
9477
}
9578

96-
97-
public AppSettings()
98-
{
79+
public AppSettings() {
9980
appSettings = configFile.AppSettings.Settings;
10081

101-
try
102-
{
82+
try {
10383
string ActivateOnLaunchresult = appSettings[nameof(ActivateOnLaunch)].Value;
10484
_activateOnLaunch = bool.Parse(ActivateOnLaunchresult);
10585
}
106-
catch (Exception)
107-
{
86+
catch (Exception) {
10887
_activateOnLaunch = false;
10988
AddUpdateAppSettings(nameof(ActivateOnLaunch), _activateOnLaunch.ToString());
11089
Console.WriteLine($"Error reading _activateOnLaunch app settings");
11190
}
11291

113-
try
114-
{
92+
try {
11593
string AutomaticallyLaunchWithWindowsresult = appSettings[nameof(AutomaticallyLaunchWithWindows)].Value;
11694
_automaticallyLaunchWithWindows = bool.Parse(AutomaticallyLaunchWithWindowsresult);
11795
}
118-
catch (Exception)
119-
{
96+
catch (Exception) {
12097
_automaticallyLaunchWithWindows = false;
12198
AddUpdateAppSettings(nameof(AutomaticallyLaunchWithWindows), _automaticallyLaunchWithWindows.ToString());
12299
Console.WriteLine("Error reading _automaticallyLaunchWithWindows app settings");
123100
}
124101

125-
try
126-
{
102+
try {
127103
string ShowMessageOnLaunchresult = appSettings[nameof(ShowMessageOnLaunch)].Value;
128104
_showMessageOnLaunch = bool.Parse(ShowMessageOnLaunchresult);
129105
}
130-
catch (Exception)
131-
{
106+
catch (Exception) {
132107
_showMessageOnLaunch = true;
133108
AddUpdateAppSettings(nameof(ShowMessageOnLaunch), _showMessageOnLaunch.ToString());
134109
Console.WriteLine($"Error reading _showMessageOnLaunch app settings");
135110
}
136111

137-
try
138-
{
112+
try {
139113
string DefaultDurationresult = appSettings[nameof(DefaultDuration)].Value;
140114
_defaultDuration = int.Parse(DefaultDurationresult);
141115
}
142-
catch (Exception)
143-
{
116+
catch (Exception) {
144117
_defaultDuration = 0;
145118
AddUpdateAppSettings(nameof(DefaultDuration), _defaultDuration.ToString());
146119
Console.WriteLine("Error reading _defaultDuration app settings");
147120
}
148121

149-
try
150-
{
122+
try {
151123
string Iconresult = appSettings[nameof(Icon)].Value;
152-
153-
switch (Iconresult)
154-
{
124+
switch (Iconresult) {
155125
case "Mug":
156126
_icon = TrayIcon.Mug;
157127
break;
@@ -163,51 +133,48 @@ public AppSettings()
163133
break;
164134
}
165135
}
166-
catch (Exception)
167-
{
136+
catch (Exception) {
168137
_icon = TrayIcon.Default;
169138
AddUpdateAppSettings(nameof(Icon), _icon.ToString());
170139
Console.WriteLine($"Error reading Icon app settings");
171140
}
172141

173-
try
174-
{
142+
try {
175143
string Durationsresult = appSettings[nameof(Durations)].Value;
176144
List<string> splitResult = Durationsresult.Split(',').ToList();
177145

178-
_durations = new List<int>();
179-
foreach (string item in splitResult)
180-
{
146+
_durations = new ObservableCollection<int>();
147+
foreach (string item in splitResult) {
181148
_durations.Add(int.Parse(item));
182149
}
183150
}
184-
catch (Exception)
185-
{
186-
_durations = new List<int> { 0, 15, 60, 120, 480 };
151+
catch (Exception) {
152+
_durations = new ObservableCollection<int> { 0, 15, 60, 120, 480 };
187153
AddUpdateAppSettings(nameof(Durations), string.Join(',', _durations.ToArray()));
188154
Console.WriteLine($"Error reading Durations app settings");
189155
}
156+
Durations.CollectionChanged += Durations_CollectionChanged;
157+
158+
configFile.Save(ConfigurationSaveMode.Modified);
159+
}
190160

161+
private void Durations_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
162+
appSettings[nameof(Durations)].Value = string.Join(',', Durations.ToArray());
191163
configFile.Save(ConfigurationSaveMode.Modified);
192164
}
193165

194-
void AddUpdateAppSettings(string key, string value)
195-
{
196-
try
197-
{
198-
if (appSettings[key] == null)
199-
{
166+
void AddUpdateAppSettings(string key, string value) {
167+
try {
168+
if (appSettings[key] == null) {
200169
appSettings.Add(key, value);
201170
}
202-
else
203-
{
171+
else {
204172
appSettings[key].Value = value;
205173
}
206174
configFile.Save(ConfigurationSaveMode.Modified);
207175
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
208176
}
209-
catch (ConfigurationErrorsException)
210-
{
177+
catch (ConfigurationErrorsException) {
211178
Console.WriteLine("Error writing app settings");
212179
}
213180
}

0 commit comments

Comments
 (0)