Skip to content

Commit 9c7461d

Browse files
committed
fix: Issue with BaseSettings not setting property notifications when new file is created
1 parent 458fb8a commit 9c7461d

File tree

1 file changed

+74
-82
lines changed

1 file changed

+74
-82
lines changed

Settings/Base/BaseSettings.cs

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -89,117 +89,109 @@ protected void LoadFrom(string file)
8989

9090
if (File.Exists(file))
9191
{
92-
try
92+
Dispatcher.Invoke(() =>
9393
{
94-
Dispatcher.Invoke(() =>
94+
try
9595
{
96-
try
97-
{
98-
JsonConvert.PopulateObject(File.ReadAllText(file), this);
99-
}
100-
catch (Exception e)
101-
{
102-
_logger.Exception(e);
103-
}
104-
});
96+
JsonConvert.PopulateObject(File.ReadAllText(file), this);
97+
}
98+
catch (Exception e)
99+
{
100+
_logger.Exception(e);
101+
}
102+
});
103+
}
105104

106-
foreach (var propertyInfo in properties)
105+
foreach (var propertyInfo in properties)
106+
{
107+
//Check if property is an observable collection
108+
if (typeof(INotifyCollectionChanged).IsAssignableFrom(propertyInfo.PropertyType))
109+
{
110+
//_logger.Debug($"Property {propertyInfo.Name} is an INotifyCollectionChanged");
111+
//Set list changed event to trigger on property change
112+
if (propertyInfo.GetValue(this) is INotifyCollectionChanged collection)
107113
{
108-
//Check if property is an observable collection
109-
if (typeof(INotifyCollectionChanged).IsAssignableFrom(propertyInfo.PropertyType))
114+
if (propertyInfo.PropertyType.IsGenericType && typeof(INotifyPropertyChanged).IsAssignableFrom(propertyInfo.PropertyType.GenericTypeArguments[0]))
110115
{
111-
//_logger.Debug($"Property {propertyInfo.Name} is an INotifyCollectionChanged");
112-
//Set list changed event to trigger on property change
113-
var collection = propertyInfo.GetValue(this) as INotifyCollectionChanged;
114-
if (collection != null)
116+
//_logger.Debug($"Property {propertyInfo.Name} is an INotifyPropertyChanged with generic type {propertyInfo.PropertyType.GenericTypeArguments[0]}");
117+
118+
//loop through all items in collection and add property changed event
119+
foreach (var item in (IEnumerable<object>)collection)
115120
{
116-
if (propertyInfo.PropertyType.IsGenericType && typeof(INotifyPropertyChanged).IsAssignableFrom(propertyInfo.PropertyType.GenericTypeArguments[0]))
121+
if (item is INotifyPropertyChanged notifyPropertyChanged)
117122
{
118-
//_logger.Debug($"Property {propertyInfo.Name} is an INotifyPropertyChanged with generic type {propertyInfo.PropertyType.GenericTypeArguments[0]}");
123+
notifyPropertyChanged.PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged;
124+
}
125+
}
119126

120-
//loop through all items in collection and add property changed event
121-
foreach (var item in (IEnumerable<object>)collection)
127+
collection.CollectionChanged += (_, args) =>
128+
{
129+
OnPropertyChanged(propertyInfo.Name);
130+
131+
if (args is { Action: NotifyCollectionChangedAction.Add, NewItems: not null })
132+
{
133+
foreach (var item in args.NewItems)
122134
{
123135
if (item is INotifyPropertyChanged notifyPropertyChanged)
124136
{
125137
notifyPropertyChanged.PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged;
126138
}
127139
}
140+
}
128141

129-
collection.CollectionChanged += (_, args) =>
142+
if (args is { Action: NotifyCollectionChangedAction.Remove, OldItems: not null })
143+
{
144+
foreach (var item in args.OldItems)
130145
{
131-
OnPropertyChanged(propertyInfo.Name);
132-
133-
if (args is { Action: NotifyCollectionChangedAction.Add, NewItems: not null })
134-
{
135-
foreach (var item in args.NewItems)
136-
{
137-
if (item is INotifyPropertyChanged notifyPropertyChanged)
138-
{
139-
notifyPropertyChanged.PropertyChanged += OnNotifyPropertyChangedOnPropertyChanged;
140-
}
141-
}
142-
}
143-
144-
if (args is { Action: NotifyCollectionChangedAction.Remove, OldItems: not null })
146+
if (item is INotifyPropertyChanged notifyPropertyChanged)
145147
{
146-
foreach (var item in args.OldItems)
147-
{
148-
if (item is INotifyPropertyChanged notifyPropertyChanged)
149-
{
150-
notifyPropertyChanged.PropertyChanged -= OnNotifyPropertyChangedOnPropertyChanged;
151-
}
152-
}
148+
notifyPropertyChanged.PropertyChanged -= OnNotifyPropertyChangedOnPropertyChanged;
153149
}
154-
};
155-
156-
void OnNotifyPropertyChangedOnPropertyChanged(object? o, PropertyChangedEventArgs eventArgs)
157-
{
158-
OnPropertyChanged(propertyInfo.Name);
159150
}
160151
}
161-
else
162-
{
163-
collection.CollectionChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
164-
}
165-
}
166-
else
167-
{
168-
_logger.Error($"Property {propertyInfo.Name} is not an INotifyCollectionChanged it is {propertyInfo.PropertyType}");
169-
}
170-
}
152+
};
171153

172-
if (typeof(IBindingList).IsAssignableFrom(propertyInfo.PropertyType))
173-
{
174-
//Set list changed event to trigger on property change
175-
var collection = propertyInfo.GetValue(this) as IBindingList;
176-
if (collection != null)
177-
{
178-
collection.ListChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
179-
}
180-
else
154+
void OnNotifyPropertyChangedOnPropertyChanged(object? o, PropertyChangedEventArgs eventArgs)
181155
{
182-
_logger.Error($"Property {propertyInfo.Name} is not an IBindingList it is {propertyInfo.PropertyType}");
156+
OnPropertyChanged(propertyInfo.Name);
183157
}
184158
}
185-
186-
if (typeof(INotifyPropertyChanged).IsAssignableFrom(propertyInfo.PropertyType))
159+
else
187160
{
188-
var notifyPropertyChanged = propertyInfo.GetValue(this) as INotifyPropertyChanged;
189-
if (notifyPropertyChanged != null)
190-
{
191-
notifyPropertyChanged.PropertyChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
192-
}
193-
else
194-
{
195-
_logger.Error($"Property {propertyInfo.Name} is not an INotifyPropertyChanged it is {propertyInfo.PropertyType}");
196-
}
161+
collection.CollectionChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
197162
}
198163
}
164+
else
165+
{
166+
_logger.Error($"Property {propertyInfo.Name} is not an INotifyCollectionChanged it is {propertyInfo.PropertyType}");
167+
}
199168
}
200-
catch (Exception e)
169+
170+
if (typeof(IBindingList).IsAssignableFrom(propertyInfo.PropertyType))
201171
{
202-
_logger.Exception(e);
172+
//Set list changed event to trigger on property change
173+
var collection = propertyInfo.GetValue(this) as IBindingList;
174+
if (collection != null)
175+
{
176+
collection.ListChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
177+
}
178+
else
179+
{
180+
_logger.Error($"Property {propertyInfo.Name} is not an IBindingList it is {propertyInfo.PropertyType}");
181+
}
182+
}
183+
184+
if (typeof(INotifyPropertyChanged).IsAssignableFrom(propertyInfo.PropertyType))
185+
{
186+
var notifyPropertyChanged = propertyInfo.GetValue(this) as INotifyPropertyChanged;
187+
if (notifyPropertyChanged != null)
188+
{
189+
notifyPropertyChanged.PropertyChanged += (_, _) => { OnPropertyChanged(propertyInfo.Name); };
190+
}
191+
else
192+
{
193+
_logger.Error($"Property {propertyInfo.Name} is not an INotifyPropertyChanged it is {propertyInfo.PropertyType}");
194+
}
203195
}
204196
}
205197

0 commit comments

Comments
 (0)