@@ -7,6 +7,8 @@ import android.graphics.drawable.Drawable
77import android.text.Editable
88import android.text.Layout
99import android.text.Spanned
10+ import android.transition.TransitionManager
11+ import android.util.Log
1012import android.view.MotionEvent
1113import android.view.View
1214import android.view.ViewTreeObserver
@@ -123,9 +125,23 @@ class PlaceholderManager(
123125 val targetItem = getTargetItem()
124126 val targetSpan = targetItem?.span
125127 val currentType = targetSpan?.attributes?.getValue(TYPE_ATTRIBUTE )
126- if (currentType != null && shouldMergeItem(currentType)) {
127- updateSpan(type, targetItem.span, targetItem.placeAtStart, updateItem, currentType)
128+ if (currentType != null ) {
129+ Log .d(" vojta" , " Item found: $currentType " )
130+ if (shouldMergeItem(currentType)) {
131+ Log .d(" vojta" , " Updating item: $currentType " )
132+ updateSpan(type, targetItem.span, targetItem.placeAtStart, updateItem, currentType)
133+ } else {
134+ val (newLinePosition, targetSelection) = if (targetItem.placeAtStart) {
135+ targetItem.spanStart to targetItem.spanStart
136+ } else {
137+ targetItem.spanEnd to targetItem.spanEnd + 2
138+ }
139+ aztecText.text.insert(newLinePosition, Constants .NEWLINE_STRING )
140+ aztecText.setSelection(targetSelection)
141+ insertItem(type, * updateItem(null , null , false ).toList().toTypedArray())
142+ }
128143 } else {
144+ Log .d(" vojta" , " Inserting item: $type " )
129145 insertItem(type, * updateItem(null , null , false ).toList().toTypedArray())
130146 }
131147 }
@@ -192,7 +208,7 @@ class PlaceholderManager(
192208 return true
193209 }
194210
195- private data class TargetItem (val span : AztecPlaceholderSpan , val placeAtStart : Boolean )
211+ private data class TargetItem (val span : AztecPlaceholderSpan , val placeAtStart : Boolean , val spanStart : Int , val spanEnd : Int )
196212
197213 private fun getTargetItem (): TargetItem ? {
198214 if (aztecText.length() == 0 ) {
@@ -224,7 +240,7 @@ class PlaceholderManager(
224240 from,
225241 to,
226242 AztecPlaceholderSpan ::class .java
227- ).map { TargetItem (it, placeAtStart) }.lastOrNull()
243+ ).map { TargetItem (it, placeAtStart, editableText.getSpanStart(it), editableText.getSpanEnd(it) ) }.lastOrNull()
228244 }
229245
230246 /* *
@@ -258,10 +274,12 @@ class PlaceholderManager(
258274 suspend fun reloadAllPlaceholders () {
259275 val tempPositionToId = positionToId.toList()
260276 tempPositionToId.forEach { placeholder ->
277+ Log .d(" vojta" , " Looking up position to ID" )
261278 val isValid = positionToIdMutex.withLock {
262279 positionToId.contains(placeholder)
263280 }
264281 if (isValid) {
282+ Log .d(" vojta" , " Reloading all placeholders" )
265283 insertContentOverSpanWithId(placeholder.uuid)
266284 }
267285 }
@@ -286,7 +304,7 @@ class PlaceholderManager(
286304 }
287305 }
288306 val targetPosition = aztecText.getElementPosition(predicate) ? : return
289-
307+ Log .d( " vojta " , " Inserting in position " )
290308 insertInPosition(aztecAttributes ? : return , targetPosition)
291309 }
292310
@@ -318,39 +336,63 @@ class PlaceholderManager(
318336 parentTextViewRect.top + = parentTextViewTopAndBottomOffset
319337 parentTextViewRect.bottom = parentTextViewRect.top + height
320338
321- positionToIdMutex.withLock {
322- positionToId.removeAll {
323- it.uuid == uuid
339+ Log .d(" vojta" , " Looking for a view with tag $uuid " )
340+ var box = container.findViewWithTag<View >(uuid)?.apply {
341+ id = uuid.hashCode()
342+ }
343+ val newWidth = adapter.calculateWidth(attrs, windowWidth) - EDITOR_INNER_PADDING
344+ val newHeight = height - EDITOR_INNER_PADDING
345+ val padding = 10
346+ val newLeftPadding = parentTextViewRect.left + padding + aztecText.paddingStart
347+ val newTopPadding = parentTextViewRect.top + padding
348+ Log .d(" vojta" , " Redrawing: top padding $newTopPadding , left padding $newLeftPadding , width $newWidth , height $newHeight " )
349+ box?.let { existingView ->
350+ val currentParams = existingView.layoutParams as FrameLayout .LayoutParams
351+ val widthSame = currentParams.width == newWidth
352+ val heightSame = currentParams.height == newHeight
353+ val topMarginSame = currentParams.topMargin == newTopPadding
354+ val leftMarginSame = currentParams.leftMargin == newLeftPadding
355+ Log .d(" vojta" , " Same: $widthSame , $heightSame , $topMarginSame , $leftMarginSame " )
356+ if (widthSame && heightSame && topMarginSame && leftMarginSame) {
357+ Log .d(" vojta" , " Not redrawing" )
358+ return
359+ }
360+ Log .d(" vojta" , " Redrawing" )
361+ if (! widthSame || ! heightSame) {
362+ TransitionManager .beginDelayedTransition(container)
363+ }
364+
365+ container.removeView(box)
366+ positionToIdMutex.withLock {
367+ positionToId.removeAll {
368+ it.uuid == uuid
369+ }
324370 }
325371 }
326372
327- var box = container.findViewWithTag<View >(uuid)
328- val exists = box != null
329- if (! exists) {
330- box = adapter.createView(container.context, uuid, attrs)
331- }
373+ box = adapter.createView(container.context, uuid, attrs)
374+ box.id = uuid.hashCode()
375+ Log .d(" vojta" , " Creating a new view with id: ${box.id} " )
376+ box.setBackgroundColor(Color .TRANSPARENT )
377+ box.setOnTouchListener(adapter)
378+ box.tag = uuid
332379 val params = FrameLayout .LayoutParams (
333- adapter.calculateWidth(attrs, windowWidth) - EDITOR_INNER_PADDING ,
334- height - EDITOR_INNER_PADDING
380+ newWidth ,
381+ newHeight
335382 )
336- val padding = 10
337383 params.setMargins(
338- parentTextViewRect.left + padding + aztecText.paddingStart ,
339- parentTextViewRect.top + padding ,
384+ newLeftPadding ,
385+ newTopPadding ,
340386 0 ,
341387 0
342388 )
343389 box.layoutParams = params
344- box.tag = uuid
345- box.setBackgroundColor(Color .TRANSPARENT )
346- box.setOnTouchListener(adapter)
390+
347391 positionToIdMutex.withLock {
348392 positionToId.add(Placeholder (targetPosition, uuid))
349393 }
350- if (! exists && box.parent == null ) {
351- container.addView(box)
352- adapter.onViewCreated(box, uuid)
353- }
394+ container.addView(box)
395+ adapter.onViewCreated(box, uuid)
354396 }
355397
356398 private fun validateAttributes (attributes : AztecAttributes ): Boolean {
@@ -485,6 +527,7 @@ class PlaceholderManager(
485527 val adapter = adapters[type] ? : return @forEach
486528 it.drawable = buildPlaceholderDrawable(adapter, it.attributes)
487529 aztecText.refreshText(false )
530+ Log .d(" vojta" , " Building view on global layout" )
488531 insertInPosition(it.attributes, aztecText.editableText.getSpanStart(it))
489532 }
490533 }
0 commit comments