FLEXShortcutsSection.m 15 KB

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