refactor(itinerary): extract reordering logic into pure functions

Extract all itinerary reordering logic from ItineraryTableViewController into
ItineraryReorderingLogic.swift for testability. Key changes:

- Add flattenDays, dayNumber, travelRow, simulateMove pure functions
- Add calculateSortOrder with proper region classification (before/after games)
- Add computeValidDestinationRowsProposed with simulation+validation pattern
- Add coordinate space conversion helpers (proposedToOriginal, originalToProposed)
- Fix DragZones coordinate space mismatch (was mixing proposed/original indices)
- Add comprehensive documentation of coordinate space conventions

Test coverage includes:
- Row flattening order and semantic travel model
- Sort order calculation for before/after games regions
- Travel constraints validation
- DragZones coordinate space correctness
- Coordinate conversion helpers
- Edge cases (empty days, multi-day trips)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-01-18 20:04:52 -06:00
parent 143b364553
commit 72447c61fe
11 changed files with 3795 additions and 557 deletions

View File

@@ -0,0 +1,921 @@
//
// ItineraryReorderingLogic.swift
// SportsTime
//
// Pure functions for itinerary reordering logic.
// Extracted from ItineraryTableViewController for testability.
//
// All functions in this enum are pure - they take inputs and return outputs
// with no side effects, making them fully unit-testable without UIKit.
//
// SEMANTIC TRAVEL MODEL:
// - Travel items are positioned semantically via (day, sortOrder), not structurally.
// - Travel can appear before games (sortOrder < 0) or after games (sortOrder >= 0).
// - The legacy `travelBefore` field on ItineraryDayData is IGNORED by flattenDays.
// - All movable items (custom + travel) use the same day computation: backward scan to nearest dayHeader.
//
// COORDINATE SPACE CONVENTIONS:
//
// Two coordinate spaces exist during drag-drop operations:
//
// 1. ORIGINAL SPACE (flatItems indices)
// - Row indices in the current flatItems array: 0..<flatItems.count
// - Used by: DragZones (invalidRowIndices, validDropRows), UI highlighting
// - Source row is always specified in original space
//
// 2. PROPOSED SPACE (UITableView post-removal)
// - Row indices after sourceRow is removed from the array
// - After removal: array has count-1 elements, valid insert positions are 0...(count-1)
// - Used by: UITableView delegate methods, computeValidDestinationRowsProposed return value
// - Proposed index N means: remove source, insert at position N in the remaining array
//
// FUNCTION REFERENCE:
// - simulateMove: Takes PROPOSED index returns post-move array + actual destination
// - computeValidDestinationRowsProposed: Returns PROPOSED indices (for tableView delegate)
// - calculateSortOrder: Takes row in POST-MOVE array (item already at destination)
// - calculateTravelDragZones/calculateCustomItemDragZones: Return ORIGINAL indices
//
// COORDINATE CONVERSION:
// - proposedToOriginal(proposed, sourceRow): Converts proposed original
// If proposed >= sourceRow: return proposed + 1 (shift up past removed source)
// If proposed < sourceRow: return proposed (unchanged)
// - originalToProposed(original, sourceRow): Converts original proposed
// If original == sourceRow: return nil (source has no proposed equivalent)
// If original > sourceRow: return original - 1 (shift down)
// If original < sourceRow: return original (unchanged)
//
// WHY THIS MATTERS:
// - DragZones are used for UI highlighting (which cells to dim/enable)
// - UI highlighting operates on the visible table, which uses ORIGINAL indices
// - But validation uses simulation, which operates in PROPOSED space
// - Getting this wrong causes visual bugs (wrong rows highlighted) or logic bugs
//
import Foundation
// MARK: - Pure Functions for Itinerary Reordering
/// Container for all pure reordering logic.
/// Using an enum (no cases) as a namespace for static functions.
enum ItineraryReorderingLogic {
// MARK: - Row Flattening
/// Default sortOrder for travel when lookup returns nil.
/// Travel defaults to after-games region (positive value).
private static let defaultTravelSortOrder: Double = 1.0
/// Flattens hierarchical day data into a single array of row items.
///
/// **SEMANTIC MODEL**: This function ignores `day.travelBefore` entirely.
/// Travel segments must be included in `day.items` with appropriate sortOrder.
///
/// For each day, rows are added in this order:
/// 1. Day header - "Day N · Date"
/// 2. Items with sortOrder < 0 (before games), sorted by sortOrder ascending
/// 3. Games - all games for this day (grouped as one row)
/// 4. Items with sortOrder >= 0 (after games), sorted by sortOrder ascending
///
/// - Parameters:
/// - days: Array of ItineraryDayData from the wrapper
/// - findTravelSortOrder: Closure to look up sortOrder for a travel segment
/// - Returns: Flattened array of ItineraryRowItem
static func flattenDays(
_ days: [ItineraryDayData],
findTravelSortOrder: (TravelSegment) -> Double?
) -> [ItineraryRowItem] {
var flatItems: [ItineraryRowItem] = []
for day in days {
// NOTE: day.travelBefore is IGNORED under semantic travel model.
// Travel must be in day.items with a sortOrder to appear.
// 1. Day header (structural anchor)
flatItems.append(.dayHeader(dayNumber: day.dayNumber, date: day.date))
// 2. Partition movable items around games boundary
// Tuple includes tiebreaker for stable sorting when sortOrders are equal
var beforeGames: [(sortOrder: Double, tiebreaker: Int, item: ItineraryRowItem)] = []
var afterGames: [(sortOrder: Double, tiebreaker: Int, item: ItineraryRowItem)] = []
var insertionOrder = 0
for row in day.items {
let sortOrder: Double
let tiebreaker = insertionOrder
insertionOrder += 1
switch row {
case .customItem(let item):
sortOrder = item.sortOrder
case .travel(let segment, _):
if let so = findTravelSortOrder(segment) {
sortOrder = so
} else {
// Travel without stored sortOrder gets a safe default.
// Log a warning in debug builds - this shouldn't happen in production.
#if DEBUG
print("⚠️ flattenDays: Travel segment missing sortOrder: \(segment.fromLocation.name)\(segment.toLocation.name). Using default: \(defaultTravelSortOrder)")
#endif
sortOrder = defaultTravelSortOrder
}
case .games, .dayHeader:
// These item types are not movable and handled separately.
// Skip explicitly - games are added after partitioning.
continue
}
if sortOrder < 0 {
beforeGames.append((sortOrder, tiebreaker, row))
} else {
afterGames.append((sortOrder, tiebreaker, row))
}
}
// Sort by sortOrder within each region, with stable tiebreaker
beforeGames.sort { ($0.sortOrder, $0.tiebreaker) < ($1.sortOrder, $1.tiebreaker) }
afterGames.sort { ($0.sortOrder, $0.tiebreaker) < ($1.sortOrder, $1.tiebreaker) }
flatItems.append(contentsOf: beforeGames.map { $0.item })
// 3. Games for this day (bundled as one row)
if !day.games.isEmpty {
flatItems.append(.games(day.games, dayNumber: day.dayNumber))
}
// 4. Items after games
flatItems.append(contentsOf: afterGames.map { $0.item })
}
return flatItems
}
// MARK: - Day Number Lookup
/// Finds which day a row at the given index belongs to.
///
/// Scans backwards from the row to find a `.dayHeader`.
/// Returns 1 as fallback if no header is found.
///
/// - Parameters:
/// - items: The flat array of row items
/// - row: The row index to look up
/// - Returns: The day number (1-indexed)
static func dayNumber(in items: [ItineraryRowItem], forRow row: Int) -> Int {
guard !items.isEmpty else { return 1 }
let clamped = min(max(0, row), items.count - 1)
for i in stride(from: clamped, through: 0, by: -1) {
if case .dayHeader(let dayNum, _) = items[i] {
return dayNum
}
}
return 1
}
/// Finds the row index of the day header for a specific day number.
///
/// - Parameters:
/// - items: The flat array of row items
/// - day: The day number to find
/// - Returns: The row index, or nil if not found
static func dayHeaderRow(in items: [ItineraryRowItem], forDay day: Int) -> Int? {
for (index, item) in items.enumerated() {
if case .dayHeader(let dayNum, _) = item, dayNum == day {
return index
}
}
return nil
}
/// Finds the row index of the travel segment on a specific day.
///
/// **SEMANTIC MODEL**: Does NOT use the embedded dayNumber in .travel().
/// Instead, scans the day section (between dayHeader(day) and dayHeader(day+1))
/// and returns the first travel row found.
///
/// - Parameters:
/// - items: The flat array of row items
/// - day: The day number to find
/// - Returns: The row index, or nil if no travel on that day
static func travelRow(in items: [ItineraryRowItem], forDay day: Int) -> Int? {
// Find the day header row
guard let headerRow = dayHeaderRow(in: items, forDay: day) else {
return nil
}
// Scan forward until next day header, looking for travel
for i in (headerRow + 1)..<items.count {
switch items[i] {
case .dayHeader:
// Reached next day, no travel found
return nil
case .travel:
return i
default:
continue
}
}
return nil
}
/// Legacy version that uses embedded dayNumber (unreliable under semantic model).
@available(*, deprecated, message: "Use travelRow(in:forDay:) which uses semantic day lookup")
static func travelRowByEmbeddedDay(in items: [ItineraryRowItem], forDay day: Int) -> Int? {
for (index, item) in items.enumerated() {
if case .travel(_, let dayNum) = item, dayNum == day {
return index
}
}
return nil
}
/// Determines which day a travel segment belongs to at a given row position.
///
/// **SEMANTIC MODEL**: Uses backward scan to find the nearest preceding dayHeader.
/// This is consistent with how all movable items determine their day.
///
/// - Parameters:
/// - row: The row index of the travel
/// - items: The flat array of row items
/// - Returns: The day number the travel belongs to
static func dayForTravelAt(row: Int, in items: [ItineraryRowItem]) -> Int {
// Semantic model: scan backward to find the day this item belongs to
// (same logic as dayNumber)
return dayNumber(in: items, forRow: row)
}
// MARK: - Move Simulation
/// Result of simulating a move operation.
struct SimulatedMove {
let items: [ItineraryRowItem]
let destinationRowInNewArray: Int
let didMove: Bool // false if move was invalid/no-op
}
/// Simulates UITableView move semantics with bounds safety.
///
/// UITableView moves work as: remove at sourceRow from ORIGINAL array,
/// then insert at destinationProposedRow in the NEW array (post-removal coordinate space).
///
/// - Parameters:
/// - original: The original flat items array
/// - sourceRow: Where the item is being moved from
/// - destinationProposedRow: Where it's being moved to (in post-removal space)
/// - Returns: The new array, the actual destination row, and whether the move occurred
static func simulateMove(
original: [ItineraryRowItem],
sourceRow: Int,
destinationProposedRow: Int
) -> SimulatedMove {
// Bounds safety: return original unchanged if sourceRow is invalid
guard sourceRow >= 0 && sourceRow < original.count else {
return SimulatedMove(items: original, destinationRowInNewArray: sourceRow, didMove: false)
}
var items = original
let moving = items.remove(at: sourceRow)
let clampedDest = min(max(0, destinationProposedRow), items.count)
items.insert(moving, at: clampedDest)
return SimulatedMove(items: items, destinationRowInNewArray: clampedDest, didMove: true)
}
// MARK: - Coordinate Space Conversion
/// Converts a proposed destination index to the equivalent original index.
///
/// UITableView move semantics: remove at sourceRow first, then insert at proposed position.
/// This means proposed indices >= sourceRow map to original indices + 1.
///
/// - Parameters:
/// - proposed: Index in post-removal coordinate space
/// - sourceRow: The row being moved (in original space)
/// - Returns: Equivalent index in original coordinate space
static func proposedToOriginal(_ proposed: Int, sourceRow: Int) -> Int {
if proposed >= sourceRow {
return proposed + 1
} else {
return proposed
}
}
/// Converts an original index to the equivalent proposed destination index.
///
/// - Parameters:
/// - original: Index in original coordinate space
/// - sourceRow: The row being moved (in original space)
/// - Returns: Equivalent index in post-removal coordinate space, or nil if original == sourceRow
static func originalToProposed(_ original: Int, sourceRow: Int) -> Int? {
if original == sourceRow {
// The dragged item itself has no proposed equivalent
return nil
} else if original > sourceRow {
return original - 1
} else {
return original
}
}
// MARK: - Sort Order Calculation
/// Calculates the sortOrder for an item dropped at the given row position.
///
/// Uses **midpoint insertion** algorithm to avoid renumbering existing items:
/// - Between items A(1.0) and B(2.0): new sortOrder = 1.5
/// - First item in empty day: sortOrder = 1.0
/// - After last item: sortOrder = last + 1.0
/// - Before first item: sortOrder = first / 2.0
///
/// **Region classification**:
/// - `row < gamesRow` => before-games region => sortOrder < 0
/// - `row > gamesRow` => after-games region => sortOrder >= 0
/// - `row == gamesRow` => treated as after-games (cannot drop ON games row)
/// - No games on day => after-games region (sortOrder >= 0)
///
/// - Parameters:
/// - items: The flat array of row items (with moved item already in place)
/// - row: The row index where the item was dropped
/// - findTravelSortOrder: Closure to look up sortOrder for travel segments
/// - Returns: The calculated sortOrder
static func calculateSortOrder(
in items: [ItineraryRowItem],
at row: Int,
findTravelSortOrder: (TravelSegment) -> Double?
) -> Double {
let day = dayNumber(in: items, forRow: row)
// Find games row for this day (if any)
var gamesRow: Int? = nil
for i in 0..<items.count {
if case .games(_, let d) = items[i], d == day {
gamesRow = i
break
}
if case .dayHeader(let d, _) = items[i], d > day {
break
}
}
// Strict region classification:
// - row < gamesRow => before-games (negative sortOrder)
// - row >= gamesRow OR no games => after-games (positive sortOrder)
let isBeforeGames: Bool
if let gr = gamesRow {
isBeforeGames = row < gr
} else {
isBeforeGames = false // No games means everything is "after games"
}
/// Get sortOrder from a movable item (custom item or travel)
func movableSortOrder(_ idx: Int) -> Double? {
guard idx >= 0 && idx < items.count else { return nil }
switch items[idx] {
case .customItem(let item):
return item.sortOrder
case .travel(let segment, _):
return findTravelSortOrder(segment)
default:
return nil
}
}
/// Scan backward from start, stopping at boundaries, looking for movable items in the same region
func scanBackward(from start: Int) -> Double? {
var i = start
while i >= 0 {
// Stop at day boundaries
if case .dayHeader(let d, _) = items[i] {
if d != day { break }
break // Stop at own day header too
}
// Stop at games boundary (don't cross into other region)
if case .games(_, let d) = items[i], d == day { break }
if let v = movableSortOrder(i) {
// Only return values in the correct region
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i -= 1
}
return nil
}
/// Scan forward from start, stopping at boundaries, looking for movable items in the same region
func scanForward(from start: Int) -> Double? {
var i = start
while i < items.count {
// Stop at day boundaries
if case .dayHeader(let d, _) = items[i] {
if d != day { break }
break // Stop at any day header
}
// Stop at games boundary (don't cross into other region)
if case .games(_, let d) = items[i], d == day { break }
if let v = movableSortOrder(i) {
// Only return values in the correct region
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i += 1
}
return nil
}
if isBeforeGames {
// Above games: sortOrder should be negative
let prev = scanBackward(from: row - 1)
let next = scanForward(from: row + 1)
let upperBound: Double = 0.0 // Games boundary
switch (prev, next) {
case (nil, nil):
return -1.0
case (let p?, nil):
return (p + upperBound) / 2.0
case (nil, let n?):
// First item before games: place it before the next item.
// n should always be negative (scanForward filters for region).
if n >= 0 {
// This shouldn't happen - scanForward should only return negative values
// in before-games region. Return safe default and assert in debug.
assertionFailure("Before-games region has non-negative sortOrder: \(n)")
return -1.0
}
// Place before n by subtracting 1.0 (simpler and more consistent than min(n/2, n-1))
return n - 1.0
case (let p?, let n?):
return (p + n) / 2.0
}
} else {
// Below games: sortOrder should be >= 0
let prev = scanBackward(from: row - 1) ?? 0.0
let next = scanForward(from: row + 1)
switch next {
case nil:
return (prev == 0.0) ? 1.0 : (prev + 1.0)
case let n?:
return (prev + n) / 2.0
}
}
}
// MARK: - Valid Drop Computation
/// Computes all valid destination rows in **proposed** coordinate space.
///
/// For BOTH travel and custom items, we:
/// 1. Simulate the move
/// 2. Compute the resulting (day, sortOrder)
/// 3. Validate with ItineraryConstraints
///
/// This ensures drop targets match what will actually be persisted.
///
/// - Parameters:
/// - flatItems: The current flat items array
/// - sourceRow: The row being moved
/// - dragged: The item being dragged
/// - travelValidRanges: Valid day ranges for travel segments
/// - constraints: The constraint system for validation
/// - findTravelItem: Closure to find ItineraryItem for a travel segment
/// - makeTravelItem: Closure to create a default ItineraryItem for travel
/// - findCustomItem: Closure to find ItineraryItem for a custom item row
/// - findTravelSortOrder: Closure to find sortOrder for travel segments
/// - Returns: Array of valid row indices in proposed coordinate space
static func computeValidDestinationRowsProposed(
flatItems: [ItineraryRowItem],
sourceRow: Int,
dragged: ItineraryRowItem,
travelValidRanges: [String: ClosedRange<Int>],
constraints: ItineraryConstraints?,
findTravelItem: (TravelSegment) -> ItineraryItem?,
makeTravelItem: (TravelSegment) -> ItineraryItem,
findTravelSortOrder: @escaping (TravelSegment) -> Double?
) -> [Int] {
let maxProposed = max(0, flatItems.count - 1)
guard maxProposed > 0 else { return [] }
switch dragged {
case .customItem(let customItem):
// Custom items use the same simulation+validation approach as travel
guard let constraints = constraints else {
// No constraint engine: allow all rows except 0 and day headers
return (1...maxProposed).filter { proposedRow in
let simulated = simulateMove(original: flatItems, sourceRow: sourceRow, destinationProposedRow: proposedRow)
guard simulated.didMove else { return false }
// Don't allow dropping ON a day header
if case .dayHeader = simulated.items[simulated.destinationRowInNewArray] {
return false
}
return true
}
}
var valid: [Int] = []
valid.reserveCapacity(maxProposed)
for proposedRow in 1...maxProposed {
let simulated = simulateMove(original: flatItems, sourceRow: sourceRow, destinationProposedRow: proposedRow)
guard simulated.didMove else { continue }
let destRowInSim = simulated.destinationRowInNewArray
// Don't allow dropping ON a day header
if case .dayHeader = simulated.items[destRowInSim] {
continue
}
let day = dayNumber(in: simulated.items, forRow: destRowInSim)
let sortOrder = calculateSortOrder(in: simulated.items, at: destRowInSim, findTravelSortOrder: findTravelSortOrder)
// Create a temporary item model with the computed position
let testItem = ItineraryItem(
id: customItem.id,
tripId: customItem.tripId,
day: day,
sortOrder: sortOrder,
kind: customItem.kind
)
if constraints.isValidPosition(for: testItem, day: day, sortOrder: sortOrder) {
valid.append(proposedRow)
}
}
return valid
case .travel(let segment, _):
let travelId = "travel:\(segment.fromLocation.name.lowercased())->\(segment.toLocation.name.lowercased())"
let validDayRange = travelValidRanges[travelId]
// Use existing model if available, otherwise create a default
let model = findTravelItem(segment) ?? makeTravelItem(segment)
guard let constraints = constraints else {
// No constraint engine, allow all rows except 0 and day headers
return (1...maxProposed).filter { proposedRow in
let simulated = simulateMove(original: flatItems, sourceRow: sourceRow, destinationProposedRow: proposedRow)
guard simulated.didMove else { return false }
if case .dayHeader = simulated.items[simulated.destinationRowInNewArray] {
return false
}
return true
}
}
var valid: [Int] = []
valid.reserveCapacity(maxProposed)
for proposedRow in 1...maxProposed {
let simulated = simulateMove(original: flatItems, sourceRow: sourceRow, destinationProposedRow: proposedRow)
guard simulated.didMove else { continue }
let destRowInSim = simulated.destinationRowInNewArray
// Don't allow dropping ON a day header
if case .dayHeader = simulated.items[destRowInSim] {
continue
}
let day = dayNumber(in: simulated.items, forRow: destRowInSim)
// Check day range constraint (quick rejection)
if let range = validDayRange, !range.contains(day) {
continue
}
// Check sortOrder constraint
let sortOrder = calculateSortOrder(in: simulated.items, at: destRowInSim, findTravelSortOrder: findTravelSortOrder)
// Create a testItem with computed day/sortOrder (like custom items do)
// This ensures constraints.isValidPosition sees the actual proposed position
let testItem = ItineraryItem(
id: model.id,
tripId: model.tripId,
day: day,
sortOrder: sortOrder,
kind: model.kind
)
if constraints.isValidPosition(for: testItem, day: day, sortOrder: sortOrder) {
valid.append(proposedRow)
}
}
return valid
default:
// Day headers and games can't be moved
return []
}
}
// MARK: - Drag Zones
/// Result of calculating drag zones for visual feedback.
///
/// **COORDINATE SPACE**: All indices are in ORIGINAL coordinate space (current flatItems indices).
/// This is what the UI needs for highlighting rows before the move occurs.
struct DragZones {
/// Rows that should be dimmed/disabled in the UI (original indices)
let invalidRowIndices: Set<Int>
/// Rows where drop is allowed (original indices)
let validDropRows: [Int]
/// Game IDs that act as barriers for this drag
let barrierGameIds: Set<String>
}
/// Calculates drag zones for a travel segment using simulation+validation.
///
/// This ensures UI feedback matches what will actually be accepted on drop.
/// Returns indices in ORIGINAL coordinate space for direct use in UI highlighting.
///
/// - Parameters:
/// - segment: The travel segment being dragged
/// - sourceRow: The current row of the travel (original index)
/// - flatItems: The current flat items array
/// - travelValidRanges: Valid day ranges for travel segments
/// - constraints: The constraint system
/// - findTravelItem: Closure to find ItineraryItem for travel
/// - makeTravelItem: Closure to create a default ItineraryItem for travel
/// - findTravelSortOrder: Closure to find sortOrder for travel
/// - Returns: Drag zones with invalid rows, valid rows, and barrier game IDs (all in original space)
static func calculateTravelDragZones(
segment: TravelSegment,
sourceRow: Int,
flatItems: [ItineraryRowItem],
travelValidRanges: [String: ClosedRange<Int>],
constraints: ItineraryConstraints?,
findTravelItem: (TravelSegment) -> ItineraryItem?,
makeTravelItem: (TravelSegment) -> ItineraryItem,
findTravelSortOrder: @escaping (TravelSegment) -> Double?
) -> DragZones {
// Get valid rows in PROPOSED coordinate space
let validRowsProposed = computeValidDestinationRowsProposed(
flatItems: flatItems,
sourceRow: sourceRow,
dragged: .travel(segment, dayNumber: 0), // dayNumber doesn't matter for validation
travelValidRanges: travelValidRanges,
constraints: constraints,
findTravelItem: findTravelItem,
makeTravelItem: makeTravelItem,
findTravelSortOrder: findTravelSortOrder
)
// Convert valid rows from proposed to original coordinate space
let validRowsOriginal = validRowsProposed.map { proposedToOriginal($0, sourceRow: sourceRow) }
let validSet = Set(validRowsOriginal)
// Compute invalid rows in original coordinate space
var invalidRows = Set<Int>()
for i in 0..<flatItems.count {
if i == sourceRow {
// The source row itself is neither valid nor invalid - it's being dragged
continue
}
if !validSet.contains(i) {
invalidRows.insert(i)
}
}
// Find barrier games using constraints
var barrierGameIds = Set<String>()
if let travelItem = findTravelItem(segment),
let constraints = constraints {
let barriers = constraints.barrierGames(for: travelItem)
barrierGameIds = Set(barriers.compactMap { $0.gameId })
}
return DragZones(
invalidRowIndices: invalidRows,
validDropRows: validRowsOriginal,
barrierGameIds: barrierGameIds
)
}
/// Calculates drag zones for a custom item using simulation+validation.
///
/// This ensures UI feedback matches what will actually be accepted on drop.
/// Returns indices in ORIGINAL coordinate space for direct use in UI highlighting.
///
/// - Parameters:
/// - item: The custom item being dragged
/// - sourceRow: The current row of the item (original index)
/// - flatItems: The current flat items array
/// - constraints: The constraint system
/// - findTravelSortOrder: Closure to find sortOrder for travel
/// - Returns: Drag zones with invalid rows and valid rows (all in original space)
static func calculateCustomItemDragZones(
item: ItineraryItem,
sourceRow: Int,
flatItems: [ItineraryRowItem],
constraints: ItineraryConstraints?,
findTravelSortOrder: @escaping (TravelSegment) -> Double?
) -> DragZones {
// Get valid rows in PROPOSED coordinate space
let validRowsProposed = computeValidDestinationRowsProposed(
flatItems: flatItems,
sourceRow: sourceRow,
dragged: .customItem(item),
travelValidRanges: [:], // Custom items don't use travel ranges
constraints: constraints,
findTravelItem: { _ in nil },
makeTravelItem: { _ in
// This won't be called for custom items
fatalError("makeTravelItem called for custom item")
},
findTravelSortOrder: findTravelSortOrder
)
// Convert valid rows from proposed to original coordinate space
let validRowsOriginal = validRowsProposed.map { proposedToOriginal($0, sourceRow: sourceRow) }
let validSet = Set(validRowsOriginal)
// Compute invalid rows in original coordinate space
var invalidRows = Set<Int>()
for i in 0..<flatItems.count {
if i == sourceRow {
// The source row itself is neither valid nor invalid - it's being dragged
continue
}
if !validSet.contains(i) {
invalidRows.insert(i)
}
}
return DragZones(
invalidRowIndices: invalidRows,
validDropRows: validRowsOriginal,
barrierGameIds: [] // No barrier highlighting for custom items
)
}
// MARK: - Legacy Compatibility
/// Legacy version of calculateTravelDragZones that doesn't require sourceRow.
/// Uses day-range-based calculation only.
///
/// - Note: Prefer the version with sourceRow for accurate validation.
@available(*, deprecated, message: "Use calculateTravelDragZones(segment:sourceRow:...) for accurate validation")
static func calculateTravelDragZones(
segment: TravelSegment,
flatItems: [ItineraryRowItem],
travelValidRanges: [String: ClosedRange<Int>],
constraints: ItineraryConstraints?,
findTravelItem: (TravelSegment) -> ItineraryItem?
) -> DragZones {
let travelId = "travel:\(segment.fromLocation.name.lowercased())->\(segment.toLocation.name.lowercased())"
guard let validRange = travelValidRanges[travelId] else {
return DragZones(invalidRowIndices: [], validDropRows: [], barrierGameIds: [])
}
var invalidRows = Set<Int>()
var validRows: [Int] = []
for (index, rowItem) in flatItems.enumerated() {
let dayNum: Int
switch rowItem {
case .dayHeader(let d, _):
dayNum = d
case .games(_, let d):
dayNum = d
case .travel(_, let d):
dayNum = d
case .customItem(let item):
dayNum = item.day
}
if validRange.contains(dayNum) {
validRows.append(index)
} else {
invalidRows.insert(index)
}
}
// Find barrier games using constraints
var barrierGameIds = Set<String>()
if let travelItem = findTravelItem(segment),
let constraints = constraints {
let barriers = constraints.barrierGames(for: travelItem)
barrierGameIds = Set(barriers.compactMap { $0.gameId })
}
return DragZones(
invalidRowIndices: invalidRows,
validDropRows: validRows,
barrierGameIds: barrierGameIds
)
}
/// Legacy version of calculateCustomItemDragZones that doesn't require sourceRow.
///
/// - Note: Prefer the version with sourceRow for accurate validation.
@available(*, deprecated, message: "Use calculateCustomItemDragZones(item:sourceRow:...) for accurate validation")
static func calculateCustomItemDragZones(
item: ItineraryItem,
flatItems: [ItineraryRowItem]
) -> DragZones {
var invalidRows = Set<Int>()
var validRows: [Int] = []
for (index, rowItem) in flatItems.enumerated() {
if case .dayHeader = rowItem {
invalidRows.insert(index)
} else {
validRows.append(index)
}
}
return DragZones(
invalidRowIndices: invalidRows,
validDropRows: validRows,
barrierGameIds: []
)
}
// MARK: - Utility Functions
/// Finds the nearest value in a sorted array using binary search.
///
/// - Parameters:
/// - sorted: A sorted array of integers
/// - target: The target value to find the nearest match for
/// - Returns: The nearest value, or nil if array is empty
static func nearestValue(in sorted: [Int], to target: Int) -> Int? {
guard !sorted.isEmpty else { return nil }
var low = 0
var high = sorted.count
// Binary search for insertion point
while low < high {
let mid = (low + high) / 2
if sorted[mid] < target {
low = mid + 1
} else {
high = mid
}
}
let after = (low < sorted.count) ? sorted[low] : nil
let before = (low > 0) ? sorted[low - 1] : nil
switch (before, after) {
case let (b?, a?):
// Both exist, return the closer one
return (target - b) <= (a - target) ? b : a
case let (b?, nil):
return b
case let (nil, a?):
return a
default:
return nil
}
}
/// Calculates target destination with constraint snapping.
///
/// If the proposed row is valid, returns it. Otherwise, snaps to nearest valid row.
///
/// **COORDINATE SPACE**: This function expects all indices in PROPOSED coordinate space.
/// The caller must ensure validDestinationRows comes from computeValidDestinationRowsProposed.
///
/// **UX RULE**: Row 0 is forbidden (always a day header). If proposedRow <= 0, it's clamped to 1.
/// This is a UX-level rule, not a semantic constraint - day headers cannot receive drops.
///
/// - Parameters:
/// - proposedRow: The user's proposed drop position (in proposed coordinate space)
/// - validDestinationRows: Pre-computed valid rows from computeValidDestinationRowsProposed
/// - sourceRow: The original row (fallback if no valid destination found)
/// - Returns: The target row to use (in proposed coordinate space)
///
/// - Note: Uses O(n) contains check. For repeated calls, consider passing a Set instead.
/// However, validDestinationRows is typically small (< 50 items), so this is fine.
static func calculateTargetRow(
proposedRow: Int,
validDestinationRows: [Int],
sourceRow: Int
) -> Int {
// UX rule: forbid dropping at absolute top (row 0 is always a day header)
var row = proposedRow
if row <= 0 { row = 1 }
// If already valid, use it
if validDestinationRows.contains(row) {
return row
}
// Snap to nearest valid destination (validDestinationRows must be sorted for binary search)
return nearestValue(in: validDestinationRows, to: row) ?? sourceRow
}
}

View File

@@ -462,20 +462,8 @@ final class ItineraryTableViewController: UITableViewController {
/// Transforms hierarchical day data into a flat row list and refreshes the table.
///
/// This is the core data transformation method. It takes structured `[ItineraryDayData]`
/// from the wrapper and flattens it into `[ItineraryRowItem]` for UITableView display.
///
/// **Flattening Algorithm:**
/// For each day, rows are added in this exact order:
/// 1. Travel (if arriving this day) - appears visually BEFORE the day header
/// 2. Day header (with Add button) - "Day N · Date" + tappable Add button
/// 3. Games - all games for this day (grouped as one row)
/// 4. Custom items - user-added items, already sorted by sortOrder
///
/// **Why this order matters:**
/// - Travel before header creates visual grouping: "you travel, then you're on day N"
/// - Add button is part of header row (can't drag items between header and Add)
/// - Games before custom items preserves the "trip-determined, then user-added" hierarchy
/// Delegates to `ItineraryReorderingLogic.flattenDays` for the pure transformation,
/// then updates the table view.
///
/// - Parameters:
/// - days: Array of ItineraryDayData from ItineraryTableViewWrapper
@@ -489,99 +477,33 @@ final class ItineraryTableViewController: UITableViewController {
self.travelValidRanges = travelValidRanges
self.allItineraryItems = itineraryItems
self.tripDayCount = days.count
// Rebuild constraints with new data
self.constraints = ItineraryConstraints(tripDayCount: tripDayCount, items: itineraryItems)
flatItems = []
for day in days {
// 1. Travel that arrives on this day (renders BEFORE the day header)
// Example: "Detroit Milwaukee" appears above "Day 3" header
if let travel = day.travelBefore {
flatItems.append(.travel(travel, dayNumber: day.dayNumber))
}
// 2. Day header with Add button (structural anchor - cannot be moved or deleted)
// Add button is embedded in the header to prevent items being dragged between them
flatItems.append(.dayHeader(dayNumber: day.dayNumber, date: day.date))
// 3. Movable items (travel + custom) split around games boundary.
// Convention: sortOrder < 0 renders ABOVE games; sortOrder >= 0 renders BELOW games.
var beforeGames: [ItineraryRowItem] = []
var afterGames: [ItineraryRowItem] = []
for row in day.items {
let so: Double?
switch row {
case .customItem(let item):
so = item.sortOrder
case .travel(let segment, _):
// Travel sortOrder is stored in itineraryItems (kind: .travel)
so = findItineraryItem(for: segment)?.sortOrder
default:
so = nil
}
guard let sortOrder = so else { continue }
if sortOrder < 0 {
beforeGames.append(row)
} else {
afterGames.append(row)
}
}
flatItems.append(contentsOf: beforeGames)
// 4. Games for this day (bundled as one row, not individually reorderable)
if !day.games.isEmpty {
flatItems.append(.games(day.games, dayNumber: day.dayNumber))
}
flatItems.append(contentsOf: afterGames)
// Use pure function for flattening
flatItems = ItineraryReorderingLogic.flattenDays(days) { [weak self] segment in
self?.findItineraryItem(for: segment)?.sortOrder
}
tableView.reloadData()
}
// MARK: - Row-to-Day Mapping Helpers
// MARK: - Row-to-Day Mapping Helpers (delegating to pure functions)
/// Finds which day a row at the given index belongs to.
///
/// Scans backwards from the row to find either:
/// - A `.dayHeader` that's the day
/// - A `.travel` uses the dayNumber stored in the travel item
///
/// This is used when a custom item is dropped to determine its new day.
private func dayNumber(forRow row: Int) -> Int {
for i in stride(from: row, through: 0, by: -1) {
if case .dayHeader(let dayNum, _) = flatItems[i] {
return dayNum
}
}
return 1
ItineraryReorderingLogic.dayNumber(in: flatItems, forRow: row)
}
/// Finds the row index of the day header for a specific day number.
/// Returns nil if no header exists for that day (shouldn't happen in valid data).
private func dayHeaderRow(forDay day: Int) -> Int? {
for (index, item) in flatItems.enumerated() {
if case .dayHeader(let dayNum, _) = item, dayNum == day {
return index
}
}
return nil
ItineraryReorderingLogic.dayHeaderRow(in: flatItems, forDay: day)
}
/// Finds the row index of the travel segment arriving on a specific day.
/// Returns nil if no travel arrives on that day.
private func travelRow(forDay day: Int) -> Int? {
for (index, item) in flatItems.enumerated() {
if case .travel(_, let dayNum) = item, dayNum == day {
return index
}
}
return nil
ItineraryReorderingLogic.travelRow(in: flatItems, forDay: day)
}
// MARK: - Drag State Management
@@ -647,83 +569,27 @@ final class ItineraryTableViewController: UITableViewController {
}
/// Calculates invalid zones for a travel segment drag.
///
/// Travel items have hard constraints:
/// - Can't leave before finishing games in departure city
/// - Must arrive by the first game in destination city
///
/// Invalid zones are any rows outside the valid day range.
/// Delegates to pure function and applies results to instance state.
private func calculateTravelDragZones(segment: TravelSegment) {
let travelId = "travel:\(segment.fromLocation.name.lowercased())->\(segment.toLocation.name.lowercased())"
// Get valid day range from pre-calculated ranges
guard let validRange = travelValidRanges[travelId] else {
invalidRowIndices = []
validDropRows = []
barrierGameIds = []
return
}
// Calculate invalid and valid row indices based on day range
// Pre-calculate ALL valid positions for stable drag behavior
var invalidRows = Set<Int>()
var validRows: [Int] = []
for (index, rowItem) in flatItems.enumerated() {
let dayNum: Int
switch rowItem {
case .dayHeader(let d, _):
dayNum = d
case .games(_, let d):
dayNum = d
case .travel(_, let d):
dayNum = d
case .customItem(let item):
dayNum = item.day
}
if validRange.contains(dayNum) {
validRows.append(index)
} else {
invalidRows.insert(index)
}
}
invalidRowIndices = invalidRows
validDropRows = validRows // Already sorted since we iterate in order
// Find barrier games using ItineraryConstraints
if let travelItem = findItineraryItem(for: segment),
let constraints = constraints {
let barriers = constraints.barrierGames(for: travelItem)
barrierGameIds = Set(barriers.compactMap { $0.gameId })
} else {
barrierGameIds = []
}
let zones = ItineraryReorderingLogic.calculateTravelDragZones(
segment: segment,
flatItems: flatItems,
travelValidRanges: travelValidRanges,
constraints: constraints,
findTravelItem: { [weak self] segment in self?.findItineraryItem(for: segment) }
)
invalidRowIndices = zones.invalidRowIndices
validDropRows = zones.validDropRows
barrierGameIds = zones.barrierGameIds
}
/// Calculates invalid zones for a custom item drag.
///
/// Custom items can go on any day, but we mark certain positions as
/// less ideal (e.g., directly on day headers or before travel).
/// Delegates to pure function and applies results to instance state.
private func calculateCustomItemDragZones(item: ItineraryItem) {
// Custom items are flexible - can go anywhere except ON day headers
// Pre-calculate ALL valid row indices for stable drag behavior
var invalidRows = Set<Int>()
var validRows: [Int] = []
for (index, rowItem) in flatItems.enumerated() {
if case .dayHeader = rowItem {
invalidRows.insert(index)
} else {
// All non-header rows are valid drop targets
validRows.append(index)
}
}
invalidRowIndices = invalidRows
validDropRows = validRows // Already sorted since we iterate in order
barrierGameIds = [] // No barrier highlighting for custom items
let zones = ItineraryReorderingLogic.calculateCustomItemDragZones(item: item, flatItems: flatItems)
invalidRowIndices = zones.invalidRowIndices
validDropRows = zones.validDropRows
barrierGameIds = zones.barrierGameIds
}
/// Finds the ItineraryItem model for a travel segment.
@@ -914,52 +780,16 @@ final class ItineraryTableViewController: UITableViewController {
}
/// Determines which day a travel segment belongs to at a given row position.
///
/// Travel conceptually "arrives on" a day - it appears BEFORE that day's header.
/// So we scan FORWARD from the travel's position to find the next day header.
///
/// Example:
/// ```
/// [0] Travel: Detroit Milwaukee If travel is here...
/// [1] Day 3 header ...it belongs to Day 3
/// ```
private func dayForTravelAt(row: Int) -> Int {
// Scan forward to find the day header this travel precedes
for i in row..<flatItems.count {
if case .dayHeader(let dayNum, _) = flatItems[i] {
return dayNum
}
}
// Fallback: scan backwards to find any day header
for i in stride(from: flatItems.count - 1, through: 0, by: -1) {
if case .dayHeader(let dayNum, _) = flatItems[i] {
return dayNum
}
}
return 1 // Ultimate fallback
ItineraryReorderingLogic.dayForTravelAt(row: row, in: flatItems)
}
/// Called DURING a drag to validate and potentially modify the drop position.
///
/// This is the core drag constraint logic. UITableView calls this continuously
/// as the user drags, allowing us to redirect the drop to a valid position.
///
/// **Key behaviors:**
///
/// **Travel segments:** Constrained to their valid day range. If user tries to
/// drag outside the range, we snap to the nearest valid day. This prevents
/// impossible itineraries (e.g., arriving before you've left).
///
/// **Custom items:** Can go almost anywhere, but we prevent:
/// - Dropping ON a day header (redirect to after header)
/// - Dropping BEFORE travel at start of day (redirect to after header)
///
/// **Fixed items:** Day headers, games, add buttons return their source position
/// (they never actually drag since canMoveRowAt returns false).
///
/// **Drag State Management:**
/// - First call: Initializes drag state, calculates invalid zones, triggers pickup haptic
/// - Subsequent calls: Checks zone transitions for haptic feedback
/// Delegates constraint logic to pure functions, handles only UIKit-specific concerns:
/// - Drag state initialization (first call)
/// - Haptic/visual feedback
/// - Converting pure function results to IndexPath
///
/// - Parameters:
/// - sourceIndexPath: Where the item is being dragged FROM
@@ -970,256 +800,59 @@ final class ItineraryTableViewController: UITableViewController {
targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath,
toProposedIndexPath proposedDestinationIndexPath: IndexPath
) -> IndexPath {
let sourceRow = sourceIndexPath.row
let item = flatItems[sourceRow]
// Drag start detection
// Drag start detection - initialize state and compute valid destinations
if draggingItem == nil {
beginDrag(at: sourceIndexPath)
validDestinationRowsProposed = computeValidDestinationRowsProposed(sourceRow: sourceRow, dragged: item)
}
// Clamp proposed row
var proposedRow = proposedDestinationIndexPath.row
// Avoid absolute top (keeps UX sane)
if proposedRow <= 0 { proposedRow = 1 }
proposedRow = min(max(0, proposedRow), max(0, flatItems.count - 1))
proposedRow = min(max(1, proposedRow), max(0, flatItems.count - 1))
// Haptics / visuals
checkZoneTransition(at: proposedRow)
// If already valid, allow it.
if validDestinationRowsProposed.contains(proposedRow) {
return IndexPath(row: proposedRow, section: 0)
}
// Snap to nearest valid destination (proposed coordinate space)
guard let snapped = nearestValue(in: validDestinationRowsProposed, to: proposedRow) else {
return sourceIndexPath
}
return IndexPath(row: snapped, section: 0)
// Use pure function for target calculation
let targetRow = ItineraryReorderingLogic.calculateTargetRow(
proposedRow: proposedRow,
validDestinationRows: validDestinationRowsProposed,
sourceRow: sourceRow
)
return IndexPath(row: targetRow, section: 0)
}
// MARK: - Drag Destination Precomputation (semantic day + sortOrder)
/// Nearest value in a sorted Int array to the target (binary search).
private func nearestValue(in sorted: [Int], to target: Int) -> Int? {
guard !sorted.isEmpty else { return nil }
var low = 0
var high = sorted.count
while low < high {
let mid = (low + high) / 2
if sorted[mid] < target { low = mid + 1 } else { high = mid }
}
let after = (low < sorted.count) ? sorted[low] : nil
let before = (low > 0) ? sorted[low - 1] : nil
switch (before, after) {
case let (b?, a?):
return (target - b) <= (a - target) ? b : a
case let (b?, nil):
return b
case let (nil, a?):
return a
default:
return nil
}
}
/// Computes all valid destination rows in **proposed** coordinate space (UIKit's coordinate space during drag).
/// We simulate the move and validate using semantic constraints: (day, sortOrder).
// MARK: - Drag Destination Precomputation (delegating to pure functions)
/// Computes all valid destination rows in **proposed** coordinate space.
/// Delegates to pure function with closures for model lookups.
private func computeValidDestinationRowsProposed(sourceRow: Int, dragged: ItineraryRowItem) -> [Int] {
// Proposed rows are in the array AFTER removing the source row.
let maxProposed = max(0, flatItems.count - 1)
guard maxProposed > 0 else { return [] }
switch dragged {
case .customItem:
// Custom items can go basically anywhere (including before headers = "between days").
// Keep row 0 blocked.
return Array(1...maxProposed)
case .travel(let segment, _):
let travelId = "travel:\(segment.fromLocation.name.lowercased())->\(segment.toLocation.name.lowercased())"
let validDayRange = travelValidRanges[travelId]
// Use existing itinerary model if available (for constraints)
let model: ItineraryItem = findItineraryItem(for: segment) ?? ItineraryItem(
tripId: allItineraryItems.first?.tripId ?? UUID(),
day: 1,
sortOrder: 0,
kind: .travel(TravelInfo(fromCity: segment.fromLocation.name, toCity: segment.toLocation.name, distanceMeters: segment.distanceMeters, durationSeconds: segment.durationSeconds))
)
guard let constraints else {
// If no constraint engine, allow all rows (except 0)
return Array(1...maxProposed)
}
var valid: [Int] = []
valid.reserveCapacity(maxProposed)
for proposedRow in 1...maxProposed {
let simulated = simulateMove(original: flatItems, sourceRow: sourceRow, destinationProposedRow: proposedRow)
let destRowInSim = simulated.destinationRowInNewArray
let day = dayNumber(in: simulated.items, forRow: destRowInSim)
if let r = validDayRange, !r.contains(day) {
continue
}
let sortOrder = calculateSortOrder(in: simulated.items, at: destRowInSim)
if constraints.isValidPosition(for: model, day: day, sortOrder: sortOrder) {
valid.append(proposedRow)
}
}
return valid
default:
return []
}
}
private struct SimulatedMove {
let items: [ItineraryRowItem]
let destinationRowInNewArray: Int
}
/// Simulate UITableView move semantics: remove at sourceRow from ORIGINAL array, then insert at destinationProposedRow
/// in the NEW array (post-removal coordinate space).
private func simulateMove(original: [ItineraryRowItem], sourceRow: Int, destinationProposedRow: Int) -> SimulatedMove {
var items = original
let moving = items.remove(at: sourceRow)
let clampedDest = min(max(0, destinationProposedRow), items.count)
items.insert(moving, at: clampedDest)
return SimulatedMove(items: items, destinationRowInNewArray: clampedDest)
}
/// Day number lookup within an arbitrary flat array (used during simulation).
private func dayNumber(in items: [ItineraryRowItem], forRow row: Int) -> Int {
guard !items.isEmpty else { return 1 }
let clamped = min(max(0, row), items.count - 1)
for i in stride(from: clamped, through: 0, by: -1) {
if case .dayHeader(let dayNum, _) = items[i] {
return dayNum
}
}
return 1
}
/// Calculates sortOrder for insertion at a row within an arbitrary flat array.
/// Uses the same convention as the main function:
/// - sortOrder < 0 => above games
/// - sortOrder >= 0 => below games
private func calculateSortOrder(in items: [ItineraryRowItem], at row: Int) -> Double {
let day = dayNumber(in: items, forRow: row)
// Find games row for this day in the provided items
var gamesRow: Int? = nil
for i in 0..<items.count {
if case .games(_, let d) = items[i], d == day {
gamesRow = i
break
}
if case .dayHeader(let d, _) = items[i], d > day {
break
}
}
let isBeforeGames = (gamesRow != nil && row <= gamesRow!)
func movableSortOrder(_ idx: Int) -> Double? {
guard idx >= 0 && idx < items.count else { return nil }
switch items[idx] {
case .customItem(let item):
return item.sortOrder
case .travel(let segment, _):
return findItineraryItem(for: segment)?.sortOrder
default:
return nil
}
}
func scanBackward(from start: Int) -> Double? {
var i = start
while i >= 0 {
if case .dayHeader(let d, _) = items[i], d != day { break }
if case .dayHeader = items[i] { break }
if case .games(_, let d) = items[i], d == day { break }
if let v = movableSortOrder(i) {
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i -= 1
}
return nil
}
func scanForward(from start: Int) -> Double? {
var i = start
while i < items.count {
if case .dayHeader(let d, _) = items[i], d != day { break }
if case .dayHeader = items[i] { break }
if case .games(_, let d) = items[i], d == day { break }
if let v = movableSortOrder(i) {
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i += 1
}
return nil
}
if isBeforeGames {
let prev = scanBackward(from: row - 1)
let next = scanForward(from: row)
let upperBound: Double = 0.0
switch (prev, next) {
case (nil, nil):
return -1.0
case (let p?, nil):
return (p + upperBound) / 2.0
case (nil, let n?):
return n / 2.0
case (let p?, let n?):
return (p + n) / 2.0
}
} else {
let prev = scanBackward(from: row - 1) ?? 0.0
let next = scanForward(from: row)
switch next {
case nil:
return (prev == 0.0) ? 1.0 : (prev + 1.0)
case let n?:
return (prev + n) / 2.0
}
}
}
private func dayForTravelAtProposed(row: Int, excluding: Int) -> Int {
// Scan forward, skipping the item being moved
for i in row..<flatItems.count {
if i == excluding { continue }
if case .dayHeader(let dayNum, _) = flatItems[i] {
return dayNum
}
}
// Fallback: scan backwards
for i in stride(from: flatItems.count - 1, through: 0, by: -1) {
if i == excluding { continue }
if case .dayHeader(let dayNum, _) = flatItems[i] {
return dayNum
}
}
return 1
ItineraryReorderingLogic.computeValidDestinationRowsProposed(
flatItems: flatItems,
sourceRow: sourceRow,
dragged: dragged,
travelValidRanges: travelValidRanges,
constraints: constraints,
findTravelItem: { [weak self] segment in self?.findItineraryItem(for: segment) },
makeTravelItem: { [weak self] segment in
ItineraryItem(
tripId: self?.allItineraryItems.first?.tripId ?? UUID(),
day: 1,
sortOrder: 0,
kind: .travel(TravelInfo(
fromCity: segment.fromLocation.name,
toCity: segment.toLocation.name,
distanceMeters: segment.distanceMeters,
durationSeconds: segment.durationSeconds
))
)
},
findTravelSortOrder: { [weak self] segment in self?.findItineraryItem(for: segment)?.sortOrder }
)
}
// MARK: - Editing Style Configuration
@@ -1297,120 +930,15 @@ final class ItineraryTableViewController: UITableViewController {
}
// MARK: - Sort Order Calculation
/// Calculates the sortOrder for an item dropped at the given row position.
///
/// Uses **midpoint insertion** algorithm to avoid renumbering existing items:
///
/// ```
/// Existing items: A (sortOrder: 1.0) B (sortOrder: 2.0)
/// Drop between: A DROP HERE B
/// New sortOrder: 1.5 (midpoint of 1.0 and 2.0)
/// ```
///
/// **Edge cases:**
/// - First item in empty day: sortOrder = 1.0
/// - After last item: sortOrder = last + 1.0
/// - Before first item: sortOrder = first / 2.0
///
/// **Precision:** Double has ~15 significant digits. Even with millions of midpoint
/// insertions, precision remains sufficient. Example worst case:
/// - 50 insertions between 1.0 and 2.0: sortOrder 1.0000000000000009
/// - Still distinguishable and orderable
///
/// **Scanning logic:** We scan backwards and forwards from the drop position
/// to find adjacent custom items, stopping at day boundaries (headers, travel).
/// Delegates to pure function with closure for travel sortOrder lookup.
private func calculateSortOrder(at row: Int) -> Double {
let day = dayNumber(forRow: row)
// Find games row for this day (if any)
var gamesRow: Int? = nil
for i in 0..<flatItems.count {
if case .games(_, let d) = flatItems[i], d == day {
gamesRow = i
break
}
if case .dayHeader(let d, _) = flatItems[i], d > day {
break
}
}
let isBeforeGames = (gamesRow != nil && row <= gamesRow!)
func movableSortOrder(_ idx: Int) -> Double? {
guard idx >= 0 && idx < flatItems.count else { return nil }
switch flatItems[idx] {
case .customItem(let item):
return item.sortOrder
case .travel(let segment, _):
return findItineraryItem(for: segment)?.sortOrder
default:
return nil
}
}
func scanBackward(from start: Int) -> Double? {
var i = start
while i >= 0 {
if case .dayHeader(let d, _) = flatItems[i], d != day { break }
if case .dayHeader = flatItems[i] { break }
if case .games(_, let d) = flatItems[i], d == day { break }
if let v = movableSortOrder(i) {
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i -= 1
}
return nil
}
func scanForward(from start: Int) -> Double? {
var i = start
while i < flatItems.count {
if case .dayHeader(let d, _) = flatItems[i], d != day { break }
if case .dayHeader = flatItems[i] { break }
if case .games(_, let d) = flatItems[i], d == day { break }
if let v = movableSortOrder(i) {
if isBeforeGames {
if v < 0 { return v }
} else {
if v >= 0 { return v }
}
}
i += 1
}
return nil
}
if isBeforeGames {
let prev = scanBackward(from: row - 1)
let next = scanForward(from: row)
let upperBound: Double = 0.0 // games boundary
switch (prev, next) {
case (nil, nil):
return -1.0
case (let p?, nil):
return (p + upperBound) / 2.0
case (nil, let n?):
return n / 2.0
case (let p?, let n?):
return (p + n) / 2.0
}
} else {
let prev = scanBackward(from: row - 1) ?? 0.0
let next = scanForward(from: row)
switch next {
case nil:
return (prev == 0.0) ? 1.0 : (prev + 1.0)
case let n?:
return (prev + n) / 2.0
}
}
ItineraryReorderingLogic.calculateSortOrder(
in: flatItems,
at: row,
findTravelSortOrder: { [weak self] segment in self?.findItineraryItem(for: segment)?.sortOrder }
)
}
// MARK: - Cell Configuration