123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- //
- // FLEXObjectExplorerViewController.m
- // Flipboard
- //
- // Created by Ryan Olson on 2014-05-03.
- // Copyright (c) 2020 FLEX Team. All rights reserved.
- //
- #import "FLEXObjectExplorerViewController.h"
- #import "FLEXUtility.h"
- #import "FLEXRuntimeUtility.h"
- #import "UIBarButtonItem+FLEX.h"
- #import "FLEXMultilineTableViewCell.h"
- #import "FLEXObjectExplorerFactory.h"
- #import "FLEXFieldEditorViewController.h"
- #import "FLEXMethodCallingViewController.h"
- #import "FLEXObjectListViewController.h"
- #import "FLEXTabsViewController.h"
- #import "FLEXBookmarkManager.h"
- #import "FLEXTableView.h"
- #import "FLEXResources.h"
- #import "FLEXTableViewCell.h"
- #import "FLEXScopeCarousel.h"
- #import "FLEXMetadataSection.h"
- #import "FLEXSingleRowSection.h"
- #import "FLEXShortcutsSection.h"
- #import "NSUserDefaults+FLEX.h"
- #import <objc/runtime.h>
- #import <TargetConditionals.h>
- #pragma mark - Private properties
- @interface FLEXObjectExplorerViewController () <UIGestureRecognizerDelegate>{
- BOOL _addedSwipeGestures;
- }
- @property (nonatomic, readonly) FLEXSingleRowSection *descriptionSection;
- @property (nonatomic, readonly) FLEXTableViewSection *customSection;
- @property (nonatomic) NSIndexSet *customSectionVisibleIndexes;
- @property (nonatomic, readonly) NSArray<NSString *> *observedNotifications;
- @end
- @implementation FLEXObjectExplorerViewController
- #pragma mark - Initialization
- + (instancetype)exploringObject:(id)target {
- return [self exploringObject:target customSection:[FLEXShortcutsSection forObject:target]];
- }
- + (instancetype)exploringObject:(id)target customSection:(FLEXTableViewSection *)section {
- return [[self alloc]
- initWithObject:target
- explorer:[FLEXObjectExplorer forObject:target]
- customSection:section
- ];
- }
- - (id)initWithObject:(id)target
- explorer:(__kindof FLEXObjectExplorer *)explorer
- customSection:(FLEXTableViewSection *)customSection {
- NSParameterAssert(target);
-
- self = [super initWithStyle:UITableViewStyleGrouped];
- if (self) {
- _object = target;
- _explorer = explorer;
- _customSection = customSection;
- }
- return self;
- }
- - (NSArray<NSString *> *)observedNotifications {
- return @[
- kFLEXDefaultsHidePropertyIvarsKey,
- kFLEXDefaultsHidePropertyMethodsKey,
- kFLEXDefaultsHideMethodOverridesKey,
- kFLEXDefaultsHideVariablePreviewsKey,
- ];
- }
- #pragma mark - View controller lifecycle
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.showsShareToolbarItem = YES;
- self.wantsSectionIndexTitles = YES;
- // Use [object class] here rather than object_getClass
- // to avoid the KVO prefix for observed objects
- self.title = [FLEXRuntimeUtility safeClassNameForObject:self.object];
- // Search
- self.showsSearchBar = YES;
- self.searchBarDebounceInterval = kFLEXDebounceInstant;
- self.showsCarousel = YES;
- // Carousel scope bar
- [self.explorer reloadClassHierarchy];
- self.carousel.items = [self.explorer.classHierarchyClasses flex_mapped:^id(Class cls, NSUInteger idx) {
- return NSStringFromClass(cls);
- }];
-
- // ... button for extra options
- [self addToolbarItems:@[[UIBarButtonItem
- flex_itemWithImage:FLEXResources.moreIcon target:self action:@selector(moreButtonPressed:)
- ]]];
- // Swipe gestures to swipe between classes in the hierarchy
- UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleSwipeGesture:)
- ];
- UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleSwipeGesture:)
- ];
- leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
- rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
- leftSwipe.delegate = self;
- rightSwipe.delegate = self;
- [self.tableView addGestureRecognizer:leftSwipe];
- [self.tableView addGestureRecognizer:rightSwipe];
-
- // Observe preferences which may change on other screens
- //
- // "If your app targets iOS 9.0 and later or macOS 10.11 and later,
- // you don't need to unregister an observer in its dealloc method."
- for (NSString *pref in self.observedNotifications) {
- [NSNotificationCenter.defaultCenter
- addObserver:self
- selector:@selector(fullyReloadData)
- name:pref
- object:nil
- ];
- }
- #if TARGET_OS_TV
- [self addlongPressGestureRecognizer];
- #endif
- }
- - (void)addlongPressGestureRecognizer {
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
- longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
- [self.tableView addGestureRecognizer:longPress];
- UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
- rightTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeRightArrow]];
- [self.tableView addGestureRecognizer:rightTap];
- }
- - (void)longPress:(UILongPressGestureRecognizer*)gesture {
- if ( gesture.state == UIGestureRecognizerStateEnded) {
- NSLog(@"do something different for long press!");
- UITableView *tv = [self tableView];
- //naughty naughty
- NSIndexPath *focus = [tv valueForKey:@"_focusedCellIndexPath"];
- NSLog(@"[FLEX] focusedIndexPath: %@", focus);
- [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:focus];
- }
- }
- - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
- #if !TARGET_OS_TV
- [self.navigationController setToolbarHidden:NO animated:YES];
- #endif
- return YES;
- }
- #pragma mark - Overrides
- /// Override to hide the description section when searching
- - (NSArray<FLEXTableViewSection *> *)nonemptySections {
- if (self.shouldShowDescription) {
- return super.nonemptySections;
- }
-
- return [super.nonemptySections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
- return section != self.descriptionSection;
- }];
- }
- - (NSArray<FLEXTableViewSection *> *)makeSections {
- FLEXObjectExplorer *explorer = self.explorer;
-
- // Description section is only for instances
- if (self.explorer.objectIsInstance) {
- _descriptionSection = [FLEXSingleRowSection
- title:@"Description" reuse:kFLEXMultilineCell cell:^(FLEXTableViewCell *cell) {
- cell.titleLabel.font = UIFont.flex_defaultTableCellFont;
- cell.titleLabel.text = explorer.objectDescription;
- }
- ];
- self.descriptionSection.filterMatcher = ^BOOL(NSString *filterText) {
- return [explorer.objectDescription localizedCaseInsensitiveContainsString:filterText];
- };
- }
- // Object graph section
- FLEXSingleRowSection *referencesSection = [FLEXSingleRowSection
- title:@"Object Graph" reuse:kFLEXDefaultCell cell:^(FLEXTableViewCell *cell) {
- cell.titleLabel.text = @"See Objects with References to This Object";
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }
- ];
- referencesSection.selectionAction = ^(UIViewController *host) {
- UIViewController *references = [FLEXObjectListViewController
- objectsWithReferencesToObject:explorer.object
- ];
- [host.navigationController pushViewController:references animated:YES];
- };
- NSMutableArray *sections = [NSMutableArray arrayWithArray:@[
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProperties],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassProperties],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindIvars],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindMethods],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassMethods],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassHierarchy],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProtocols],
- [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindOther],
- referencesSection
- ]];
- if (self.customSection) {
- [sections insertObject:self.customSection atIndex:0];
- }
- if (self.descriptionSection) {
- [sections insertObject:self.descriptionSection atIndex:0];
- }
- return sections.copy;
- }
- /// In our case, all this does is reload the table view,
- /// or reload the sections' data if we changed places
- /// in the class hierarchy. Doesn't refresh \c self.explorer
- - (void)reloadData {
- // Check to see if class scope changed, update accordingly
- if (self.explorer.classScope != self.selectedScope) {
- self.explorer.classScope = self.selectedScope;
- [self reloadSections];
- }
-
- [super reloadData];
- }
- - (void)shareButtonPressed:(UIBarButtonItem *)sender {
- [FLEXAlert makeSheet:^(FLEXAlert *make) {
- make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
- [FLEXBookmarkManager.bookmarks addObject:self.object];
- });
- make.button(@"Copy Description").handler(^(NSArray<NSString *> *strings) {
- #if !TARGET_OS_TV
- UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
- #endif
- });
- make.button(@"Copy Address").handler(^(NSArray<NSString *> *strings) {
- #if !TARGET_OS_TV
- UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
- #endif
- });
- make.button(@"Cancel").cancelStyle();
- } showFrom:self source:sender];
- }
- #pragma mark - Private
- /// Unlike \c -reloadData, this refreshes everything, including the explorer.
- - (void)fullyReloadData {
- [self.explorer reloadMetadata];
- [self reloadSections];
- [self reloadData];
- }
- - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
- if (gesture.state == UIGestureRecognizerStateEnded) {
- switch (gesture.direction) {
- case UISwipeGestureRecognizerDirectionRight:
- if (self.selectedScope > 0) {
- self.selectedScope -= 1;
- [self.tableView reloadData];
- }
- break;
- case UISwipeGestureRecognizerDirectionLeft:
- if (self.selectedScope != self.explorer.classHierarchy.count - 1) {
- self.selectedScope += 1;
- [self.tableView reloadData];
- }
- break;
- default:
- break;
- }
- }
- }
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
- // Prioritize important pan gestures over our swipe gesture
- #if !TARGET_OS_TV
- if ([g2 isKindOfClass:[UIPanGestureRecognizer class]]) {
- if (g2 == self.navigationController.interactivePopGestureRecognizer ||
- g2 == self.navigationController.barHideOnSwipeGestureRecognizer ||
- g2 == self.tableView.panGestureRecognizer) {
- return NO;
- }
- }
- #else
- if ([g2 isKindOfClass:[UIPanGestureRecognizer class]]) {
- if (g2 == self.tableView.panGestureRecognizer) {
- return NO;
- }
- }
- #endif
-
- return YES;
- }
- - (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gesture {
- // Don't allow swiping from the carousel
- CGPoint location = [gesture locationInView:self.tableView];
- if ([self.carousel hitTest:location withEvent:nil]) {
- return NO;
- }
-
- return YES;
- }
-
- - (void)moreButtonPressed:(UIBarButtonItem *)sender {
- NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
- // Maps preference keys to a description of what they affect
- NSDictionary<NSString *, NSString *> *explorerToggles = @{
- kFLEXDefaultsHidePropertyIvarsKey: @"Property-Backing Ivars",
- kFLEXDefaultsHidePropertyMethodsKey: @"Property-Backing Methods",
- kFLEXDefaultsHideMethodOverridesKey: @"Method Overrides",
- kFLEXDefaultsHideVariablePreviewsKey: @"Variable Previews"
- };
-
- // Maps the key of the action itself to a map of a description
- // of the action ("hide X") mapped to the current state.
- //
- // So keys that are hidden by default have NO mapped to "Show"
- NSDictionary<NSString *, NSDictionary *> *nextStateDescriptions = @{
- kFLEXDefaultsHidePropertyIvarsKey: @{ @NO: @"Hide ", @YES: @"Show " },
- kFLEXDefaultsHidePropertyMethodsKey: @{ @NO: @"Hide ", @YES: @"Show " },
- kFLEXDefaultsHideMethodOverridesKey: @{ @NO: @"Show ", @YES: @"Hide " },
- kFLEXDefaultsHideVariablePreviewsKey: @{ @NO: @"Hide ", @YES: @"Show " },
- };
-
- [FLEXAlert makeSheet:^(FLEXAlert *make) {
- make.title(@"Options");
-
- for (NSString *option in explorerToggles.allKeys) {
- BOOL current = [defaults boolForKey:option];
- NSString *title = [nextStateDescriptions[option][@(current)]
- stringByAppendingString:explorerToggles[option]
- ];
- make.button(title).handler(^(NSArray<NSString *> *strings) {
- [NSUserDefaults.standardUserDefaults flex_toggleBoolForKey:option];
- [self fullyReloadData];
- });
- }
-
- make.button(@"Cancel").cancelStyle();
- } showFrom:self source:sender];
- }
- #pragma mark - Description
- - (BOOL)shouldShowDescription {
- // Hide if we have filter text; it is rarely
- // useful to see the description when searching
- // since it's already at the top of the screen
- if (self.filterText.length) {
- return NO;
- }
- return YES;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- // For the description section, we want that nice slim/snug looking row.
- // Other rows use the automatic size.
- FLEXTableViewSection *section = self.filterDelegate.sections[indexPath.section];
-
- if (section == self.descriptionSection) {
- NSAttributedString *attributedText = [[NSAttributedString alloc]
- initWithString:self.explorer.objectDescription
- attributes:@{ NSFontAttributeName : UIFont.flex_defaultTableCellFont }
- ];
-
- return [FLEXMultilineTableViewCell
- preferredHeightWithAttributedText:attributedText
- maxWidth:tableView.frame.size.width - tableView.separatorInset.right
- style:tableView.style
- showsAccessory:NO
- ];
- }
- return UITableViewAutomaticDimension;
- }
- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
- return self.filterDelegate.sections[indexPath.section] == self.descriptionSection;
- }
- - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
- // Only the description section has "actions"
- if (self.filterDelegate.sections[indexPath.section] == self.descriptionSection) {
- return action == @selector(copy:);
- }
- return NO;
- }
- - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
- if (action == @selector(copy:)) {
- #if !TARGET_OS_TV
- UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
- #endif
- }
- }
- @end
|