FLEXDefaultsContentSection.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // FLEXDefaultsContentSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/28/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXDefaultsContentSection.h"
  9. @interface FLEXDefaultsContentSection ()
  10. @property (nonatomic) NSUserDefaults *defaults;
  11. @property (nonatomic) NSArray *keys;
  12. @property (nonatomic, readonly) NSDictionary *whitelistedDefaults;
  13. @end
  14. @implementation FLEXDefaultsContentSection
  15. @synthesize keys = _keys;
  16. #pragma mark Initialization
  17. + (instancetype)forObject:(id)object {
  18. return [self forDefaults:object];
  19. }
  20. + (instancetype)standard {
  21. return [self forDefaults:NSUserDefaults.standardUserDefaults];
  22. }
  23. + (instancetype)forDefaults:(NSUserDefaults *)userDefaults {
  24. FLEXDefaultsContentSection *section = [self forReusableFuture:^id(FLEXDefaultsContentSection *section) {
  25. section.defaults = userDefaults;
  26. section.onlyShowKeysForAppPrefs = YES;
  27. return section.whitelistedDefaults;
  28. }];
  29. return section;
  30. }
  31. #pragma mark - Overrides
  32. - (NSString *)title {
  33. return @"Defaults";
  34. }
  35. #pragma mark - Private
  36. - (NSArray *)keys {
  37. if (!_keys) {
  38. if (self.onlyShowKeysForAppPrefs) {
  39. // Read keys from preferences file
  40. NSString *bundle = NSBundle.mainBundle.bundleIdentifier;
  41. NSString *prefsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
  42. NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", prefsPath, bundle];
  43. self.keys = [NSDictionary dictionaryWithContentsOfFile:filePath].allKeys;
  44. } else {
  45. self.keys = self.defaults.dictionaryRepresentation.allKeys;
  46. }
  47. }
  48. return _keys;
  49. }
  50. - (void)setKeys:(NSArray *)keys {
  51. _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  52. }
  53. - (NSDictionary *)whitelistedDefaults {
  54. // Case: no whitelisting
  55. if (!self.onlyShowKeysForAppPrefs) {
  56. return self.defaults.dictionaryRepresentation;
  57. }
  58. // Always regenerate key whitelist when this method is called
  59. _keys = nil;
  60. // Generate new dictionary from whitelisted keys
  61. NSArray *values = [self.defaults.dictionaryRepresentation
  62. objectsForKeys:self.keys notFoundMarker:NSNull.null
  63. ];
  64. return [NSDictionary dictionaryWithObjects:values forKeys:self.keys];
  65. }
  66. #pragma mark - Public
  67. - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs {
  68. if (onlyShowKeysForAppPrefs) {
  69. // This property only applies if we're using standardUserDefaults
  70. if (self.defaults != NSUserDefaults.standardUserDefaults) return;
  71. }
  72. _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
  73. }
  74. @end