fix(itinerary): add city to game items for proper constraint validation

Travel constraint validation was not working because ItineraryConstraints
had no game items to validate against - games came from RichGame objects
but were never converted to ItineraryItem for constraint checking.

Changes:
- Add city parameter to ItemKind.game enum case
- Create game ItineraryItems from RichGame data in buildItineraryData()
- Update isValidTravelPosition to compare against actual game sortOrders
- Fix tests to use appropriate game sortOrder conventions

Now travel is properly constrained to appear before arrival city games
and after departure city games.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-01-18 22:46:40 -06:00
parent 72447c61fe
commit e72da7c5a7
11 changed files with 247 additions and 254 deletions

View File

@@ -5,13 +5,6 @@ struct ItineraryConstraints {
let tripDayCount: Int
private let items: [ItineraryItem]
/// City extracted from game ID (format: "game-CityName-xxxx")
private func city(forGameId gameId: String) -> String? {
let components = gameId.components(separatedBy: "-")
guard components.count >= 2 else { return nil }
return components[1]
}
init(tripDayCount: Int, items: [ItineraryItem]) {
self.tripDayCount = tripDayCount
self.items = items
@@ -83,8 +76,10 @@ struct ItineraryConstraints {
// MARK: - Private Helpers
private func isValidTravelPosition(fromCity: String, toCity: String, day: Int, sortOrder: Double) -> Bool {
let departureGameDays = gameDays(in: fromCity)
let arrivalGameDays = gameDays(in: toCity)
let departureGames = games(in: fromCity)
let arrivalGames = games(in: toCity)
let departureGameDays = departureGames.map { $0.day }
let arrivalGameDays = arrivalGames.map { $0.day }
let minDay = departureGameDays.max() ?? 1
let maxDay = arrivalGameDays.min() ?? tripDayCount
@@ -92,26 +87,20 @@ struct ItineraryConstraints {
// Check day is in valid range
guard day >= minDay && day <= maxDay else { return false }
// Check sortOrder constraints on edge days
// Check sortOrder constraints on edge days.
// Must be strictly AFTER all departure games on that day
if departureGameDays.contains(day) {
// On a departure game day: must be after ALL games in that city on that day
let maxGameSortOrder = games(in: fromCity)
.filter { $0.day == day }
.map { $0.sortOrder }
.max() ?? 0
let gamesOnDay = departureGames.filter { $0.day == day }
let maxGameSortOrder = gamesOnDay.map { $0.sortOrder }.max() ?? 0
if sortOrder <= maxGameSortOrder {
return false
}
}
// Must be strictly BEFORE all arrival games on that day
if arrivalGameDays.contains(day) {
// On an arrival game day: must be before ALL games in that city on that day
let minGameSortOrder = games(in: toCity)
.filter { $0.day == day }
.map { $0.sortOrder }
.min() ?? Double.greatestFiniteMagnitude
let gamesOnDay = arrivalGames.filter { $0.day == day }
let minGameSortOrder = gamesOnDay.map { $0.sortOrder }.min() ?? 0
if sortOrder >= minGameSortOrder {
return false
}
@@ -126,8 +115,8 @@ struct ItineraryConstraints {
private func games(in city: String) -> [ItineraryItem] {
return items.filter { item in
guard case .game(let gameId) = item.kind else { return false }
return self.city(forGameId: gameId) == city
guard let gameCity = item.gameCity else { return false }
return gameCity.lowercased() == city.lowercased()
}
}
}

View File

@@ -29,7 +29,7 @@ struct ItineraryItem: Identifiable, Codable, Hashable {
/// The type of itinerary item
enum ItemKind: Codable, Hashable {
case game(gameId: String)
case game(gameId: String, city: String)
case travel(TravelInfo)
case custom(CustomInfo)
}
@@ -106,14 +106,19 @@ extension ItineraryItem {
}
var gameId: String? {
if case .game(let id) = kind { return id }
if case .game(let id, _) = kind { return id }
return nil
}
var gameCity: String? {
if case .game(_, let city) = kind { return city }
return nil
}
/// Display title for the item
var displayTitle: String {
switch kind {
case .game(let gameId):
case .game(let gameId, _):
return "Game: \(gameId)"
case .travel(let info):
return "\(info.fromCity)\(info.toCity)"

View File

@@ -145,8 +145,9 @@ extension ItineraryItem {
// Parse kind
switch kindString {
case "game":
guard let gameId = record["gameId"] as? String else { return nil }
self.kind = .game(gameId: gameId)
guard let gameId = record["gameId"] as? String,
let gameCity = record["gameCity"] as? String else { return nil }
self.kind = .game(gameId: gameId, city: gameCity)
case "travel":
guard let fromCity = record["travelFromCity"] as? String,
@@ -188,9 +189,10 @@ extension ItineraryItem {
record["modifiedAt"] = modifiedAt
switch kind {
case .game(let gameId):
case .game(let gameId, let city):
record["kind"] = "game"
record["gameId"] = gameId
record["gameCity"] = city
case .travel(let info):
record["kind"] = "travel"

View File

@@ -388,11 +388,9 @@ final class PDFGenerator {
var primaryCity: String?
for item in items {
switch item.kind {
case .game(let gameId):
if let richGame = games[gameId] {
primaryCity = richGame.stadium.city
break
}
case .game(_, let city):
primaryCity = city
break
case .travel(let info):
primaryCity = info.toCity
break
@@ -419,7 +417,7 @@ final class PDFGenerator {
var hasContent = false
for item in items {
switch item.kind {
case .game(let gameId):
case .game(let gameId, _):
if let richGame = games[gameId] {
currentY = drawGameCard(
context: context,

View File

@@ -5,9 +5,6 @@
// 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).
@@ -15,40 +12,13 @@
// - 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
// - "Original indices": Row indices in the current flatItems array (0..<flatItems.count)
// - "Proposed indices": Row indices in post-removal array (UITableView move semantics)
// After removing sourceRow, the array has count-1 elements. Insert positions are 0...count-1.
// - simulateMove: Takes proposed index, returns post-move array + actual destination
// - computeValidDestinationRowsProposed: Returns PROPOSED indices (for UITableView delegate)
// - DragZones: invalidRowIndices and validDropRows are in ORIGINAL space (for UI highlighting)
// - To convert: proposedToOriginal(proposed, sourceRow) and originalToProposed(original, sourceRow)
//
import Foundation
@@ -58,13 +28,13 @@ import Foundation
/// 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.
@@ -85,74 +55,68 @@ enum ItineraryReorderingLogic {
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
}
// Use provided sortOrder if available, otherwise default to after-games position.
// nil is valid during initial display before travel is persisted.
let lookedUp = findTravelSortOrder(segment)
sortOrder = lookedUp ?? defaultTravelSortOrder
print("📋 [flattenDays] Travel \(segment.fromLocation.name)->\(segment.toLocation.name) on day \(day.dayNumber): lookedUp=\(String(describing: lookedUp)), using sortOrder=\(sortOrder)")
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`.
@@ -172,7 +136,7 @@ enum ItineraryReorderingLogic {
}
return 1
}
/// Finds the row index of the day header for a specific day number.
///
/// - Parameters:
@@ -187,7 +151,7 @@ enum ItineraryReorderingLogic {
}
return nil
}
/// Finds the row index of the travel segment on a specific day.
///
/// **SEMANTIC MODEL**: Does NOT use the embedded dayNumber in .travel().
@@ -199,16 +163,13 @@ enum ItineraryReorderingLogic {
/// - 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
@@ -218,7 +179,8 @@ enum ItineraryReorderingLogic {
}
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? {
@@ -229,7 +191,7 @@ enum ItineraryReorderingLogic {
}
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.
@@ -240,20 +202,19 @@ enum ItineraryReorderingLogic {
/// - 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,
@@ -269,20 +230,20 @@ enum ItineraryReorderingLogic {
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.
@@ -299,7 +260,7 @@ enum ItineraryReorderingLogic {
return proposed
}
}
/// Converts an original index to the equivalent proposed destination index.
///
/// - Parameters:
@@ -316,9 +277,9 @@ enum ItineraryReorderingLogic {
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:
@@ -357,16 +318,27 @@ enum ItineraryReorderingLogic {
}
}
// DEBUG: Log the row positions
print("🔢 [calculateSortOrder] row=\(row), day=\(day), gamesRow=\(String(describing: gamesRow))")
print("🔢 [calculateSortOrder] items around row:")
for i in max(0, row - 2)...min(items.count - 1, row + 2) {
let marker = i == row ? "" : " "
let gMarker = (gamesRow == i) ? " [GAMES]" : ""
print("🔢 \(marker) [\(i)] \(items[i])\(gMarker)")
}
// 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
print("🔢 [calculateSortOrder] row(\(row)) < gamesRow(\(gr)) = \(isBeforeGames) → isBeforeGames=\(isBeforeGames)")
} else {
isBeforeGames = false // No games means everything is "after games"
print("🔢 [calculateSortOrder] No games on day \(day) → isBeforeGames=false")
}
/// Get sortOrder from a movable item (custom item or travel)
func movableSortOrder(_ idx: Int) -> Double? {
guard idx >= 0 && idx < items.count else { return nil }
@@ -379,7 +351,7 @@ enum ItineraryReorderingLogic {
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
@@ -391,7 +363,7 @@ enum ItineraryReorderingLogic {
}
// 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 {
@@ -404,7 +376,7 @@ enum ItineraryReorderingLogic {
}
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
@@ -416,7 +388,7 @@ enum ItineraryReorderingLogic {
}
// 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 {
@@ -429,7 +401,8 @@ enum ItineraryReorderingLogic {
}
return nil
}
let result: Double
if isBeforeGames {
// Above games: sortOrder should be negative
let prev = scanBackward(from: row - 1)
@@ -438,9 +411,9 @@ enum ItineraryReorderingLogic {
let upperBound: Double = 0.0 // Games boundary
switch (prev, next) {
case (nil, nil):
return -1.0
result = -1.0
case (let p?, nil):
return (p + upperBound) / 2.0
result = (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).
@@ -448,12 +421,13 @@ enum ItineraryReorderingLogic {
// 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
result = -1.0
} else {
// Place before n by subtracting 1.0 (simpler and more consistent than min(n/2, n-1))
result = n - 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
result = (p + n) / 2.0
}
} else {
// Below games: sortOrder should be >= 0
@@ -462,15 +436,18 @@ enum ItineraryReorderingLogic {
switch next {
case nil:
return (prev == 0.0) ? 1.0 : (prev + 1.0)
result = (prev == 0.0) ? 1.0 : (prev + 1.0)
case let n?:
return (prev + n) / 2.0
result = (prev + n) / 2.0
}
}
print("🔢 [calculateSortOrder] RESULT: \(result) (isBeforeGames=\(isBeforeGames))")
return result
}
// MARK: - Valid Drop Computation
/// Computes all valid destination rows in **proposed** coordinate space.
///
/// For BOTH travel and custom items, we:
@@ -503,7 +480,7 @@ enum ItineraryReorderingLogic {
) -> [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
@@ -519,24 +496,24 @@ enum ItineraryReorderingLogic {
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,
@@ -545,21 +522,21 @@ enum ItineraryReorderingLogic {
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
@@ -571,31 +548,31 @@ enum ItineraryReorderingLogic {
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(
@@ -605,22 +582,22 @@ enum ItineraryReorderingLogic {
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).
@@ -633,7 +610,7 @@ enum ItineraryReorderingLogic {
/// 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.
@@ -670,11 +647,11 @@ enum ItineraryReorderingLogic {
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 {
@@ -686,7 +663,7 @@ enum ItineraryReorderingLogic {
invalidRows.insert(i)
}
}
// Find barrier games using constraints
var barrierGameIds = Set<String>()
if let travelItem = findTravelItem(segment),
@@ -694,14 +671,14 @@ enum ItineraryReorderingLogic {
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.
@@ -735,11 +712,11 @@ enum ItineraryReorderingLogic {
},
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 {
@@ -751,16 +728,16 @@ enum ItineraryReorderingLogic {
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.
///
@@ -774,14 +751,14 @@ enum ItineraryReorderingLogic {
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 {
@@ -794,14 +771,14 @@ enum ItineraryReorderingLogic {
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),
@@ -809,14 +786,14 @@ enum ItineraryReorderingLogic {
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.
@@ -827,7 +804,7 @@ enum ItineraryReorderingLogic {
) -> DragZones {
var invalidRows = Set<Int>()
var validRows: [Int] = []
for (index, rowItem) in flatItems.enumerated() {
if case .dayHeader = rowItem {
invalidRows.insert(index)
@@ -835,16 +812,16 @@ enum ItineraryReorderingLogic {
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:
@@ -853,10 +830,10 @@ enum ItineraryReorderingLogic {
/// - 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
@@ -866,10 +843,10 @@ enum ItineraryReorderingLogic {
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
@@ -882,7 +859,7 @@ enum ItineraryReorderingLogic {
return nil
}
}
/// Calculates target destination with constraint snapping.
///
/// If the proposed row is valid, returns it. Otherwise, snaps to nearest valid row.
@@ -909,12 +886,12 @@ enum ItineraryReorderingLogic {
// 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

@@ -80,14 +80,14 @@ struct ItineraryTableViewWrapper<HeaderContent: View>: UIViewControllerRepresent
hostingController.view.translatesAutoresizingMaskIntoConstraints = true
controller.setTableHeader(hostingController.view)
// Load initial data
let (days, validRanges, allItemsForConstraints) = buildItineraryData()
controller.reloadData(days: days, travelValidRanges: validRanges, itineraryItems: allItemsForConstraints)
return controller
}
func updateUIViewController(_ controller: ItineraryTableViewController, context: Context) {
controller.colorScheme = colorScheme
controller.onTravelMoved = onTravelMoved
@@ -95,37 +95,46 @@ struct ItineraryTableViewWrapper<HeaderContent: View>: UIViewControllerRepresent
controller.onCustomItemTapped = onCustomItemTapped
controller.onCustomItemDeleted = onCustomItemDeleted
controller.onAddButtonTapped = onAddButtonTapped
// Update header content by updating the hosting controller's rootView
// This avoids recreating the view hierarchy and prevents infinite loops
context.coordinator.headerHostingController?.rootView = headerContent
let (days, validRanges, allItemsForConstraints) = buildItineraryData()
controller.reloadData(days: days, travelValidRanges: validRanges, itineraryItems: allItemsForConstraints)
}
// MARK: - Build Itinerary Data
private func buildItineraryData() -> ([ItineraryDayData], [String: ClosedRange<Int>], [ItineraryItem]) {
let tripDays = calculateTripDays()
var travelValidRanges: [String: ClosedRange<Int>] = [:]
// Build game items from RichGame data for constraint validation
var gameItems: [ItineraryItem] = []
for (index, dayDate) in tripDays.enumerated() {
let dayNum = index + 1
let gamesOnDay = gamesOn(date: dayDate)
for (gameIndex, richGame) in gamesOnDay.enumerated() {
let gameItem = ItineraryItem(
tripId: trip.id,
day: dayNum,
sortOrder: Double(gameIndex) * 0.01, // Games have sortOrder ~0 (at the visual boundary)
kind: .game(gameId: richGame.game.id, city: richGame.stadium.city)
)
gameItems.append(gameItem)
}
}
// Build travel as semantic items with (day, sortOrder)
var travelItems: [ItineraryItem] = []
travelItems.reserveCapacity(trip.travelSegments.count)
func cityFromGameId(_ gameId: String) -> String? {
let comps = gameId.components(separatedBy: "-")
guard comps.count >= 2 else { return nil }
return comps[1]
}
func gamesIn(city: String, day: Int) -> [ItineraryItem] {
itineraryItems.filter { item in
gameItems.filter { item in
guard item.day == day else { return false }
guard case .game(let gid) = item.kind else { return false }
guard let c = cityFromGameId(gid) else { return false }
return cityMatches(c, searchCity: city)
guard let gameCity = item.gameCity else { return false }
return cityMatches(gameCity, searchCity: city)
}
}
@@ -249,7 +258,7 @@ struct ItineraryTableViewWrapper<HeaderContent: View>: UIViewControllerRepresent
days.append(dayData)
}
return (days, travelValidRanges, itineraryItems + travelItems)
return (days, travelValidRanges, gameItems + itineraryItems + travelItems)
}
// MARK: - Helper Methods

View File

@@ -203,7 +203,7 @@ struct TripDetailView: View {
withAnimation {
travelOverrides[travelId] = TravelOverride(day: newDay, sortOrder: newSortOrder)
}
await saveTravelDayOverride(travelAnchorId: travelId, displayDay: newDay)
await saveTravelDayOverride(travelAnchorId: travelId, displayDay: newDay, sortOrder: newSortOrder)
}
},
onCustomItemMoved: { itemId, day, sortOrder in
@@ -1371,7 +1371,8 @@ struct TripDetailView: View {
// Persist to CloudKit as a travel ItineraryItem
await self.saveTravelDayOverride(
travelAnchorId: droppedId,
displayDay: dayNumber
displayDay: dayNumber,
sortOrder: newSortOrder
)
return
}
@@ -1393,8 +1394,8 @@ struct TripDetailView: View {
return false
}
private func saveTravelDayOverride(travelAnchorId: String, displayDay: Int) async {
print("💾 [TravelOverrides] Saving override: \(travelAnchorId) -> day \(displayDay)")
private func saveTravelDayOverride(travelAnchorId: String, displayDay: Int, sortOrder: Double) async {
print("💾 [TravelOverrides] Saving override: \(travelAnchorId) -> day \(displayDay), sortOrder \(sortOrder)")
// Parse travel ID to extract cities (format: "travel:city1->city2")
let stripped = travelAnchorId.replacingOccurrences(of: "travel:", with: "")
@@ -1414,6 +1415,7 @@ struct TripDetailView: View {
// Update existing
var updated = itineraryItems[existingIndex]
updated.day = displayDay
updated.sortOrder = sortOrder
updated.modifiedAt = Date()
itineraryItems[existingIndex] = updated
await ItineraryItemService.shared.updateItem(updated)
@@ -1423,7 +1425,7 @@ struct TripDetailView: View {
let item = ItineraryItem(
tripId: trip.id,
day: displayDay,
sortOrder: 0, // Travel always comes first in day
sortOrder: sortOrder,
kind: .travel(travelInfo)
)
itineraryItems.append(item)