Files
Sportstime/SportsTime/Features/Polls/Views/PollVotingView.swift
Trey t 13385b6562 feat(polls): implement group trip polling MVP
Add complete group trip polling feature allowing users to share trips
with friends for voting using Borda count scoring.

New components:
- TripPoll and PollVote domain models with share codes and rankings
- LocalTripPoll and LocalPollVote SwiftData models for persistence
- CKTripPoll and CKPollVote CloudKit record wrappers
- PollService actor for CloudKit CRUD operations and subscriptions
- PollCreation/Detail/Voting views and view models
- Deep link handling for sportstime://poll/{code} URLs
- Debug Pro status override toggle in Settings

Integration:
- HomeView shows polls section in My Trips
- SportsTimeApp registers SwiftData models and handles deep links

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-13 21:54:42 -06:00

179 lines
4.9 KiB
Swift

//
// PollVotingView.swift
// SportsTime
//
// View for ranking trips in a poll
//
import SwiftUI
struct PollVotingView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.colorScheme) private var colorScheme
@State private var viewModel = PollVotingViewModel()
let poll: TripPoll
let existingVote: PollVote?
var onVoteSubmitted: (() -> Void)?
var body: some View {
NavigationStack {
VStack(spacing: 0) {
// Instructions
instructionsHeader
// Reorderable list
List {
ForEach(Array(viewModel.rankings.enumerated()), id: \.element) { index, tripIndex in
RankingRow(
rank: index + 1,
trip: poll.tripSnapshots[tripIndex]
)
}
.onMove { source, destination in
viewModel.moveTrip(from: source, to: destination)
}
}
.listStyle(.plain)
.environment(\.editMode, .constant(.active))
// Submit button
submitButton
}
.navigationTitle("Rank Trips")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
}
.onAppear {
viewModel.initializeRankings(
tripCount: poll.tripSnapshots.count,
existingVote: existingVote
)
}
.alert("Error", isPresented: .constant(viewModel.error != nil)) {
Button("OK") {
viewModel.error = nil
}
} message: {
if let error = viewModel.error {
Text(error.localizedDescription)
}
}
.onChange(of: viewModel.didSubmit) { _, didSubmit in
if didSubmit {
onVoteSubmitted?()
dismiss()
}
}
}
}
@ViewBuilder
private var instructionsHeader: some View {
VStack(spacing: 8) {
Image(systemName: "arrow.up.arrow.down")
.font(.title2)
.foregroundStyle(Theme.warmOrange)
Text("Drag to rank your preferences")
.font(.headline)
Text("Your top choice should be at the top")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.padding()
.frame(maxWidth: .infinity)
.background(Theme.cardBackground(colorScheme))
}
@ViewBuilder
private var submitButton: some View {
Button {
Task {
if let existingVote {
await viewModel.updateVote(existingVote: existingVote)
} else {
await viewModel.submitVote(pollId: poll.id)
}
}
} label: {
HStack {
if viewModel.isLoading {
ProgressView()
.tint(.white)
} else {
Text(existingVote != nil ? "Update Vote" : "Submit Vote")
}
}
.frame(maxWidth: .infinity)
.padding()
.background(Theme.warmOrange)
.foregroundStyle(.white)
.font(.headline)
}
.disabled(viewModel.isLoading || !viewModel.canSubmit)
}
}
// MARK: - Ranking Row
private struct RankingRow: View {
let rank: Int
let trip: Trip
var body: some View {
HStack(spacing: 12) {
// Rank badge
Text("\(rank)")
.font(.headline)
.foregroundStyle(.white)
.frame(width: 28, height: 28)
.background(rankColor)
.clipShape(Circle())
VStack(alignment: .leading, spacing: 2) {
Text(trip.name)
.font(.headline)
Text(tripSummary)
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
}
.padding(.vertical, 4)
}
private var rankColor: Color {
switch rank {
case 1: return Theme.warmOrange
case 2: return .blue
case 3: return .green
default: return .secondary
}
}
private var tripSummary: String {
let cities = trip.stops.map { $0.city }.joined(separator: "")
return cities
}
}
#Preview {
PollVotingView(
poll: TripPoll(
title: "Test Poll",
ownerId: "test",
tripSnapshots: []
),
existingVote: nil
)
}