Skip to content

Commit

Permalink
Temp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 9, 2024
1 parent b568c21 commit c6d7e09
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract class ComposeViewFactory<RenderModel : Any> : ViewFactory<RenderModel>
return FeatureView(
view = view,
setOutput = outputRelay::accept,
lifecycleCallbacks = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.instacart.formula.test

import com.instacart.formula.android.BackCallback

data class TestBackCallbackRenderModel(
private val onBackPressed: () -> Unit,
val blockBackCallback: Boolean = false,
) : BackCallback {
override fun onBackPressed(): Boolean {
this.onBackPressed.invoke()
return blockBackCallback
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.instacart.formula.android.BackCallback
import com.instacart.formula.android.FormulaFragment
import com.instacart.formula.android.FragmentEnvironment
import com.instacart.formula.android.FragmentStore
import com.instacart.formula.test.TestBackCallbackRenderModel
import com.instacart.formula.test.TestKey
import com.instacart.formula.test.TestKeyWithId
import com.instacart.formula.test.TestFragmentActivity
Expand Down Expand Up @@ -275,6 +276,37 @@ class FormulaFragmentTest {
assertThat(updateThreads).containsExactly(Thread.currentThread())
}

@Test fun `back callback blocks navigation`() {
val key = TestKeyWithId(1)
navigateToTaskDetail(id = key.id)

Shadows.shadowOf(Looper.getMainLooper()).idle()

var onBackPressed = 0
sendStateUpdate(key, TestBackCallbackRenderModel(
onBackPressed = {
onBackPressed += 1
},
blockBackCallback = true
))

navigateBack()

// We blocked navigation so visible fragment should still be details
assertThat(onBackPressed).isEqualTo(1)
assertVisibleContract(key)

sendStateUpdate(key, TestBackCallbackRenderModel(
onBackPressed = { onBackPressed += 1 },
blockBackCallback = false
))

navigateBack()

assertThat(onBackPressed).isEqualTo(2)
assertVisibleContract(TestKey())
}

@Test fun `notify fragment environment if setOutput throws an error`() {
val key = TestKeyWithId(1)
navigateToTaskDetail(id = key.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment
import com.instacart.formula.FormulaAndroid
import com.instacart.formula.android.internal.FormulaFragmentDelegate
import com.instacart.formula.android.internal.getFormulaFragmentId
import com.instacart.formula.android.internal.getOrSetArguments
import java.lang.Exception

class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
Expand All @@ -18,7 +19,7 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
@JvmStatic
fun newInstance(key: FragmentKey): FormulaFragment {
val fragment = FormulaFragment()
fragment.arguments = Bundle().apply {
fragment.getOrSetArguments().apply {
putParcelable(ARG_CONTRACT, key)
}
FormulaAndroid.fragmentEnvironment().fragmentDelegate.onNewInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import com.instacart.formula.android.ViewFactory
internal object FormulaFragmentDelegate {
fun viewFactory(fragment: FormulaFragment): ViewFactory<Any>? {
val appManager = FormulaAndroid.appManagerOrThrow()

val activity = fragment.activity ?: run {
fragmentEnvironment().logger("FormulaFragment has no activity attached: ${fragment.getFragmentKey()}")
return null
}

val activity = fragment.requireActivity()
val viewFactory = appManager.findStore(activity)?.viewFactory(fragment) ?: run {
// Log view factory is missing
if (activity.isDestroyed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ private fun Fragment.getFragmentKey(): FragmentKey {
*/
private fun Fragment.getFragmentInstanceId(): String {
return if (this is BaseFormulaFragment<*>) {
val arguments = arguments ?: run {
Bundle().apply {
arguments = this
}
}
val arguments = getOrSetArguments()
val id = arguments.getString(FormulaFragment.ARG_FORMULA_ID, "")
if (id.isNullOrBlank()) {
val initializedId = UUID.randomUUID().toString()
Expand All @@ -68,4 +64,12 @@ internal fun Fragment.getFormulaFragmentId(): FragmentId {
instanceId = getFragmentInstanceId(),
key = getFragmentKey()
)
}

internal fun Fragment.getOrSetArguments(): Bundle {
return arguments ?: run {
Bundle().apply {
arguments = this
}
}
}

0 comments on commit c6d7e09

Please sign in to comment.