Skip to content

Commit

Permalink
[Android] Add ViewFactory.Params type that includes android context. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Dec 6, 2024
1 parent ba85b34 commit 6901e9a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import com.jakewharton.rxrelay3.BehaviorRelay

abstract class ComposeViewFactory<RenderModel : Any> : ViewFactory<RenderModel> {

override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView<RenderModel> {
val view = ComposeView(inflater.context)
override fun create(params: ViewFactory.Params): FeatureView<RenderModel> {
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ data class FragmentEnvironment(
open fun createView(
fragmentId: FragmentId,
viewFactory: ViewFactory<Any>,
inflater: LayoutInflater,
container: ViewGroup?,
params: ViewFactory.Params,
): FeatureView<Any> {
return viewFactory.create(inflater, container)
return viewFactory.create(params)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -32,8 +30,8 @@ abstract class LayoutViewFactory<RenderModel>(@LayoutRes private val layoutId: I

abstract fun ViewInstance.create(): FeatureView<RenderModel>

override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView<RenderModel> {
val view = inflater.inflate(layoutId, container, false)
override fun create(params: ViewFactory.Params): FeatureView<RenderModel> {
val view = params.inflater.inflate(layoutId, params.container, false)
return InflatedViewInstance(view).create()
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,11 +46,17 @@ fun interface ViewFactory<RenderModel> {
}
}

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<RenderModel>
fun create(params: Params): FeatureView<RenderModel>
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,10 +16,10 @@ internal class FormulaFragmentViewFactory(

private var factory: ViewFactory<Any>? = null

override fun create(inflater: LayoutInflater, container: ViewGroup?): FeatureView<Any> {
override fun create(params: ViewFactory.Params): FeatureView<Any> {
val viewFactory = viewFactory()
val delegate = environment.fragmentDelegate
return delegate.createView(fragmentId, viewFactory, inflater, container)
return delegate.createView(fragmentId, viewFactory, params)
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FormulaFragmentViewFactoryTest {
timesCalled += 1
val feature = Feature(
state = Observable.empty(),
viewFactory = ViewFactory { _, _ ->
viewFactory = ViewFactory { _ ->
error("should not be called")
}
)
Expand Down

0 comments on commit 6901e9a

Please sign in to comment.