Skip to content
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

Fix firstModel timing when re-creating the view. #318

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading