FLEXDefaultsContentSection.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return UITableViewCellAccessoryDetailDisclosureButton;
  55. }
  56. #pragma mark - Private
  57. - (NSArray *)keys {
  58. if (!_keys) {
  59. if (self.onlyShowKeysForAppPrefs) {
  60. // Read keys from preferences file
  61. NSString *bundle = NSBundle.mainBundle.bundleIdentifier;
  62. NSString *prefsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
  63. NSString *filePath = [NSString stringWithFormat:@"%@/%@.plist", prefsPath, bundle];
  64. self.keys = [NSDictionary dictionaryWithContentsOfFile:filePath].allKeys;
  65. } else {
  66. self.keys = self.defaults.dictionaryRepresentation.allKeys;
  67. }
  68. }
  69. return _keys;
  70. }
  71. - (void)setKeys:(NSArray *)keys {
  72. _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  73. }
  74. - (NSDictionary *)whitelistedDefaults {
  75. // Case: no whitelisting
  76. if (!self.onlyShowKeysForAppPrefs) {
  77. return self.defaults.dictionaryRepresentation;
  78. }
  79. // Always regenerate key whitelist when this method is called
  80. _keys = nil;
  81. // Generate new dictionary from whitelisted keys
  82. NSArray *values = [self.defaults.dictionaryRepresentation
  83. objectsForKeys:self.keys notFoundMarker:NSNull.null
  84. ];
  85. return [NSDictionary dictionaryWithObjects:values forKeys:self.keys];
  86. }
  87. #pragma mark - Public
  88. - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs {
  89. if (onlyShowKeysForAppPrefs) {
  90. // This property only applies if we're using standardUserDefaults
  91. if (self.defaults != NSUserDefaults.standardUserDefaults) return;
  92. }
  93. _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
  94. }
  95. @end