[fenix] Close https://github.com/mozilla-mobile/fenix/issues/18845: Adds swipe-to-delete to tabs tray refactor
Copied the TabsTouchHelper from the `tabtray` package here so we don't need to re-write our own because there's nothing more to add. We can hook this up with our tabs tray here by putting it in the `BaseBrowserTrayList` for our normal and private tabs list.pull/600/head
parent
a02ea9b6ab
commit
d2ca8da836
@ -0,0 +1,42 @@
|
|||||||
|
/* 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.tabstray.browser
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.cancel
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import mozilla.components.lib.state.ext.flowScoped
|
||||||
|
import mozilla.components.support.base.feature.LifecycleAwareFeature
|
||||||
|
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
|
||||||
|
import org.mozilla.fenix.tabstray.TabsTrayState
|
||||||
|
import org.mozilla.fenix.tabstray.TabsTrayStore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies whether a tab is accessible for using the swipe-to-delete gesture.
|
||||||
|
*/
|
||||||
|
class SwipeToDeleteBinding(
|
||||||
|
private val store: TabsTrayStore
|
||||||
|
) : LifecycleAwareFeature {
|
||||||
|
private var scope: CoroutineScope? = null
|
||||||
|
var isSwipeable = false
|
||||||
|
private set
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
override fun start() {
|
||||||
|
scope = store.flowScoped { flow ->
|
||||||
|
flow.map { it.mode }
|
||||||
|
.ifChanged()
|
||||||
|
.collect { mode ->
|
||||||
|
isSwipeable = mode == TabsTrayState.Mode.Normal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun stop() {
|
||||||
|
scope?.cancel()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,139 @@
|
|||||||
|
/* 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.tabstray.browser
|
||||||
|
|
||||||
|
import android.graphics.Canvas
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import androidx.appcompat.content.res.AppCompatResources
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_IDLE
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import mozilla.components.browser.tabstray.TabTouchCallback
|
||||||
|
import mozilla.components.concept.tabstray.TabsTray
|
||||||
|
import mozilla.components.support.base.observer.Observable
|
||||||
|
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
||||||
|
import mozilla.components.support.ktx.android.content.getDrawableWithTint
|
||||||
|
import mozilla.components.support.ktx.android.util.dpToPx
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.home.sessioncontrol.SwipeToDeleteCallback
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback for consumers to know when a [RecyclerView.ViewHolder] is about to be touched.
|
||||||
|
* Return false if the custom behaviour should be ignored.
|
||||||
|
*/
|
||||||
|
typealias OnViewHolderTouched = (RecyclerView.ViewHolder) -> Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback for consumers to know when a [RecyclerView.ViewHolder] is about to be drawn.
|
||||||
|
* Return false if the custom drawing should be ignored.
|
||||||
|
*/
|
||||||
|
typealias OnViewHolderToDraw = (RecyclerView.ViewHolder) -> Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An [ItemTouchHelper] for handling tab swiping to delete.
|
||||||
|
*
|
||||||
|
* @param onViewHolderTouched See [OnViewHolderTouched].
|
||||||
|
*/
|
||||||
|
class TabsTouchHelper(
|
||||||
|
observable: Observable<TabsTray.Observer>,
|
||||||
|
onViewHolderTouched: OnViewHolderTouched = { true },
|
||||||
|
onViewHolderDraw: OnViewHolderToDraw = { true },
|
||||||
|
delegate: Callback = TouchCallback(observable, onViewHolderTouched, onViewHolderDraw)
|
||||||
|
) : ItemTouchHelper(delegate)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An [ItemTouchHelper.Callback] for drawing custom layouts on [RecyclerView.ViewHolder] interactions.
|
||||||
|
*
|
||||||
|
* @param onViewHolderTouched invoked when a tab is about to be swiped. See [OnViewHolderTouched].
|
||||||
|
*/
|
||||||
|
class TouchCallback(
|
||||||
|
observable: Observable<TabsTray.Observer>,
|
||||||
|
private val onViewHolderTouched: OnViewHolderTouched,
|
||||||
|
private val onViewHolderDraw: OnViewHolderToDraw
|
||||||
|
) : TabTouchCallback(observable) {
|
||||||
|
|
||||||
|
override fun getMovementFlags(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder
|
||||||
|
): Int {
|
||||||
|
if (!onViewHolderTouched.invoke(viewHolder)) {
|
||||||
|
return ItemTouchHelper.Callback.makeFlag(ACTION_STATE_IDLE, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.getMovementFlags(recyclerView, viewHolder)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onChildDraw(
|
||||||
|
c: Canvas,
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
dX: Float,
|
||||||
|
dY: Float,
|
||||||
|
actionState: Int,
|
||||||
|
isCurrentlyActive: Boolean
|
||||||
|
) {
|
||||||
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
|
||||||
|
|
||||||
|
if (!onViewHolderDraw.invoke(viewHolder)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val icon = recyclerView.context.getDrawableWithTint(
|
||||||
|
R.drawable.ic_delete,
|
||||||
|
recyclerView.context.getColorFromAttr(R.attr.destructive)
|
||||||
|
)!!
|
||||||
|
val background = AppCompatResources.getDrawable(
|
||||||
|
recyclerView.context,
|
||||||
|
R.drawable.swipe_delete_background
|
||||||
|
)!!
|
||||||
|
val itemView = viewHolder.itemView
|
||||||
|
val iconLeft: Int
|
||||||
|
val iconRight: Int
|
||||||
|
val margin =
|
||||||
|
SwipeToDeleteCallback.MARGIN.dpToPx(recyclerView.resources.displayMetrics)
|
||||||
|
val iconWidth = icon.intrinsicWidth
|
||||||
|
val iconHeight = icon.intrinsicHeight
|
||||||
|
val cellHeight = itemView.bottom - itemView.top
|
||||||
|
val iconTop = itemView.top + (cellHeight - iconHeight) / 2
|
||||||
|
val iconBottom = iconTop + iconHeight
|
||||||
|
|
||||||
|
when {
|
||||||
|
dX > 0 -> { // Swiping to the right
|
||||||
|
iconLeft = itemView.left + margin
|
||||||
|
iconRight = itemView.left + margin + iconWidth
|
||||||
|
background.setBounds(
|
||||||
|
itemView.left, itemView.top,
|
||||||
|
(itemView.left + dX).toInt() + SwipeToDeleteCallback.BACKGROUND_CORNER_OFFSET,
|
||||||
|
itemView.bottom
|
||||||
|
)
|
||||||
|
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
|
||||||
|
draw(background, icon, c)
|
||||||
|
}
|
||||||
|
dX < 0 -> { // Swiping to the left
|
||||||
|
iconLeft = itemView.right - margin - iconWidth
|
||||||
|
iconRight = itemView.right - margin
|
||||||
|
background.setBounds(
|
||||||
|
(itemView.right + dX).toInt() - SwipeToDeleteCallback.BACKGROUND_CORNER_OFFSET,
|
||||||
|
itemView.top, itemView.right, itemView.bottom
|
||||||
|
)
|
||||||
|
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
|
||||||
|
draw(background, icon, c)
|
||||||
|
}
|
||||||
|
else -> { // View not swiped
|
||||||
|
background.setBounds(0, 0, 0, 0)
|
||||||
|
icon.setBounds(0, 0, 0, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun draw(
|
||||||
|
background: Drawable,
|
||||||
|
icon: Drawable,
|
||||||
|
c: Canvas
|
||||||
|
) {
|
||||||
|
background.draw(c)
|
||||||
|
icon.draw(c)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/* 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.tabstray.browser
|
||||||
|
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.test.TestCoroutineDispatcher
|
||||||
|
import mozilla.components.support.test.libstate.ext.waitUntilIdle
|
||||||
|
import mozilla.components.support.test.rule.MainCoroutineRule
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.mozilla.fenix.tabstray.TabsTrayAction
|
||||||
|
import org.mozilla.fenix.tabstray.TabsTrayState
|
||||||
|
import org.mozilla.fenix.tabstray.TabsTrayStore
|
||||||
|
|
||||||
|
class SwipeToDeleteBindingTest {
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
@get:Rule
|
||||||
|
val coroutinesTestRule = MainCoroutineRule(TestCoroutineDispatcher())
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN started THEN update the swipeable state`() {
|
||||||
|
val store = TabsTrayStore(TabsTrayState(mode = TabsTrayState.Mode.Select(emptySet())))
|
||||||
|
val binding = SwipeToDeleteBinding(store)
|
||||||
|
|
||||||
|
binding.start()
|
||||||
|
|
||||||
|
assertFalse(binding.isSwipeable)
|
||||||
|
|
||||||
|
store.dispatch(TabsTrayAction.ExitSelectMode)
|
||||||
|
|
||||||
|
store.waitUntilIdle()
|
||||||
|
|
||||||
|
assertTrue(binding.isSwipeable)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `default state of binding is false`() {
|
||||||
|
val binding = SwipeToDeleteBinding(mockk())
|
||||||
|
|
||||||
|
assertFalse(binding.isSwipeable)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/* 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.tabstray.browser
|
||||||
|
|
||||||
|
import android.widget.FrameLayout
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_IDLE
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import io.mockk.mockk
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||||
|
import org.mozilla.fenix.tabstray.viewholders.SyncedTabViewHolder
|
||||||
|
|
||||||
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
|
class TabsTouchHelperTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `movement flags remain unchanged if onSwipeToDelete is true`() {
|
||||||
|
val recyclerView = RecyclerView(testContext)
|
||||||
|
val layout = FrameLayout(testContext)
|
||||||
|
val viewHolder = SyncedTabViewHolder(layout, mockk())
|
||||||
|
val callback = TouchCallback(mockk(), { true }, mockk())
|
||||||
|
|
||||||
|
assertEquals(0, callback.getDragDirs(recyclerView, viewHolder))
|
||||||
|
assertEquals(
|
||||||
|
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
|
||||||
|
callback.getSwipeDirs(recyclerView, viewHolder)
|
||||||
|
)
|
||||||
|
|
||||||
|
val actual = callback.getMovementFlags(recyclerView, viewHolder)
|
||||||
|
val expected = makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
|
||||||
|
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `movement flags remain unchanged if onSwipeToDelete is false`() {
|
||||||
|
val recyclerView = RecyclerView(testContext)
|
||||||
|
val layout = FrameLayout(testContext)
|
||||||
|
val viewHolder = SyncedTabViewHolder(layout, mockk())
|
||||||
|
val callback = TouchCallback(mockk(), { false }, mockk())
|
||||||
|
|
||||||
|
assertEquals(0, callback.getDragDirs(recyclerView, viewHolder))
|
||||||
|
assertEquals(
|
||||||
|
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
|
||||||
|
callback.getSwipeDirs(recyclerView, viewHolder)
|
||||||
|
)
|
||||||
|
|
||||||
|
val actual = callback.getMovementFlags(recyclerView, viewHolder)
|
||||||
|
val expected = ItemTouchHelper.Callback.makeFlag(ACTION_STATE_IDLE, 0)
|
||||||
|
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue