Skip to content

Commit 5146442

Browse files
committed
Fix issues after changing textView to weak reference
1 parent ba90864 commit 5146442

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

wordpress-comments/src/main/java/org/wordpress/aztec/plugins/wpcomments/spans/GutenbergInlineCommentSpan.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import android.graphics.drawable.Drawable
55
import org.wordpress.aztec.AztecText
66
import org.wordpress.aztec.spans.AztecDynamicImageSpan
77
import org.wordpress.aztec.spans.IAztecFullWidthImageSpan
8+
import java.lang.ref.WeakReference
89

910
class GutenbergInlineCommentSpan @JvmOverloads constructor(val commentText: String, context: Context, drawable: Drawable, override var nestingLevel: Int, editor: AztecText? = null) :
1011
AztecDynamicImageSpan(context, drawable), IAztecFullWidthImageSpan {
1112

1213
init {
13-
textView = editor
14+
textView = WeakReference(editor)
1415
}
1516
}

wordpress-comments/src/main/java/org/wordpress/aztec/plugins/wpcomments/spans/WordPressCommentSpan.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import android.graphics.drawable.Drawable
55
import org.wordpress.aztec.AztecText
66
import org.wordpress.aztec.spans.AztecDynamicImageSpan
77
import org.wordpress.aztec.spans.IAztecFullWidthImageSpan
8+
import java.lang.ref.WeakReference
89

910
class WordPressCommentSpan @JvmOverloads constructor(val commentText: String, context: Context, drawable: Drawable, override var nestingLevel: Int, editor: AztecText? = null) :
1011
AztecDynamicImageSpan(context, drawable), IAztecFullWidthImageSpan {
1112

1213
init {
13-
textView = editor
14+
textView = WeakReference(editor)
1415
}
1516

1617
companion object {

wordpress-shortcodes/src/main/java/org/wordpress/aztec/plugins/shortcodes/extensions/CaptionExtensions.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ fun AztecText.hasImageCaption(attributePredicate: AztecText.AttributePredicate):
5959
}
6060

6161
fun AztecImageSpan.getCaption(): String {
62-
textView?.text?.let {
63-
val wrapper = SpanWrapper(textView!!.text, this)
64-
textView!!.text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.let {
62+
textView?.get()?.text?.let { editable ->
63+
val wrapper = SpanWrapper(editable, this)
64+
editable.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.let {
6565
return it.caption
6666
}
6767
}
6868
return ""
6969
}
7070

7171
fun AztecImageSpan.getCaptionAttributes(): AztecAttributes {
72-
textView?.text?.let {
73-
val wrapper = SpanWrapper(textView!!.text, this)
74-
textView!!.text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.let {
72+
textView?.get()?.text?.let { editable ->
73+
val wrapper = SpanWrapper(editable, this)
74+
editable.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.let {
7575
return it.attributes
7676
}
7777
}
@@ -80,17 +80,17 @@ fun AztecImageSpan.getCaptionAttributes(): AztecAttributes {
8080

8181
@JvmOverloads
8282
fun AztecImageSpan.setCaption(value: String, attrs: AztecAttributes? = null) {
83-
textView?.text?.let {
84-
val wrapper = SpanWrapper(textView!!.text, this)
83+
textView?.get()?.text?.let { editable ->
84+
val wrapper = SpanWrapper(editable, this)
8585

86-
var captionSpan = textView?.text?.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java)?.firstOrNull()
86+
var captionSpan = editable.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java)?.firstOrNull()
8787
if (captionSpan == null) {
8888
captionSpan = createCaptionShortcodeSpan(
8989
AztecAttributes(),
9090
CaptionShortcodePlugin.HTML_TAG,
91-
IAztecNestable.getNestingLevelAt(textView!!.text, wrapper.start),
92-
textView!!)
93-
textView!!.text.setSpan(captionSpan, wrapper.start, wrapper.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
91+
IAztecNestable.getNestingLevelAt(editable, wrapper.start),
92+
textView?.get()!!)
93+
editable.setSpan(captionSpan, wrapper.start, wrapper.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
9494
}
9595

9696
captionSpan.caption = value
@@ -114,8 +114,8 @@ fun AztecImageSpan.setCaption(value: String, attrs: AztecAttributes? = null) {
114114
}
115115

116116
fun AztecImageSpan.removeCaption() {
117-
textView?.text?.let {
118-
val wrapper = SpanWrapper(textView!!.text, this)
119-
textView!!.text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.remove()
117+
textView?.get()?.text?.let { editable ->
118+
val wrapper = SpanWrapper(editable, this)
119+
editable.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java).firstOrNull()?.remove()
120120
}
121121
}

0 commit comments

Comments
 (0)