Skip to content

Commit

Permalink
Fix firstModel timing when re-creating the view. (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Dec 11, 2023
1 parent 3c93bf7 commit 70f0258
Showing 1 changed file with 12 additions and 2 deletions.
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.os.Bundle
import android.os.SystemClock
import android.view.LayoutInflater
Expand Down Expand Up @@ -30,14 +31,21 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
requireArguments().getParcelable<FragmentKey>(ARG_CONTRACT)!!
}

private val initializedAtMillis = SystemClock.uptimeMillis()
private var initializedAtMillis: Long? = SystemClock.uptimeMillis()

private var featureView: FeatureView<Any>? = null
private val stateRelay: BehaviorRelay<Any> = BehaviorRelay.create()
private var cancelable: Cancelable? = null

private var lifecycleCallback: FragmentLifecycleCallback? = null

override fun onAttach(context: Context) {
super.onAttach(context)
if (initializedAtMillis == null) {
initializedAtMillis = SystemClock.uptimeMillis()
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val viewFactory = FormulaFragmentDelegate.viewFactory(this) ?: run {
// No view factory, no view
Expand All @@ -53,7 +61,7 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
super.onViewCreated(view, savedInstanceState)
featureView?.let { value ->
val state = FeatureView.State(
initializedAtMillis = initializedAtMillis,
initializedAtMillis = initializedAtMillis ?: SystemClock.uptimeMillis(),
fragmentId = getFormulaFragmentId(),
environment = FormulaFragmentDelegate.fragmentEnvironment(),
observable = stateRelay,
Expand Down Expand Up @@ -100,6 +108,8 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
}

override fun onDestroyView() {
initializedAtMillis = null

cancelable?.cancel()
cancelable = null

Expand Down

0 comments on commit 70f0258

Please sign in to comment.