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. return section.whitelistedDefaults;
  26. }];
  27. section.defaults = userDefaults;
  28. return section;
  29. }
  30. #pragma mark - Overrides
  31. - (NSString *)title {
  32. return @"Defaults";
  33. }
  34. #pragma mark - Private
  35. - (NSArray *)keys {
  36. if (!_keys) {
  37. if (self.onlyShowKeysForAppPrefs) {
  38. // Read keys from preferences file
  39. NSString *bundle = [NSBundle mainBundle].bundleIdentifier;
  40. NSString *prefsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
  41. NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", prefsPath, bundle];
  42. self.keys = [NSDictionary dictionaryWithContentsOfFile:filePath].allKeys;
  43. } else {
  44. self.keys = self.defaults.dictionaryRepresentation.allKeys;
  45. }
  46. }
  47. return _keys;
  48. }
  49. - (void)setKeys:(NSArray *)keys {
  50. _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  51. }
  52. - (NSDictionary *)whitelistedDefaults {
  53. // Case: no whitelisting
  54. if (!self.onlyShowKeysForAppPrefs) {
  55. return self.defaults.dictionaryRepresentation;
  56. }
  57. // Always regenerate key whitelist when this method is called
  58. _keys = nil;
  59. // Generate new dictionary from whitelisted keys
  60. NSArray *values = [self.defaults.dictionaryRepresentation
  61. objectsForKeys:self.keys notFoundMarker:[NSNull null]
  62. ];
  63. return [NSDictionary dictionaryWithObjects:values forKeys:self.keys];
  64. }
  65. #pragma mark - Public
  66. - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs {
  67. if (onlyShowKeysForAppPrefs) {
  68. // This property only applies if we're using standardUserDefaults
  69. if (self.defaults != NSUserDefaults.standardUserDefaults) return;
  70. }
  71. _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
  72. }
  73. @end