Merge branch 'feature/group-trip-polling' into group_voting
# Conflicts: # SportsTime/Core/Store/StoreManager.swift
This commit is contained in:
@@ -432,51 +432,240 @@ struct SavedTripsListView: View {
|
||||
let trips: [SavedTrip]
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
@State private var polls: [TripPoll] = []
|
||||
@State private var isLoadingPolls = false
|
||||
@State private var showCreatePoll = false
|
||||
@State private var selectedPoll: TripPoll?
|
||||
|
||||
/// Trips sorted by most cities (stops) first
|
||||
private var sortedTrips: [SavedTrip] {
|
||||
trips.sorted { ($0.trip?.stops.count ?? 0) > ($1.trip?.stops.count ?? 0) }
|
||||
}
|
||||
|
||||
/// Trips as domain objects for poll creation
|
||||
private var tripsForPollCreation: [Trip] {
|
||||
trips.compactMap { $0.trip }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
LazyVStack(spacing: Theme.Spacing.lg) {
|
||||
// Polls Section
|
||||
pollsSection
|
||||
|
||||
// Trips Section
|
||||
tripsSection
|
||||
}
|
||||
.padding(Theme.Spacing.md)
|
||||
}
|
||||
.themedBackground()
|
||||
.navigationTitle("My Trips")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Menu {
|
||||
Button {
|
||||
showCreatePoll = true
|
||||
} label: {
|
||||
Label("Create Poll", systemImage: "chart.bar.doc.horizontal")
|
||||
}
|
||||
.disabled(trips.count < 2)
|
||||
} label: {
|
||||
Image(systemName: "ellipsis.circle")
|
||||
}
|
||||
}
|
||||
}
|
||||
.task {
|
||||
await loadPolls()
|
||||
}
|
||||
.refreshable {
|
||||
await loadPolls()
|
||||
}
|
||||
.sheet(isPresented: $showCreatePoll) {
|
||||
PollCreationView(trips: tripsForPollCreation) { poll in
|
||||
polls.insert(poll, at: 0)
|
||||
}
|
||||
}
|
||||
.navigationDestination(for: TripPoll.self) { poll in
|
||||
PollDetailView(pollId: poll.id)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Polls Section
|
||||
|
||||
@ViewBuilder
|
||||
private var pollsSection: some View {
|
||||
VStack(alignment: .leading, spacing: Theme.Spacing.sm) {
|
||||
HStack {
|
||||
Text("Group Polls")
|
||||
.font(.title2)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
|
||||
Spacer()
|
||||
|
||||
if trips.count >= 2 {
|
||||
Button {
|
||||
showCreatePoll = true
|
||||
} label: {
|
||||
Image(systemName: "plus.circle")
|
||||
.foregroundStyle(Theme.warmOrange)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isLoadingPolls {
|
||||
ProgressView()
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.padding()
|
||||
} else if polls.isEmpty {
|
||||
emptyPollsCard
|
||||
} else {
|
||||
ForEach(polls) { poll in
|
||||
NavigationLink(value: poll) {
|
||||
PollRowCard(poll: poll)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var emptyPollsCard: some View {
|
||||
VStack(spacing: Theme.Spacing.sm) {
|
||||
Image(systemName: "person.3")
|
||||
.font(.title)
|
||||
.foregroundStyle(Theme.textMuted(colorScheme))
|
||||
|
||||
Text("No group polls yet")
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(Theme.textSecondary(colorScheme))
|
||||
|
||||
if trips.count >= 2 {
|
||||
Text("Create a poll to let friends vote on trip options")
|
||||
.font(.caption)
|
||||
.foregroundStyle(Theme.textMuted(colorScheme))
|
||||
.multilineTextAlignment(.center)
|
||||
} else {
|
||||
Text("Save at least 2 trips to create a poll")
|
||||
.font(.caption)
|
||||
.foregroundStyle(Theme.textMuted(colorScheme))
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(Theme.Spacing.lg)
|
||||
.background(Theme.cardBackground(colorScheme))
|
||||
.clipShape(RoundedRectangle(cornerRadius: Theme.CornerRadius.large))
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: Theme.CornerRadius.large)
|
||||
.stroke(Theme.surfaceGlow(colorScheme), lineWidth: 1)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Trips Section
|
||||
|
||||
@ViewBuilder
|
||||
private var tripsSection: some View {
|
||||
VStack(alignment: .leading, spacing: Theme.Spacing.sm) {
|
||||
Text("Saved Trips")
|
||||
.font(.title2)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
|
||||
if trips.isEmpty {
|
||||
VStack(spacing: 16) {
|
||||
Spacer()
|
||||
.frame(height: 100)
|
||||
|
||||
Image(systemName: "suitcase")
|
||||
.font(.largeTitle)
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
Text("No Saved Trips")
|
||||
.font(.title2)
|
||||
.fontWeight(.semibold)
|
||||
.font(.headline)
|
||||
|
||||
Text("Browse featured trips on the Home tab or create your own to get started.")
|
||||
.font(.subheadline)
|
||||
.foregroundColor(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
.padding(.horizontal, 40)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(Theme.Spacing.xl)
|
||||
.background(Theme.cardBackground(colorScheme))
|
||||
.clipShape(RoundedRectangle(cornerRadius: Theme.CornerRadius.large))
|
||||
} else {
|
||||
LazyVStack(spacing: Theme.Spacing.md) {
|
||||
ForEach(Array(sortedTrips.enumerated()), id: \.element.id) { index, savedTrip in
|
||||
if let trip = savedTrip.trip {
|
||||
NavigationLink {
|
||||
TripDetailView(trip: trip, games: savedTrip.games)
|
||||
} label: {
|
||||
SavedTripListRow(trip: trip)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.staggeredAnimation(index: index)
|
||||
ForEach(Array(sortedTrips.enumerated()), id: \.element.id) { index, savedTrip in
|
||||
if let trip = savedTrip.trip {
|
||||
NavigationLink {
|
||||
TripDetailView(trip: trip, games: savedTrip.games)
|
||||
} label: {
|
||||
SavedTripListRow(trip: trip)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.staggeredAnimation(index: index)
|
||||
}
|
||||
}
|
||||
.padding(Theme.Spacing.md)
|
||||
}
|
||||
}
|
||||
.themedBackground()
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
private func loadPolls() async {
|
||||
isLoadingPolls = true
|
||||
do {
|
||||
polls = try await PollService.shared.fetchMyPolls()
|
||||
} catch {
|
||||
// Silently fail - polls just won't show
|
||||
}
|
||||
isLoadingPolls = false
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Poll Row Card
|
||||
|
||||
private struct PollRowCard: View {
|
||||
let poll: TripPoll
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: Theme.Spacing.md) {
|
||||
// Icon
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(Theme.warmOrange.opacity(0.15))
|
||||
.frame(width: 44, height: 44)
|
||||
|
||||
Image(systemName: "chart.bar.doc.horizontal")
|
||||
.foregroundStyle(Theme.warmOrange)
|
||||
}
|
||||
|
||||
VStack(alignment: .leading, spacing: Theme.Spacing.xs) {
|
||||
Text(poll.title)
|
||||
.font(.headline)
|
||||
.foregroundStyle(Theme.textPrimary(colorScheme))
|
||||
|
||||
HStack(spacing: Theme.Spacing.sm) {
|
||||
Label("\(poll.tripSnapshots.count) trips", systemImage: "map")
|
||||
Text("•")
|
||||
Text(poll.shareCode)
|
||||
.fontWeight(.semibold)
|
||||
.foregroundStyle(Theme.warmOrange)
|
||||
}
|
||||
.font(.caption)
|
||||
.foregroundStyle(Theme.textSecondary(colorScheme))
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Image(systemName: "chevron.right")
|
||||
.font(.caption)
|
||||
.foregroundStyle(Theme.textMuted(colorScheme))
|
||||
}
|
||||
.padding(Theme.Spacing.md)
|
||||
.background(Theme.cardBackground(colorScheme))
|
||||
.clipShape(RoundedRectangle(cornerRadius: Theme.CornerRadius.large))
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: Theme.CornerRadius.large)
|
||||
.stroke(Theme.surfaceGlow(colorScheme), lineWidth: 1)
|
||||
}
|
||||
.shadow(color: Theme.cardShadow(colorScheme), radius: 6, y: 3)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// PollCreationViewModel.swift
|
||||
// SportsTime
|
||||
//
|
||||
// ViewModel for creating trip polls
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
final class PollCreationViewModel {
|
||||
var title: String = ""
|
||||
var selectedTripIds: Set<UUID> = []
|
||||
var isLoading = false
|
||||
var error: PollError?
|
||||
var createdPoll: TripPoll?
|
||||
|
||||
private let pollService = PollService.shared
|
||||
|
||||
var canCreate: Bool {
|
||||
!title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
&& selectedTripIds.count >= 2
|
||||
}
|
||||
|
||||
var validationMessage: String? {
|
||||
if title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
return "Enter a title for your poll"
|
||||
}
|
||||
if selectedTripIds.count < 2 {
|
||||
return "Select at least 2 trips to create a poll"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createPoll(trips: [Trip]) async {
|
||||
guard canCreate else { return }
|
||||
|
||||
isLoading = true
|
||||
error = nil
|
||||
|
||||
do {
|
||||
let userId = try await pollService.getCurrentUserRecordID()
|
||||
let selectedTrips = trips.filter { selectedTripIds.contains($0.id) }
|
||||
|
||||
let poll = TripPoll(
|
||||
title: title.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
ownerId: userId,
|
||||
tripSnapshots: selectedTrips
|
||||
)
|
||||
|
||||
createdPoll = try await pollService.createPoll(poll)
|
||||
} catch let pollError as PollError {
|
||||
error = pollError
|
||||
} catch {
|
||||
self.error = .unknown(error)
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
func toggleTrip(_ tripId: UUID) {
|
||||
if selectedTripIds.contains(tripId) {
|
||||
selectedTripIds.remove(tripId)
|
||||
} else {
|
||||
selectedTripIds.insert(tripId)
|
||||
}
|
||||
}
|
||||
|
||||
func reset() {
|
||||
title = ""
|
||||
selectedTripIds = []
|
||||
error = nil
|
||||
createdPoll = nil
|
||||
}
|
||||
}
|
||||
144
SportsTime/Features/Polls/ViewModels/PollDetailViewModel.swift
Normal file
144
SportsTime/Features/Polls/ViewModels/PollDetailViewModel.swift
Normal file
@@ -0,0 +1,144 @@
|
||||
//
|
||||
// PollDetailViewModel.swift
|
||||
// SportsTime
|
||||
//
|
||||
// ViewModel for viewing poll details and results
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
final class PollDetailViewModel {
|
||||
var poll: TripPoll?
|
||||
var votes: [PollVote] = []
|
||||
var myVote: PollVote?
|
||||
var isLoading = false
|
||||
var isRefreshing = false
|
||||
var error: PollError?
|
||||
|
||||
private let pollService = PollService.shared
|
||||
|
||||
var results: PollResults? {
|
||||
guard let poll else { return nil }
|
||||
return PollResults(poll: poll, votes: votes)
|
||||
}
|
||||
|
||||
var isOwner: Bool {
|
||||
get async {
|
||||
guard let poll else { return false }
|
||||
do {
|
||||
let userId = try await pollService.getCurrentUserRecordID()
|
||||
return poll.ownerId == userId
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var hasVoted: Bool {
|
||||
myVote != nil
|
||||
}
|
||||
|
||||
var shareURL: URL? {
|
||||
poll?.shareURL
|
||||
}
|
||||
|
||||
func loadPoll(byId pollId: UUID) async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
|
||||
do {
|
||||
async let pollTask = pollService.fetchPoll(byId: pollId)
|
||||
async let votesTask = pollService.fetchVotes(forPollId: pollId)
|
||||
async let myVoteTask = pollService.fetchMyVote(forPollId: pollId)
|
||||
|
||||
let (fetchedPoll, fetchedVotes, fetchedMyVote) = try await (pollTask, votesTask, myVoteTask)
|
||||
|
||||
self.poll = fetchedPoll
|
||||
self.votes = fetchedVotes
|
||||
self.myVote = fetchedMyVote
|
||||
|
||||
// Subscribe to vote updates
|
||||
try? await pollService.subscribeToVoteUpdates(forPollId: pollId)
|
||||
} catch let pollError as PollError {
|
||||
error = pollError
|
||||
} catch {
|
||||
self.error = .unknown(error)
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
func loadPoll(byShareCode shareCode: String) async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
|
||||
do {
|
||||
let fetchedPoll = try await pollService.fetchPoll(byShareCode: shareCode)
|
||||
self.poll = fetchedPoll
|
||||
|
||||
// Now fetch votes with the poll ID
|
||||
async let votesTask = pollService.fetchVotes(forPollId: fetchedPoll.id)
|
||||
async let myVoteTask = pollService.fetchMyVote(forPollId: fetchedPoll.id)
|
||||
|
||||
let (fetchedVotes, fetchedMyVote) = try await (votesTask, myVoteTask)
|
||||
self.votes = fetchedVotes
|
||||
self.myVote = fetchedMyVote
|
||||
|
||||
// Subscribe to vote updates
|
||||
try? await pollService.subscribeToVoteUpdates(forPollId: fetchedPoll.id)
|
||||
} catch let pollError as PollError {
|
||||
error = pollError
|
||||
} catch {
|
||||
self.error = .unknown(error)
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
func refresh() async {
|
||||
guard let poll else { return }
|
||||
|
||||
isRefreshing = true
|
||||
|
||||
do {
|
||||
async let votesTask = pollService.fetchVotes(forPollId: poll.id)
|
||||
async let myVoteTask = pollService.fetchMyVote(forPollId: poll.id)
|
||||
|
||||
let (fetchedVotes, fetchedMyVote) = try await (votesTask, myVoteTask)
|
||||
self.votes = fetchedVotes
|
||||
self.myVote = fetchedMyVote
|
||||
} catch {
|
||||
// Silently fail refresh - user can pull to refresh again
|
||||
}
|
||||
|
||||
isRefreshing = false
|
||||
}
|
||||
|
||||
func deletePoll() async -> Bool {
|
||||
guard let poll else { return false }
|
||||
|
||||
isLoading = true
|
||||
error = nil
|
||||
|
||||
do {
|
||||
try await pollService.deletePoll(poll.id)
|
||||
try? await pollService.unsubscribeFromVoteUpdates()
|
||||
self.poll = nil
|
||||
return true
|
||||
} catch let pollError as PollError {
|
||||
error = pollError
|
||||
} catch {
|
||||
self.error = .unknown(error)
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
return false
|
||||
}
|
||||
|
||||
func cleanup() async {
|
||||
try? await pollService.unsubscribeFromVoteUpdates()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// 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 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
|
||||
} 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
|
||||
}
|
||||
}
|
||||
133
SportsTime/Features/Polls/Views/PollCreationView.swift
Normal file
133
SportsTime/Features/Polls/Views/PollCreationView.swift
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// PollCreationView.swift
|
||||
// SportsTime
|
||||
//
|
||||
// View for creating a new trip poll
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct PollCreationView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
@State private var viewModel = PollCreationViewModel()
|
||||
|
||||
let trips: [Trip]
|
||||
var onPollCreated: ((TripPoll) -> Void)?
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section {
|
||||
TextField("Poll Title", text: $viewModel.title)
|
||||
.textInputAutocapitalization(.words)
|
||||
} header: {
|
||||
Text("Title")
|
||||
} footer: {
|
||||
Text("Give your poll a name, like \"Summer Road Trip Options\"")
|
||||
}
|
||||
|
||||
Section {
|
||||
ForEach(trips) { trip in
|
||||
TripSelectionRow(
|
||||
trip: trip,
|
||||
isSelected: viewModel.selectedTripIds.contains(trip.id)
|
||||
) {
|
||||
viewModel.toggleTrip(trip.id)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Text("Select Trips (\(viewModel.selectedTripIds.count) selected)")
|
||||
} footer: {
|
||||
if let message = viewModel.validationMessage {
|
||||
Text(message)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Create Poll")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel") {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Create") {
|
||||
Task {
|
||||
await viewModel.createPoll(trips: trips)
|
||||
}
|
||||
}
|
||||
.disabled(!viewModel.canCreate || viewModel.isLoading)
|
||||
}
|
||||
}
|
||||
.overlay {
|
||||
if viewModel.isLoading {
|
||||
ProgressView()
|
||||
.scaleEffect(1.2)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(.ultraThinMaterial)
|
||||
}
|
||||
}
|
||||
.alert("Error", isPresented: .constant(viewModel.error != nil)) {
|
||||
Button("OK") {
|
||||
viewModel.error = nil
|
||||
}
|
||||
} message: {
|
||||
if let error = viewModel.error {
|
||||
Text(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
.onChange(of: viewModel.createdPoll) { _, newPoll in
|
||||
if let poll = newPoll {
|
||||
onPollCreated?(poll)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Trip Selection Row
|
||||
|
||||
private struct TripSelectionRow: View {
|
||||
let trip: Trip
|
||||
let isSelected: Bool
|
||||
let onTap: () -> Void
|
||||
|
||||
var body: some View {
|
||||
Button(action: onTap) {
|
||||
HStack {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(trip.name)
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
|
||||
Text(tripSummary)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")
|
||||
.font(.title2)
|
||||
.foregroundStyle(isSelected ? Theme.warmOrange : .secondary)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
private var tripSummary: String {
|
||||
let stopCount = trip.stops.count
|
||||
let gameCount = trip.stops.flatMap { $0.games }.count
|
||||
return "\(stopCount) stops, \(gameCount) games"
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PollCreationView(trips: [])
|
||||
}
|
||||
318
SportsTime/Features/Polls/Views/PollDetailView.swift
Normal file
318
SportsTime/Features/Polls/Views/PollDetailView.swift
Normal file
@@ -0,0 +1,318 @@
|
||||
//
|
||||
// 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?
|
||||
|
||||
init(pollId: UUID) {
|
||||
self.pollId = pollId
|
||||
self.shareCode = nil
|
||||
}
|
||||
|
||||
init(shareCode: String) {
|
||||
self.pollId = nil
|
||||
self.shareCode = shareCode
|
||||
}
|
||||
|
||||
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 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)
|
||||
}
|
||||
|
||||
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(1)
|
||||
}
|
||||
.padding()
|
||||
.background(Theme.cardBackground(colorScheme))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
PollDetailView(shareCode: "ABC123")
|
||||
}
|
||||
}
|
||||
178
SportsTime/Features/Polls/Views/PollVotingView.swift
Normal file
178
SportsTime/Features/Polls/Views/PollVotingView.swift
Normal file
@@ -0,0 +1,178 @@
|
||||
//
|
||||
// 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
|
||||
)
|
||||
}
|
||||
152
SportsTime/Features/Polls/Views/PollsListView.swift
Normal file
152
SportsTime/Features/Polls/Views/PollsListView.swift
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// PollsListView.swift
|
||||
// SportsTime
|
||||
//
|
||||
// View for listing user's polls
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct PollsListView: View {
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
@State private var polls: [TripPoll] = []
|
||||
@State private var isLoading = false
|
||||
@State private var error: PollError?
|
||||
@State private var showJoinPoll = false
|
||||
@State private var joinCode = ""
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if isLoading && polls.isEmpty {
|
||||
ProgressView("Loading polls...")
|
||||
} else if polls.isEmpty {
|
||||
emptyState
|
||||
} else {
|
||||
pollsList
|
||||
}
|
||||
}
|
||||
.navigationTitle("Group Polls")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button {
|
||||
showJoinPoll = true
|
||||
} label: {
|
||||
Image(systemName: "link.badge.plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
.refreshable {
|
||||
await loadPolls()
|
||||
}
|
||||
.task {
|
||||
await loadPolls()
|
||||
}
|
||||
.alert("Join Poll", isPresented: $showJoinPoll) {
|
||||
TextField("Enter code", text: $joinCode)
|
||||
.textInputAutocapitalization(.characters)
|
||||
Button("Join") {
|
||||
// Navigation will be handled by deep link
|
||||
if !joinCode.isEmpty {
|
||||
// TODO: Navigate to poll detail
|
||||
}
|
||||
}
|
||||
Button("Cancel", role: .cancel) {
|
||||
joinCode = ""
|
||||
}
|
||||
} message: {
|
||||
Text("Enter the 6-character poll code")
|
||||
}
|
||||
.alert("Error", isPresented: .constant(error != nil)) {
|
||||
Button("OK") {
|
||||
error = nil
|
||||
}
|
||||
} message: {
|
||||
if let error {
|
||||
Text(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var emptyState: some View {
|
||||
ContentUnavailableView {
|
||||
Label("No Polls", systemImage: "chart.bar.doc.horizontal")
|
||||
} description: {
|
||||
Text("Create a poll from your saved trips to let friends vote on which trip to take.")
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var pollsList: some View {
|
||||
List {
|
||||
ForEach(polls) { poll in
|
||||
NavigationLink(value: poll) {
|
||||
PollRowView(poll: poll)
|
||||
}
|
||||
}
|
||||
}
|
||||
.listStyle(.plain)
|
||||
.navigationDestination(for: TripPoll.self) { poll in
|
||||
PollDetailView(pollId: poll.id)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadPolls() async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
|
||||
do {
|
||||
polls = try await PollService.shared.fetchMyPolls()
|
||||
} catch let pollError as PollError {
|
||||
error = pollError
|
||||
} catch {
|
||||
self.error = .unknown(error)
|
||||
}
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Poll Row View
|
||||
|
||||
private struct PollRowView: View {
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
let poll: TripPoll
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack {
|
||||
Text(poll.title)
|
||||
.font(.headline)
|
||||
|
||||
Spacer()
|
||||
|
||||
Text(poll.shareCode)
|
||||
.font(.caption)
|
||||
.fontWeight(.semibold)
|
||||
.foregroundStyle(Theme.warmOrange)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(Theme.warmOrange.opacity(0.15))
|
||||
.clipShape(Capsule())
|
||||
}
|
||||
|
||||
HStack {
|
||||
Label("\(poll.tripSnapshots.count) trips", systemImage: "map")
|
||||
|
||||
Spacer()
|
||||
|
||||
Text(poll.createdAt, style: .date)
|
||||
}
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationStack {
|
||||
PollsListView()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user