Merge branch 'custom_ish'
This commit is contained in:
@@ -20,6 +20,7 @@ enum CKRecordType {
|
||||
static let stadiumAlias = "StadiumAlias"
|
||||
static let tripPoll = "TripPoll"
|
||||
static let pollVote = "PollVote"
|
||||
static let customItineraryItem = "CustomItineraryItem"
|
||||
}
|
||||
|
||||
// MARK: - CKTeam
|
||||
@@ -622,3 +623,74 @@ struct CKPollVote {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - CKCustomItineraryItem
|
||||
|
||||
struct CKCustomItineraryItem {
|
||||
static let itemIdKey = "itemId"
|
||||
static let tripIdKey = "tripId"
|
||||
static let categoryKey = "category"
|
||||
static let titleKey = "title"
|
||||
static let anchorTypeKey = "anchorType"
|
||||
static let anchorIdKey = "anchorId"
|
||||
static let anchorDayKey = "anchorDay"
|
||||
static let sortOrderKey = "sortOrder"
|
||||
static let createdAtKey = "createdAt"
|
||||
static let modifiedAtKey = "modifiedAt"
|
||||
|
||||
let record: CKRecord
|
||||
|
||||
init(record: CKRecord) {
|
||||
self.record = record
|
||||
}
|
||||
|
||||
init(item: CustomItineraryItem) {
|
||||
let record = CKRecord(
|
||||
recordType: CKRecordType.customItineraryItem,
|
||||
recordID: CKRecord.ID(recordName: item.id.uuidString)
|
||||
)
|
||||
record[CKCustomItineraryItem.itemIdKey] = item.id.uuidString
|
||||
record[CKCustomItineraryItem.tripIdKey] = item.tripId.uuidString
|
||||
record[CKCustomItineraryItem.categoryKey] = item.category.rawValue
|
||||
record[CKCustomItineraryItem.titleKey] = item.title
|
||||
record[CKCustomItineraryItem.anchorTypeKey] = item.anchorType.rawValue
|
||||
record[CKCustomItineraryItem.anchorIdKey] = item.anchorId
|
||||
record[CKCustomItineraryItem.anchorDayKey] = item.anchorDay
|
||||
record[CKCustomItineraryItem.sortOrderKey] = item.sortOrder
|
||||
record[CKCustomItineraryItem.createdAtKey] = item.createdAt
|
||||
record[CKCustomItineraryItem.modifiedAtKey] = item.modifiedAt
|
||||
self.record = record
|
||||
}
|
||||
|
||||
func toItem() -> CustomItineraryItem? {
|
||||
guard let itemIdString = record[CKCustomItineraryItem.itemIdKey] as? String,
|
||||
let itemId = UUID(uuidString: itemIdString),
|
||||
let tripIdString = record[CKCustomItineraryItem.tripIdKey] as? String,
|
||||
let tripId = UUID(uuidString: tripIdString),
|
||||
let categoryString = record[CKCustomItineraryItem.categoryKey] as? String,
|
||||
let category = CustomItineraryItem.ItemCategory(rawValue: categoryString),
|
||||
let title = record[CKCustomItineraryItem.titleKey] as? String,
|
||||
let anchorTypeString = record[CKCustomItineraryItem.anchorTypeKey] as? String,
|
||||
let anchorType = CustomItineraryItem.AnchorType(rawValue: anchorTypeString),
|
||||
let anchorDay = record[CKCustomItineraryItem.anchorDayKey] as? Int,
|
||||
let createdAt = record[CKCustomItineraryItem.createdAtKey] as? Date,
|
||||
let modifiedAt = record[CKCustomItineraryItem.modifiedAtKey] as? Date
|
||||
else { return nil }
|
||||
|
||||
let anchorId = record[CKCustomItineraryItem.anchorIdKey] as? String
|
||||
let sortOrder = record[CKCustomItineraryItem.sortOrderKey] as? Int ?? 0
|
||||
|
||||
return CustomItineraryItem(
|
||||
id: itemId,
|
||||
tripId: tripId,
|
||||
category: category,
|
||||
title: title,
|
||||
anchorType: anchorType,
|
||||
anchorId: anchorId,
|
||||
anchorDay: anchorDay,
|
||||
sortOrder: sortOrder,
|
||||
createdAt: createdAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
83
SportsTime/Core/Models/Domain/CustomItineraryItem.swift
Normal file
83
SportsTime/Core/Models/Domain/CustomItineraryItem.swift
Normal file
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// CustomItineraryItem.swift
|
||||
// SportsTime
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct CustomItineraryItem: Identifiable, Codable, Hashable {
|
||||
let id: UUID
|
||||
let tripId: UUID
|
||||
var category: ItemCategory
|
||||
var title: String
|
||||
var anchorType: AnchorType
|
||||
var anchorId: String?
|
||||
var anchorDay: Int
|
||||
var sortOrder: Int // For ordering within same anchor position
|
||||
let createdAt: Date
|
||||
var modifiedAt: Date
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
tripId: UUID,
|
||||
category: ItemCategory,
|
||||
title: String,
|
||||
anchorType: AnchorType = .startOfDay,
|
||||
anchorId: String? = nil,
|
||||
anchorDay: Int,
|
||||
sortOrder: Int = 0,
|
||||
createdAt: Date = Date(),
|
||||
modifiedAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.tripId = tripId
|
||||
self.category = category
|
||||
self.title = title
|
||||
self.anchorType = anchorType
|
||||
self.anchorId = anchorId
|
||||
self.anchorDay = anchorDay
|
||||
self.sortOrder = sortOrder
|
||||
self.createdAt = createdAt
|
||||
self.modifiedAt = modifiedAt
|
||||
}
|
||||
|
||||
enum ItemCategory: String, Codable, CaseIterable {
|
||||
case restaurant
|
||||
case hotel
|
||||
case activity
|
||||
case note
|
||||
|
||||
var icon: String {
|
||||
switch self {
|
||||
case .restaurant: return "🍽️"
|
||||
case .hotel: return "🏨"
|
||||
case .activity: return "🎯"
|
||||
case .note: return "📝"
|
||||
}
|
||||
}
|
||||
|
||||
var label: String {
|
||||
switch self {
|
||||
case .restaurant: return "Restaurant"
|
||||
case .hotel: return "Hotel"
|
||||
case .activity: return "Activity"
|
||||
case .note: return "Note"
|
||||
}
|
||||
}
|
||||
|
||||
var systemImage: String {
|
||||
switch self {
|
||||
case .restaurant: return "fork.knife"
|
||||
case .hotel: return "bed.double.fill"
|
||||
case .activity: return "figure.run"
|
||||
case .note: return "note.text"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum AnchorType: String, Codable {
|
||||
case startOfDay
|
||||
case afterGame
|
||||
case afterTravel
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,79 @@ final class TripVote {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Local Custom Item (Cache)
|
||||
|
||||
@Model
|
||||
final class LocalCustomItem {
|
||||
@Attribute(.unique) var id: UUID
|
||||
var tripId: UUID
|
||||
var category: String
|
||||
var title: String
|
||||
var anchorType: String
|
||||
var anchorId: String?
|
||||
var anchorDay: Int
|
||||
var createdAt: Date
|
||||
var modifiedAt: Date
|
||||
var pendingSync: Bool // True if needs to sync to CloudKit
|
||||
|
||||
init(
|
||||
id: UUID = UUID(),
|
||||
tripId: UUID,
|
||||
category: CustomItineraryItem.ItemCategory,
|
||||
title: String,
|
||||
anchorType: CustomItineraryItem.AnchorType = .startOfDay,
|
||||
anchorId: String? = nil,
|
||||
anchorDay: Int,
|
||||
createdAt: Date = Date(),
|
||||
modifiedAt: Date = Date(),
|
||||
pendingSync: Bool = false
|
||||
) {
|
||||
self.id = id
|
||||
self.tripId = tripId
|
||||
self.category = category.rawValue
|
||||
self.title = title
|
||||
self.anchorType = anchorType.rawValue
|
||||
self.anchorId = anchorId
|
||||
self.anchorDay = anchorDay
|
||||
self.createdAt = createdAt
|
||||
self.modifiedAt = modifiedAt
|
||||
self.pendingSync = pendingSync
|
||||
}
|
||||
|
||||
var toItem: CustomItineraryItem? {
|
||||
guard let category = CustomItineraryItem.ItemCategory(rawValue: category),
|
||||
let anchorType = CustomItineraryItem.AnchorType(rawValue: anchorType)
|
||||
else { return nil }
|
||||
|
||||
return CustomItineraryItem(
|
||||
id: id,
|
||||
tripId: tripId,
|
||||
category: category,
|
||||
title: title,
|
||||
anchorType: anchorType,
|
||||
anchorId: anchorId,
|
||||
anchorDay: anchorDay,
|
||||
createdAt: createdAt,
|
||||
modifiedAt: modifiedAt
|
||||
)
|
||||
}
|
||||
|
||||
static func from(_ item: CustomItineraryItem, pendingSync: Bool = false) -> LocalCustomItem {
|
||||
LocalCustomItem(
|
||||
id: item.id,
|
||||
tripId: item.tripId,
|
||||
category: item.category,
|
||||
title: item.title,
|
||||
anchorType: item.anchorType,
|
||||
anchorId: item.anchorId,
|
||||
anchorDay: item.anchorDay,
|
||||
createdAt: item.createdAt,
|
||||
modifiedAt: item.modifiedAt,
|
||||
pendingSync: pendingSync
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - User Preferences
|
||||
|
||||
@Model
|
||||
|
||||
48
SportsTime/Core/Services/AppDelegate.swift
Normal file
48
SportsTime/Core/Services/AppDelegate.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// AppDelegate.swift
|
||||
// SportsTime
|
||||
//
|
||||
// Handles push notification registration and CloudKit subscription notifications
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import CloudKit
|
||||
import UserNotifications
|
||||
|
||||
class AppDelegate: NSObject, UIApplicationDelegate {
|
||||
|
||||
func application(
|
||||
_ application: UIApplication,
|
||||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
|
||||
) -> Bool {
|
||||
// Register for remote notifications (required for CloudKit subscriptions)
|
||||
application.registerForRemoteNotifications()
|
||||
return true
|
||||
}
|
||||
|
||||
func application(
|
||||
_ application: UIApplication,
|
||||
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
|
||||
) {
|
||||
print("📡 [Push] Registered for remote notifications")
|
||||
}
|
||||
|
||||
func application(
|
||||
_ application: UIApplication,
|
||||
didFailToRegisterForRemoteNotificationsWithError error: Error
|
||||
) {
|
||||
print("📡 [Push] Failed to register: \(error.localizedDescription)")
|
||||
}
|
||||
|
||||
func application(
|
||||
_ application: UIApplication,
|
||||
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
||||
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
|
||||
) {
|
||||
// Handle CloudKit subscription notification
|
||||
Task {
|
||||
await CustomItemSubscriptionService.shared.handleRemoteNotification(userInfo: userInfo)
|
||||
completionHandler(.newData)
|
||||
}
|
||||
}
|
||||
}
|
||||
204
SportsTime/Core/Services/CustomItemService.swift
Normal file
204
SportsTime/Core/Services/CustomItemService.swift
Normal file
@@ -0,0 +1,204 @@
|
||||
//
|
||||
// CustomItemService.swift
|
||||
// SportsTime
|
||||
//
|
||||
// CloudKit service for custom itinerary items
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CloudKit
|
||||
|
||||
actor CustomItemService {
|
||||
static let shared = CustomItemService()
|
||||
|
||||
private let container: CKContainer
|
||||
private let publicDatabase: CKDatabase
|
||||
|
||||
private init() {
|
||||
self.container = CKContainer(identifier: "iCloud.com.sportstime.app")
|
||||
self.publicDatabase = container.publicCloudDatabase
|
||||
}
|
||||
|
||||
// MARK: - CRUD Operations
|
||||
|
||||
func createItem(_ item: CustomItineraryItem) async throws -> CustomItineraryItem {
|
||||
print("☁️ [CloudKit] Creating record for item: \(item.id)")
|
||||
let ckItem = CKCustomItineraryItem(item: item)
|
||||
|
||||
do {
|
||||
let savedRecord = try await publicDatabase.save(ckItem.record)
|
||||
print("☁️ [CloudKit] Record saved: \(savedRecord.recordID.recordName)")
|
||||
return item
|
||||
} catch let error as CKError {
|
||||
print("☁️ [CloudKit] CKError on create: \(error.code.rawValue) - \(error.localizedDescription)")
|
||||
throw mapCloudKitError(error)
|
||||
} catch {
|
||||
print("☁️ [CloudKit] Unknown error on create: \(error)")
|
||||
throw CustomItemError.unknown(error)
|
||||
}
|
||||
}
|
||||
|
||||
func updateItem(_ item: CustomItineraryItem) async throws -> CustomItineraryItem {
|
||||
// Fetch existing record to get changeTag
|
||||
let recordID = CKRecord.ID(recordName: item.id.uuidString)
|
||||
let existingRecord: CKRecord
|
||||
|
||||
do {
|
||||
existingRecord = try await publicDatabase.record(for: recordID)
|
||||
} catch let error as CKError {
|
||||
throw mapCloudKitError(error)
|
||||
} catch {
|
||||
throw CustomItemError.unknown(error)
|
||||
}
|
||||
|
||||
// Update fields
|
||||
let now = Date()
|
||||
existingRecord[CKCustomItineraryItem.categoryKey] = item.category.rawValue
|
||||
existingRecord[CKCustomItineraryItem.titleKey] = item.title
|
||||
existingRecord[CKCustomItineraryItem.anchorTypeKey] = item.anchorType.rawValue
|
||||
existingRecord[CKCustomItineraryItem.anchorIdKey] = item.anchorId
|
||||
existingRecord[CKCustomItineraryItem.anchorDayKey] = item.anchorDay
|
||||
existingRecord[CKCustomItineraryItem.sortOrderKey] = item.sortOrder
|
||||
existingRecord[CKCustomItineraryItem.modifiedAtKey] = now
|
||||
|
||||
do {
|
||||
try await publicDatabase.save(existingRecord)
|
||||
var updatedItem = item
|
||||
updatedItem.modifiedAt = now
|
||||
return updatedItem
|
||||
} catch let error as CKError {
|
||||
throw mapCloudKitError(error)
|
||||
} catch {
|
||||
throw CustomItemError.unknown(error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Batch update sortOrder for multiple items (for reordering)
|
||||
func updateSortOrders(_ items: [CustomItineraryItem]) async throws {
|
||||
guard !items.isEmpty else { return }
|
||||
print("☁️ [CloudKit] Batch updating sortOrder for \(items.count) items")
|
||||
|
||||
// Fetch all records
|
||||
let recordIDs = items.map { CKRecord.ID(recordName: $0.id.uuidString) }
|
||||
let fetchResults = try await publicDatabase.records(for: recordIDs)
|
||||
|
||||
// Update each record's sortOrder
|
||||
var recordsToSave: [CKRecord] = []
|
||||
let now = Date()
|
||||
|
||||
for item in items {
|
||||
let recordID = CKRecord.ID(recordName: item.id.uuidString)
|
||||
guard case .success(let record) = fetchResults[recordID] else { continue }
|
||||
|
||||
record[CKCustomItineraryItem.sortOrderKey] = item.sortOrder
|
||||
record[CKCustomItineraryItem.modifiedAtKey] = now
|
||||
recordsToSave.append(record)
|
||||
}
|
||||
|
||||
// Save all in one batch operation
|
||||
let modifyOp = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: nil)
|
||||
modifyOp.savePolicy = .changedKeys
|
||||
|
||||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
|
||||
modifyOp.modifyRecordsResultBlock = { result in
|
||||
switch result {
|
||||
case .success:
|
||||
print("☁️ [CloudKit] Batch sortOrder update complete")
|
||||
continuation.resume()
|
||||
case .failure(let error):
|
||||
print("☁️ [CloudKit] Batch update failed: \(error)")
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
publicDatabase.add(modifyOp)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteItem(_ itemId: UUID) async throws {
|
||||
let recordID = CKRecord.ID(recordName: itemId.uuidString)
|
||||
|
||||
do {
|
||||
try await publicDatabase.deleteRecord(withID: recordID)
|
||||
} catch let error as CKError {
|
||||
if error.code != .unknownItem {
|
||||
throw mapCloudKitError(error)
|
||||
}
|
||||
// Item already deleted - ignore
|
||||
} catch {
|
||||
throw CustomItemError.unknown(error)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchItems(forTripId tripId: UUID) async throws -> [CustomItineraryItem] {
|
||||
print("☁️ [CloudKit] Fetching items for tripId: \(tripId.uuidString)")
|
||||
let predicate = NSPredicate(
|
||||
format: "%K == %@",
|
||||
CKCustomItineraryItem.tripIdKey,
|
||||
tripId.uuidString
|
||||
)
|
||||
let query = CKQuery(recordType: CKRecordType.customItineraryItem, predicate: predicate)
|
||||
|
||||
do {
|
||||
let (results, _) = try await publicDatabase.records(matching: query)
|
||||
print("☁️ [CloudKit] Query returned \(results.count) records")
|
||||
|
||||
let items = results.compactMap { result -> CustomItineraryItem? in
|
||||
guard case .success(let record) = result.1 else {
|
||||
print("☁️ [CloudKit] Record fetch failed for: \(result.0.recordName)")
|
||||
return nil
|
||||
}
|
||||
let item = CKCustomItineraryItem(record: record).toItem()
|
||||
if item == nil {
|
||||
print("☁️ [CloudKit] Failed to parse record: \(record.recordID.recordName)")
|
||||
}
|
||||
return item
|
||||
}.sorted { ($0.anchorDay, $0.sortOrder) < ($1.anchorDay, $1.sortOrder) }
|
||||
|
||||
print("☁️ [CloudKit] Parsed \(items.count) valid items")
|
||||
return items
|
||||
} catch let error as CKError {
|
||||
print("☁️ [CloudKit] CKError on fetch: \(error.code.rawValue) - \(error.localizedDescription)")
|
||||
throw mapCloudKitError(error)
|
||||
} catch {
|
||||
print("☁️ [CloudKit] Unknown error on fetch: \(error)")
|
||||
throw CustomItemError.unknown(error)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Error Mapping
|
||||
|
||||
private func mapCloudKitError(_ error: CKError) -> CustomItemError {
|
||||
switch error.code {
|
||||
case .notAuthenticated:
|
||||
return .notSignedIn
|
||||
case .networkUnavailable, .networkFailure:
|
||||
return .networkUnavailable
|
||||
case .unknownItem:
|
||||
return .itemNotFound
|
||||
default:
|
||||
return .unknown(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
enum CustomItemError: Error, LocalizedError {
|
||||
case notSignedIn
|
||||
case itemNotFound
|
||||
case networkUnavailable
|
||||
case unknown(Error)
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .notSignedIn:
|
||||
return "Please sign in to iCloud to use custom items."
|
||||
case .itemNotFound:
|
||||
return "Item not found. It may have been deleted."
|
||||
case .networkUnavailable:
|
||||
return "Unable to connect. Please check your internet connection."
|
||||
case .unknown(let error):
|
||||
return "An error occurred: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
}
|
||||
147
SportsTime/Core/Services/CustomItemSubscriptionService.swift
Normal file
147
SportsTime/Core/Services/CustomItemSubscriptionService.swift
Normal file
@@ -0,0 +1,147 @@
|
||||
//
|
||||
// CustomItemSubscriptionService.swift
|
||||
// SportsTime
|
||||
//
|
||||
// CloudKit subscription service for real-time custom item updates
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CloudKit
|
||||
import Combine
|
||||
|
||||
/// Manages CloudKit subscriptions for custom itinerary items on shared trips
|
||||
actor CustomItemSubscriptionService {
|
||||
static let shared = CustomItemSubscriptionService()
|
||||
|
||||
private let container: CKContainer
|
||||
private let publicDatabase: CKDatabase
|
||||
|
||||
/// Publisher that emits trip IDs when their custom items change
|
||||
private let changeSubject = PassthroughSubject<UUID, Never>()
|
||||
var changePublisher: AnyPublisher<UUID, Never> {
|
||||
changeSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
/// Track which trip IDs have active subscriptions
|
||||
private var subscribedTripIds: Set<UUID> = []
|
||||
|
||||
private init() {
|
||||
self.container = CKContainer(identifier: "iCloud.com.sportstime.app")
|
||||
self.publicDatabase = container.publicCloudDatabase
|
||||
}
|
||||
|
||||
// MARK: - Subscription Management
|
||||
|
||||
/// Subscribe to changes for a specific trip's custom items
|
||||
func subscribeToTrip(_ tripId: UUID) async throws {
|
||||
guard !subscribedTripIds.contains(tripId) else {
|
||||
print("📡 [Subscription] Already subscribed to trip: \(tripId)")
|
||||
return
|
||||
}
|
||||
|
||||
let subscriptionID = "custom-items-\(tripId.uuidString)"
|
||||
|
||||
// Check if subscription already exists
|
||||
do {
|
||||
_ = try await publicDatabase.subscription(for: subscriptionID)
|
||||
subscribedTripIds.insert(tripId)
|
||||
print("📡 [Subscription] Found existing subscription for trip: \(tripId)")
|
||||
return
|
||||
} catch let error as CKError where error.code == .unknownItem {
|
||||
// Subscription doesn't exist, create it
|
||||
} catch {
|
||||
print("📡 [Subscription] Error checking subscription: \(error)")
|
||||
}
|
||||
|
||||
// Create subscription for this trip's custom items
|
||||
let predicate = NSPredicate(
|
||||
format: "%K == %@",
|
||||
CKCustomItineraryItem.tripIdKey,
|
||||
tripId.uuidString
|
||||
)
|
||||
|
||||
let subscription = CKQuerySubscription(
|
||||
recordType: CKRecordType.customItineraryItem,
|
||||
predicate: predicate,
|
||||
subscriptionID: subscriptionID,
|
||||
options: [.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion]
|
||||
)
|
||||
|
||||
// Configure notification
|
||||
let notificationInfo = CKSubscription.NotificationInfo()
|
||||
notificationInfo.shouldSendContentAvailable = true // Silent notification
|
||||
subscription.notificationInfo = notificationInfo
|
||||
|
||||
do {
|
||||
try await publicDatabase.save(subscription)
|
||||
subscribedTripIds.insert(tripId)
|
||||
print("📡 [Subscription] Created subscription for trip: \(tripId)")
|
||||
} catch let error as CKError {
|
||||
print("📡 [Subscription] Failed to create subscription: \(error.code.rawValue) - \(error.localizedDescription)")
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsubscribe from a specific trip's custom items
|
||||
func unsubscribeFromTrip(_ tripId: UUID) async throws {
|
||||
guard subscribedTripIds.contains(tripId) else { return }
|
||||
|
||||
let subscriptionID = "custom-items-\(tripId.uuidString)"
|
||||
|
||||
do {
|
||||
try await publicDatabase.deleteSubscription(withID: subscriptionID)
|
||||
subscribedTripIds.remove(tripId)
|
||||
print("📡 [Subscription] Removed subscription for trip: \(tripId)")
|
||||
} catch let error as CKError where error.code == .unknownItem {
|
||||
// Already deleted
|
||||
subscribedTripIds.remove(tripId)
|
||||
}
|
||||
}
|
||||
|
||||
/// Subscribe to all trips that the user has access to (via polls)
|
||||
func subscribeToSharedTrips(tripIds: [UUID]) async {
|
||||
for tripId in tripIds {
|
||||
do {
|
||||
try await subscribeToTrip(tripId)
|
||||
} catch {
|
||||
print("📡 [Subscription] Failed to subscribe to trip \(tripId): \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Handling
|
||||
|
||||
/// Handle CloudKit notification - call this from AppDelegate/SceneDelegate
|
||||
func handleNotification(_ notification: CKNotification) {
|
||||
guard let queryNotification = notification as? CKQueryNotification,
|
||||
let subscriptionID = queryNotification.subscriptionID,
|
||||
subscriptionID.hasPrefix("custom-items-") else {
|
||||
return
|
||||
}
|
||||
|
||||
// Extract trip ID from subscription ID
|
||||
let tripIdString = String(subscriptionID.dropFirst("custom-items-".count))
|
||||
guard let tripId = UUID(uuidString: tripIdString) else { return }
|
||||
|
||||
print("📡 [Subscription] Received change notification for trip: \(tripId)")
|
||||
changeSubject.send(tripId)
|
||||
}
|
||||
|
||||
/// Handle notification from userInfo dictionary (from push notification)
|
||||
nonisolated func handleRemoteNotification(userInfo: [AnyHashable: Any]) async {
|
||||
guard let notification = CKNotification(fromRemoteNotificationDictionary: userInfo) else {
|
||||
return
|
||||
}
|
||||
await handleNotification(notification)
|
||||
}
|
||||
|
||||
// MARK: - Cleanup
|
||||
|
||||
/// Remove all subscriptions (call on sign out or cleanup)
|
||||
func removeAllSubscriptions() async {
|
||||
for tripId in subscribedTripIds {
|
||||
try? await unsubscribeFromTrip(tripId)
|
||||
}
|
||||
subscribedTripIds.removeAll()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user