FLEXShortcutsSection.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // FLEXShortcutsSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/29/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXShortcutsSection.h"
  9. #import "FLEXTableView.h"
  10. #import "FLEXTableViewCell.h"
  11. #import "FLEXUtility.h"
  12. #import "FLEXShortcut.h"
  13. #import "FLEXProperty.h"
  14. #import "FLEXPropertyAttributes.h"
  15. #import "FLEXIvar.h"
  16. #import "FLEXMethod.h"
  17. #import "FLEXRuntime+UIKitHelpers.h"
  18. #import "FLEXObjectExplorer.h"
  19. #if TARGET_OS_TV
  20. #import "fakes.h"
  21. #endif
  22. #pragma mark Private
  23. @interface FLEXShortcutsSection ()
  24. @property (nonatomic, copy) NSArray<NSString *> *titles;
  25. @property (nonatomic, copy) NSArray<NSString *> *subtitles;
  26. @property (nonatomic, copy) NSArray<NSString *> *allTitles;
  27. @property (nonatomic, copy) NSArray<NSString *> *allSubtitles;
  28. // Shortcuts are not used if initialized with static titles and subtitles
  29. @property (nonatomic, copy) NSArray<id<FLEXShortcut>> *shortcuts;
  30. @property (nonatomic, readonly) NSArray<id<FLEXShortcut>> *allShortcuts;
  31. @end
  32. @implementation FLEXShortcutsSection
  33. #pragma mark Initialization
  34. + (instancetype)forObject:(id)objectOrClass rowTitles:(NSArray<NSString *> *)titles {
  35. return [self forObject:objectOrClass rowTitles:titles rowSubtitles:nil];
  36. }
  37. + (instancetype)forObject:(id)objectOrClass
  38. rowTitles:(NSArray<NSString *> *)titles
  39. rowSubtitles:(NSArray<NSString *> *)subtitles {
  40. return [[self alloc] initWithObject:objectOrClass titles:titles subtitles:subtitles];
  41. }
  42. + (instancetype)forObject:(id)objectOrClass rows:(NSArray *)rows {
  43. return [[self alloc] initWithObject:objectOrClass rows:rows];
  44. }
  45. + (instancetype)forObject:(id)objectOrClass additionalRows:(NSArray *)toPrepend {
  46. NSArray *rows = [FLEXShortcutsFactory shortcutsForObjectOrClass:objectOrClass];
  47. NSArray *allRows = [toPrepend arrayByAddingObjectsFromArray:rows] ?: rows;
  48. return [self forObject:objectOrClass rows:allRows];
  49. }
  50. + (instancetype)forObject:(id)objectOrClass {
  51. return [self forObject:objectOrClass additionalRows:nil];
  52. }
  53. - (id)initWithObject:(id)object
  54. titles:(NSArray<NSString *> *)titles
  55. subtitles:(NSArray<NSString *> *)subtitles {
  56. NSParameterAssert(titles.count == subtitles.count || !subtitles);
  57. NSParameterAssert(titles.count);
  58. self = [super init];
  59. if (self) {
  60. _object = object;
  61. _allTitles = titles.copy;
  62. _allSubtitles = subtitles.copy;
  63. _numberOfLines = 1;
  64. }
  65. return self;
  66. }
  67. - (id)initWithObject:object rows:(NSArray *)rows {
  68. self = [super init];
  69. if (self) {
  70. _object = object;
  71. _allShortcuts = [rows flex_mapped:^id(id obj, NSUInteger idx) {
  72. return [FLEXShortcut shortcutFor:obj];
  73. }];
  74. _numberOfLines = 1;
  75. // Populate titles and subtitles
  76. [self reloadData];
  77. }
  78. return self;
  79. }
  80. #pragma mark - Public
  81. - (void)setCacheSubtitles:(BOOL)cacheSubtitles {
  82. if (_cacheSubtitles == cacheSubtitles) return;
  83. // cacheSubtitles only applies if we have shortcut objects
  84. if (self.allShortcuts) {
  85. _cacheSubtitles = cacheSubtitles;
  86. [self reloadData];
  87. } else {
  88. NSLog(@"Warning: setting 'cacheSubtitles' on a shortcut section with static subtitles");
  89. }
  90. }
  91. #pragma mark - Overrides
  92. - (UITableViewCellAccessoryType)accessoryTypeForRow:(NSInteger)row {
  93. if (_allShortcuts) {
  94. return [self.shortcuts[row] accessoryTypeWith:self.object];
  95. }
  96. return UITableViewCellAccessoryNone;
  97. }
  98. - (void)setFilterText:(NSString *)filterText {
  99. super.filterText = filterText;
  100. NSAssert(
  101. self.allTitles.count == self.allSubtitles.count,
  102. @"Each title needs a (possibly empty) subtitle"
  103. );
  104. if (filterText.length) {
  105. // Tally up indexes of titles and subtitles matching the filter
  106. NSMutableIndexSet *filterMatches = [NSMutableIndexSet new];
  107. id filterBlock = ^BOOL(NSString *obj, NSUInteger idx) {
  108. if ([obj localizedCaseInsensitiveContainsString:filterText]) {
  109. [filterMatches addIndex:idx];
  110. return YES;
  111. }
  112. return NO;
  113. };
  114. // Get all matching indexes, including subtitles
  115. [self.allTitles flex_forEach:filterBlock];
  116. [self.allSubtitles flex_forEach:filterBlock];
  117. // Filter to matching indexes only
  118. self.titles = [self.allTitles objectsAtIndexes:filterMatches];
  119. self.subtitles = [self.allSubtitles objectsAtIndexes:filterMatches];
  120. self.shortcuts = [self.allShortcuts objectsAtIndexes:filterMatches];
  121. } else {
  122. self.shortcuts = self.allShortcuts;
  123. self.titles = self.allTitles;
  124. self.subtitles = [self.allSubtitles flex_filtered:^BOOL(NSString *sub, NSUInteger idx) {
  125. return sub.length > 0;
  126. }];
  127. }
  128. }
  129. - (void)reloadData {
  130. [FLEXObjectExplorer configureDefaultsForItems:self.allShortcuts];
  131. // Generate all (sub)titles from shortcuts
  132. if (self.allShortcuts) {
  133. self.allTitles = [self.allShortcuts flex_mapped:^id(FLEXShortcut *s, NSUInteger idx) {
  134. return [s titleWith:self.object];
  135. }];
  136. self.allSubtitles = [self.allShortcuts flex_mapped:^id(FLEXShortcut *s, NSUInteger idx) {
  137. return [s subtitleWith:self.object] ?: @"";
  138. }];
  139. }
  140. // Re-generate filtered (sub)titles and shortcuts
  141. self.filterText = self.filterText;
  142. }
  143. - (NSString *)title {
  144. return @"Shortcuts";
  145. }
  146. - (NSInteger)numberOfRows {
  147. return self.titles.count;
  148. }
  149. - (BOOL)canSelectRow:(NSInteger)row {
  150. UITableViewCellAccessoryType type = [self.shortcuts[row] accessoryTypeWith:self.object];
  151. BOOL hasDisclosure = NO;
  152. hasDisclosure |= type == UITableViewCellAccessoryDisclosureIndicator;
  153. #if !TARGET_OS_TV
  154. hasDisclosure |= type == UITableViewCellAccessoryDetailDisclosureButton;
  155. #else
  156. hasDisclosure |= type == TVTableViewCellAccessoryDetailDisclosureButton;
  157. #endif
  158. return hasDisclosure;
  159. }
  160. - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row {
  161. return [self.shortcuts[row] didSelectActionWith:self.object];
  162. }
  163. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  164. /// Nil if shortcuts is nil, i.e. if initialized with forObject:rowTitles:rowSubtitles:
  165. return [self.shortcuts[row] viewerWith:self.object];
  166. }
  167. - (void (^)(__kindof UIViewController *))didPressInfoButtonAction:(NSInteger)row {
  168. id<FLEXShortcut> shortcut = self.shortcuts[row];
  169. if ([shortcut respondsToSelector:@selector(editorWith:forSection:)]) {
  170. id object = self.object;
  171. return ^(UIViewController *host) {
  172. UIViewController *editor = [shortcut editorWith:object forSection:self];
  173. [host.navigationController pushViewController:editor animated:YES];
  174. };
  175. }
  176. return nil;
  177. }
  178. - (NSString *)reuseIdentifierForRow:(NSInteger)row {
  179. FLEXTableViewCellReuseIdentifier defaultReuse = kFLEXDetailCell;
  180. if (@available(iOS 11, *)) {
  181. defaultReuse = kFLEXMultilineDetailCell;
  182. }
  183. return [self.shortcuts[row] customReuseIdentifierWith:self.object] ?: defaultReuse;
  184. }
  185. - (void)configureCell:(__kindof FLEXTableViewCell *)cell forRow:(NSInteger)row {
  186. cell.titleLabel.text = [self titleForRow:row];
  187. cell.titleLabel.numberOfLines = self.numberOfLines;
  188. cell.subtitleLabel.text = [self subtitleForRow:row];
  189. cell.subtitleLabel.numberOfLines = self.numberOfLines;
  190. cell.accessoryType = [self accessoryTypeForRow:row];
  191. }
  192. - (NSString *)titleForRow:(NSInteger)row {
  193. return self.titles[row];
  194. }
  195. - (NSString *)subtitleForRow:(NSInteger)row {
  196. // Case: dynamic, uncached subtitles
  197. if (!self.cacheSubtitles) {
  198. NSString *subtitle = [self.shortcuts[row] subtitleWith:self.object];
  199. return subtitle.length ? subtitle : nil;
  200. }
  201. // Case: static subtitles, or cached subtitles
  202. return self.subtitles[row];
  203. }
  204. @end
  205. #pragma mark - Global shortcut registration
  206. @interface FLEXShortcutsFactory () {
  207. BOOL _append, _prepend, _replace, _notInstance;
  208. NSArray<NSString *> *_properties, *_ivars, *_methods;
  209. }
  210. @end
  211. #define NewAndSet(ivar) ({ FLEXShortcutsFactory *r = [self sharedFactory]; r->ivar = YES; r; })
  212. #define SetIvar(ivar) ({ self->ivar = YES; self; })
  213. #define SetParamBlock(ivar) ^(NSArray *p) { self->ivar = p; return self; }
  214. typedef NSMutableDictionary<Class, NSMutableArray<id<FLEXRuntimeMetadata>> *> RegistrationBuckets;
  215. @implementation FLEXShortcutsFactory {
  216. // Class buckets
  217. RegistrationBuckets *cProperties;
  218. RegistrationBuckets *cIvars;
  219. RegistrationBuckets *cMethods;
  220. // Metaclass buckets
  221. RegistrationBuckets *mProperties;
  222. RegistrationBuckets *mMethods;
  223. }
  224. + (instancetype)sharedFactory {
  225. static FLEXShortcutsFactory *shared = nil;
  226. static dispatch_once_t onceToken;
  227. dispatch_once(&onceToken, ^{
  228. shared = [self new];
  229. });
  230. return shared;
  231. }
  232. - (id)init {
  233. self = [super init];
  234. if (self) {
  235. cProperties = [NSMutableDictionary new];
  236. cIvars = [NSMutableDictionary new];
  237. cMethods = [NSMutableDictionary new];
  238. mProperties = [NSMutableDictionary new];
  239. mMethods = [NSMutableDictionary new];
  240. }
  241. return self;
  242. }
  243. + (NSArray<id<FLEXRuntimeMetadata>> *)shortcutsForObjectOrClass:(id)objectOrClass {
  244. return [[self sharedFactory] shortcutsForObjectOrClass:objectOrClass];
  245. }
  246. - (NSArray<id<FLEXRuntimeMetadata>> *)shortcutsForObjectOrClass:(id)objectOrClass {
  247. NSMutableArray<id<FLEXRuntimeMetadata>> *shortcuts = [NSMutableArray new];
  248. BOOL isClass = object_isClass(objectOrClass);
  249. // The -class does not give you a metaclass, and we want a metaclass
  250. // if a class is passed in, or a class if an object is passed in
  251. Class classKey = object_getClass(objectOrClass);
  252. RegistrationBuckets *propertyBucket = isClass ? mProperties : cProperties;
  253. RegistrationBuckets *methodBucket = isClass ? mMethods : cMethods;
  254. RegistrationBuckets *ivarBucket = isClass ? nil : cIvars;
  255. BOOL stop = NO;
  256. while (!stop && classKey) {
  257. NSArray *properties = propertyBucket[classKey];
  258. NSArray *ivars = ivarBucket[classKey];
  259. NSArray *methods = methodBucket[classKey];
  260. // Stop if we found anything
  261. stop = properties || ivars || methods;
  262. if (stop) {
  263. // Add things we found to the list
  264. [shortcuts addObjectsFromArray:properties];
  265. [shortcuts addObjectsFromArray:ivars];
  266. [shortcuts addObjectsFromArray:methods];
  267. } else {
  268. classKey = class_getSuperclass(classKey);
  269. }
  270. }
  271. [FLEXObjectExplorer configureDefaultsForItems:shortcuts];
  272. return shortcuts;
  273. }
  274. + (FLEXShortcutsFactory *)append {
  275. return NewAndSet(_append);
  276. }
  277. + (FLEXShortcutsFactory *)prepend {
  278. return NewAndSet(_prepend);
  279. }
  280. + (FLEXShortcutsFactory *)replace {
  281. return NewAndSet(_replace);
  282. }
  283. - (void)_register:(NSArray<id<FLEXRuntimeMetadata>> *)items to:(RegistrationBuckets *)global class:(Class)key {
  284. @synchronized (self) {
  285. // Get (or initialize) the bucket for this class
  286. NSMutableArray *bucket = ({
  287. id bucket = global[key];
  288. if (!bucket) {
  289. bucket = [NSMutableArray new];
  290. global[(id)key] = bucket;
  291. }
  292. bucket;
  293. });
  294. if (self->_append) { [bucket addObjectsFromArray:items]; }
  295. if (self->_replace) { [bucket setArray:items]; }
  296. if (self->_prepend) {
  297. if (bucket.count) {
  298. // Set new items as array, add old items behind them
  299. id copy = bucket.copy;
  300. [bucket setArray:items];
  301. [bucket addObjectsFromArray:copy];
  302. } else {
  303. [bucket addObjectsFromArray:items];
  304. }
  305. }
  306. [self reset];
  307. }
  308. }
  309. - (void)reset {
  310. _append = NO;
  311. _prepend = NO;
  312. _replace = NO;
  313. _notInstance = NO;
  314. _properties = nil;
  315. _ivars = nil;
  316. _methods = nil;
  317. }
  318. - (FLEXShortcutsFactory *)class {
  319. return SetIvar(_notInstance);
  320. }
  321. - (FLEXShortcutsFactoryNames)properties {
  322. NSAssert(!_notInstance, @"Do not try to set properties+classProperties at the same time");
  323. return SetParamBlock(_properties);
  324. }
  325. - (FLEXShortcutsFactoryNames)classProperties {
  326. _notInstance = YES;
  327. return SetParamBlock(_properties);
  328. }
  329. - (FLEXShortcutsFactoryNames)ivars {
  330. return SetParamBlock(_ivars);
  331. }
  332. - (FLEXShortcutsFactoryNames)methods {
  333. NSAssert(!_notInstance, @"Do not try to set methods+classMethods at the same time");
  334. return SetParamBlock(_methods);
  335. }
  336. - (FLEXShortcutsFactoryNames)classMethods {
  337. _notInstance = YES;
  338. return SetParamBlock(_methods);
  339. }
  340. - (FLEXShortcutsFactoryTarget)forClass {
  341. return ^(Class cls) {
  342. NSAssert(
  343. ( self->_append && !self->_prepend && !self->_replace) ||
  344. (!self->_append && self->_prepend && !self->_replace) ||
  345. (!self->_append && !self->_prepend && self->_replace),
  346. @"You can only do one of [append, prepend, replace]"
  347. );
  348. /// Whether the metadata we're about to add is instance or
  349. /// class metadata, i.e. class properties vs instance properties
  350. BOOL instanceMetadata = !self->_notInstance;
  351. /// Whether the given class is a metaclass or not; we need to switch to
  352. /// the metaclass to add class metadata if we are given the normal class object
  353. BOOL isMeta = class_isMetaClass(cls);
  354. /// Whether the shortcuts we're about to add should appear for classes or instances
  355. BOOL instanceShortcut = !isMeta;
  356. if (instanceMetadata) {
  357. NSAssert(!isMeta,
  358. @"Instance metadata can only be added as an instance shortcut"
  359. );
  360. }
  361. Class metaclass = isMeta ? cls : object_getClass(cls);
  362. Class clsForMetadata = instanceMetadata ? cls : metaclass;
  363. // The factory is a singleton so we don't need to worry about "leaking" it
  364. #pragma clang diagnostic push
  365. #pragma clang diagnostic ignored "-Wimplicit-retain-self"
  366. RegistrationBuckets *propertyBucket = instanceShortcut ? cProperties : mProperties;
  367. RegistrationBuckets *methodBucket = instanceShortcut ? cMethods : mMethods;
  368. RegistrationBuckets *ivarBucket = instanceShortcut ? cIvars : nil;
  369. #pragma clang diagnostic pop
  370. if (self->_properties) {
  371. NSArray *items = [self->_properties flex_mapped:^id(NSString *name, NSUInteger idx) {
  372. return [FLEXProperty named:name onClass:clsForMetadata];
  373. }];
  374. [self _register:items to:propertyBucket class:cls];
  375. }
  376. if (self->_methods) {
  377. NSArray *items = [self->_methods flex_mapped:^id(NSString *name, NSUInteger idx) {
  378. return [FLEXMethod selector:NSSelectorFromString(name) class:clsForMetadata];
  379. }];
  380. [self _register:items to:methodBucket class:cls];
  381. }
  382. if (self->_ivars) {
  383. NSAssert(instanceMetadata, @"Instance metadata can only be added as an instance shortcut (%@)", cls);
  384. NSArray *items = [self->_ivars flex_mapped:^id(NSString *name, NSUInteger idx) {
  385. return [FLEXIvar named:name onClass:clsForMetadata];
  386. }];
  387. [self _register:items to:ivarBucket class:cls];
  388. }
  389. };
  390. }
  391. @end