Repository Public
1//2// VQuant iOS3// File: VQuant/Charts/ChartAnnotationLayer.swift4// Description (2.0): only the store of the 1.0 device-local PencilKit annotations remains here; drawings are now5// anchored to time and price and synced (VQuant/Next/ChartDrawing.swift). 1.0 description: Freehand annotations over a chart with PencilKit (Apple Pencil, or a finger while editing), saved on6// this device per symbol.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10//1112import PencilKit13import SwiftUI1415/// 1.0 kept one PencilKit drawing per symbol on the device, tied to the screen. 2.0 reads them to offer anchoring16/// them to a chart (see `ChartWorkstation`), and never writes new ones.17enum ChartAnnotationStore {18 static var directory: URL {19 let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]20 return base.appendingPathComponent("annotations", isDirectory: true)21 }2223 /// `<symbol>.drawing`, with the characters a file name cannot carry replaced (`^GSPC` → `_GSPC`).24 static func url(for symbol: String) -> URL {25 let safe = String(symbol.uppercased().map { $0.isLetter || $0.isNumber || $0 == "." || $0 == "-" ? $0 : "_" }.prefix(40))26 return directory.appendingPathComponent((safe.isEmpty ? "_" : safe) + ".drawing")27 }2829 static func load(_ symbol: String) -> PKDrawing? {30 guard let data = try? Data(contentsOf: url(for: symbol)) else { return nil }31 return try? PKDrawing(data: data)32 }3334 static func save(_ drawing: PKDrawing, for symbol: String) {35 let target = url(for: symbol)36 if drawing.strokes.isEmpty { try? FileManager.default.removeItem(at: target); return }37 try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)38 try? drawing.dataRepresentation().write(to: target, options: [.atomic, .completeFileProtectionUnlessOpen])39 }4041 /// 2.0: the person removed the 1.0 annotation of a symbol.42 static func remove(_ symbol: String) { try? FileManager.default.removeItem(at: url(for: symbol)) }43 /// 2.0: the annotation was anchored to a chart; the file is kept beside as `<name>.migrated`.44 static func archive(_ symbol: String) {45 let source = url(for: symbol), target = source.appendingPathExtension("migrated")46 try? FileManager.default.removeItem(at: target)47 try? FileManager.default.moveItem(at: source, to: target)48 }49 /// Symbols that still have a 1.0 annotation on this device.50 static func pending() -> [String] {51 ((try? FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)) ?? []).filter { $0.pathExtension == "drawing" }.map { $0.deletingPathExtension().lastPathComponent }.sorted()52 }53}54