Files
Sportstime/SportsTime/Features/Polls/Views/PollDetailView.swift
Trey t d034ee8612 fix: multiple bug fixes and improvements
- Fix suggested trips showing wrong sports for cross-country trips
- Remove quick start sections from home variants (Classic, Spotify)
- Remove dead quickActions code from HomeView
- Fix pace capsule animation in TripCreationView
- Add text wrapping to achievement descriptions
- Improve poll parsing with better error handling
- Various sharing system improvements

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 09:35:18 -06:00

341 lines
10 KiB
Swift

//
// PollDetailView.swift
// SportsTime
//
// View for displaying poll details and results
//
import SwiftUI
struct PollDetailView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.colorScheme) private var colorScheme
@State private var viewModel = PollDetailViewModel()
@State private var showShareSheet = false
@State private var showDeleteConfirmation = false
@State private var showVotingSheet = false
@State private var isOwner = false
let pollId: UUID?
let shareCode: String?
let initialPoll: TripPoll?
init(pollId: UUID) {
self.pollId = pollId
self.shareCode = nil
self.initialPoll = nil
}
init(shareCode: String) {
self.pollId = nil
self.shareCode = shareCode
self.initialPoll = nil
}
/// Initialize with a poll object directly (avoids fetch delay)
init(poll: TripPoll) {
self.pollId = poll.id
self.shareCode = nil
self.initialPoll = poll
}
var body: some View {
Group {
if viewModel.isLoading && viewModel.poll == nil {
ProgressView("Loading poll...")
} else if let poll = viewModel.poll {
pollContent(poll)
} else if let error = viewModel.error {
ContentUnavailableView(
"Poll Not Found",
systemImage: "exclamationmark.triangle",
description: Text(error.localizedDescription)
)
}
}
.navigationTitle(viewModel.poll?.title ?? "Poll")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
if viewModel.poll != nil {
ToolbarItem(placement: .primaryAction) {
Menu {
Button {
showShareSheet = true
} label: {
Label("Share Poll", systemImage: "square.and.arrow.up")
}
if isOwner {
Divider()
Button(role: .destructive) {
showDeleteConfirmation = true
} label: {
Label("Delete Poll", systemImage: "trash")
}
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
}
}
.refreshable {
await viewModel.refresh()
}
.task {
await loadPoll()
isOwner = await viewModel.isOwner
}
.task(id: viewModel.poll?.id) {
if viewModel.poll != nil {
isOwner = await viewModel.isOwner
}
}
.onDisappear {
Task {
await viewModel.cleanup()
}
}
.sheet(isPresented: $showShareSheet) {
if let url = viewModel.shareURL {
ShareSheet(items: [url])
}
}
.sheet(isPresented: $showVotingSheet) {
if let poll = viewModel.poll {
PollVotingView(poll: poll, existingVote: viewModel.myVote) {
Task {
await viewModel.refresh()
}
}
}
}
.confirmationDialog("Delete Poll", isPresented: $showDeleteConfirmation, titleVisibility: .visible) {
Button("Delete", role: .destructive) {
Task {
if await viewModel.deletePoll() {
dismiss()
}
}
}
Button("Cancel", role: .cancel) {}
} message: {
Text("This will permanently delete the poll and all votes. This action cannot be undone.")
}
}
@ViewBuilder
private func pollContent(_ poll: TripPoll) -> some View {
ScrollView {
VStack(spacing: 20) {
// Share Code Card
shareCodeCard(poll)
// Voting Status
votingStatusCard
// Results
if let results = viewModel.results {
resultsSection(results)
}
// Trip Previews
tripPreviewsSection(poll)
}
.padding()
}
}
@ViewBuilder
private func shareCodeCard(_ poll: TripPoll) -> some View {
VStack(spacing: 8) {
Text("Share Code")
.font(.caption)
.foregroundStyle(.secondary)
Text(poll.shareCode)
.font(.system(size: 32, weight: .bold, design: .monospaced))
.foregroundStyle(Theme.warmOrange)
Text("sportstime://poll/\(poll.shareCode)")
.font(.caption2)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity)
.padding()
.background(Theme.cardBackground(colorScheme))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
@ViewBuilder
private var votingStatusCard: some View {
HStack {
VStack(alignment: .leading, spacing: 4) {
Text(viewModel.hasVoted ? "You voted" : "You haven't voted yet")
.font(.headline)
Text("\(viewModel.votes.count) total votes")
.font(.subheadline)
.foregroundStyle(.secondary)
}
Spacer()
Button(viewModel.hasVoted ? "Change Vote" : "Vote Now") {
showVotingSheet = true
}
.buttonStyle(.borderedProminent)
.tint(Theme.warmOrange)
}
.padding()
.background(Theme.cardBackground(colorScheme))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
@ViewBuilder
private func resultsSection(_ results: PollResults) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text("Results")
.font(.headline)
ForEach(results.tripScores, id: \.tripIndex) { item in
let trip = results.poll.tripSnapshots[item.tripIndex]
ResultRow(
rank: results.tripScores.firstIndex { $0.tripIndex == item.tripIndex }! + 1,
tripName: trip.name,
score: item.score,
percentage: results.scorePercentage(for: item.tripIndex)
)
}
}
.padding()
.background(Theme.cardBackground(colorScheme))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
@ViewBuilder
private func tripPreviewsSection(_ poll: TripPoll) -> some View {
VStack(alignment: .leading, spacing: 12) {
Text("Trip Options")
.font(.headline)
ForEach(Array(poll.tripSnapshots.enumerated()), id: \.element.id) { index, trip in
TripPreviewCard(trip: trip, index: index + 1)
}
}
}
private func loadPoll() async {
// If we have an initial poll object, use it directly to avoid CloudKit fetch delay
if let initialPoll {
await viewModel.loadPoll(from: initialPoll)
} else if let pollId {
await viewModel.loadPoll(byId: pollId)
} else if let shareCode {
await viewModel.loadPoll(byShareCode: shareCode)
}
}
}
// MARK: - Result Row
private struct ResultRow: View {
let rank: Int
let tripName: String
let score: Int
let percentage: Double
var body: some View {
HStack(spacing: 12) {
Text("#\(rank)")
.font(.headline)
.foregroundStyle(rank == 1 ? Theme.warmOrange : .secondary)
.frame(width: 30)
VStack(alignment: .leading, spacing: 4) {
Text(tripName)
.font(.subheadline)
GeometryReader { geometry in
ZStack(alignment: .leading) {
Rectangle()
.fill(Color.secondary.opacity(0.2))
.frame(height: 8)
.clipShape(RoundedRectangle(cornerRadius: 4))
Rectangle()
.fill(rank == 1 ? Theme.warmOrange : Color.secondary)
.frame(width: geometry.size.width * percentage, height: 8)
.clipShape(RoundedRectangle(cornerRadius: 4))
}
}
.frame(height: 8)
}
Text("\(score)")
.font(.caption)
.foregroundStyle(.secondary)
.frame(width: 40, alignment: .trailing)
}
}
}
// MARK: - Trip Preview Card
private struct TripPreviewCard: View {
@Environment(\.colorScheme) private var colorScheme
let trip: Trip
let index: Int
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Text("Option \(index)")
.font(.caption)
.fontWeight(.semibold)
.foregroundStyle(.white)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Theme.warmOrange)
.clipShape(Capsule())
Text(trip.name)
.font(.headline)
}
// Date range and duration
HStack {
Label(trip.formattedDateRange, systemImage: "calendar")
Spacer()
Label("\(trip.tripDuration) days", systemImage: "clock")
}
.font(.caption)
.foregroundStyle(.secondary)
HStack {
Label("\(trip.stops.count) stops", systemImage: "mappin.and.ellipse")
Spacer()
Label("\(trip.stops.flatMap { $0.games }.count) games", systemImage: "sportscourt")
}
.font(.caption)
.foregroundStyle(.secondary)
// Show cities
Text(trip.stops.map { $0.city }.joined(separator: ""))
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
}
.padding()
.background(Theme.cardBackground(colorScheme))
.clipShape(RoundedRectangle(cornerRadius: 12))
}
}
#Preview {
NavigationStack {
PollDetailView(shareCode: "ABC123")
}
}