-
Notifications
You must be signed in to change notification settings - Fork 16
Remove LayoutViewFactory + refactor FeatureView to make it compose-first.
#511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4fc15c1
2ce321c
867d15d
c50a24c
9b91e9e
f64d69b
116eb76
836c0f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.instacart.formula.android | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
|
|
||
| /** | ||
| * [ViewFactory] subtype for Compose-rendered routes. Subclasses implement | ||
| * [Content] to render the route's render model, and may optionally override | ||
| * [initialModel] to supply a model rendered before the first state emission. | ||
| * | ||
| * ``` | ||
| * class MyViewFactory : ComposeViewFactory<MyRenderModel>() { | ||
| * @Composable | ||
| * override fun Content(model: MyRenderModel) { | ||
| * MyScreen(model) | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| abstract class ComposeViewFactory<RenderModel : Any> : ViewFactory<RenderModel> { | ||
|
Laimiux marked this conversation as resolved.
|
||
|
|
||
| @Composable | ||
| abstract fun Content(model: RenderModel) | ||
|
|
||
| /** Optional initial model rendered before the first state emission. Defaults to null. */ | ||
| open fun initialModel(): RenderModel? = null | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ import android.os.Bundle | |
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.compose.runtime.MutableState | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.ui.platform.ComposeView | ||
| import androidx.compose.ui.platform.ViewCompositionStrategy | ||
| import androidx.fragment.app.Fragment | ||
| import com.instacart.formula.android.internal.getOrSetArguments | ||
| import java.lang.Exception | ||
|
|
@@ -22,7 +26,7 @@ class FormulaFragment : Fragment() { | |
| } | ||
|
|
||
| private val key: RouteKey by lazy(LazyThreadSafetyMode.NONE) { | ||
| requireArguments().getParcelable<RouteKey>(ARG_CONTRACT)!! | ||
| requireArguments().getParcelable(ARG_CONTRACT)!! | ||
| } | ||
|
|
||
| private val formulaRouteId: RouteId<*> by lazy { | ||
|
|
@@ -37,47 +41,34 @@ class FormulaFragment : Fragment() { | |
| private val routeDelegate: RouteEnvironment.RouteDelegate | ||
| get() = environment.routeDelegate | ||
|
|
||
| private var featureView: FeatureView<Any>? = null | ||
| private var output: Any? = null | ||
| private val outputState: MutableState<Any?> = mutableStateOf(null) | ||
|
|
||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
| val viewFactory = navigationStore.getViewFactory(formulaRouteId) ?: run { | ||
| // No view factory, no view | ||
| return null | ||
| val viewFactory = navigationStore.getViewFactory(formulaRouteId) ?: return null | ||
| val initial: Any? = when (viewFactory) { | ||
| is ComposeViewFactory -> viewFactory.initialModel() | ||
| } | ||
| return ComposeView(requireContext()).apply { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the framework's error-handling contract for render failures.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually changes nothing for the upstream app, given it was not using
So |
||
| // Based-on: https://developer.android.com/develop/ui/compose/migrate/interoperability-apis/compose-in-views#compose-in-fragments | ||
| setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
| setContent { | ||
| val output = outputState.value ?: initial | ||
| if (output != null) { | ||
| routeDelegate.Content(formulaRouteId, viewFactory, output) | ||
| } | ||
| } | ||
|
Comment on lines
+52
to
+59
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving Alternative navigation host (Compose Nav 3) doesn't need |
||
| } | ||
| val params = ViewFactory.Params( | ||
| context = requireContext(), | ||
| inflater = inflater, | ||
| container = container, | ||
| ) | ||
|
|
||
| val featureView = environment.routeDelegate.createView( | ||
| routeId = formulaRouteId, | ||
| viewFactory = viewFactory, | ||
| params = params, | ||
| ) | ||
| this.featureView = featureView | ||
| return featureView.view | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| tryToSetState() | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| featureView = null | ||
| } | ||
|
|
||
| fun setState(state: Any) { | ||
| output = state | ||
| tryToSetState() | ||
| try { | ||
| routeDelegate.setOutput(formulaRouteId, state) { outputState.value = it } | ||
| } catch (exception: Exception) { | ||
| environment.onScreenError(key, exception) | ||
| } | ||
| } | ||
|
|
||
| fun currentState(): Any? { | ||
| return output | ||
| } | ||
| fun currentState(): Any? = outputState.value | ||
|
|
||
| fun getRouteKey(): RouteKey { | ||
| return key | ||
|
|
@@ -86,15 +77,4 @@ class FormulaFragment : Fragment() { | |
| override fun toString(): String { | ||
| return "${key.tag} -> $key" | ||
| } | ||
|
|
||
| private fun tryToSetState() { | ||
| val output = output ?: return | ||
| val view = featureView ?: return | ||
|
|
||
| try { | ||
| routeDelegate.setOutput(formulaRouteId, output, view.setOutput) | ||
| } catch (exception: Exception) { | ||
| environment.onScreenError(key, exception) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not valid test anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes -
TestViewFactorywas extendingLayoutViewFactory, which propagated view binding exceptions toRouteEnvironment.onScreenError. WithComposeViewFactory, rendering happens within compose recomposition, so we need to think of an alternative mechanism to catch/report rendering errors there.More in this comment:
#511 (comment)