| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // FLEXDefaultsContentSection.m
- // FLEX
- //
- // Created by Tanner Bennett on 8/28/19.
- // Copyright © 2019 Flipboard. All rights reserved.
- //
- #import "FLEXDefaultsContentSection.h"
- @interface FLEXDefaultsContentSection ()
- @property (nonatomic) NSUserDefaults *defaults;
- @property (nonatomic) NSArray *keys;
- @property (nonatomic, readonly) NSDictionary *whitelistedDefaults;
- @end
- @implementation FLEXDefaultsContentSection
- @synthesize keys = _keys;
- #pragma mark Initialization
- + (instancetype)forObject:(id)object {
- return [self forDefaults:object];
- }
- + (instancetype)standard {
- return [self forDefaults:NSUserDefaults.standardUserDefaults];
- }
- + (instancetype)forDefaults:(NSUserDefaults *)userDefaults {
- FLEXDefaultsContentSection *section = [self forReusableFuture:^id(FLEXDefaultsContentSection *section) {
- return section.whitelistedDefaults;
- }];
- section.defaults = userDefaults;
- return section;
- }
- #pragma mark - Overrides
- - (NSString *)title {
- return @"Defaults";
- }
- #pragma mark - Private
- - (NSArray *)keys {
- if (!_keys) {
- if (self.onlyShowKeysForAppPrefs) {
- // Read keys from preferences file
- NSString *bundle = [NSBundle mainBundle].bundleIdentifier;
- NSString *prefsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
- NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", prefsPath, bundle];
- self.keys = [NSDictionary dictionaryWithContentsOfFile:filePath].allKeys;
- } else {
- self.keys = self.defaults.dictionaryRepresentation.allKeys;
- }
- }
- return _keys;
- }
- - (void)setKeys:(NSArray *)keys {
- _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
- }
- - (NSDictionary *)whitelistedDefaults {
- // Case: no whitelisting
- if (!self.onlyShowKeysForAppPrefs) {
- return self.defaults.dictionaryRepresentation;
- }
- // Always regenerate key whitelist when this method is called
- _keys = nil;
- // Generate new dictionary from whitelisted keys
- NSArray *values = [self.defaults.dictionaryRepresentation
- objectsForKeys:self.keys notFoundMarker:[NSNull null]
- ];
- return [NSDictionary dictionaryWithObjects:values forKeys:self.keys];
- }
- #pragma mark - Public
- - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs {
- if (onlyShowKeysForAppPrefs) {
- // This property only applies if we're using standardUserDefaults
- if (self.defaults != NSUserDefaults.standardUserDefaults) return;
- }
- _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
- }
- @end
|