- Replace progressive reveal with single fade-in of all steps - Add canPlanTrip validation requiring all fields before planning - Disable Plan Trip button until all selections are made - Simplify ViewModel by removing step-by-step visibility logic - Update tests for new validation-based approach Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
123 lines
2.8 KiB
Swift
123 lines
2.8 KiB
Swift
//
|
|
// TripWizardViewModel.swift
|
|
// SportsTime
|
|
//
|
|
// ViewModel for trip wizard.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
@Observable
|
|
final class TripWizardViewModel {
|
|
|
|
// MARK: - Planning Mode
|
|
|
|
var planningMode: PlanningMode? = nil {
|
|
didSet {
|
|
if oldValue != nil && oldValue != planningMode {
|
|
resetAllSelections()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Sports Selection
|
|
|
|
var selectedSports: Set<Sport> = []
|
|
|
|
// MARK: - Dates
|
|
|
|
var startDate: Date = Date()
|
|
var endDate: Date = Date().addingTimeInterval(86400 * 7)
|
|
var hasSetDates: Bool = false
|
|
|
|
// MARK: - Regions
|
|
|
|
var selectedRegions: Set<Region> = []
|
|
|
|
// MARK: - Route Preferences
|
|
|
|
var routePreference: RoutePreference = .balanced
|
|
var hasSetRoutePreference: Bool = false
|
|
|
|
// MARK: - Repeat Cities
|
|
|
|
var allowRepeatCities: Bool = false
|
|
var hasSetRepeatCities: Bool = false
|
|
|
|
// MARK: - Must Stops
|
|
|
|
var mustStopLocations: [LocationInput] = []
|
|
|
|
// MARK: - Planning State
|
|
|
|
var isPlanning: Bool = false
|
|
|
|
// MARK: - Sport Availability
|
|
|
|
var sportAvailability: [Sport: Bool] = [:]
|
|
var isLoadingSportAvailability: Bool = false
|
|
|
|
// MARK: - Visibility
|
|
|
|
/// All steps visible once planning mode is selected
|
|
var areStepsVisible: Bool {
|
|
planningMode != nil
|
|
}
|
|
|
|
// MARK: - Validation
|
|
|
|
/// All required fields must be set before planning
|
|
var canPlanTrip: Bool {
|
|
planningMode != nil &&
|
|
hasSetDates &&
|
|
!selectedSports.isEmpty &&
|
|
!selectedRegions.isEmpty &&
|
|
hasSetRoutePreference &&
|
|
hasSetRepeatCities
|
|
}
|
|
|
|
// MARK: - Sport Availability
|
|
|
|
func canSelectSport(_ sport: Sport) -> Bool {
|
|
sportAvailability[sport] ?? true // Default to available if not checked
|
|
}
|
|
|
|
func fetchSportAvailability() async {
|
|
guard hasSetDates else { return }
|
|
|
|
isLoadingSportAvailability = true
|
|
defer { isLoadingSportAvailability = false }
|
|
|
|
var availability: [Sport: Bool] = [:]
|
|
|
|
for sport in Sport.supported {
|
|
do {
|
|
let games = try await AppDataProvider.shared.filterGames(
|
|
sports: [sport],
|
|
startDate: startDate,
|
|
endDate: endDate
|
|
)
|
|
availability[sport] = !games.isEmpty
|
|
} catch {
|
|
availability[sport] = true // Default to available on error
|
|
}
|
|
}
|
|
|
|
await MainActor.run {
|
|
self.sportAvailability = availability
|
|
}
|
|
}
|
|
|
|
// MARK: - Reset Logic
|
|
|
|
private func resetAllSelections() {
|
|
selectedSports = []
|
|
hasSetDates = false
|
|
selectedRegions = []
|
|
hasSetRoutePreference = false
|
|
hasSetRepeatCities = false
|
|
mustStopLocations = []
|
|
}
|
|
}
|