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

Fixed issue with non selectable links on iOS 13 #606

Merged
merged 6 commits into from
Sep 5, 2019
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 @@ -148,6 +148,7 @@ open class BaseMessageCollectionViewCell<BubbleViewType>: UICollectionViewCell,

public private(set) lazy var tapGestureRecognizer: UITapGestureRecognizer = {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(BaseMessageCollectionViewCell.bubbleTapped(_:)))
tapGestureRecognizer.delegate = self
return tapGestureRecognizer
}()

Expand All @@ -170,6 +171,7 @@ open class BaseMessageCollectionViewCell<BubbleViewType>: UICollectionViewCell,
self.bubbleView.isExclusiveTouch = true
self.bubbleView.addGestureRecognizer(self.tapGestureRecognizer)
self.bubbleView.addGestureRecognizer(self.longPressGestureRecognizer)
self.tapGestureRecognizer.require(toFail: self.longPressGestureRecognizer)
self.contentView.addSubview(self.avatarView)
self.contentView.addSubview(self.bubbleView)
self.contentView.addSubview(self.failedButton)
Expand All @@ -187,11 +189,19 @@ open class BaseMessageCollectionViewCell<BubbleViewType>: UICollectionViewCell,
}

open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer === self.longPressGestureRecognizer
let allowLongPressGestureRecognizerToBeRecognizedWithAnyOtherGestureRecognizers = gestureRecognizer === self.longPressGestureRecognizer
let allowTapGestureRecognizerToBeRecognizedWithOtherTapGestures = gestureRecognizer === self.tapGestureRecognizer && otherGestureRecognizer is UITapGestureRecognizer
return allowLongPressGestureRecognizerToBeRecognizedWithAnyOtherGestureRecognizers
|| allowTapGestureRecognizerToBeRecognizedWithOtherTapGestures
}

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer === self.longPressGestureRecognizer && otherGestureRecognizer is UILongPressGestureRecognizer
guard let otherLongPressGestureRecognizer = otherGestureRecognizer as? UILongPressGestureRecognizer else {
return false
}

let allowLongPressGestureToWaitUntilOtherLongPressGesturesWithSingleTouchFail = gestureRecognizer == self.longPressGestureRecognizer && otherLongPressGestureRecognizer.numberOfTouchesRequired == 1
return allowLongPressGestureToWaitUntilOtherLongPressGesturesWithSingleTouchFail
}

open override func prepareForReuse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public final class TextBubbleView: UIView, MaximumLayoutWidthSpecificable, Backg
textView.showsVerticalScrollIndicator = false
textView.isExclusiveTouch = true
textView.textContainer.lineFragmentPadding = 0
textView.disableDragInteraction()
textView.disableLargeContentViewer()
return textView
}()

Expand Down Expand Up @@ -286,10 +288,13 @@ private final class ChatMessageTextView: UITextView {
}
get {
return super.gestureRecognizers?.filter { gestureRecognizer in
if type(of: gestureRecognizer) == UILongPressGestureRecognizer.self, gestureRecognizer.delaysTouchesEnded {
if #available(iOS 13, *) {
return !ChatMessageTextView.notAllowedGestureRecognizerNames.contains(gestureRecognizer.name?.base64String ?? "")
}
if #available(iOS 11, *), gestureRecognizer.name?.base64String == SystemGestureRecognizerNames.linkTap.rawValue {
return true
}
if #available(iOS 11, *), gestureRecognizer.name == "UITextInteractionNameLinkTap" {
if type(of: gestureRecognizer) == UILongPressGestureRecognizer.self, gestureRecognizer.delaysTouchesEnded {
return true
}
return false
Expand Down Expand Up @@ -320,4 +325,38 @@ private final class ChatMessageTextView: UITextView {
// See https://github.com/badoo/Chatto/pull/144
}
}

fileprivate func disableDragInteraction() {
if #available(iOS 11.0, *) {
self.textDragInteraction?.isEnabled = false
}
}

fileprivate func disableLargeContentViewer() {
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
self.showsLargeContentViewer = false
}
#endif
}

private static let notAllowedGestureRecognizerNames: Set<String> = Set([
SystemGestureRecognizerNames.forcePress.rawValue,
SystemGestureRecognizerNames.loupe.rawValue
])
}

private enum SystemGestureRecognizerNames: String {
// _UIKeyboardTextSelectionGestureForcePress
case forcePress = "X1VJS2V5Ym9hcmRUZXh0U2VsZWN0aW9uR2VzdHVyZUZvcmNlUHJlc3M="
// UITextInteractionNameLoupe
case loupe = "VUlUZXh0SW50ZXJhY3Rpb25OYW1lTG91cGU="
// UITextInteractionNameLinkTap
case linkTap = "VUlUZXh0SW50ZXJhY3Rpb25OYW1lTGlua1RhcA=="
}

private extension String {
var base64String: String? {
return self.data(using: .utf8)?.base64EncodedString()
}
}