Skip to content

Commit 72a1a57

Browse files
committed
Make sure layout is present before inserting a placeholder
1 parent 5efb60d commit 72a1a57

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.core.content.ContextCompat
1515
import kotlinx.coroutines.CoroutineScope
1616
import kotlinx.coroutines.Dispatchers
1717
import kotlinx.coroutines.Job
18+
import kotlinx.coroutines.delay
1819
import kotlinx.coroutines.launch
1920
import kotlinx.coroutines.runBlocking
2021
import org.wordpress.aztec.AztecAttributes
@@ -143,7 +144,14 @@ class PlaceholderManager(
143144
}
144145
val uuid = attrs.getValue(UUID_ATTRIBUTE)
145146
val type = attrs.getValue(TYPE_ATTRIBUTE)
146-
val textViewLayout: Layout = aztecText.layout
147+
// At this point we can get to a race condition where the aztec text layout is not yet initialized.
148+
// We want to wait a bit and make sure it's properly loaded.
149+
var counter = 0
150+
while (aztecText.layout == null && counter < 10) {
151+
delay(50)
152+
counter += 1
153+
}
154+
val textViewLayout: Layout = aztecText.layout ?: return
147155
val parentTextViewRect = Rect()
148156
val targetLineOffset = textViewLayout.getLineForOffset(targetPosition)
149157
textViewLayout.getLineBounds(targetLineOffset, parentTextViewRect)

0 commit comments

Comments
 (0)