From 6901e9a531e4c9c6349e0ead1361d908512a3a88 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Fri, 6 Dec 2024 13:56:33 -0500 Subject: [PATCH] [Android] Add ViewFactory.Params type that includes android context. (#406) --- .../formula/android/compose/ComposeViewFactory.kt | 4 ++-- .../com/instacart/formula/android/FormulaFragment.kt | 7 ++++++- .../com/instacart/formula/android/FragmentEnvironment.kt | 5 ++--- .../com/instacart/formula/android/LayoutViewFactory.kt | 6 ++---- .../java/com/instacart/formula/android/ViewFactory.kt | 9 ++++++++- .../android/internal/FormulaFragmentViewFactory.kt | 6 ++---- .../android/internal/FormulaFragmentViewFactoryTest.kt | 2 +- 7 files changed, 23 insertions(+), 16 deletions(-) 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 7174d8e1c..5d8cdf943 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 @@ -13,8 +13,8 @@ import com.jakewharton.rxrelay3.BehaviorRelay abstract class ComposeViewFactory : ViewFactory { - override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView { - val view = ComposeView(inflater.context) + override fun create(params: ViewFactory.Params): FeatureView { + val view = ComposeView(params.context) // Based-on: https://developer.android.com/develop/ui/compose/migrate/interoperability-apis/compose-in-views#compose-in-fragments view.setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) 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 3453c7de8..0145de658 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 @@ -48,7 +48,12 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { // No view factory, no view return null } - val featureView = viewFactory.create(inflater, container).apply { + val params = ViewFactory.Params( + context = requireContext(), + inflater = inflater, + container = container, + ) + val featureView = viewFactory.create(params).apply { featureView = this } return featureView.view 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 8adf2672d..fc6203f05 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 @@ -32,10 +32,9 @@ data class FragmentEnvironment( open fun createView( fragmentId: FragmentId, viewFactory: ViewFactory, - inflater: LayoutInflater, - container: ViewGroup?, + params: ViewFactory.Params, ): FeatureView { - return viewFactory.create(inflater, container) + return viewFactory.create(params) } /** diff --git a/formula-android/src/main/java/com/instacart/formula/android/LayoutViewFactory.kt b/formula-android/src/main/java/com/instacart/formula/android/LayoutViewFactory.kt index eca8bee5d..e8332bf08 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/LayoutViewFactory.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/LayoutViewFactory.kt @@ -1,7 +1,5 @@ package com.instacart.formula.android -import android.view.LayoutInflater -import android.view.ViewGroup import androidx.annotation.LayoutRes import com.instacart.formula.android.views.InflatedViewInstance @@ -32,8 +30,8 @@ abstract class LayoutViewFactory(@LayoutRes private val layoutId: I abstract fun ViewInstance.create(): FeatureView - override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView { - val view = inflater.inflate(layoutId, container, false) + override fun create(params: ViewFactory.Params): FeatureView { + val view = params.inflater.inflate(layoutId, params.container, false) return InflatedViewInstance(view).create() } } \ No newline at end of file diff --git a/formula-android/src/main/java/com/instacart/formula/android/ViewFactory.kt b/formula-android/src/main/java/com/instacart/formula/android/ViewFactory.kt index d334dbc3a..25b865495 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/ViewFactory.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/ViewFactory.kt @@ -1,5 +1,6 @@ package com.instacart.formula.android +import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup import androidx.annotation.LayoutRes @@ -45,11 +46,17 @@ fun interface ViewFactory { } } + class Params( + val context: Context, + val inflater: LayoutInflater, + val container: ViewGroup?, + ) + /** * This method is called from [FormulaFragment.onCreateView] function. Use it to * instantiate an Android view instance and return a [FeatureView] which knows how to * bind the state management to view rendering. Usually, you should use [LayoutViewFactory] * or [ViewFactory.fromLayout] instead of implementing this method directly. */ - fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView + fun create(params: Params): FeatureView } \ No newline at end of file diff --git a/formula-android/src/main/java/com/instacart/formula/android/internal/FormulaFragmentViewFactory.kt b/formula-android/src/main/java/com/instacart/formula/android/internal/FormulaFragmentViewFactory.kt index f6ac5f105..f797c7c14 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/internal/FormulaFragmentViewFactory.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/internal/FormulaFragmentViewFactory.kt @@ -1,7 +1,5 @@ package com.instacart.formula.android.internal -import android.view.LayoutInflater -import android.view.ViewGroup import androidx.annotation.VisibleForTesting import com.instacart.formula.android.FeatureView import com.instacart.formula.android.ViewFactory @@ -18,10 +16,10 @@ internal class FormulaFragmentViewFactory( private var factory: ViewFactory? = null - override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView { + override fun create(params: ViewFactory.Params): FeatureView { val viewFactory = viewFactory() val delegate = environment.fragmentDelegate - return delegate.createView(fragmentId, viewFactory, inflater, container) + return delegate.createView(fragmentId, viewFactory, params) } @VisibleForTesting diff --git a/formula-android/src/test/java/com/instacart/formula/android/internal/FormulaFragmentViewFactoryTest.kt b/formula-android/src/test/java/com/instacart/formula/android/internal/FormulaFragmentViewFactoryTest.kt index b460fa85e..c8e6ac297 100644 --- a/formula-android/src/test/java/com/instacart/formula/android/internal/FormulaFragmentViewFactoryTest.kt +++ b/formula-android/src/test/java/com/instacart/formula/android/internal/FormulaFragmentViewFactoryTest.kt @@ -46,7 +46,7 @@ class FormulaFragmentViewFactoryTest { timesCalled += 1 val feature = Feature( state = Observable.empty(), - viewFactory = ViewFactory { _, _ -> + viewFactory = ViewFactory { _ -> error("should not be called") } )