Skip to content

Commit 91433d2

Browse files
committed
Cleanup the interface
1 parent a980d6c commit 91433d2

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.wordpress.aztec.placeholders
22

33
import android.content.Context
4-
import android.util.Log
54
import android.view.Gravity
65
import android.view.View
76
import android.view.animation.Animation
@@ -42,7 +41,6 @@ class ImageWithCaptionAdapter(
4241
}.stateIn(scope)
4342
media[placeholderUuid] = stateFlow
4443
val imageWithCaptionObject = stateFlow.value
45-
Log.d("vojta2", "Drawing image with caption ${imageWithCaptionObject.src}")
4644
val captionLayoutId = View.generateViewId()
4745
val imageLayoutId = imageWithCaptionObject.layoutId
4846
val linearLayout = LinearLayout(context)
@@ -75,10 +73,6 @@ class ImageWithCaptionAdapter(
7573
return linearLayout
7674
}
7775

78-
override fun animateLayoutChanges(): Boolean {
79-
return true
80-
}
81-
8276
suspend override fun onViewCreated(view: View, placeholderUuid: String) {
8377
val image = media[placeholderUuid]!!
8478
scope.launch {
@@ -107,7 +101,6 @@ class ImageWithCaptionAdapter(
107101
width = startWidth + ((newWidth - startWidth) * interpolatedTime).toInt()
108102
height = startHeight + ((newHeight - startHeight) * interpolatedTime).toInt()
109103
}
110-
Log.d("vojta", "Changing height to ${view.layoutParams.height} and width to ${view.layoutParams.width}")
111104
view.requestLayout()
112105
}
113106

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.graphics.drawable.Drawable
77
import android.text.Editable
88
import android.text.Layout
99
import android.text.Spanned
10-
import android.util.Log
1110
import android.view.MotionEvent
1211
import android.view.View
1312
import android.view.ViewTreeObserver
@@ -351,7 +350,7 @@ class PlaceholderManager(
351350
return
352351
}
353352
val propertiesChanged = !widthSame || !heightSame
354-
recreateView = !propertiesChanged || !adapter.animateLayoutChanges()
353+
recreateView = !propertiesChanged || adapter is StaticPlaceholderAdapter
355354
if (recreateView) {
356355
container.removeView(box)
357356
positionToIdMutex.withLock {
@@ -365,7 +364,6 @@ class PlaceholderManager(
365364
it.uuid == uuid
366365
}?.viewParams ?: MutableStateFlow(Placeholder.ViewParams(newWidth, newHeight, attrs, initial = true))
367366
if (box == null || recreateView) {
368-
Log.d("vojta", "Creating new view")
369367
box = adapter.createView(container.context, uuid, paramsFlow)
370368
box.id = uuid.hashCode()
371369
box.setBackgroundColor(Color.TRANSPARENT)
@@ -376,10 +374,8 @@ class PlaceholderManager(
376374
newHeight
377375
)
378376
} else {
379-
Log.d("vojta", "Updating params")
380377
paramsFlow.emit(Placeholder.ViewParams(newWidth, newHeight, attrs, initial = false))
381378
}
382-
Log.d("vojta", "Creating view with $newWidth x $newHeight")
383379

384380
box.updateLayoutParams<FrameLayout.LayoutParams> {
385381
leftMargin = newLeftPadding
@@ -390,7 +386,6 @@ class PlaceholderManager(
390386
positionToId.add(Placeholder(targetPosition, uuid, paramsFlow))
391387
}
392388
if (recreateView) {
393-
Log.d("vojta", "Adding view with $newWidth x $newHeight")
394389
container.addView(box)
395390
}
396391
adapter.onViewCreated(box, uuid)
@@ -528,7 +523,6 @@ class PlaceholderManager(
528523
val adapter = adapters[type] ?: return@forEach
529524
it.drawable = buildPlaceholderDrawable(adapter, it.attributes)
530525
aztecText.refreshText(false)
531-
Log.d("vojta", "Building view on global layout")
532526
insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
533527
}
534528
}
@@ -558,18 +552,36 @@ class PlaceholderManager(
558552
}
559553

560554
/**
561-
* A adapter for a custom view drawn over the placeholder in the Aztec text.
555+
* Use this method if you want your placeholders to be recreated on each size change.
562556
*/
563-
interface PlaceholderAdapter : View.OnTouchListener {
557+
interface StaticPlaceholderAdapter : PlaceholderAdapter {
558+
override suspend fun createView(context: Context, placeholderUuid: String, viewParamsUpdate: StateFlow<Placeholder.ViewParams>): View {
559+
return createView(context, placeholderUuid, attrs = viewParamsUpdate.value.attrs)
560+
}
564561
/**
565562
* Creates the view but it's called before the view is measured. If you need the actual width and height. Use
566563
* the `onViewCreated` method where the view is already present in its correct size.
567564
* @param context necessary to build custom views
568565
* @param placeholderUuid the placeholder UUID
569566
* @param attrs aztec attributes of the view
570567
*/
571-
suspend fun createView(context: Context, placeholderUuid: String, viewParamsUpdate: StateFlow<Placeholder.ViewParams>): View
568+
suspend fun createView(context: Context, placeholderUuid: String, attrs: AztecAttributes): View
569+
}
572570

571+
/**
572+
* A adapter for a custom view drawn over the placeholder in the Aztec text. The default implementation propagates
573+
* media size changes through the flow. If you want your views to be recreated automatically, use
574+
* the StaticPlaceholderAdapter.
575+
*/
576+
interface PlaceholderAdapter : View.OnTouchListener {
577+
/**
578+
* Creates the view but it's called before the view is measured. If you need the actual width and height. Use
579+
* the `onViewCreated` method where the view is already present in its correct size.
580+
* @param context necessary to build custom views
581+
* @param placeholderUuid the placeholder UUID
582+
* @param viewParamsUpdate flow of attribute changes used to update the current view instead of redrawing it
583+
*/
584+
suspend fun createView(context: Context, placeholderUuid: String, viewParamsUpdate: StateFlow<Placeholder.ViewParams>): View
573585
/**
574586
* Called after the view is measured. Use this method if you need the actual width and height of the view to
575587
* draw your media.
@@ -578,8 +590,6 @@ class PlaceholderManager(
578590
*/
579591
suspend fun onViewCreated(view: View, placeholderUuid: String) {}
580592

581-
fun animateLayoutChanges() = false
582-
583593
/**
584594
* Called when the placeholder is deleted by the user. Use this method if you need to clear your data when the
585595
* item is deleted (for example delete an image in your DB).

0 commit comments

Comments
 (0)