Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/CodeEditUI/Views/AreaTabBar.swift (Swift)

Code before:
                // Update the last dragging location if there's enough offset
                let currentLocationOnAxis = ((position == .top) ? value.location.x : value.location.y)
                if draggingLastLocation == nil || abs(currentLocationOnAxis - draggingLastLocation!) >= 10 {
                    draggingLastLocation = (position == .top) ? value.location.x : value.location.y
                }
            })
            .onEnded({ _ in
                draggingStartLocation = nil
                draggingLastLocation = nil
                withAnimation(.easeInOut(duration: 0.25)) {
                    tabOffsets = [:]
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
                    draggingTab = nil
                }
            })
    }

    private func initializeDragGesture(value: DragGesture.Value, for tab: Tab) {
        draggingTab = tab
        let initialLocation = position == .top ? value.startLocation.x : value.startLocation.y
        draggingStartLocation = initialLocation
        draggingLastLocation = initialLocation
    }

    private func swapWithPreviousTab(
        tab: Tab, currentIndex: Int, currentLocation: CGFloat, dragDifference: CGFloat, currentTabWidth: CGFloat
    ) {
        guard let previousIndex = currentIndex > 0 ? currentIndex - 1 : nil,
              dragDifference < 0 else { return }

        // Get info about the previous tab
        let previousTab = items[previousIndex]
        guard let previousTabLocation = tabLocations[previousTab],
              let previousTabWidth = tabWidth[previousTab]
        else { return }

        // Did we pass the threshold to swap positions with the previous tab?
        var isWithinBounds = false
        if position == .top {
            isWithinBounds = currentLocation < max(
                previousTabLocation.maxX - previousTabWidth * 0.1,
                previousTabLocation.minX + previousTabWidth * 0.9
            )
        } else {
            isWithinBounds = currentLocation < max(
                previousTabLocation.maxY - previousTabWidth * 0.1,
                previousTabLocation.minY + previousTabWidth * 0.9
            )
        }


Review comment (refactor):
There is a lot of duplicate code here, between `swapWithPreviousTab` and `swapWithNextTab`. The functions are also quite large. Can this be simplified?

Code after:
                    direction: .next
                )

                // Update the last dragging location if there's enough offset
                let currentLocationOnAxis = ((position == .top) ? value.location.x : value.location.y)
                if draggingLastLocation == nil || abs(currentLocationOnAxis - draggingLastLocation!) >= 10 {
                    draggingLastLocation = (position == .top) ? value.location.x : value.location.y
                }
            })
            .onEnded({ _ in
                draggingStartLocation = nil
                draggingLastLocation = nil
                withAnimation(.easeInOut(duration: 0.25)) {
                    tabOffsets = [:]
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
                    draggingTab = nil
                }
            })
    }

    private func initializeDragGesture(value: DragGesture.Value, for tab: Tab) {
        draggingTab = tab
        let initialLocation = position == .top ? value.startLocation.x : value.startLocation.y
        draggingStartLocation = initialLocation
        draggingLastLocation = initialLocation
    }

    enum SwapDirection {
        case previous
        case next
    }

    // swiftlint: disable function_parameter_count
    private func swapTab(
        tab: Tab,
        currentIndex: Int,
        currentLocation: CGFloat,
        dragDifference: CGFloat,
        currentTabWidth: CGFloat,
        direction: SwapDirection
    ) {
        // Determine the index to swap with based on direction
        var swapIndex: Int?
        if direction == .previous {
            if currentIndex > 0 {
                swapIndex = currentIndex - 1
            }
        } else {
            if currentIndex < items.count - 1 {
                swapIndex = currentIndex + 1