NSUserDefaults+FLEX.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // NSUserDefaults+FLEX.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/10/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "NSUserDefaults+FLEX.h"
  9. NSString * const kFLEXDefaultsToolbarTopMarginKey = @"com.flex.FLEXToolbar.topMargin";
  10. NSString * const kFLEXDefaultsiOSPersistentOSLogKey = @"com.flipborad.flex.enable_persistent_os_log";
  11. NSString * const kFLEXDefaultsHidePropertyIvarsKey = @"com.flipboard.FLEX.hide_property_ivars";
  12. NSString * const kFLEXDefaultsHidePropertyMethodsKey = @"com.flipboard.FLEX.hide_property_methods";
  13. NSString * const kFLEXDefaultsHideMethodOverridesKey = @"com.flipboard.FLEX.hide_method_overrides";
  14. NSString * const kFLEXDefaultsHideVariablePreviewsKey = @"com.flipboard.FLEX.hide_variable_previews";
  15. NSString * const kFLEXDefaultsNetworkHostBlacklistKey = @"com.flipboard.FLEX.network_host_blacklist";
  16. NSString * const kFLEXDefaultsDisableOSLogForceASLKey = @"com.flipboard.FLEX.try_disable_os_log";
  17. NSString * const kFLEXDefaultsRegisterJSONExplorerKey = @"com.flipboard.FLEX.view_json_as_object";
  18. #define FLEXDefaultsPathForFile(name) ({ \
  19. NSArray *paths = NSSearchPathForDirectoriesInDomains( \
  20. NSLibraryDirectory, NSUserDomainMask, YES \
  21. ); \
  22. [paths[0] stringByAppendingPathComponent:@"Preferences"]; \
  23. })
  24. @implementation NSUserDefaults (FLEX)
  25. #pragma mark Internal
  26. /// @param filename the name of a plist file without any extension
  27. - (NSString *)flex_defaultsPathForFile:(NSString *)filename {
  28. filename = [filename stringByAppendingPathExtension:@"plist"];
  29. NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(
  30. NSLibraryDirectory, NSUserDomainMask, YES
  31. );
  32. NSString *preferences = [paths[0] stringByAppendingPathComponent:@"Preferences"];
  33. return [preferences stringByAppendingPathComponent:filename];
  34. }
  35. #pragma mark Helper
  36. - (void)flex_toggleBoolForKey:(NSString *)key {
  37. [self setBool:![self boolForKey:key] forKey:key];
  38. [NSNotificationCenter.defaultCenter postNotificationName:key object:nil];
  39. }
  40. #pragma mark Misc
  41. - (double)flex_toolbarTopMargin {
  42. if ([self objectForKey:kFLEXDefaultsToolbarTopMarginKey]) {
  43. return [self doubleForKey:kFLEXDefaultsToolbarTopMarginKey];
  44. }
  45. return 100;
  46. }
  47. - (void)setFlex_toolbarTopMargin:(double)margin {
  48. [self setDouble:margin forKey:kFLEXDefaultsToolbarTopMarginKey];
  49. }
  50. - (NSArray<NSString *> *)flex_networkHostBlacklist {
  51. return [NSArray arrayWithContentsOfFile:[
  52. self flex_defaultsPathForFile:kFLEXDefaultsNetworkHostBlacklistKey
  53. ]] ?: @[];
  54. }
  55. - (void)setFlex_networkHostBlacklist:(NSArray<NSString *> *)blacklist {
  56. NSParameterAssert(blacklist);
  57. [blacklist writeToFile:[
  58. self flex_defaultsPathForFile:kFLEXDefaultsNetworkHostBlacklistKey
  59. ] atomically:YES];
  60. }
  61. - (BOOL)flex_registerDictionaryJSONViewerOnLaunch {
  62. return [self boolForKey:kFLEXDefaultsRegisterJSONExplorerKey];
  63. }
  64. - (void)setFlex_registerDictionaryJSONViewerOnLaunch:(BOOL)enable {
  65. [self setBool:enable forKey:kFLEXDefaultsRegisterJSONExplorerKey];
  66. }
  67. #pragma mark System Log
  68. - (BOOL)flex_disableOSLog {
  69. return [self boolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
  70. }
  71. - (void)setFlex_disableOSLog:(BOOL)disable {
  72. [self setBool:disable forKey:kFLEXDefaultsDisableOSLogForceASLKey];
  73. }
  74. - (BOOL)flex_cacheOSLogMessages {
  75. return [self boolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
  76. }
  77. - (void)setFlex_cacheOSLogMessages:(BOOL)cache {
  78. [self setBool:cache forKey:kFLEXDefaultsiOSPersistentOSLogKey];
  79. [NSNotificationCenter.defaultCenter
  80. postNotificationName:kFLEXDefaultsiOSPersistentOSLogKey
  81. object:nil
  82. ];
  83. }
  84. #pragma mark Object Explorer
  85. - (BOOL)flex_explorerHidesPropertyIvars {
  86. return [self boolForKey:kFLEXDefaultsHidePropertyIvarsKey];
  87. }
  88. - (void)setFlex_explorerHidesPropertyIvars:(BOOL)hide {
  89. [self setBool:hide forKey:kFLEXDefaultsHidePropertyIvarsKey];
  90. [NSNotificationCenter.defaultCenter
  91. postNotificationName:kFLEXDefaultsHidePropertyIvarsKey
  92. object:nil
  93. ];
  94. }
  95. - (BOOL)flex_explorerHidesPropertyMethods {
  96. return [self boolForKey:kFLEXDefaultsHidePropertyMethodsKey];
  97. }
  98. - (void)setFlex_explorerHidesPropertyMethods:(BOOL)hide {
  99. [self setBool:hide forKey:kFLEXDefaultsHidePropertyMethodsKey];
  100. [NSNotificationCenter.defaultCenter
  101. postNotificationName:kFLEXDefaultsHidePropertyMethodsKey
  102. object:nil
  103. ];
  104. }
  105. - (BOOL)flex_explorerShowsMethodOverrides {
  106. return [self boolForKey:kFLEXDefaultsHideMethodOverridesKey];
  107. }
  108. - (void)setFlex_explorerShowsMethodOverrides:(BOOL)show {
  109. [self setBool:show forKey:kFLEXDefaultsHideMethodOverridesKey];
  110. [NSNotificationCenter.defaultCenter
  111. postNotificationName:kFLEXDefaultsHideMethodOverridesKey
  112. object:nil
  113. ];
  114. }
  115. - (BOOL)flex_explorerHidesVariablePreviews {
  116. return [self boolForKey:kFLEXDefaultsHideVariablePreviewsKey];
  117. }
  118. - (void)setFlex_explorerHidesVariablePreviews:(BOOL)hide {
  119. [self setBool:hide forKey:kFLEXDefaultsHideVariablePreviewsKey];
  120. [NSNotificationCenter.defaultCenter
  121. postNotificationName:kFLEXDefaultsHideVariablePreviewsKey
  122. object:nil
  123. ];
  124. }
  125. @end