Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provided support for iOS 13 Context Menus #1430

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,26 @@ final class MonthSectionController: ListBindingSectionController<ListDiffable>,

func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, didUnhighlightItemAt index: Int, viewModel: Any) {}

@available(iOS 13.0, *)
func sectionController(_ sectionController: ListBindingSectionController<ListDiffable>, contextMenuConfigurationForItemAt index: Int, point: CGPoint, viewModel: Any) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
// Create an action for sharing
let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
// Show share sheet
}

// Create an action for copy
let rename = UIAction(title: "Copy", image: UIImage(systemName: "doc.on.doc")) { _ in
// Perform copy
}

// Create an action for delete with destructive attributes (highligh in red)
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
// Perform delete
}

// Create a UIMenu with all the actions as children
return UIMenu(title: "", children: [share, rename, delete])
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Source/IGListKit/IGListBindingSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@ - (void)didUnhighlightItemAtIndex:(NSInteger)index {
[self.selectionDelegate sectionController:self didUnhighlightItemAtIndex:index viewModel:self.viewModels[index]];
}

#if !TARGET_OS_TV
- (UIContextMenuConfiguration * _Nullable)contextMenuConfigurationForItemAtIndex:(NSInteger)index point:(CGPoint)point {
return [self.selectionDelegate sectionController:self contextMenuConfigurationForItemAtIndex:index point:point viewModel:self.viewModels[index]];
}
#endif

@end
15 changes: 15 additions & 0 deletions Source/IGListKit/IGListBindingSectionControllerSelectionDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ NS_SWIFT_NAME(ListBindingSectionControllerSelectionDelegate)
didUnhighlightItemAtIndex:(NSInteger)index
viewModel:(id)viewModel;

/**
Tells the delegate that a cell has requested a menu configuration.

@param sectionController The section controller the request of a menu configuration occurred in.
@param index The index of the cell that is being longed tap.
@param point The point of the tap on the cell.
@param viewModel The view model that was bound to the cell.

@return An object that conforms to `UIContextMenuConfiguration`.
*/
- (UIContextMenuConfiguration * _Nullable)sectionController:(IGListBindingSectionController *)sectionController
Copy link

@sjchmiela sjchmiela Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super familiar with IGListKit, but this seems like an unnecessary forwarding of the call — after all it's IGListBindingSectionController**Selection**Delegate, not a "general delegate" — why would a "delegate that receives selection events from cells" be interested in context menu events?

contextMenuConfigurationForItemAtIndex:(NSInteger)index
point:(CGPoint)point
viewModel:(id)viewModel API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos);

@end

NS_ASSUME_NONNULL_END
12 changes: 12 additions & 0 deletions Source/IGListKit/IGListSectionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ NS_SWIFT_NAME(ListSectionController)
*/
- (void)didUnhighlightItemAtIndex:(NSInteger)index;

/**
Tells the section controller that the cell has requested a menu configuration.

@param index The index of the cell that requested the menu.
@param point The point of the tap on the cell.

@return An object that conforms to `UIContextMenuConfiguration`

@note The default implementation does nothing. **Calling super is not required.**
*/
- (UIContextMenuConfiguration * _Nullable)contextMenuConfigurationForItemAtIndex:(NSInteger)index point:(CGPoint)point API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos);

/**
Identifies whether an object can be moved through interactive reordering.

Expand Down
4 changes: 4 additions & 0 deletions Source/IGListKit/IGListSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ - (void)didHighlightItemAtIndex:(NSInteger)index {}

- (void)didUnhighlightItemAtIndex:(NSInteger)index {}

- (UIContextMenuConfiguration * _Nullable)contextMenuConfigurationForItemAtIndex:(NSInteger)index point:(CGPoint)point {
return nil;
}

- (BOOL)canMoveItemAtIndex:(NSInteger)index {
return NO;
}
Expand Down
13 changes: 13 additions & 0 deletions Source/IGListKit/Internal/IGListAdapter+UICollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIn
[sectionController didUnhighlightItemAtIndex:indexPath.item];
}

#if !TARGET_OS_TV
- (UIContextMenuConfiguration *)collectionView:(UICollectionView *)collectionView contextMenuConfigurationForItemAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point API_AVAILABLE(ios(13.0)) {
// forward this method to the delegate b/c this implementation will steal the message from the proxy
id<UICollectionViewDelegate> collectionViewDelegate = self.collectionViewDelegate;
if ([collectionViewDelegate respondsToSelector:@selector(collectionView:contextMenuConfigurationForItemAtIndexPath:point:)]) {
return [collectionViewDelegate collectionView:collectionView contextMenuConfigurationForItemAtIndexPath:indexPath point:point];
}

IGListSectionController * sectionController = [self sectionControllerForSection:indexPath.section];
return [sectionController contextMenuConfigurationForItemAtIndex:indexPath.item point:point];
}
#endif

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
1 change: 1 addition & 0 deletions Source/IGListKit/Internal/IGListAdapterProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static BOOL isInterceptedSelector(SEL sel) {
sel == @selector(collectionView:didDeselectItemAtIndexPath:) ||
sel == @selector(collectionView:didHighlightItemAtIndexPath:) ||
sel == @selector(collectionView:didUnhighlightItemAtIndexPath:) ||
sel == @selector(collectionView:contextMenuConfigurationForItemAtIndexPath:point:) ||
// UICollectionViewDelegateFlowLayout
sel == @selector(collectionView:layout:sizeForItemAtIndexPath:) ||
sel == @selector(collectionView:layout:insetForSectionAtIndex:) ||
Expand Down
Loading