From 94aebd82161aef8014e0ea80b05b9576052c05f1 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Tue, 3 Dec 2024 07:35:05 -0800 Subject: [PATCH] [Android] Add onPreRenderFragmentState callback. (#404) --- .../instacart/formula/android/compose/ComposeViewFactory.kt | 4 ++++ .../main/java/com/instacart/formula/android/ActivityStore.kt | 4 +++- .../com/instacart/formula/android/internal/ActivityManager.kt | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt b/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt index 2f6cf898..7174d8e1 100644 --- a/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt +++ b/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt @@ -5,6 +5,7 @@ import android.view.ViewGroup import androidx.compose.runtime.Composable import androidx.compose.runtime.rxjava3.subscribeAsState import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.ViewCompositionStrategy import com.instacart.formula.android.FeatureView import com.instacart.formula.android.ViewFactory import com.jakewharton.rxrelay3.BehaviorRelay @@ -14,6 +15,9 @@ abstract class ComposeViewFactory : ViewFactory override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView { val view = ComposeView(inflater.context) + // Based-on: https://developer.android.com/develop/ui/compose/migrate/interoperability-apis/compose-in-views#compose-in-fragments + view.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) + val outputRelay = BehaviorRelay.create() view.setContent { val model = outputRelay.subscribeAsState(null).value diff --git a/formula-android/src/main/java/com/instacart/formula/android/ActivityStore.kt b/formula-android/src/main/java/com/instacart/formula/android/ActivityStore.kt index 0452147d..a565d255 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/ActivityStore.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/ActivityStore.kt @@ -14,13 +14,15 @@ import io.reactivex.rxjava3.disposables.Disposable * configuration changes. Check [com.instacart.formula.android.StreamConfigurator] for utility methods. * @param configureActivity This is invoked as part of [com.instacart.formula.FormulaAndroid.onPreCreate]. You can * use this callback to inject the activity. - * @param onRenderFragmentState This is invoked after [FragmentState] has been updated. + * @param onPreRenderFragmentState This is invoked before [FragmentState] is applied. + * @param onRenderFragmentState This is invoked after [FragmentState] has been applied. * @param onFragmentLifecycleEvent This is callback for when a fragment is added or removed. */ class ActivityStore( val fragmentStore: FragmentStore = FragmentStore.EMPTY, val streams: (StreamConfigurator.() -> Disposable)? = null, val configureActivity: ((Activity) -> Unit)? = null, + val onPreRenderFragmentState: ((Activity, FragmentState) -> Unit)? = null, val onRenderFragmentState: ((Activity, FragmentState) -> Unit)? = null, val onFragmentLifecycleEvent: ((FragmentLifecycleEvent) -> Unit)? = null ) diff --git a/formula-android/src/main/java/com/instacart/formula/android/internal/ActivityManager.kt b/formula-android/src/main/java/com/instacart/formula/android/internal/ActivityManager.kt index af00e8d9..09776678 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/internal/ActivityManager.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/internal/ActivityManager.kt @@ -57,6 +57,7 @@ internal class ActivityManager( val renderView = fragmentRenderView ?: throw callOnPreCreateException(activity) uiSubscription = delegate.fragmentState().subscribe { + store.onPreRenderFragmentState?.invoke(activity, it) renderView.render(it) store.onRenderFragmentState?.invoke(activity, it) }