Files
IYmtg/IYmtg_App_iOS/Services/CoreML/ConditionEngine.swift
Mike Wichers 24dcb44af4 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>
2026-03-05 12:13:17 -05:00

51 lines
1.8 KiB
Swift

import Vision
import CoreML
import UIKit
struct DamageObservation: Identifiable, Sendable {
let id = UUID()
let type: String
let rect: CGRect
let confidence: Float
}
// MARK: - CONDITION ENGINE
class ConditionEngine {
enum Severity: Int { case minor = 1; case critical = 10 }
static var model: VNCoreMLModel? = {
guard AppConfig.enableConditionGrading else { return nil }
return ModelManager.shared.getModel(name: "IYmtgConditionClassifier")
}()
static func getSeverity(for type: String) -> Severity {
return (type == "Inking" || type == "Rips" || type == "WaterDamage") ? .critical : .minor
}
static func detectDamage(image: CGImage, orientation: CGImagePropertyOrientation = .up) -> [DamageObservation] {
guard let model = model else { return [] }
let request = VNCoreMLRequest(model: model)
request.imageCropAndScaleOption = .scaleFill
let handler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
do {
try handler.perform([request])
guard let results = request.results as? [VNRecognizedObjectObservation] else { return [] }
return results.filter { $0.confidence > 0.7 }.map { obs in
DamageObservation(type: obs.labels.first?.identifier ?? "Unknown", rect: obs.boundingBox, confidence: obs.confidence)
}
} catch { return [] }
}
static func overallGrade(damages: [DamageObservation]) -> String {
if model == nil && damages.isEmpty { return "Ungraded" }
var score = 0
for d in damages {
if getSeverity(for: d.type) == .critical { return "Damaged" }
score += 1
}
if score == 0 { return "Near Mint (NM)" }
if score <= 2 { return "Excellent (EX)" }
return "Played (PL)"
}
}