Bug 1850610 - Fix review checker onboarding bottom sheet height

fenix/120.0
rahulsainani 1 year ago committed by mergify[bot]
parent 1d386b729c
commit 32e8ef35a1

@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.shopping
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.scan
import mozilla.components.lib.state.helpers.AbstractBinding
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckStore
/**
* View-bound feature that requests the bottom sheet to be expanded when the store state changes
* from [ReviewQualityCheckState.Initial] to [ReviewQualityCheckState.NotOptedIn].
*
* @param store The store to observe.
* @param onRequestStateExpanded Callback to request the bottom sheet to be expanded.
*/
class ReviewQualityCheckBottomSheetStateFeature(
store: ReviewQualityCheckStore,
private val onRequestStateExpanded: () -> Unit,
) : AbstractBinding<ReviewQualityCheckState>(store) {
override suspend fun onState(flow: Flow<ReviewQualityCheckState>) {
val initial = Pair<ReviewQualityCheckState?, ReviewQualityCheckState?>(null, null)
flow.scan(initial) { acc, value ->
Pair(acc.second, value)
}.filter {
it.first is ReviewQualityCheckState.Initial && it.second is ReviewQualityCheckState.NotOptedIn
}.collect {
onRequestStateExpanded()
}
}
}

@ -16,6 +16,7 @@ import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.shopping.di.ReviewQualityCheckMiddlewareProvider
import org.mozilla.fenix.shopping.store.ReviewQualityCheckStore
@ -28,6 +29,8 @@ import org.mozilla.fenix.theme.FirefoxTheme
class ReviewQualityCheckFragment : BottomSheetDialogFragment() {
private var behavior: BottomSheetBehavior<View>? = null
private val bottomSheetStateFeature =
ViewBoundFeatureWrapper<ReviewQualityCheckBottomSheetStateFeature>()
private val store by lazy {
ReviewQualityCheckStore(
middleware = ReviewQualityCheckMiddlewareProvider.provideMiddleware(
@ -66,4 +69,15 @@ class ReviewQualityCheckFragment : BottomSheetDialogFragment() {
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bottomSheetStateFeature.set(
feature = ReviewQualityCheckBottomSheetStateFeature(store) {
behavior?.state = BottomSheetBehavior.STATE_EXPANDED
},
owner = viewLifecycleOwner,
view = view,
)
}
}

@ -0,0 +1,59 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.shopping
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.shopping.store.ReviewQualityCheckAction
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckStore
class ReviewQualityCheckBottomSheetStateFeatureTest {
@get:Rule
val coroutinesTestRule = MainCoroutineRule()
@Test
fun `WHEN store state changes to not opted in from any other state THEN callback is not invoked`() {
val store = ReviewQualityCheckStore(emptyList())
var isInvoked = false
val tested = ReviewQualityCheckBottomSheetStateFeature(
store = store,
onRequestStateExpanded = {
isInvoked = true
},
)
tested.start()
store.dispatch(ReviewQualityCheckAction.OptInCompleted(isProductRecommendationsEnabled = true))
.joinBlocking()
store.dispatch(ReviewQualityCheckAction.OptOutCompleted(emptyList())).joinBlocking()
assertFalse(isInvoked)
}
@Test
fun `WHEN store state changes to not opted in from initial state THEN callback is invoked`() {
val store = ReviewQualityCheckStore(emptyList())
var isInvoked = false
val tested = ReviewQualityCheckBottomSheetStateFeature(
store = store,
onRequestStateExpanded = {
isInvoked = true
},
)
assertEquals(ReviewQualityCheckState.Initial, store.state)
tested.start()
store.dispatch(ReviewQualityCheckAction.OptOutCompleted(emptyList())).joinBlocking()
assertTrue(isInvoked)
}
}
Loading…
Cancel
Save