From c4964adda5f467e3f794b71289bcfa15cf26c77a Mon Sep 17 00:00:00 2001 From: Steven Knipe Date: Tue, 9 Nov 2021 02:24:33 -0800 Subject: [PATCH] Replace Pair<>s with data classes --- .../browser/AbstractBrowserTabViewHolder.kt | 4 +- .../browser/AbstractBrowserTrayList.kt | 107 +++++++++--------- .../fenix/tabstray/browser/TabDragData.kt | 10 ++ 3 files changed, 64 insertions(+), 57 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/tabstray/browser/TabDragData.kt diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTabViewHolder.kt b/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTabViewHolder.kt index c9f3ffcdc..31806e821 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTabViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTabViewHolder.kt @@ -61,7 +61,7 @@ abstract class AbstractBrowserTabViewHolder( @VisibleForTesting internal val featureName: String, private val store: BrowserStore = itemView.context.components.core.store, - private val metrics: MetricController = itemView.context.components.analytics.metrics, + private val metrics: MetricController = itemView.context.components.analytics.metrics ) : TabViewHolder(itemView) { private val faviconView: ImageView? = @@ -249,7 +249,7 @@ abstract class AbstractBrowserTabViewHolder( val point = PointF(motionEvent.x, motionEvent.y) // startDragAndDrop is the non-deprecated version, but requires API 24 @Suppress("DEPRECATION") - view.startDrag(null, shadow, Pair(item, point), 0) + view.startDrag(null, shadow, TabDragData(item, point), 0) } } else -> view.onTouchEvent(motionEvent) diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTrayList.kt b/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTrayList.kt index 6b8446678..da5417cb1 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTrayList.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/browser/AbstractBrowserTrayList.kt @@ -5,13 +5,11 @@ package org.mozilla.fenix.tabstray.browser import android.content.Context -import android.graphics.PointF import android.graphics.Rect import android.util.AttributeSet import android.view.DragEvent import android.view.View import androidx.recyclerview.widget.RecyclerView -import mozilla.components.browser.state.state.TabSessionState import mozilla.components.browser.tabstray.TabViewHolder import org.mozilla.fenix.tabstray.TabsTrayInteractor import org.mozilla.fenix.tabstray.TabsTrayStore @@ -54,10 +52,11 @@ abstract class AbstractBrowserTrayList @JvmOverloads constructor( } // Find the closest item to the x/y position of the drop. - private fun getDropPosition(x: Float, y: Float, source: String): Pair? { + private data class DropPositionData(val id: String, val placeAfter: Boolean) + private fun getDropPosition(x: Float, y: Float, source: String): DropPositionData? { if (childCount < 2) return null // If there's 0 or 1 tabs visible, can't reorder var bestDist = Float.MAX_VALUE - var bestOut: Pair? = null + var bestOut: DropPositionData? = null var seenSource = false for (i in 0 until childCount) { val proposedTarget = getChildAt(i) @@ -74,7 +73,7 @@ abstract class AbstractBrowserTrayList @JvmOverloads constructor( if (id == source) seenSource = true if (dist < bestDist && id != null) { bestDist = dist - bestOut = Pair(id, seenSource) + bestOut = DropPositionData(id, seenSource) } } } @@ -91,63 +90,61 @@ abstract class AbstractBrowserTrayList @JvmOverloads constructor( return null } private val dragListen = OnDragListener { _, event -> - if (event.localState is Pair<*, *>) { - val (tab, dragOffset) = event.localState as Pair<*, *> - if (tab is TabSessionState && dragOffset is PointF) { - val sourceId = tab.id - val sources = findSourceViewAndHolder(sourceId) + if (event.localState is TabDragData) { + val (tab, dragOffset) = event.localState as TabDragData + val sourceId = tab.id + val sources = findSourceViewAndHolder(sourceId) - when (event.action) { - DragEvent.ACTION_DRAG_STARTED -> { - // Put the dragged tab on top of all other tabs - if (sources != null) { - val (sourceView, _) = sources - sourceView.elevation += DRAGGED_TAB_ELEVATION - } - true + when (event.action) { + DragEvent.ACTION_DRAG_STARTED -> { + // Put the dragged tab on top of all other tabs + if (sources != null) { + val (sourceView, _) = sources + sourceView.elevation += DRAGGED_TAB_ELEVATION } - DragEvent.ACTION_DRAG_ENTERED -> { - true - } - DragEvent.ACTION_DRAG_LOCATION -> { - val target = getDropPosition(event.x, event.y, tab.id) - if (target != null) { - val (targetId, placeAfter) = target - interactor.onTabsMove(tab.id, targetId, placeAfter) - } - // Move the tab's visual position - if (sources != null) { - val (sourceView, _) = sources - sourceView.x = event.x - dragOffset.x - sourceView.y = event.y - dragOffset.y - } - - true - } - DragEvent.ACTION_DRAG_EXITED -> { - true + true + } + DragEvent.ACTION_DRAG_ENTERED -> { + true + } + DragEvent.ACTION_DRAG_LOCATION -> { + val target = getDropPosition(event.x, event.y, tab.id) + if (target != null) { + val (targetId, placeAfter) = target + interactor.onTabsMove(tab.id, targetId, placeAfter) } - DragEvent.ACTION_DROP -> { - true + // Move the tab's visual position + if (sources != null) { + val (sourceView, sourceViewHolder) = sources + sourceView.x = event.x - dragOffset.x + sourceView.y = event.y - dragOffset.y + sourceViewHolder.beingDragged = true } - DragEvent.ACTION_DRAG_ENDED -> { - // Move tab to center, set dragging to false, return tab to normal height - if (sources != null) { - val (sourceView, sourceViewHolder) = sources - sourceView.elevation -= DRAGGED_TAB_ELEVATION - sourceView.animate() - .translationX(0f).translationY(0f) - .setDuration(itemAnimator?.moveDuration ?: 0) + true + } + DragEvent.ACTION_DRAG_EXITED -> { + true + } + DragEvent.ACTION_DROP -> { + true + } + DragEvent.ACTION_DRAG_ENDED -> { + // Move tab to center, set dragging to false, return tab to normal height + if (sources != null) { + val (sourceView, sourceViewHolder) = sources + sourceView.elevation -= DRAGGED_TAB_ELEVATION + sourceView.animate() + .translationX(0f).translationY(0f) + .setDuration(itemAnimator?.moveDuration ?: 0) - sourceViewHolder.beingDragged = false - } - true - } - else -> { // Unknown action - false + sourceViewHolder.beingDragged = false } + true } - } else false + else -> { // Unknown action + false + } + } } else false } companion object { diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/browser/TabDragData.kt b/app/src/main/java/org/mozilla/fenix/tabstray/browser/TabDragData.kt new file mode 100644 index 000000000..811118857 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/tabstray/browser/TabDragData.kt @@ -0,0 +1,10 @@ +/* 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.PointF +import mozilla.components.browser.state.state.TabSessionState + +data class TabDragData(val tab: TabSessionState, val dragOffset: PointF)