Skip to content

Commit 8576565

Browse files
committed
Fixed IASKSettingChangedNotification
we no longer mess with the documented IASKSettingChangedNotification registration by separating internal and public notifications; IASKSettingChangedNotification notification observer on the IASKAppSettingsViewController was removed when pushing a child pane;
1 parent 5ac5c57 commit 8576565

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

InAppSettingsKitSampleApp/Classes/MainViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ class MainViewController: UIViewController {
5151
}
5252

5353
@objc func settingDidChange(notification: Notification?) {
54+
print("settingDidChange: \(notification?.userInfo.debugDescription ?? "-")")
5455
updateHiddenKeys()
5556
}
57+
5658
func updateHiddenKeys() {
5759
var hiddenKeys = Set<String>()
5860
if UserDefaults.standard.bool(forKey: "AutoConnect") {

Sources/InAppSettingsKit/Controllers/IASKAppSettingsViewController.m

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ - (void)viewWillAppear:(BOOL)animated {
236236
}
237237

238238
NSNotificationCenter *dc = NSNotificationCenter.defaultCenter;
239-
[dc removeObserver:self name:kIASKAppSettingChanged object:nil];
240-
[dc addObserver:self selector:@selector(didChangeSettingViaIASK:) name:kIASKAppSettingChanged object:nil];
239+
[dc removeObserver:self name:kIASKInternalAppSettingChanged object:nil];
240+
[dc addObserver:self selector:@selector(didChangeSettingViaIASK:) name:kIASKInternalAppSettingChanged object:nil];
241241
if ([self.settingsStore isKindOfClass:[IASKSettingsStoreUserDefaults class]]) {
242242
IASKSettingsStoreUserDefaults *udSettingsStore = (id)self.settingsStore;
243243
[dc removeObserver:self name:NSUserDefaultsDidChangeNotification object:udSettingsStore.defaults];
@@ -272,7 +272,7 @@ - (void)viewDidDisappear:(BOOL)animated NS_EXTENSION_UNAVAILABLE("Uses APIs (i.e
272272
if ([self.settingsStore isKindOfClass:[IASKSettingsStoreUserDefaults class]]) {
273273
IASKSettingsStoreUserDefaults *udSettingsStore = (id)self.settingsStore;
274274
[dc removeObserver:self name:NSUserDefaultsDidChangeNotification object:udSettingsStore.defaults];
275-
[dc removeObserver:self name:kIASKAppSettingChanged object:self];
275+
[dc removeObserver:self name:kIASKInternalAppSettingChanged object:nil];
276276
}
277277
[dc removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:UIApplication.sharedApplication];
278278
[dc removeObserver:self name:UIApplicationWillEnterForegroundNotification object:UIApplication.sharedApplication];
@@ -452,18 +452,14 @@ - (void)setSpecifier:(IASKSpecifier*)specifier on:(BOOL)on {
452452
NSIndexPath* indexPath = [_settingsReader indexPathForKey:key];
453453
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
454454
cell.detailTextLabel.text = [specifier subtitleForValue:on ? @"YES" : @"NO"];
455+
[self postChange:specifier value: [self.settingsStore objectForSpecifier:specifier]];
455456
}
456-
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged
457-
object:self
458-
userInfo:@{(id)specifier.key: [self.settingsStore objectForSpecifier:specifier] ?: NSNull.null}];
459457
}
460458

461459
- (void)sliderChangedValue:(id)sender {
462460
IASKSlider *slider = (IASKSlider*)sender;
463461
[self.settingsStore setFloat:slider.value forSpecifier:slider.specifier];
464-
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged
465-
object:self
466-
userInfo:@{(id)slider.specifier.key: @(slider.value)}];
462+
[self postChange:slider.specifier value: @(slider.value)];
467463
}
468464

469465
- (void)datePickerChangedValue:(IASKDatePicker*)datePicker {
@@ -472,6 +468,7 @@ - (void)datePickerChangedValue:(IASKDatePicker*)datePicker {
472468
[self.delegate settingsViewController:self setDate:datePicker.date forSpecifier:datePicker.specifier];
473469
} else {
474470
[self.settingsStore setObject:datePicker.date forSpecifier:datePicker.specifier];
471+
[self postChange:datePicker.specifier value:datePicker.date];
475472
}
476473
datePicker.editing = NO;
477474
}
@@ -1077,8 +1074,8 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
10771074
[self.settingsStore removeObjectWithSpecifier:specifier];
10781075
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
10791076

1080-
NSDictionary *userInfo = specifier.parentSpecifier.key && [self.settingsStore objectForSpecifier:(id)specifier.parentSpecifier] ? @{(id)specifier.parentSpecifier.key: [self.settingsStore objectForSpecifier:(id)specifier.parentSpecifier] ?: @[]} : nil;
1081-
[NSNotificationCenter.defaultCenter postNotificationName:kIASKAppSettingChanged object:self userInfo:userInfo];
1077+
[self postChange:specifier.parentSpecifier
1078+
value:[self.settingsStore objectForSpecifier:specifier.parentSpecifier] ?: @[]];
10821079
}
10831080

10841081
- (void)presentChildViewController:(UITableViewController<IASKViewController> *)targetViewController specifier:(IASKSpecifier *)specifier indexPath:(NSIndexPath*)indexPath {
@@ -1094,7 +1091,7 @@ - (void)presentChildViewController:(UITableViewController<IASKViewController> *)
10941091
if ([value isKindOfClass:NSDictionary.class]) {
10951092
itemDict = value;
10961093
} else if (specifier.key && value) {
1097-
itemDict = @{(id)specifier.key: value};
1094+
itemDict = @{specifier.key: value};
10981095
}
10991096
}
11001097
IASKSettingsStoreInMemory *inMemoryStore = [[IASKSettingsStoreInMemory alloc] initWithDictionary:itemDict];
@@ -1130,8 +1127,9 @@ - (void)presentChildViewController:(UITableViewController<IASKViewController> *)
11301127
} else {
11311128
[weakSelf.settingsStore setObject:inMemoryStore.dictionary forSpecifier:specifier];
11321129
}
1133-
NSDictionary *userInfo = specifier.parentSpecifier.key && [weakSelf.settingsStore objectForSpecifier:(id)specifier.parentSpecifier] ? @{(id)specifier.parentSpecifier.key: (id)[weakSelf.settingsStore objectForSpecifier:(id)specifier.parentSpecifier]} : nil;
1134-
[NSNotificationCenter.defaultCenter postNotificationName:kIASKAppSettingChanged object:weakSelf userInfo:userInfo];
1130+
[weakSelf postChange:specifier.parentSpecifier
1131+
value:[weakSelf.settingsStore objectForSpecifier:specifier.parentSpecifier]];
1132+
11351133
[weakSelf.tableView reloadData];
11361134
};
11371135
self.childPaneHandler(NO); // perform initial validation
@@ -1208,10 +1206,7 @@ - (BOOL)textField:(IASKTextField*)textField shouldChangeCharactersInRange:(NSRan
12081206
}
12091207
if (storeToSettings) {
12101208
[self.settingsStore setObject:newText forSpecifier:textField.specifier];
1211-
NSDictionary *userInfo = textField.specifier.key && newText ? @{(id)textField.specifier.key : newText} : nil;
1212-
[NSNotificationCenter.defaultCenter postNotificationName:kIASKAppSettingChanged
1213-
object:self
1214-
userInfo:userInfo];
1209+
[self postChange:textField.specifier value:newText];
12151210
}
12161211
}
12171212
return result != IASKValidationResultOkWithReplacement;
@@ -1251,19 +1246,19 @@ - (void)textFieldDidEndEditing:(IASKTextField *)textField {
12511246
if (![self.settingsStore objectForSpecifier:specifier] && textField.text.length == 0) {
12521247
return;
12531248
}
1249+
NSString* oldValue = [self.settingsStore objectForSpecifier:specifier];
12541250
[self.settingsStore setObject:textField.text forSpecifier:specifier];
12551251
if (specifier.isAddSpecifier) {
1256-
NSUInteger section = [self.settingsReader indexPathForKey:(id)specifier.parentSpecifier.key].section;
1252+
NSUInteger section = [self.settingsReader indexPathForKey:specifier.parentSpecifier.key].section;
12571253
NSUInteger row = [self tableView:self.tableView numberOfRowsInSection:section] - 2;
12581254
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
12591255
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
12601256
indexPath = [NSIndexPath indexPathForRow:row + 1 inSection:section];
12611257
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
12621258
}
1263-
NSDictionary *userInfo = specifier.key && textField.text ? @{(id)specifier.key: (id)textField.text} : nil;
1264-
[NSNotificationCenter.defaultCenter postNotificationName:kIASKAppSettingChanged
1265-
object:self
1266-
userInfo:userInfo];
1259+
if (textField.text && ![oldValue isEqual:textField.text]) {
1260+
[self postChange:specifier value:textField.text];
1261+
}
12671262
break;
12681263
}
12691264
case IASKValidationResultFailed:
@@ -1291,17 +1286,15 @@ - (void)textViewDidChange:(IASKTextView *)textView {
12911286
[self cacheRowHeightForTextView:textView animated:YES];
12921287

12931288
CGRect visibleTableRect = UIEdgeInsetsInsetRect(self.tableView.bounds, self.tableView.contentInset);
1294-
NSIndexPath *indexPath = [self.settingsReader indexPathForKey:(id)textView.specifier.key];
1289+
NSIndexPath *indexPath = [self.settingsReader indexPathForKey:textView.specifier.key];
12951290
CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath];
12961291

12971292
if (!CGRectContainsRect(visibleTableRect, cellFrame)) {
12981293
[self.tableView scrollRectToVisible:CGRectInset(cellFrame, 0, - 30) animated:YES];
12991294
}
13001295

13011296
[self.settingsStore setObject:textView.text forSpecifier:textView.specifier];
1302-
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged
1303-
object:self
1304-
userInfo:@{(id)textView.specifier.key: textView.text}];
1297+
[self postChange:textView.specifier value:textView.text];
13051298

13061299
}
13071300

@@ -1372,6 +1365,19 @@ - (void)userDefaultsDidChange {
13721365
});
13731366
}
13741367

1368+
- (void)postChange:(IASKSpecifier*)specifier value:(id)value {
1369+
if (!specifier.key) {
1370+
return;
1371+
}
1372+
NSDictionary *userInfo = @{specifier.key: value ?: NSNull.null};
1373+
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKInternalAppSettingChanged
1374+
object:self
1375+
userInfo:userInfo];
1376+
[[NSNotificationCenter defaultCenter] postNotificationName:IASKSettingChangedNotification
1377+
object:self
1378+
userInfo:userInfo];
1379+
}
1380+
13751381
- (void)didChangeSettingViaIASK:(NSNotification*)notification {
13761382
NSString *key = notification.userInfo.allKeys.firstObject;
13771383
if (key) {

Sources/InAppSettingsKit/Controllers/IASKMultipleValueSelection.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath {
108108
[self.settingsStore setObject:[values objectAtIndex:indexPath.row] forSpecifier:self.specifier];
109109
[self.settingsStore synchronize];
110110
NSDictionary *userInfo = self.specifier.key && values[indexPath.row] ? @{(id)self.specifier.key: (id)values[indexPath.row]} : nil;
111-
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKAppSettingChanged object:self userInfo:userInfo];
111+
[[NSNotificationCenter defaultCenter] postNotificationName:kIASKInternalAppSettingChanged object:self userInfo:userInfo];
112+
[[NSNotificationCenter defaultCenter] postNotificationName:IASKSettingChangedNotification object:self userInfo:userInfo];
112113
};
113114

114115
- (void)updateSelectionInCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {

Sources/InAppSettingsKit/include/IASKSettingsReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ NS_ASSUME_NONNULL_BEGIN
167167

168168
extern NSString * const IASKSettingChangedNotification;
169169
#define kIASKAppSettingChanged IASKSettingChangedNotification
170+
#define kIASKInternalAppSettingChanged @"IASKInternalSettingChangedNotification"
170171

171172
#define kIASKSectionHeaderIndex 0
172173

0 commit comments

Comments
 (0)