Skip to content

Commit

Permalink
Bug fix: iPad support for FLEXAlert action sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
NSExceptional committed Apr 6, 2020
1 parent 88c7ca9 commit 2ca563f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Classes/Core/Controllers/FLEXTableViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ extern CGFloat const kFLEXDebounceForExpensiveIO;
@property (nonatomic) BOOL showsShareToolbarItem;
/// Called when the share button is pressed.
/// Default implementation does nothign. Subclasses may override.
- (void)shareButtonPressed;
- (void)shareButtonPressed:(UIBarButtonItem *)sender;

/// Subclasses may call this to opt-out of all toolbar related behavior.
/// This is necessary if you want to disable the gesture which reveals the toolbar.
Expand Down
4 changes: 2 additions & 2 deletions Classes/Core/Controllers/FLEXTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ - (void)loadView {
self.tableView.dataSource = self;
self.tableView.delegate = self;

_shareToolbarItem = FLEXBarButtonItemSystem(Action, self, @selector(shareButtonPressed));
_shareToolbarItem = FLEXBarButtonItemSystem(Action, self, @selector(shareButtonPressed:));
_bookmarksToolbarItem = [UIBarButtonItem
itemWithImage:FLEXResources.bookmarksIcon target:self action:@selector(showBookmarks)
];
Expand Down Expand Up @@ -386,7 +386,7 @@ - (void)setShowsShareToolbarItem:(BOOL)showShare {
[self setupToolbarItems];
}

- (void)shareButtonPressed {
- (void)shareButtonPressed:(UIBarButtonItem *)sender {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (void)setupDefaultBarItems {
- (void)setupEditingBarItems {
self.navigationItem.rightBarButtonItem = nil;
self.toolbarItems = @[
[UIBarButtonItem itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed)],
[UIBarButtonItem itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed:)],
UIBarButtonItem.flex_flexibleSpace,
// We use a non-system done item because we change its title dynamically
[UIBarButtonItem doneStyleitemWithTitle:@"Done" target:self action:@selector(toggleEditing)]
Expand Down Expand Up @@ -149,7 +149,7 @@ - (void)toggleEditing {
}
}

- (void)closeAllButtonPressed {
- (void)closeAllButtonPressed:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
NSInteger count = self.bookmarks.count;
NSString *title = FLEXPluralFormatString(count, @"Remove %@ bookmarks", @"Remove %@ bookmark");
Expand All @@ -158,7 +158,7 @@ - (void)closeAllButtonPressed {
[self toggleEditing];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}

- (void)closeAll {
Expand Down
12 changes: 6 additions & 6 deletions Classes/ExplorerInterface/Tabs/FLEXTabsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ - (void)setupDefaultBarItems {
self.toolbarItems = @[
UIBarButtonItem.flex_fixedSpace,
UIBarButtonItem.flex_flexibleSpace,
FLEXBarButtonItemSystem(Add, self, @selector(addTabButtonPressed)),
FLEXBarButtonItemSystem(Add, self, @selector(addTabButtonPressed:)),
UIBarButtonItem.flex_flexibleSpace,
FLEXBarButtonItemSystem(Edit, self, @selector(toggleEditing)),
];
Expand All @@ -121,7 +121,7 @@ - (void)setupDefaultBarItems {
- (void)setupEditingBarItems {
self.navigationItem.rightBarButtonItem = nil;
self.toolbarItems = @[
[UIBarButtonItem itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed)],
[UIBarButtonItem itemWithTitle:@"Close All" target:self action:@selector(closeAllButtonPressed:)],
UIBarButtonItem.flex_flexibleSpace,
[UIBarButtonItem disabledSystemItem:UIBarButtonSystemItemAdd],
UIBarButtonItem.flex_flexibleSpace,
Expand Down Expand Up @@ -193,7 +193,7 @@ - (void)toggleEditing {
}
}

- (void)addTabButtonPressed {
- (void)addTabButtonPressed:(UIBarButtonItem *)sender {
if (FLEXBookmarkManager.bookmarks.count) {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
make.title(@"New Tab");
Expand All @@ -208,7 +208,7 @@ - (void)addTabButtonPressed {
] animated:YES completion:nil];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
} else {
// No bookmarks, just open the main menu
[self addTabAndDismiss:[FLEXNavigationController
Expand All @@ -224,7 +224,7 @@ - (void)addTabAndDismiss:(UINavigationController *)newTab {
}];
}

- (void)closeAllButtonPressed {
- (void)closeAllButtonPressed:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
NSInteger count = self.openTabs.count;
NSString *title = FLEXPluralFormatString(count, @"Close %@ tabs", @"Close %@ tab");
Expand All @@ -233,7 +233,7 @@ - (void)closeAllButtonPressed {
[self toggleEditing];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}

- (void)closeAll {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "FLEXMutableListSection.h"
#import "FLEXUtility.h"
#import "UIPasteboard+FLEX.h"
#import "UIBarButtonItem+FLEX.h"

@interface FLEXKeychainTableViewController ()
@property (nonatomic, readonly) FLEXMutableListSection<NSDictionary *> *section;
Expand All @@ -30,12 +31,8 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.navigationItem.rightBarButtonItems = @[
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashPressed)
],
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)
],
[UIBarButtonItem systemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashPressed:)],
[UIBarButtonItem systemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)],
];

[self reloadData];
Expand Down Expand Up @@ -127,7 +124,7 @@ - (void)deleteItem:(NSDictionary *)item {

#pragma mark Buttons

- (void)trashPressed {
- (void)trashPressed:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
make.title(@"Clear Keychain");
make.message(@"This will remove all keychain items for this app.\n");
Expand All @@ -140,7 +137,7 @@ - (void)trashPressed {
[self reloadData];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}

- (void)addPressed {
Expand Down
6 changes: 3 additions & 3 deletions Classes/Network/FLEXNetworkMITMViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ - (void)registerForNotifications {

#pragma mark Button Actions

- (void)settingsButtonTapped:(id)sender {
- (void)settingsButtonTapped:(UIBarButtonItem *)sender {
UIViewController *settings = [FLEXNetworkSettingsController new];
settings.navigationItem.rightBarButtonItem = FLEXBarButtonItemSystem(
Done, self, @selector(settingsViewControllerDoneTapped:)
Expand All @@ -121,7 +121,7 @@ - (void)settingsButtonTapped:(id)sender {
[self presentViewController:nav animated:YES completion:nil];
}

- (void)trashButtonTapped:(id)sender {
- (void)trashButtonTapped:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
make.title(@"Clear All Recorded Requests?");
make.message(@"This cannot be undone.");
Expand All @@ -130,7 +130,7 @@ - (void)trashButtonTapped:(id)sender {
make.button(@"Clear All").destructiveStyle().handler(^(NSArray *strings) {
[FLEXNetworkRecorder.defaultRecorder clearRecordedActivity];
});
} showFrom:self];
} showFrom:self source:sender];
}

- (void)settingsViewControllerDoneTapped:(id)sender {
Expand Down
10 changes: 5 additions & 5 deletions Classes/ObjectExplorers/FLEXObjectExplorerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ - (void)viewDidLoad {

// ... button for extra options
[self addToolbarItems:@[[UIBarButtonItem
itemWithImage:FLEXResources.moreIcon target:self action:@selector(moreButtonPressed)
itemWithImage:FLEXResources.moreIcon target:self action:@selector(moreButtonPressed:)
]]];

// Swipe gestures to swipe between classes in the hierarchy
Expand Down Expand Up @@ -217,7 +217,7 @@ - (void)reloadData {
[super reloadData];
}

- (void)shareButtonPressed {
- (void)shareButtonPressed:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
[FLEXBookmarkManager.bookmarks addObject:self.object];
Expand All @@ -229,7 +229,7 @@ - (void)shareButtonPressed {
UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}


Expand Down Expand Up @@ -285,7 +285,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gesture {
return YES;
}

- (void)moreButtonPressed {
- (void)moreButtonPressed:(UIBarButtonItem *)sender {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
// Maps preference keys to a description of what they affect
NSDictionary<NSString *, NSString *> *explorerToggles = @{
Expand Down Expand Up @@ -321,7 +321,7 @@ - (void)moreButtonPressed {
}

make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}

#pragma mark - Description
Expand Down
4 changes: 3 additions & 1 deletion Classes/Utility/FLEXAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ typedef FLEXAlertAction *(^FLEXAlertActionHandler)(void(^handler)(NSArray<NSStri
/// Construct and display an alert
+ (void)makeAlert:(FLEXAlertBuilder)block showFrom:(UIViewController *)viewController;
/// Construct and display an action sheet-style alert
+ (void)makeSheet:(FLEXAlertBuilder)block showFrom:(UIViewController *)viewController;
+ (void)makeSheet:(FLEXAlertBuilder)block
showFrom:(UIViewController *)viewController
source:(id)viewOrBarItem;

/// Construct an alert
+ (UIAlertController *)makeAlert:(FLEXAlertBuilder)block;
Expand Down
35 changes: 30 additions & 5 deletions Classes/Utility/FLEXAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,42 @@ + (UIAlertController *)make:(FLEXAlertBuilder)block withStyle:(UIAlertController
return alert._controller;
}

+ (void)make:(FLEXAlertBuilder)block withStyle:(UIAlertControllerStyle)style showFrom:(UIViewController *)viewController {
+ (void)make:(FLEXAlertBuilder)block
withStyle:(UIAlertControllerStyle)style
showFrom:(UIViewController *)viewController
source:(id)viewOrBarItem {
UIAlertController *alert = [self make:block withStyle:style];
if ([viewOrBarItem isKindOfClass:[UIBarButtonItem class]]) {
alert.popoverPresentationController.barButtonItem = viewOrBarItem;
} else if ([viewOrBarItem isKindOfClass:[UIView class]]) {
alert.popoverPresentationController.sourceView = viewOrBarItem;
alert.popoverPresentationController.sourceRect = [viewOrBarItem bounds];
} else if (viewOrBarItem) {
NSParameterAssert(
[viewOrBarItem isKindOfClass:[UIBarButtonItem class]] ||
[viewOrBarItem isKindOfClass:[UIView class]] ||
!viewOrBarItem
);
}
[viewController presentViewController:alert animated:YES completion:nil];
}

+ (void)makeAlert:(FLEXAlertBuilder)block showFrom:(UIViewController *)viewController {
[self make:block withStyle:UIAlertControllerStyleAlert showFrom:viewController];
+ (void)makeAlert:(FLEXAlertBuilder)block showFrom:(UIViewController *)controller {
[self make:block withStyle:UIAlertControllerStyleAlert showFrom:controller source:nil];
}

+ (void)makeSheet:(FLEXAlertBuilder)block showFrom:(UIViewController *)controller {
[self make:block withStyle:UIAlertControllerStyleActionSheet showFrom:controller source:nil];
}

+ (void)makeSheet:(FLEXAlertBuilder)block showFrom:(UIViewController *)viewController {
[self make:block withStyle:UIAlertControllerStyleActionSheet showFrom:viewController];
/// Construct and display an action sheet-style alert
+ (void)makeSheet:(FLEXAlertBuilder)block
showFrom:(UIViewController *)controller
source:(id)viewOrBarItem {
[self make:block
withStyle:UIAlertControllerStyleActionSheet
showFrom:controller
source:viewOrBarItem];
}

+ (UIAlertController *)makeAlert:(FLEXAlertBuilder)block {
Expand Down
6 changes: 3 additions & 3 deletions Classes/ViewHierarchy/SnapshotExplorer/FHSViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ - (void)setSnapshotView:(FHSSnapshotView *)snapshotView {
UIBarButtonItem.flex_flexibleSpace,
[UIBarButtonItem
itemWithImage:FLEXResources.moreIcon
target:self action:@selector(didPressOptionsButton)
target:self action:@selector(didPressOptionsButton:)
],
UIBarButtonItem.flex_flexibleSpace,
[UIBarButtonItem itemWithCustomView:snapshotView.depthSlider]
Expand Down Expand Up @@ -205,7 +205,7 @@ - (FHSViewSnapshot *)snapshotForView:(UIView *)view {

#pragma mark Events

- (void)didPressOptionsButton {
- (void)didPressOptionsButton:(UIBarButtonItem *)sender {
[FLEXAlert makeSheet:^(FLEXAlert *make) {
if (self.selectedView) {
make.button(@"Hide selected view").handler(^(NSArray<NSString *> *strings) {
Expand All @@ -227,7 +227,7 @@ - (void)didPressOptionsButton {
[self.snapshotView toggleShowBorders];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
} showFrom:self source:sender];
}

- (void)resizeToolbarItems:(CGSize)viewSize {
Expand Down

0 comments on commit 2ca563f

Please sign in to comment.