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>
49 lines
1.7 KiB
Swift
49 lines
1.7 KiB
Swift
import Vision
|
|
import CoreML
|
|
import CoreGraphics
|
|
|
|
// MARK: - FOIL ENGINE
|
|
actor FoilEngine {
|
|
private var frameCounter = 0
|
|
private var lowConfidenceStreak = 0
|
|
|
|
static var model: VNCoreMLModel? = {
|
|
guard AppConfig.enableFoilDetection else { return nil }
|
|
return ModelManager.shared.getModel(name: "IYmtgFoilClassifier")
|
|
}()
|
|
|
|
private lazy var request: VNCoreMLRequest? = {
|
|
guard let model = FoilEngine.model else { return nil }
|
|
let req = VNCoreMLRequest(model: model)
|
|
req.imageCropAndScaleOption = .scaleFill
|
|
return req
|
|
}()
|
|
|
|
func addFrame(_ image: CGImage, orientation: CGImagePropertyOrientation = .up) -> String? {
|
|
frameCounter += 1
|
|
// SAFETY: Prevent integer overflow in long-running sessions
|
|
if frameCounter > 1000 { frameCounter = 0 }
|
|
if frameCounter % 5 != 0 { return nil }
|
|
guard let request = self.request else { return AppConfig.Defaults.defaultFoil }
|
|
|
|
let handler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
|
|
|
|
do {
|
|
try handler.perform([request])
|
|
guard let results = request.results as? [VNClassificationObservation], let top = results.first else { return AppConfig.Defaults.defaultFoil }
|
|
|
|
if top.confidence > 0.85 {
|
|
lowConfidenceStreak = 0
|
|
return top.identifier
|
|
} else {
|
|
lowConfidenceStreak += 1
|
|
if lowConfidenceStreak >= 3 {
|
|
return AppConfig.Defaults.defaultFoil
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
} catch { return AppConfig.Defaults.defaultFoil }
|
|
}
|
|
}
|