Skip to content

Commit c8c70e7

Browse files
committed
Make height and width calculation suspended
1 parent 2da6e8b commit c8c70e7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ImageWithCaptionAdapter(
1818
override val type: String = "image_with_caption"
1919
) : PlaceholderManager.PlaceholderAdapter {
2020
private val media = mutableMapOf<String, ImageWithCaptionObject>()
21-
override fun getHeight(attrs: AztecAttributes): Proportion {
21+
suspend override fun getHeight(attrs: AztecAttributes): Proportion {
2222
return Proportion.Ratio(0.5f)
2323
}
2424

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.widget.FrameLayout
1414
import androidx.core.content.ContextCompat
1515
import kotlinx.coroutines.CoroutineScope
1616
import kotlinx.coroutines.launch
17+
import kotlinx.coroutines.runBlocking
1718
import org.wordpress.aztec.AztecAttributes
1819
import org.wordpress.aztec.AztecContentChangeWatcher
1920
import org.wordpress.aztec.AztecText
@@ -100,7 +101,7 @@ class PlaceholderManager(
100101
aztecText.refreshText(false)
101102
}
102103

103-
private fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
104+
private suspend fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
104105
val drawable = ContextCompat.getDrawable(aztecText.context, android.R.color.transparent)!!
105106
updateDrawableBounds(adapter, attrs, drawable)
106107
return drawable
@@ -247,7 +248,7 @@ class PlaceholderManager(
247248
val adapter = adapters[type] ?: return false
248249
val aztecAttributes = AztecAttributes(attributes)
249250
aztecAttributes.setValue(UUID_ATTRIBUTE, UUID.randomUUID().toString())
250-
val drawable = buildPlaceholderDrawable(adapter, aztecAttributes)
251+
val drawable = runBlocking { buildPlaceholderDrawable(adapter, aztecAttributes) }
251252
val span = AztecPlaceholderSpan(
252253
context = aztecText.context,
253254
drawable = drawable,
@@ -283,8 +284,8 @@ class PlaceholderManager(
283284
spans.forEach {
284285
val type = it.attributes.getValue(TYPE_ATTRIBUTE)
285286
val adapter = adapters[type] ?: return
286-
updateDrawableBounds(adapter, it.attributes, it.drawable)
287287
coroutineScope.launch {
288+
updateDrawableBounds(adapter, it.attributes, it.drawable)
288289
aztecText.refreshText(false)
289290
insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
290291
}
@@ -293,7 +294,7 @@ class PlaceholderManager(
293294
})
294295
}
295296

296-
private fun updateDrawableBounds(adapter: PlaceholderAdapter, attrs: AztecAttributes, drawable: Drawable?) {
297+
private suspend fun updateDrawableBounds(adapter: PlaceholderAdapter, attrs: AztecAttributes, drawable: Drawable?) {
297298
val editorWidth = if (aztecText.width > 0) aztecText.width else aztecText.maxImagesWidth
298299
if (drawable?.bounds?.right != editorWidth) {
299300
drawable?.setBounds(0, 0, adapter.calculateWidth(attrs, editorWidth), adapter.calculateHeight(attrs, editorWidth))
@@ -366,18 +367,18 @@ class PlaceholderManager(
366367
* Returns width of the view based on the HTML attributes. Use this method to either set fixed width or to
367368
* calculate width based on the view.
368369
*/
369-
fun getWidth(attrs: AztecAttributes): Proportion = Proportion.Ratio(1.0f)
370+
suspend fun getWidth(attrs: AztecAttributes): Proportion = Proportion.Ratio(1.0f)
370371

371372
/**
372373
* Returns height of the view based on the HTML attributes. Use this method to either set fixed height or to
373374
* calculate width based on the view.
374375
*/
375-
fun getHeight(attrs: AztecAttributes): Proportion
376+
suspend fun getHeight(attrs: AztecAttributes): Proportion
376377

377378
/**
378379
* Returns height of the view based on the width and the placeholder height.
379380
*/
380-
fun calculateHeight(attrs: AztecAttributes, windowWidth: Int): Int {
381+
suspend fun calculateHeight(attrs: AztecAttributes, windowWidth: Int): Int {
381382
return getHeight(attrs).let { height ->
382383
when (height) {
383384
is Proportion.Fixed -> height.value
@@ -396,7 +397,7 @@ class PlaceholderManager(
396397
/**
397398
* Returns height of the view based on the width and the placeholder height.
398399
*/
399-
fun calculateWidth(attrs: AztecAttributes, windowWidth: Int): Int {
400+
suspend fun calculateWidth(attrs: AztecAttributes, windowWidth: Int): Int {
400401
return getWidth(attrs).let { width ->
401402
when (width) {
402403
is Proportion.Fixed -> min(windowWidth, width.value)

0 commit comments

Comments
 (0)