@@ -7,10 +7,9 @@ import android.view.LayoutInflater
7
7
import android.view.View
8
8
import android.view.ViewGroup
9
9
import androidx.fragment.app.Fragment
10
- import com.instacart.formula.Cancelable
11
10
import com.instacart.formula.android.internal.FormulaFragmentDelegate
12
11
import com.instacart.formula.android.internal.getFormulaFragmentId
13
- import com.jakewharton.rxrelay3.BehaviorRelay
12
+ import java.lang.Exception
14
13
15
14
class FormulaFragment : Fragment (), BaseFormulaFragment<Any> {
16
15
companion object {
@@ -31,13 +30,14 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
31
30
requireArguments().getParcelable<FragmentKey >(ARG_CONTRACT )!!
32
31
}
33
32
34
- private var initializedAtMillis: Long? = SystemClock .uptimeMillis()
35
-
36
33
private var featureView: FeatureView <Any >? = null
37
- private val stateRelay: BehaviorRelay <Any > = BehaviorRelay .create()
38
- private var cancelable: Cancelable ? = null
34
+ private var output: Any? = null
39
35
40
- private var lifecycleCallback: FragmentLifecycleCallback ? = null
36
+ private var initializedAtMillis: Long? = SystemClock .uptimeMillis()
37
+ private var firstRender = true
38
+
39
+ private val lifecycleCallback: FragmentLifecycleCallback ?
40
+ get() = featureView?.lifecycleCallbacks
41
41
42
42
override fun onAttach (context : Context ) {
43
43
super .onAttach(context)
@@ -47,6 +47,8 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
47
47
}
48
48
49
49
override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
50
+ firstRender = true
51
+
50
52
val viewFactory = FormulaFragmentDelegate .viewFactory(this ) ? : run {
51
53
// No view factory, no view
52
54
return null
@@ -59,17 +61,9 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
59
61
60
62
override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
61
63
super .onViewCreated(view, savedInstanceState)
62
- featureView?.let { value ->
63
- val state = FeatureView .State (
64
- initializedAtMillis = initializedAtMillis ? : SystemClock .uptimeMillis(),
65
- fragmentId = getFormulaFragmentId(),
66
- environment = FormulaFragmentDelegate .fragmentEnvironment(),
67
- observable = stateRelay,
68
- )
69
- cancelable = value.bind(state)
70
- this .lifecycleCallback = value.lifecycleCallbacks
71
- lifecycleCallback?.onViewCreated(view, savedInstanceState)
72
- }
64
+ tryToSetState()
65
+
66
+ lifecycleCallback?.onViewCreated(view, savedInstanceState)
73
67
}
74
68
75
69
override fun onActivityCreated (savedInstanceState : Bundle ? ) {
@@ -110,21 +104,18 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
110
104
override fun onDestroyView () {
111
105
initializedAtMillis = null
112
106
113
- cancelable?.cancel()
114
- cancelable = null
115
-
116
107
lifecycleCallback?.onDestroyView()
117
- lifecycleCallback = null
118
108
super .onDestroyView()
119
109
featureView = null
120
110
}
121
111
122
112
override fun setState (state : Any ) {
123
- stateRelay.accept(state)
113
+ output = state
114
+ tryToSetState()
124
115
}
125
116
126
117
override fun currentState (): Any? {
127
- return stateRelay.value
118
+ return output
128
119
}
129
120
130
121
override fun getFragmentKey (): FragmentKey {
@@ -134,4 +125,33 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
134
125
override fun toString (): String {
135
126
return " ${key.tag} -> $key "
136
127
}
128
+
129
+ private fun tryToSetState () {
130
+ val output = output ? : return
131
+ val view = featureView ? : return
132
+
133
+ val fragmentId = getFormulaFragmentId()
134
+ val environment = FormulaFragmentDelegate .fragmentEnvironment()
135
+
136
+ try {
137
+ val start = SystemClock .uptimeMillis()
138
+ view.setOutput(output)
139
+ val end = SystemClock .uptimeMillis()
140
+
141
+ environment.eventListener?.onRendered(
142
+ fragmentId = fragmentId,
143
+ durationInMillis = end - start,
144
+ )
145
+
146
+ if (firstRender) {
147
+ firstRender = false
148
+ environment.eventListener?.onFirstModelRendered(
149
+ fragmentId = fragmentId,
150
+ durationInMillis = end - (initializedAtMillis ? : SystemClock .uptimeMillis()),
151
+ )
152
+ }
153
+ } catch (exception: Exception ) {
154
+ environment.onScreenError(key, exception)
155
+ }
156
+ }
137
157
}
0 commit comments