FLEXDefaultsContentSection.m 3.7 KB

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