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

Properly deprecate UITextItemInteraction for iOS 17 #2086

Merged
merged 2 commits into from
Sep 1, 2024
Merged
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 @@ -133,11 +133,9 @@ extension TableViewHeaderFooterViewDemoController {
}
footer?.setup(style: .footer, attributedTitle: title)

#if os(iOS)
if section.hasCustomLinkHandler {
footer?.delegate = self
}
#endif
}
footer?.titleNumberOfLines = section.numberOfLines
footer?.tokenSet.replaceAllOverrides(with: overrideTokens)
Expand Down Expand Up @@ -165,16 +163,24 @@ extension TableViewHeaderFooterViewDemoController {

// MARK: - TableViewHeaderFooterViewDemoController: TableViewHeaderFooterViewDelegate

#if os(iOS)
extension TableViewHeaderFooterViewDemoController: TableViewHeaderFooterViewDelegate {
@available (visionOS, deprecated: 1.0)
func headerFooterView(_ headerFooterView: TableViewHeaderFooterView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
let alertController = UIAlertController(title: "Link tapped", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
return false
}

@available(iOS, introduced: 17)
func headerFooterView(_ headerFooterView: TableViewHeaderFooterView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
return UIAction { [weak self] _ in
let alertController = UIAlertController(title: "Link tapped", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self?.present(alertController, animated: true, completion: nil)
}
}
}
#endif

extension TableViewHeaderFooterViewDemoController: DemoAppearanceDelegate {
func themeWideOverrideDidChange(isOverrideEnabled: Bool) {
Expand Down
43 changes: 35 additions & 8 deletions ios/FluentUI/Table View/TableViewHeaderFooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,34 @@ import UIKit

// MARK: TableViewHeaderFooterViewDelegate

#if os(iOS)
@objc(MSFTableViewHeaderFooterViewDelegate)
public protocol TableViewHeaderFooterViewDelegate: AnyObject {
/// Returns: true if the interaction with the header view should be allowed; false if the interaction should not be allowed.
@available(iOS, deprecated: 17, message: "Replaced by primaryActionForTextItem: and menuConfigurationForTextItem: for additional customization options.")
@available(visionOS, deprecated: 1, message: "Replaced by primaryActionForTextItem: and menuConfigurationForTextItem: for additional customization options.")
@objc optional func headerFooterView(_ headerFooterView: TableViewHeaderFooterView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool

/// Asks the delegate for the action to be performed when interacting with a text item. If a nil action is provided, the text view
/// will request a menu to be presented on primary action if possible.
///
/// @param headerFooterView The `TableViewHeaderFooterView` requesting the primary action.
/// @param textItem The text item for performing said action.
/// @param defaultAction The default action for the text item. Return this to perform the default action.
///
/// @return Return a UIAction to be performed when the text item is interacted with. Return @c nil to prevent the action from being performed.
@available(iOS, introduced: 17)
@objc optional func headerFooterView(_ headerFooterView: TableViewHeaderFooterView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction?

/// Asks the delegate for the menu configuration to be performed when interacting with a text item.
///
/// @param headerFooterView The `TableViewHeaderFooterView` requesting the menu.
/// @param textItem The text item for performing said action.
/// @param defaultMenu The default menu for the specified text item.
///
/// @return Return a menu configuration to be presented when the text item is interacted with. Return @c nil to prevent the menu from being presented.
@available(iOS, introduced: 17)
@objc optional func headerFooterView(_ headerFooterView: TableViewHeaderFooterView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration?
}
#endif // os(iOS)

// MARK: - TableViewHeaderFooterView

Expand Down Expand Up @@ -125,9 +146,7 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
}
}

#if os(iOS)
@objc public weak var delegate: TableViewHeaderFooterViewDelegate?
#endif // os(iOS)

open override var intrinsicContentSize: CGSize {
return CGSize(
Expand Down Expand Up @@ -407,9 +426,7 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
open override func prepareForReuse() {
super.prepareForReuse()

#if os(iOS)
delegate = nil
#endif // os(iOS)

accessoryButtonStyle = .regular
titleNumberOfLines = 1
Expand Down Expand Up @@ -553,12 +570,22 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
// MARK: - TableViewHeaderFooterView: UITextViewDelegate

extension TableViewHeaderFooterView: UITextViewDelegate {
#if os(iOS)
@available(iOS, deprecated: 17, message: "Replaced by primaryActionForTextItem: and menuConfigurationForTextItem: for additional customization options.")
@available(visionOS, deprecated: 1, message: "Replaced by primaryActionForTextItem: and menuConfigurationForTextItem: for additional customization options.")
public func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
// If the delegate function is not set, return `true` to let the default interaction handle this
return delegate?.headerFooterView?(self, shouldInteractWith: URL, in: characterRange, interaction: interaction) ?? true
}
#endif // os(iOS)

@available(iOS, introduced: 17)
public func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
return delegate?.headerFooterView?(self, primaryActionFor: textItem, defaultAction: defaultAction) ?? defaultAction
}

@available(iOS, introduced: 17)
public func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? {
return delegate?.headerFooterView?(self, menuConfigurationFor: textItem, defaultMenu: defaultMenu) ?? .init(menu: defaultMenu)
}
}

// MARK: - TableViewHeaderFooterTitleView
Expand Down
Loading