diff --git a/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt b/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt index 9129cd64..e34c49bb 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt @@ -132,21 +132,16 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { val fragmentId = getFormulaFragmentId() val environment = FormulaFragmentDelegate.fragmentEnvironment() + val fragmentDelegate = environment.fragmentDelegate try { - val start = SystemClock.uptimeMillis() - view.setOutput(output) - val end = SystemClock.uptimeMillis() - - - environment.eventListener?.onRendered( - fragmentId = fragmentId, - durationInMillis = end - start, - ) + fragmentDelegate.setOutput(fragmentId, output, view.setOutput) if (firstRender) { + val end = SystemClock.uptimeMillis() + firstRender = false - environment.eventListener?.onFirstModelRendered( + fragmentDelegate.onFirstModelRendered( fragmentId = fragmentId, durationInMillis = end - (initializedAtMillis ?: SystemClock.uptimeMillis()), ) diff --git a/formula-android/src/main/java/com/instacart/formula/android/FragmentEnvironment.kt b/formula-android/src/main/java/com/instacart/formula/android/FragmentEnvironment.kt index d284d412..d416397c 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/FragmentEnvironment.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/FragmentEnvironment.kt @@ -1,35 +1,54 @@ package com.instacart.formula.android +import android.view.LayoutInflater +import android.view.ViewGroup + data class FragmentEnvironment( val logger: (String) -> Unit = {}, val onScreenError: (FragmentKey, Throwable) -> Unit = { _, it -> throw it }, - val eventListener: EventListener? = null, + val fragmentDelegate: FragmentDelegate = FragmentDelegate(), ) { /** * Introspection API to track various formula fragment events and their performance. */ - interface EventListener { + open class FragmentDelegate { /** - * Called after [FeatureFactory.initialize] is called. + * Instantiates the feature. */ - fun onFeatureInitialized(fragmentId: FragmentId, durationInMillis: Long) + fun initializeFeature( + fragmentId: FragmentId, + factory: FeatureFactory, + dependencies: DependenciesT, + key: KeyT, + ): Feature<*> { + return factory.initialize(dependencies, key) + } /** - * Called when [FormulaFragment] view is inflated. + * Called from [FormulaFragment.onCreateView] to instantiate the view. */ - fun onViewInflated(fragmentId: FragmentId, durationInMillis: Long) + fun createView( + fragmentId: FragmentId, + viewFactory: ViewFactory, + inflater: LayoutInflater, + container: ViewGroup?, + ): FeatureView { + return viewFactory.create(inflater, container) + } /** - * Called after render model was applied to the [FeatureView]. + * Called when we are ready to apply [output] to the view. */ - fun onRendered(fragmentId: FragmentId, durationInMillis: Long) + fun setOutput(fragmentId: FragmentId, output: Any, applyOutputToView: (Any) -> Unit) { + applyOutputToView(output) + } /** * Called after first render model is rendered. The [durationInMillis] starts * when formula fragment is initialized and ends after first render model is applied. */ - fun onFirstModelRendered(fragmentId: FragmentId, durationInMillis: Long) + fun onFirstModelRendered(fragmentId: FragmentId, durationInMillis: Long) = Unit } } diff --git a/formula-android/src/main/java/com/instacart/formula/android/internal/FeatureBinding.kt b/formula-android/src/main/java/com/instacart/formula/android/internal/FeatureBinding.kt index 5710e18b..06ab7f2a 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/internal/FeatureBinding.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/internal/FeatureBinding.kt @@ -34,13 +34,12 @@ internal class FeatureBinding { - val start = SystemClock.uptimeMillis() val key = fragmentId.key val featureEvent = featureProvider.getFeature(fragmentId) ?: throw IllegalStateException("Could not find feature for $key.") val viewFactory = factory ?: when (featureEvent) { @@ -35,9 +33,7 @@ internal class FormulaFragmentViewFactory( } } this.factory = viewFactory - val view = viewFactory.create(inflater, container) - val endTime = SystemClock.uptimeMillis() - environment.eventListener?.onViewInflated(fragmentId, endTime - start) - return view + val delegate = environment.fragmentDelegate + return delegate.createView(fragmentId, viewFactory, inflater, container) } } \ No newline at end of file