Skip to content

Commit 493357c

Browse files
committed
Use weak references and clear them when aztec is detached from window.
1 parent 320e42f commit 493357c

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
542542
exclusiveBlockStyles = BlockFormatter.ExclusiveBlockStyles(styles.getBoolean(R.styleable.AztecText_exclusiveBlocks, false), verticalParagraphPadding),
543543
paragraphStyle = BlockFormatter.ParagraphStyle(verticalParagraphMargin)
544544
)
545-
EnhancedMovementMethod.taskListClickHandler = TaskListClickHandler(listStyle)
545+
EnhancedMovementMethod.setTaskListClickHandler(TaskListClickHandler(listStyle))
546546

547547
linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor(
548548
R.styleable.AztecText_linkColor, 0),
@@ -954,6 +954,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
954954
if (blockEditorDialog != null && blockEditorDialog!!.isShowing) {
955955
blockEditorDialog!!.dismiss()
956956
}
957+
EnhancedMovementMethod.setLinkTappedListener(null)
958+
EnhancedMovementMethod.setTaskListClickHandler(null)
957959
}
958960

959961
// We are exposing this method in order to allow subclasses to set their own alpha value
@@ -1178,7 +1180,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11781180
}
11791181

11801182
fun setOnLinkTappedListener(listener: OnLinkTappedListener) {
1181-
EnhancedMovementMethod.linkTappedListener = listener
1183+
EnhancedMovementMethod.setLinkTappedListener(listener)
11821184
}
11831185

11841186
fun setLinkTapEnabled(isLinkTapEnabled: Boolean) {

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ import android.widget.TextView
99
import org.wordpress.aztec.spans.AztecMediaClickableSpan
1010
import org.wordpress.aztec.spans.AztecURLSpan
1111
import org.wordpress.aztec.spans.UnknownClickableSpan
12+
import java.lang.ref.WeakReference
1213

1314
/**
1415
* http://stackoverflow.com/a/23566268/569430
1516
*/
1617
object EnhancedMovementMethod : ArrowKeyMovementMethod() {
17-
var taskListClickHandler: TaskListClickHandler? = null
18+
private var taskListClickHandlerRef: WeakReference<TaskListClickHandler?> = WeakReference(null)
19+
private var linkTappedListenerRef: WeakReference<AztecText.OnLinkTappedListener?> = WeakReference(null)
1820
var isLinkTapEnabled = false
19-
var linkTappedListener: AztecText.OnLinkTappedListener? = null
21+
22+
fun setTaskListClickHandler(handler: TaskListClickHandler?) {
23+
taskListClickHandlerRef = WeakReference(handler)
24+
}
25+
26+
fun setLinkTappedListener(listener: AztecText.OnLinkTappedListener?) {
27+
linkTappedListenerRef = WeakReference(listener)
28+
}
2029

2130
override fun onTouchEvent(widget: TextView, text: Spannable, event: MotionEvent): Boolean {
2231
val action = event.action
@@ -38,7 +47,9 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
3847
val off = layout.getOffsetForHorizontal(line, x.toFloat())
3948

4049
// This handles the case when the task list checkbox is clicked
41-
if (taskListClickHandler?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true) return true
50+
if (taskListClickHandlerRef.get()?.handleTaskListClick(text, off, x, widget.totalPaddingStart) == true){
51+
return true
52+
}
4253

4354
// get the character's position. This may be the left or the right edge of the character so, find the
4455
// other edge by inspecting nearby characters (if they exist)
@@ -85,7 +96,7 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
8596
link.onClick(widget)
8697
return true
8798
} else if (link is AztecURLSpan && isLinkTapEnabled) {
88-
linkTappedListener?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
99+
linkTappedListenerRef.get()?.onLinkTapped(widget, link.url) ?: link.onClick(widget)
89100
return true
90101
}
91102
}

0 commit comments

Comments
 (0)