package com.revenuecat.purchases.react.ui.views import android.content.Context import android.util.AttributeSet import android.view.View import android.view.ViewGroup import android.widget.FrameLayout abstract class ComposeViewWrapper : FrameLayout { protected var wrappedView: T? = null protected var isAttached = false constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { init(context, attrs) } constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init(context, attrs) } constructor(context: Context) : super(context) { init(context, null) } protected abstract fun createWrappedView(context: Context, attrs: AttributeSet?): T private fun init(context: Context, attrs: AttributeSet?) { wrappedView = createWrappedView(context, attrs).apply { layoutParams = LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) } addView(wrappedView) } override fun onAttachedToWindow() { super.onAttachedToWindow() isAttached = true post { requestLayout() } } override fun onDetachedFromWindow() { super.onDetachedFromWindow() isAttached = false } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { if (isAttached) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) } else { setMeasuredDimension( MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec) ) } } override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { super.onLayout(changed, left, top, right, bottom) if (isAttached) { wrappedView?.requestLayout() } } }