Implement storage architecture from ai_blueprint.md
Primary sync: replace PersistenceActor JSON file with SwiftData + CloudKit - Add SavedCardModel (@Model class) and PersistenceController (ModelContainer with .automatic CloudKit, fallback to local). BackgroundPersistenceActor (@ModelActor) handles all DB I/O off the main thread. - One-time migration imports user_collection.json into SwiftData and renames the original file to prevent re-import. - Inject modelContainer into SwiftUI environment in IYmtgApp. Image storage: Documents/UserContent/ subfolder (blueprint requirement) - ImageManager.dir now targets iCloud Documents/UserContent/ (or local equiv). - migrateImagesToUserContent() moves existing JPGs to the new subfolder on first launch; called during the SwiftData migration. Firebase: demoted to optional manual backup (metadata only, no images) - Remove all automatic CloudEngine.save/delete/batchUpdatePrices calls from CollectionViewModel mutations. - Add backupAllToFirebase() for user-triggered metadata sync. - Add isFirebaseBackupEnabled to AppConfig (default false). - Add Cloud Backup section in Library settings with iCloud vs Firebase explanation and "Backup Metadata to Firebase Now" button. Also: full modular refactor (Data/, Features/, Services/ directories) and README updated with CloudKit setup steps and revised release checklist. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
55
IYmtg_App_iOS/Services/Utilities/StoreEngine.swift
Normal file
55
IYmtg_App_iOS/Services/Utilities/StoreEngine.swift
Normal file
@@ -0,0 +1,55 @@
|
||||
import StoreKit
|
||||
|
||||
@MainActor
|
||||
class StoreEngine: ObservableObject {
|
||||
@Published var products: [Product] = []
|
||||
@Published var showThankYou = false
|
||||
var transactionListener: Task<Void, Never>? = nil
|
||||
|
||||
init() {
|
||||
// FIX: Added [weak self] to prevent retain cycle
|
||||
transactionListener = Task.detached { [weak self] in
|
||||
for await result in Transaction.updates {
|
||||
do {
|
||||
guard let self = self else { return }
|
||||
let transaction = try self.checkVerified(result)
|
||||
await transaction.finish()
|
||||
await MainActor.run { self.showThankYou = true }
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deinit { transactionListener?.cancel() }
|
||||
|
||||
func loadProducts() async {
|
||||
do {
|
||||
let p = try await Product.products(for: AppConfig.tipJarProductIDs)
|
||||
self.products = p
|
||||
#if DEBUG
|
||||
if p.isEmpty { print("⚠️ StoreEngine: No products found. Verify IAP ID in AppConfig.") }
|
||||
#endif
|
||||
} catch { print("Store Error: \(error)") }
|
||||
}
|
||||
|
||||
func purchase(_ product: Product) async {
|
||||
guard let result = try? await product.purchase() else { return }
|
||||
switch result {
|
||||
case .success(let verification):
|
||||
if let transaction = try? checkVerified(verification) {
|
||||
await transaction.finish()
|
||||
self.showThankYou = true
|
||||
}
|
||||
case .pending, .userCancelled: break
|
||||
@unknown default: break
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
|
||||
switch result {
|
||||
case .unverified: throw StoreError.failedVerification
|
||||
case .verified(let safe): return safe
|
||||
}
|
||||
}
|
||||
enum StoreError: Error { case failedVerification }
|
||||
}
|
||||
Reference in New Issue
Block a user