FLEXDefaultsContentSection.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // FLEXDefaultsContentSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/28/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXDefaultsContentSection.h"
  9. #import "FLEXDefaultEditorViewController.h"
  10. #import "FLEXUtility.h"
  11. #if TARGET_OS_TV
  12. #import "fakes.h"
  13. #endif
  14. @interface FLEXDefaultsContentSection ()
  15. @property (nonatomic) NSUserDefaults *defaults;
  16. @property (nonatomic) NSArray *keys;
  17. @property (nonatomic, readonly) NSDictionary *whitelistedDefaults;
  18. @end
  19. @implementation FLEXDefaultsContentSection
  20. @synthesize keys = _keys;
  21. #pragma mark Initialization
  22. + (instancetype)forObject:(id)object {
  23. return [self forDefaults:object];
  24. }
  25. + (instancetype)standard {
  26. return [self forDefaults:NSUserDefaults.standardUserDefaults];
  27. }
  28. + (instancetype)forDefaults:(NSUserDefaults *)userDefaults {
  29. FLEXDefaultsContentSection *section = [self forReusableFuture:^id(FLEXDefaultsContentSection *section) {
  30. section.defaults = userDefaults;
  31. section.onlyShowKeysForAppPrefs = YES;
  32. return section.whitelistedDefaults;
  33. }];
  34. return section;
  35. }
  36. #pragma mark - Overrides
  37. - (NSString *)title {
  38. return @"Defaults";
  39. }
  40. - (void (^)(__kindof UIViewController *))didPressInfoButtonAction:(NSInteger)row {
  41. return ^(UIViewController *host) {
  42. if ([FLEXDefaultEditorViewController canEditDefaultWithValue:[self objectForRow:row]]) {
  43. // We use titleForRow: to get the key because self.keys is not
  44. // necessarily in the same order as the keys being displayed
  45. FLEXVariableEditorViewController *controller = [FLEXDefaultEditorViewController
  46. target:self.defaults key:[self titleForRow:row] commitHandler:^{
  47. [self reloadData:YES];
  48. }
  49. ];
  50. [host.navigationController pushViewController:controller animated:YES];
  51. } else {
  52. [FLEXAlert showAlert:@"Oh No…" message:@"We can't edit this entry :(" from:host];
  53. }
  54. };
  55. }
  56. - (UITableViewCellAccessoryType)accessoryTypeForRow:(NSInteger)row {
  57. #if !TARGET_OS_TV
  58. return UITableViewCellAccessoryDetailDisclosureButton;
  59. #else
  60. return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
  61. #endif
  62. }
  63. #pragma mark - Private
  64. - (NSArray *)keys {
  65. if (!_keys) {
  66. if (self.onlyShowKeysForAppPrefs) {
  67. // Read keys from preferences file
  68. NSString *bundle = NSBundle.mainBundle.bundleIdentifier;
  69. NSString *prefsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
  70. NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", prefsPath, bundle];
  71. self.keys = [NSDictionary dictionaryWithContentsOfFile:filePath].allKeys;
  72. } else {
  73. self.keys = self.defaults.dictionaryRepresentation.allKeys;
  74. }
  75. }
  76. return _keys;
  77. }
  78. - (void)setKeys:(NSArray *)keys {
  79. _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  80. }
  81. - (NSDictionary *)whitelistedDefaults {
  82. // Case: no whitelisting
  83. if (!self.onlyShowKeysForAppPrefs) {
  84. return self.defaults.dictionaryRepresentation;
  85. }
  86. // Always regenerate key whitelist when this method is called
  87. _keys = nil;
  88. // Generate new dictionary from whitelisted keys
  89. NSArray *values = [self.defaults.dictionaryRepresentation
  90. objectsForKeys:self.keys notFoundMarker:NSNull.null
  91. ];
  92. return [NSDictionary dictionaryWithObjects:values forKeys:self.keys];
  93. }
  94. #pragma mark - Public
  95. - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs {
  96. if (onlyShowKeysForAppPrefs) {
  97. // This property only applies if we're using standardUserDefaults
  98. if (self.defaults != NSUserDefaults.standardUserDefaults) return;
  99. }
  100. _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
  101. }
  102. @end