FLEXDefaultsExplorerViewController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // FLEXDefaultsExplorerViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXDefaultsExplorerViewController.h"
  9. #import "FLEXObjectExplorerFactory.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXDefaultEditorViewController.h"
  12. @interface FLEXDefaultsExplorerViewController ()
  13. @property (nonatomic, readonly) NSUserDefaults *defaults;
  14. @property (nonatomic) BOOL onlyShowKeysForAppPrefs;
  15. @property (nonatomic) NSArray *keyWhitelist;
  16. @property (nonatomic) NSArray *keys;
  17. @end
  18. @implementation FLEXDefaultsExplorerViewController
  19. @synthesize keys = _keys;
  20. - (NSUserDefaults *)defaults
  21. {
  22. return [self.object isKindOfClass:[NSUserDefaults class]] ? self.object : nil;
  23. }
  24. - (NSArray *)keys
  25. {
  26. if (!_keys) {
  27. self.keys = self.defaults.dictionaryRepresentation.allKeys;
  28. }
  29. return _keys;
  30. }
  31. - (void)setKeys:(NSArray *)keys {
  32. _keys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  33. }
  34. - (void)setOnlyShowKeysForAppPrefs:(BOOL)onlyShowKeysForAppPrefs
  35. {
  36. if (_onlyShowKeysForAppPrefs == onlyShowKeysForAppPrefs) return;
  37. _onlyShowKeysForAppPrefs = onlyShowKeysForAppPrefs;
  38. if (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. }
  45. }
  46. #pragma mark - Superclass Overrides
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. // Hide keys not present in the preferences file.
  50. // Useful because standardUserDefaults includes a lot of keys that are
  51. // included by default in every app, and probably aren't what you wan to see.
  52. self.onlyShowKeysForAppPrefs = self.defaults == [NSUserDefaults standardUserDefaults];
  53. }
  54. - (NSString *)customSectionTitle
  55. {
  56. return @"Defaults";
  57. }
  58. - (NSArray *)customSectionRowCookies
  59. {
  60. return self.keys;
  61. }
  62. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  63. {
  64. return rowCookie;
  65. }
  66. - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
  67. {
  68. return [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self.defaults objectForKey:rowCookie]];
  69. }
  70. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  71. {
  72. UIViewController *drillInViewController = nil;
  73. NSString *key = rowCookie;
  74. id drillInObject = [self.defaults objectForKey:key];
  75. if ([FLEXDefaultEditorViewController canEditDefaultWithValue:drillInObject]) {
  76. drillInViewController = [[FLEXDefaultEditorViewController alloc] initWithDefaults:self.defaults key:key];
  77. } else {
  78. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInObject];
  79. }
  80. return drillInViewController;
  81. }
  82. @end