@@ -16,8 +16,6 @@ import kotlinx.coroutines.CoroutineScope
1616import kotlinx.coroutines.Dispatchers
1717import kotlinx.coroutines.Job
1818import kotlinx.coroutines.delay
19- import kotlinx.coroutines.flow.MutableStateFlow
20- import kotlinx.coroutines.flow.StateFlow
2119import kotlinx.coroutines.launch
2220import kotlinx.coroutines.runBlocking
2321import kotlinx.coroutines.sync.Mutex
@@ -338,7 +336,6 @@ class PlaceholderManager(
338336 val padding = 10
339337 val newLeftPadding = parentTextViewRect.left + padding + aztecText.paddingStart
340338 val newTopPadding = parentTextViewRect.top + padding
341- var recreateView = box == null
342339 box?.let { existingView ->
343340 val currentParams = existingView.layoutParams as FrameLayout .LayoutParams
344341 val widthSame = currentParams.width == newWidth
@@ -348,43 +345,31 @@ class PlaceholderManager(
348345 if (widthSame && heightSame && topMarginSame && leftMarginSame) {
349346 return
350347 }
351- val propertiesChanged = ! widthSame || ! heightSame
352- recreateView = ! propertiesChanged || adapter is StaticPlaceholderAdapter
353- if (recreateView) {
354- container.removeView(box)
355- positionToIdMutex.withLock {
356- positionToId.removeAll {
357- it.uuid == uuid
358- }
348+ container.removeView(box)
349+ positionToIdMutex.withLock {
350+ positionToId.removeAll {
351+ it.uuid == uuid
359352 }
360353 }
361354 }
362- val paramsFlow = positionToId.find {
363- it.uuid == uuid
364- }?.viewParams ? : MutableStateFlow (Placeholder .ViewParams (newWidth, newHeight, attrs, initial = true ))
365- if (box == null || recreateView) {
366- box = adapter.createView(container.context, uuid, paramsFlow)
367- box.id = uuid.hashCode()
368- box.setBackgroundColor(Color .TRANSPARENT )
369- box.setOnTouchListener(adapter)
370- box.tag = uuid
371- box.layoutParams = FrameLayout .LayoutParams (
372- newWidth,
373- newHeight
374- )
375- } else {
376- paramsFlow.emit(Placeholder .ViewParams (newWidth, newHeight, attrs, initial = false ))
377- }
355+ box = adapter.createView(container.context, uuid, attrs)
378356
379- (box.layoutParams as FrameLayout .LayoutParams ).apply {
357+ box.id = uuid.hashCode()
358+ box.setBackgroundColor(Color .TRANSPARENT )
359+ box.setOnTouchListener(adapter)
360+ box.tag = uuid
361+ box.layoutParams = FrameLayout .LayoutParams (
362+ newWidth,
363+ newHeight
364+ ).apply {
380365 leftMargin = newLeftPadding
381366 topMargin = newLeftPadding
382367 }
383368
384369 positionToIdMutex.withLock {
385- positionToId.add(Placeholder (targetPosition, uuid, paramsFlow ))
370+ positionToId.add(Placeholder (targetPosition, uuid))
386371 }
387- if (recreateView ) {
372+ if (box.parent == null ) {
388373 container.addView(box)
389374 }
390375 adapter.onViewCreated(box, uuid)
@@ -557,12 +542,9 @@ class PlaceholderManager(
557542 }
558543
559544 /* *
560- * Use this method if you want your placeholders to be recreated on each size change .
545+ * A adapter for a custom view drawn over the placeholder in the Aztec text .
561546 */
562- interface StaticPlaceholderAdapter : PlaceholderAdapter {
563- suspend override fun createView (context : Context , placeholderUuid : String , viewParamsUpdate : StateFlow <Placeholder .ViewParams >): View {
564- return createView(context, placeholderUuid, attrs = viewParamsUpdate.value.attrs)
565- }
547+ interface PlaceholderAdapter : View .OnTouchListener {
566548 /* *
567549 * Creates the view but it's called before the view is measured. If you need the actual width and height. Use
568550 * the `onViewCreated` method where the view is already present in its correct size.
@@ -571,22 +553,7 @@ class PlaceholderManager(
571553 * @param attrs aztec attributes of the view
572554 */
573555 suspend fun createView (context : Context , placeholderUuid : String , attrs : AztecAttributes ): View
574- }
575556
576- /* *
577- * A adapter for a custom view drawn over the placeholder in the Aztec text. The default implementation propagates
578- * media size changes through the flow. If you want your views to be recreated automatically, use
579- * the StaticPlaceholderAdapter.
580- */
581- interface PlaceholderAdapter : View .OnTouchListener {
582- /* *
583- * Creates the view but it's called before the view is measured. If you need the actual width and height. Use
584- * the `onViewCreated` method where the view is already present in its correct size.
585- * @param context necessary to build custom views
586- * @param placeholderUuid the placeholder UUID
587- * @param viewParamsUpdate flow of attribute changes used to update the current view instead of redrawing it
588- */
589- suspend fun createView (context : Context , placeholderUuid : String , viewParamsUpdate : StateFlow <Placeholder .ViewParams >): View
590557 /* *
591558 * Called after the view is measured. Use this method if you need the actual width and height of the view to
592559 * draw your media.
@@ -686,9 +653,7 @@ class PlaceholderManager(
686653 }
687654 }
688655
689- data class Placeholder (val elementPosition : Int , val uuid : String , val viewParams : MutableStateFlow <ViewParams >) {
690- data class ViewParams (val width : Int , val height : Int , val attrs : AztecAttributes , val initial : Boolean = false )
691- }
656+ data class Placeholder (val elementPosition : Int , val uuid : String )
692657
693658 companion object {
694659 private const val TAG = " PlaceholderManager"
0 commit comments