Files
Sportstime/SportsTime/Features/Polls/ViewModels/PollVotingViewModel.swift
Trey t d63d311cab feat: add WCAG AA accessibility app-wide, fix CloudKit container config, remove debug logs
- Add VoiceOver labels, hints, and element grouping across all 60+ views
- Add Reduce Motion support (Theme.Animation.prefersReducedMotion) to all animations
- Replace fixed font sizes with semantic Dynamic Type styles
- Hide decorative elements from VoiceOver with .accessibilityHidden(true)
- Add .minimumHitTarget() modifier ensuring 44pt touch targets
- Add AccessibilityAnnouncer utility for VoiceOver announcements
- Improve color contrast values in Theme.swift for WCAG AA compliance
- Extract CloudKitContainerConfig for explicit container identity
- Remove PostHog debug console log from AnalyticsManager

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 09:27:23 -06:00

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,
odg: 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
}
}