FLEXShortcutsSection.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // FLEXShortcutsSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/29/19.
  6. // Copyright © 2019 Flipboard. 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:)]) {
  163. id object = self.object;
  164. return ^(UIViewController *host) {
  165. UIViewController *editor = [shortcut editorWith:object];
  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 new]; r->ivar = YES; r; })
  205. #define SetIvar(ivar) ({ self->ivar = YES; self; })
  206. #define SetParamBlock(ivar) ^(NSArray *p) { self->ivar = p; return self; }
  207. @implementation FLEXShortcutsFactory
  208. typedef NSMutableDictionary<Class, NSMutableArray<id<FLEXRuntimeMetadata>> *> RegistrationBuckets;
  209. // Class buckets
  210. static RegistrationBuckets *cProperties = nil;
  211. static RegistrationBuckets *cIvars = nil;
  212. static RegistrationBuckets *cMethods = nil;
  213. // Metaclass buckets
  214. static RegistrationBuckets *mProperties = nil;
  215. static RegistrationBuckets *mMethods = nil;
  216. + (void)load {
  217. cProperties = [NSMutableDictionary new];
  218. cIvars = [NSMutableDictionary new];
  219. cMethods = [NSMutableDictionary new];
  220. mProperties = [NSMutableDictionary new];
  221. mMethods = [NSMutableDictionary new];
  222. }
  223. + (NSArray<id<FLEXRuntimeMetadata>> *)shortcutsForObjectOrClass:(id)objectOrClass {
  224. NSMutableArray<id<FLEXRuntimeMetadata>> *shortcuts = [NSMutableArray new];
  225. BOOL isClass = object_isClass(objectOrClass);
  226. // The -class does not give you a metaclass, and we want a metaclass
  227. // if a class is passed in, or a class if an object is passed in
  228. Class classKey = object_getClass(objectOrClass);
  229. RegistrationBuckets *propertyBucket = isClass ? mProperties : cProperties;
  230. RegistrationBuckets *methodBucket = isClass ? mMethods : cMethods;
  231. RegistrationBuckets *ivarBucket = isClass ? nil : cIvars;
  232. BOOL stop = NO;
  233. while (!stop && classKey) {
  234. NSArray *properties = propertyBucket[classKey];
  235. NSArray *ivars = ivarBucket[classKey];
  236. NSArray *methods = methodBucket[classKey];
  237. // Stop if we found anything
  238. stop = properties || ivars || methods;
  239. if (stop) {
  240. // Add things we found to the list
  241. [shortcuts addObjectsFromArray:properties];
  242. [shortcuts addObjectsFromArray:ivars];
  243. [shortcuts addObjectsFromArray:methods];
  244. } else {
  245. classKey = class_getSuperclass(classKey);
  246. }
  247. }
  248. [FLEXObjectExplorer configureDefaultsForItems:shortcuts];
  249. return shortcuts;
  250. }
  251. + (FLEXShortcutsFactory *)append {
  252. return NewAndSet(_append);
  253. }
  254. + (FLEXShortcutsFactory *)prepend {
  255. return NewAndSet(_prepend);
  256. }
  257. + (FLEXShortcutsFactory *)replace {
  258. return NewAndSet(_replace);
  259. }
  260. - (void)_register:(NSArray<id<FLEXRuntimeMetadata>> *)items to:(RegistrationBuckets *)global class:(Class)key {
  261. // Get (or initialize) the bucket for this class
  262. NSMutableArray *bucket = ({
  263. id bucket = global[key];
  264. if (!bucket) {
  265. bucket = [NSMutableArray new];
  266. global[(id)key] = bucket;
  267. }
  268. bucket;
  269. });
  270. if (self->_append) { [bucket addObjectsFromArray:items]; }
  271. if (self->_replace) { [bucket setArray:items]; }
  272. if (self->_prepend) {
  273. if (bucket.count) {
  274. // Set new items as array, add old items behind them
  275. id copy = bucket.copy;
  276. [bucket setArray:items];
  277. [bucket addObjectsFromArray:copy];
  278. } else {
  279. [bucket addObjectsFromArray:items];
  280. }
  281. }
  282. }
  283. - (FLEXShortcutsFactory *)class {
  284. return SetIvar(_notInstance);
  285. }
  286. - (FLEXShortcutsFactoryNames)properties {
  287. NSAssert(!_notInstance, @"Do not try to set properties+classProperties at the same time");
  288. return SetParamBlock(_properties);
  289. }
  290. - (FLEXShortcutsFactoryNames)classProperties {
  291. _notInstance = YES;
  292. return SetParamBlock(_properties);
  293. }
  294. - (FLEXShortcutsFactoryNames)ivars {
  295. return SetParamBlock(_ivars);
  296. }
  297. - (FLEXShortcutsFactoryNames)methods {
  298. NSAssert(!_notInstance, @"Do not try to set methods+classMethods at the same time");
  299. return SetParamBlock(_methods);
  300. }
  301. - (FLEXShortcutsFactoryNames)classMethods {
  302. _notInstance = YES;
  303. return SetParamBlock(_methods);
  304. }
  305. - (FLEXShortcutsFactoryTarget)forClass {
  306. return ^(Class cls) {
  307. NSAssert(
  308. ( self->_append && !self->_prepend && !self->_replace) ||
  309. (!self->_append && self->_prepend && !self->_replace) ||
  310. (!self->_append && !self->_prepend && self->_replace),
  311. @"You can only do one of [append, prepend, replace]"
  312. );
  313. /// Whether the metadata we're about to add is instance or
  314. /// class metadata, i.e. class properties vs instance properties
  315. BOOL instanceMetadata = !self->_notInstance;
  316. /// Whether the given class is a metaclass or not; we need to switch to
  317. /// the metaclass to add class metadata if we are given the normal class object
  318. BOOL isMeta = class_isMetaClass(cls);
  319. /// Whether the shortcuts we're about to add should appear for classes or instances
  320. BOOL instanceShortcut = !isMeta;
  321. if (instanceMetadata) {
  322. NSAssert(!isMeta,
  323. @"Instance metadata can only be added as an instance shortcut"
  324. );
  325. }
  326. Class metaclass = isMeta ? cls : object_getClass(cls);
  327. Class clsForMetadata = instanceMetadata ? cls : metaclass;
  328. RegistrationBuckets *propertyBucket = instanceShortcut ? cProperties : mProperties;
  329. RegistrationBuckets *methodBucket = instanceShortcut ? cMethods : mMethods;
  330. RegistrationBuckets *ivarBucket = instanceShortcut ? cIvars : nil;
  331. if (self->_properties) {
  332. NSArray *items = [self->_properties flex_mapped:^id(NSString *name, NSUInteger idx) {
  333. return [FLEXProperty named:name onClass:clsForMetadata];
  334. }];
  335. [self _register:items to:propertyBucket class:cls];
  336. }
  337. if (self->_methods) {
  338. NSArray *items = [self->_methods flex_mapped:^id(NSString *name, NSUInteger idx) {
  339. return [FLEXMethod selector:NSSelectorFromString(name) class:clsForMetadata];
  340. }];
  341. [self _register:items to:methodBucket class:cls];
  342. }
  343. if (self->_ivars) {
  344. NSAssert(instanceMetadata, @"Instance metadata can only be added as an instance shortcut (%@)", cls);
  345. NSArray *items = [self->_ivars flex_mapped:^id(NSString *name, NSUInteger idx) {
  346. return [FLEXIvar named:name onClass:clsForMetadata];
  347. }];
  348. [self _register:items to:ivarBucket class:cls];
  349. }
  350. };
  351. }
  352. @end