Fixes ~95 issues from deep audit across 12 categories in 82 files: - Crash prevention: double-resume in PhotoMetadataExtractor, force unwraps in DateRangePicker, array bounds checks in polls/achievements, ProGate hit-test bypass, Dictionary(uniqueKeysWithValues:) → uniquingKeysWith in 4 files - Silent failure elimination: all 34 try? sites replaced with do/try/catch + logging (SavedTrip, TripDetailView, CanonicalSyncService, BootstrapService, CanonicalModels, CKModels, SportsTimeApp, and more) - Performance: cached DateFormatters (7 files), O(1) team lookups via AppDataProvider, achievement definition dictionary, AnimatedBackground consolidated from 19 Tasks to 1, task cancellation in SharePreviewView - Concurrency: UIKit drawing → MainActor.run, background fetch timeout guard, @MainActor on ThemeManager/AppearanceManager, SyncLogger read/write race fix - Planning engine: game end time in travel feasibility, state-aware city normalization, exact city matching, DrivingConstraints parameter propagation - IAP: unknown subscription states → expired, unverified transaction logging, entitlements updated before paywall dismiss, restore visible to all users - Security: API key to Info.plist lookup, filename sanitization in PDF export, honest User-Agent, removed stale "Feels" analytics super properties - Navigation: consolidated competing navigationDestination, boolean → value-based - Testing: 8 sleep() → waitForExistence, duplicates extracted, Swift 6 compat - Service bugs: infinite retry cap, duplicate achievement prevention, TOCTOU vote fix, PollVote.odg → voterId rename, deterministic placeholder IDs, parallel MKDirections, Sendable-safe POI struct Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
102 lines
2.4 KiB
Swift
102 lines
2.4 KiB
Swift
//
|
|
// PollVotingViewModel.swift
|
|
// SportsTime
|
|
//
|
|
// ViewModel for voting on trip polls
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
@Observable
|
|
@MainActor
|
|
final class PollVotingViewModel {
|
|
var rankings: [Int] = [] // Trip indices in preference order
|
|
var isLoading = false
|
|
var error: PollError?
|
|
var didSubmit = false
|
|
|
|
private let pollService = PollService.shared
|
|
|
|
var canSubmit: Bool {
|
|
!rankings.isEmpty
|
|
}
|
|
|
|
func initializeRankings(tripCount: Int, existingVote: PollVote?) {
|
|
if let vote = existingVote {
|
|
rankings = vote.rankings
|
|
} else {
|
|
// Default: trips in original order
|
|
rankings = Array(0..<tripCount)
|
|
}
|
|
}
|
|
|
|
func moveTrip(from source: IndexSet, to destination: Int) {
|
|
rankings.move(fromOffsets: source, toOffset: destination)
|
|
}
|
|
|
|
func moveTripUp(at index: Int) {
|
|
guard index > 0, index < rankings.count else { return }
|
|
rankings.swapAt(index, index - 1)
|
|
}
|
|
|
|
func moveTripDown(at index: Int) {
|
|
guard index >= 0, index < rankings.count - 1 else { return }
|
|
rankings.swapAt(index, index + 1)
|
|
}
|
|
|
|
func submitVote(pollId: UUID) async {
|
|
guard canSubmit else { return }
|
|
|
|
isLoading = true
|
|
error = nil
|
|
|
|
do {
|
|
let userId = try await pollService.getCurrentUserRecordID()
|
|
|
|
let vote = PollVote(
|
|
pollId: pollId,
|
|
voterId: userId,
|
|
rankings: rankings
|
|
)
|
|
|
|
_ = try await pollService.submitVote(vote)
|
|
didSubmit = true
|
|
AnalyticsManager.shared.track(.pollVoted(pollId: pollId.uuidString))
|
|
} catch let pollError as PollError {
|
|
error = pollError
|
|
} catch {
|
|
self.error = .unknown(error)
|
|
}
|
|
|
|
isLoading = false
|
|
}
|
|
|
|
func updateVote(existingVote: PollVote) async {
|
|
guard canSubmit else { return }
|
|
|
|
isLoading = true
|
|
error = nil
|
|
|
|
do {
|
|
var updatedVote = existingVote
|
|
updatedVote.rankings = rankings
|
|
|
|
_ = try await pollService.updateVote(updatedVote)
|
|
didSubmit = true
|
|
} catch let pollError as PollError {
|
|
error = pollError
|
|
} catch {
|
|
self.error = .unknown(error)
|
|
}
|
|
|
|
isLoading = false
|
|
}
|
|
|
|
func reset() {
|
|
rankings = []
|
|
error = nil
|
|
didSubmit = false
|
|
}
|
|
}
|