@@ -3,20 +3,10 @@ package org.wordpress.aztec.placeholders
33import android.content.Context
44import android.view.Gravity
55import android.view.View
6- import android.view.animation.Animation
7- import android.view.animation.Transformation
8- import android.widget.FrameLayout
96import android.widget.ImageView
107import android.widget.LinearLayout
118import android.widget.TextView
129import com.bumptech.glide.Glide
13- import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
14- import kotlinx.coroutines.CoroutineScope
15- import kotlinx.coroutines.Dispatchers
16- import kotlinx.coroutines.flow.StateFlow
17- import kotlinx.coroutines.flow.map
18- import kotlinx.coroutines.flow.stateIn
19- import kotlinx.coroutines.launch
2010import org.wordpress.aztec.AztecAttributes
2111import org.wordpress.aztec.placeholders.PlaceholderManager.PlaceholderAdapter.Proportion
2212
@@ -27,25 +17,25 @@ import org.wordpress.aztec.placeholders.PlaceholderManager.PlaceholderAdapter.Pr
2717class ImageWithCaptionAdapter (
2818 override val type : String = " image_with_caption"
2919) : PlaceholderManager.PlaceholderAdapter {
30- private val media = mutableMapOf<String , StateFlow <ImageWithCaptionObject >>()
31- private val scope = CoroutineScope (Dispatchers .Main )
20+ private val media = mutableMapOf<String , ImageWithCaptionObject >()
3221 suspend override fun getHeight (attrs : AztecAttributes ): Proportion {
3322 return Proportion .Ratio (0.5f )
3423 }
3524
36- suspend override fun createView (context : Context , placeholderUuid : String , viewParamsUpdate : StateFlow <PlaceholderManager .Placeholder .ViewParams >): View {
37- val attrs = viewParamsUpdate.value.attrs
38- val imageViewId = View .generateViewId()
39- val stateFlow = viewParamsUpdate.map {
40- ImageWithCaptionObject (placeholderUuid, it.attrs.getValue(SRC_ATTRIBUTE ), imageViewId, it.width, it.height, it.initial)
41- }.stateIn(scope)
42- media[placeholderUuid] = stateFlow
43- val imageWithCaptionObject = stateFlow.value
25+ suspend override fun createView (context : Context , placeholderUuid : String , attrs : AztecAttributes ): View {
26+ val imageWithCaptionObject = media[placeholderUuid]
27+ ? : ImageWithCaptionObject (placeholderUuid, attrs.getValue(SRC_ATTRIBUTE ), View .generateViewId()).apply {
28+ media[placeholderUuid] = this
29+ }
4430 val captionLayoutId = View .generateViewId()
4531 val imageLayoutId = imageWithCaptionObject.layoutId
4632 val linearLayout = LinearLayout (context)
4733 linearLayout.orientation = LinearLayout .VERTICAL
4834
35+ val layoutParams = LinearLayout .LayoutParams (
36+ LinearLayout .LayoutParams .MATCH_PARENT ,
37+ LinearLayout .LayoutParams .MATCH_PARENT )
38+ linearLayout.layoutParams = layoutParams
4939 val image = ImageView (context)
5040 image.id = imageLayoutId
5141 val imageParams = LinearLayout .LayoutParams (
@@ -75,45 +65,16 @@ class ImageWithCaptionAdapter(
7565
7666 suspend override fun onViewCreated (view : View , placeholderUuid : String ) {
7767 val image = media[placeholderUuid]!!
78- scope.launch {
79- image.collect {
80- val imageView = view.findViewById<ImageView >(it.layoutId)
81- ResizeAnimation (view, it.width, it.height).apply {
82- duration = if (it.initialLoad) {
83- 0
84- } else {
85- 200
86- }
87- view.startAnimation(this )
88- }
89- Glide .with (view).load(it.src).transition(DrawableTransitionOptions .withCrossFade()).into(imageView)
90- }
91- }
68+ val imageView = view.findViewById<ImageView >(image.layoutId)
69+ Glide .with (view).load(image.src).into(imageView)
9270 super .onViewCreated(view, placeholderUuid)
9371 }
9472
95- class ResizeAnimation (private val view : View , private val newWidth : Int , private val newHeight : Int ) : Animation() {
96- private val startWidth: Int = view.width
97- private val startHeight: Int = view.height
98-
99- override fun applyTransformation (interpolatedTime : Float , t : Transformation ) {
100- (view.layoutParams as FrameLayout .LayoutParams ).apply {
101- width = startWidth + ((newWidth - startWidth) * interpolatedTime).toInt()
102- height = startHeight + ((newHeight - startHeight) * interpolatedTime).toInt()
103- }
104- view.requestLayout()
105- }
106-
107- override fun willChangeBounds (): Boolean {
108- return true
109- }
110- }
111-
11273 override fun onPlaceholderDeleted (placeholderUuid : String ) {
11374 media.remove(placeholderUuid)
11475 }
11576
116- data class ImageWithCaptionObject (val id : String , val src : String , val layoutId : Int , val width : Int , val height : Int , val initialLoad : Boolean )
77+ data class ImageWithCaptionObject (val id : String , val src : String , val layoutId : Int )
11778
11879 companion object {
11980 private const val ADAPTER_TYPE = " image_with_caption"
0 commit comments