Skip to content

Commit 747e62c

Browse files
committed
Refactor code to reuse handleLinkTouchEvent
1 parent 770c31a commit 747e62c

File tree

1 file changed

+3
-48
lines changed

1 file changed

+3
-48
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,54 +47,9 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
4747
return true
4848
}
4949

50-
// get the character's position. This may be the left or the right edge of the character so, find the
51-
// other edge by inspecting nearby characters (if they exist)
52-
val charX = layout.getPrimaryHorizontal(off)
53-
val charPrevX = if (off > 0) layout.getPrimaryHorizontal(off - 1) else charX
54-
val charNextX = if (off < text.length) layout.getPrimaryHorizontal(off + 1) else charX
55-
56-
val lineRect = Rect()
57-
layout.getLineBounds(line, lineRect)
58-
59-
val clickedWithinLineHeight = y >= lineRect.top && y <= lineRect.bottom
60-
val clickedOnSpanToTheLeftOfCursor = x.toFloat() in charPrevX..charX
61-
val clickedOnSpanToTheRightOfCursor = x.toFloat() in charX..charNextX
62-
63-
val clickedOnSpan = clickedWithinLineHeight &&
64-
(clickedOnSpanToTheLeftOfCursor || clickedOnSpanToTheRightOfCursor)
65-
66-
val clickedSpanBordersAnotherOne = (text.getSpans(off, off, ClickableSpan::class.java).size == 1 &&
67-
text.getSpans(off + 1, off + 1, ClickableSpan::class.java).isNotEmpty())
68-
69-
val isClickedSpanAmbiguous = text.getSpans(off, off, ClickableSpan::class.java).size > 1
70-
71-
val failedToPinpointClickedSpan = (isClickedSpanAmbiguous || clickedSpanBordersAnotherOne)
72-
&& !clickedOnSpanToTheLeftOfCursor && !clickedOnSpanToTheRightOfCursor
73-
74-
var link: ClickableSpan? = null
75-
76-
if (clickedOnSpan) {
77-
if (isClickedSpanAmbiguous) {
78-
if (clickedOnSpanToTheLeftOfCursor) {
79-
link = text.getSpans(off, off, ClickableSpan::class.java)[0]
80-
} else if (clickedOnSpanToTheRightOfCursor) {
81-
link = text.getSpans(off, off, ClickableSpan::class.java)[1]
82-
}
83-
} else {
84-
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
85-
}
86-
} else if (failedToPinpointClickedSpan) {
87-
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull { text.getSpanStart(it) == off }
88-
}
89-
90-
if (link != null) {
91-
if (link is AztecMediaClickableSpan || link is UnknownClickableSpan) {
92-
link.onClick(widget)
93-
return true
94-
} else if (link is AztecURLSpan && isLinkTapEnabled) {
95-
linkTappedListenerRef.get()?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
96-
return true
97-
}
50+
// If we find that the touch event was on a link, then, we handle it and return immediately.
51+
if (handleLinkTouchEvent(widget = widget, text = text, event = event).handled) {
52+
return true
9853
}
9954
}
10055

0 commit comments

Comments
 (0)