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>
This commit is contained in:
@@ -18,6 +18,8 @@ enum CKRecordType {
|
||||
static let leagueStructure = "LeagueStructure"
|
||||
static let teamAlias = "TeamAlias"
|
||||
static let stadiumAlias = "StadiumAlias"
|
||||
static let tripPoll = "TripPoll"
|
||||
static let pollVote = "PollVote"
|
||||
}
|
||||
|
||||
// MARK: - CKTeam
|
||||
@@ -472,3 +474,114 @@ struct CKTeamAlias {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - CKTripPoll
|
||||
|
||||
struct CKTripPoll {
|
||||
static let pollIdKey = "pollId"
|
||||
static let titleKey = "title"
|
||||
static let ownerIdKey = "ownerId"
|
||||
static let shareCodeKey = "shareCode"
|
||||
static let tripSnapshotsKey = "tripSnapshots"
|
||||
static let tripVersionsKey = "tripVersions"
|
||||
static let createdAtKey = "createdAt"
|
||||
static let modifiedAtKey = "modifiedAt"
|
||||
|
||||
let record: CKRecord
|
||||
|
||||
init(record: CKRecord) {
|
||||
self.record = record
|
||||
}
|
||||
|
||||
init(poll: TripPoll) {
|
||||
let record = CKRecord(recordType: CKRecordType.tripPoll, recordID: CKRecord.ID(recordName: poll.id.uuidString))
|
||||
record[CKTripPoll.pollIdKey] = poll.id.uuidString
|
||||
record[CKTripPoll.titleKey] = poll.title
|
||||
record[CKTripPoll.ownerIdKey] = poll.ownerId
|
||||
record[CKTripPoll.shareCodeKey] = poll.shareCode
|
||||
// Encode trips as JSON data
|
||||
if let tripsData = try? JSONEncoder().encode(poll.tripSnapshots) {
|
||||
record[CKTripPoll.tripSnapshotsKey] = tripsData
|
||||
}
|
||||
record[CKTripPoll.tripVersionsKey] = poll.tripVersions
|
||||
record[CKTripPoll.createdAtKey] = poll.createdAt
|
||||
record[CKTripPoll.modifiedAtKey] = poll.modifiedAt
|
||||
self.record = record
|
||||
}
|
||||
|
||||
func toPoll() -> TripPoll? {
|
||||
guard let pollIdString = record[CKTripPoll.pollIdKey] as? String,
|
||||
let pollId = UUID(uuidString: pollIdString),
|
||||
let title = record[CKTripPoll.titleKey] as? String,
|
||||
let ownerId = record[CKTripPoll.ownerIdKey] as? String,
|
||||
let shareCode = record[CKTripPoll.shareCodeKey] as? String,
|
||||
let tripsData = record[CKTripPoll.tripSnapshotsKey] as? Data,
|
||||
let tripSnapshots = try? JSONDecoder().decode([Trip].self, from: tripsData),
|
||||
let tripVersions = record[CKTripPoll.tripVersionsKey] as? [String],
|
||||
let createdAt = record[CKTripPoll.createdAtKey] as? Date,
|
||||
let modifiedAt = record[CKTripPoll.modifiedAtKey] as? Date
|
||||
else { return nil }
|
||||
|
||||
var poll = TripPoll(
|
||||
id: pollId,
|
||||
title: title,
|
||||
ownerId: ownerId,
|
||||
shareCode: shareCode,
|
||||
tripSnapshots: tripSnapshots,
|
||||
createdAt: createdAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
// Preserve the actual stored versions (not recomputed)
|
||||
poll.tripVersions = tripVersions
|
||||
return poll
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - CKPollVote
|
||||
|
||||
struct CKPollVote {
|
||||
static let voteIdKey = "voteId"
|
||||
static let pollIdKey = "pollId"
|
||||
static let voterIdKey = "voterId"
|
||||
static let rankingsKey = "rankings"
|
||||
static let votedAtKey = "votedAt"
|
||||
static let modifiedAtKey = "modifiedAt"
|
||||
|
||||
let record: CKRecord
|
||||
|
||||
init(record: CKRecord) {
|
||||
self.record = record
|
||||
}
|
||||
|
||||
init(vote: PollVote) {
|
||||
let record = CKRecord(recordType: CKRecordType.pollVote, recordID: CKRecord.ID(recordName: vote.id.uuidString))
|
||||
record[CKPollVote.voteIdKey] = vote.id.uuidString
|
||||
record[CKPollVote.pollIdKey] = vote.pollId.uuidString
|
||||
record[CKPollVote.voterIdKey] = vote.odg
|
||||
record[CKPollVote.rankingsKey] = vote.rankings
|
||||
record[CKPollVote.votedAtKey] = vote.votedAt
|
||||
record[CKPollVote.modifiedAtKey] = vote.modifiedAt
|
||||
self.record = record
|
||||
}
|
||||
|
||||
func toVote() -> PollVote? {
|
||||
guard let voteIdString = record[CKPollVote.voteIdKey] as? String,
|
||||
let voteId = UUID(uuidString: voteIdString),
|
||||
let pollIdString = record[CKPollVote.pollIdKey] as? String,
|
||||
let pollId = UUID(uuidString: pollIdString),
|
||||
let voterId = record[CKPollVote.voterIdKey] as? String,
|
||||
let rankings = record[CKPollVote.rankingsKey] as? [Int],
|
||||
let votedAt = record[CKPollVote.votedAtKey] as? Date,
|
||||
let modifiedAt = record[CKPollVote.modifiedAtKey] as? Date
|
||||
else { return nil }
|
||||
|
||||
return PollVote(
|
||||
id: voteId,
|
||||
pollId: pollId,
|
||||
odg: voterId,
|
||||
rankings: rankings,
|
||||
votedAt: votedAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
139
SportsTime/Core/Models/Domain/TripPoll.swift
Normal file
139
SportsTime/Core/Models/Domain/TripPoll.swift
Normal file
@@ -0,0 +1,139 @@
|
||||
//
|
||||
// TripPoll.swift
|
||||
// SportsTime
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// MARK: - Trip Poll
|
||||
|
||||
struct TripPoll: Identifiable, Codable, Hashable {
|
||||
let id: UUID
|
||||
var title: String
|
||||
let ownerId: String
|
||||
let shareCode: String
|
||||
var tripSnapshots: [Trip]
|
||||
var tripVersions: [String]
|
||||
let createdAt: Date
|
||||
var modifiedAt: Date
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
title: String,
|
||||
ownerId: String,
|
||||
shareCode: String = TripPoll.generateShareCode(),
|
||||
tripSnapshots: [Trip],
|
||||
createdAt: Date = Date(),
|
||||
modifiedAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.ownerId = ownerId
|
||||
self.shareCode = shareCode
|
||||
self.tripSnapshots = tripSnapshots
|
||||
self.tripVersions = tripSnapshots.map { TripPoll.computeTripHash($0) }
|
||||
self.createdAt = createdAt
|
||||
self.modifiedAt = modifiedAt
|
||||
}
|
||||
|
||||
// MARK: - Share Code Generation
|
||||
|
||||
private static let shareCodeCharacters = Array("ABCDEFGHJKMNPQRSTUVWXYZ23456789")
|
||||
|
||||
static func generateShareCode() -> String {
|
||||
String((0..<6).map { _ in shareCodeCharacters.randomElement()! })
|
||||
}
|
||||
|
||||
// MARK: - Trip Hash
|
||||
|
||||
static func computeTripHash(_ trip: Trip) -> String {
|
||||
var hasher = Hasher()
|
||||
hasher.combine(trip.stops.map { $0.city })
|
||||
hasher.combine(trip.stops.flatMap { $0.games })
|
||||
hasher.combine(trip.preferences.startDate)
|
||||
hasher.combine(trip.preferences.endDate)
|
||||
return String(hasher.finalize())
|
||||
}
|
||||
|
||||
// MARK: - Deep Link URL
|
||||
|
||||
var shareURL: URL {
|
||||
URL(string: "sportstime://poll/\(shareCode)")!
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Poll Vote
|
||||
|
||||
struct PollVote: Identifiable, Codable, Hashable {
|
||||
let id: UUID
|
||||
let pollId: UUID
|
||||
let odg: String // voter's userRecordID
|
||||
var rankings: [Int] // trip indices in preference order
|
||||
let votedAt: Date
|
||||
var modifiedAt: Date
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
pollId: UUID,
|
||||
odg: String,
|
||||
rankings: [Int],
|
||||
votedAt: Date = Date(),
|
||||
modifiedAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.pollId = pollId
|
||||
self.odg = odg
|
||||
self.rankings = rankings
|
||||
self.votedAt = votedAt
|
||||
self.modifiedAt = modifiedAt
|
||||
}
|
||||
|
||||
/// Calculate Borda count scores from rankings
|
||||
/// Returns array where index = trip index, value = score
|
||||
static func calculateScores(rankings: [Int], tripCount: Int) -> [Int] {
|
||||
var scores = Array(repeating: 0, count: tripCount)
|
||||
for (rank, tripIndex) in rankings.enumerated() {
|
||||
guard tripIndex < tripCount else { continue }
|
||||
let points = tripCount - rank
|
||||
scores[tripIndex] = points
|
||||
}
|
||||
return scores
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Poll Results
|
||||
|
||||
struct PollResults {
|
||||
let poll: TripPoll
|
||||
let votes: [PollVote]
|
||||
|
||||
var voterCount: Int { votes.count }
|
||||
|
||||
var tripScores: [(tripIndex: Int, score: Int)] {
|
||||
guard !votes.isEmpty else {
|
||||
return poll.tripSnapshots.indices.map { ($0, 0) }
|
||||
}
|
||||
|
||||
var totalScores = Array(repeating: 0, count: poll.tripSnapshots.count)
|
||||
for vote in votes {
|
||||
let scores = PollVote.calculateScores(rankings: vote.rankings, tripCount: poll.tripSnapshots.count)
|
||||
for (index, score) in scores.enumerated() {
|
||||
totalScores[index] += score
|
||||
}
|
||||
}
|
||||
|
||||
return totalScores.enumerated()
|
||||
.map { ($0.offset, $0.element) }
|
||||
.sorted { $0.score > $1.score }
|
||||
}
|
||||
|
||||
var maxScore: Int {
|
||||
tripScores.first?.score ?? 0
|
||||
}
|
||||
|
||||
func scorePercentage(for tripIndex: Int) -> Double {
|
||||
guard maxScore > 0 else { return 0 }
|
||||
let score = tripScores.first { $0.tripIndex == tripIndex }?.score ?? 0
|
||||
return Double(score) / Double(maxScore)
|
||||
}
|
||||
}
|
||||
134
SportsTime/Core/Models/Local/LocalPoll.swift
Normal file
134
SportsTime/Core/Models/Local/LocalPoll.swift
Normal file
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// LocalPoll.swift
|
||||
// SportsTime
|
||||
//
|
||||
// SwiftData models for local poll persistence
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
// MARK: - Local Trip Poll
|
||||
|
||||
@Model
|
||||
final class LocalTripPoll {
|
||||
@Attribute(.unique) var id: UUID
|
||||
var title: String
|
||||
var ownerId: String
|
||||
var shareCode: String
|
||||
var tripSnapshotsData: Data // Encoded [Trip]
|
||||
var tripVersions: [String]
|
||||
var createdAt: Date
|
||||
var modifiedAt: Date
|
||||
var lastSyncedAt: Date?
|
||||
|
||||
@Relationship(deleteRule: .cascade)
|
||||
var votes: [LocalPollVote]?
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
title: String,
|
||||
ownerId: String,
|
||||
shareCode: String,
|
||||
tripSnapshotsData: Data,
|
||||
tripVersions: [String],
|
||||
createdAt: Date = Date(),
|
||||
modifiedAt: Date = Date(),
|
||||
lastSyncedAt: Date? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
self.ownerId = ownerId
|
||||
self.shareCode = shareCode
|
||||
self.tripSnapshotsData = tripSnapshotsData
|
||||
self.tripVersions = tripVersions
|
||||
self.createdAt = createdAt
|
||||
self.modifiedAt = modifiedAt
|
||||
self.lastSyncedAt = lastSyncedAt
|
||||
}
|
||||
|
||||
var tripSnapshots: [Trip] {
|
||||
(try? JSONDecoder().decode([Trip].self, from: tripSnapshotsData)) ?? []
|
||||
}
|
||||
|
||||
func toPoll() -> TripPoll {
|
||||
var poll = TripPoll(
|
||||
id: id,
|
||||
title: title,
|
||||
ownerId: ownerId,
|
||||
shareCode: shareCode,
|
||||
tripSnapshots: tripSnapshots,
|
||||
createdAt: createdAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
poll.tripVersions = tripVersions
|
||||
return poll
|
||||
}
|
||||
|
||||
static func from(_ poll: TripPoll) -> LocalTripPoll? {
|
||||
guard let tripsData = try? JSONEncoder().encode(poll.tripSnapshots) else { return nil }
|
||||
return LocalTripPoll(
|
||||
id: poll.id,
|
||||
title: poll.title,
|
||||
ownerId: poll.ownerId,
|
||||
shareCode: poll.shareCode,
|
||||
tripSnapshotsData: tripsData,
|
||||
tripVersions: poll.tripVersions,
|
||||
createdAt: poll.createdAt,
|
||||
modifiedAt: poll.modifiedAt
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Local Poll Vote
|
||||
|
||||
@Model
|
||||
final class LocalPollVote {
|
||||
@Attribute(.unique) var id: UUID
|
||||
var pollId: UUID
|
||||
var voterId: String
|
||||
var rankings: [Int]
|
||||
var votedAt: Date
|
||||
var modifiedAt: Date
|
||||
var lastSyncedAt: Date?
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
pollId: UUID,
|
||||
voterId: String,
|
||||
rankings: [Int],
|
||||
votedAt: Date = Date(),
|
||||
modifiedAt: Date = Date(),
|
||||
lastSyncedAt: Date? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.pollId = pollId
|
||||
self.voterId = voterId
|
||||
self.rankings = rankings
|
||||
self.votedAt = votedAt
|
||||
self.modifiedAt = modifiedAt
|
||||
self.lastSyncedAt = lastSyncedAt
|
||||
}
|
||||
|
||||
func toVote() -> PollVote {
|
||||
PollVote(
|
||||
id: id,
|
||||
pollId: pollId,
|
||||
odg: voterId,
|
||||
rankings: rankings,
|
||||
votedAt: votedAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
}
|
||||
|
||||
static func from(_ vote: PollVote) -> LocalPollVote {
|
||||
LocalPollVote(
|
||||
id: vote.id,
|
||||
pollId: vote.pollId,
|
||||
voterId: vote.odg,
|
||||
rankings: vote.rankings,
|
||||
votedAt: vote.votedAt,
|
||||
modifiedAt: vote.modifiedAt
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user