package com.fclivelike.widget import android.util.Log import android.view.Choreographer import android.view.LayoutInflater import android.widget.LinearLayout import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.uimanager.ThemedReactContext import com.fclivelike.LiveLikeManager import com.fclivelike.R import com.livelike.engagementsdk.LiveLikeContentSession import com.livelike.engagementsdk.widget.view.WidgetView class FCWidgetView(val context: ThemedReactContext, val applicationContext: ReactApplicationContext) : LinearLayout(context), LifecycleEventListener { lateinit var contentSession: LiveLikeContentSession var widgetView: WidgetView; var fallback: Choreographer.FrameCallback; init { this.applicationContext.addLifecycleEventListener(this) this.fallback = Choreographer.FrameCallback() { manuallyLayoutChildren(); getViewTreeObserver().dispatchOnGlobalLayout(); Choreographer.getInstance().postFrameCallback(this!!.fallback); } Choreographer.getInstance().postFrameCallback(fallback); val parentView = LayoutInflater.from(context).inflate(R.layout.fc_widget_view, null) as LinearLayout; widgetView = parentView.findViewById(R.id.widget_view); addView(parentView) Log.v(LiveLikeManager.LIVE_LIKE_LOG, "FCWidgetView Created") } override fun onHostResume() { contentSession.resume() } override fun onHostPause() { contentSession.pause() } override fun onHostDestroy() { contentSession.close() } fun updateContentSession(contentSession: LiveLikeContentSession) { Log.v(LiveLikeManager.LIVE_LIKE_LOG, "updateContentSession") this.contentSession = contentSession; widgetView!!.setSession(contentSession); } fun manuallyLayoutChildren() { for (i in 0 until getChildCount()) { var child = getChildAt(i); child.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight()); } } }