Add lead workflows, PDF signing, AR projects and photo gallery integration
This commit is contained in:
47
GasTankAR/Services/Signing/LeadSignedDocumentStore.swift
Normal file
47
GasTankAR/Services/Signing/LeadSignedDocumentStore.swift
Normal file
@ -0,0 +1,47 @@
|
||||
import Foundation
|
||||
|
||||
/// Keeps locally signed copies discoverable after reopening a lead or restarting the app.
|
||||
/// The source version is part of the key so a newer contract never inherits an older signature.
|
||||
struct LeadSignedDocumentStore {
|
||||
var directory: URL = URL.applicationSupportDirectory
|
||||
.appending(path: "SignedLeadDocuments", directoryHint: .isDirectory)
|
||||
|
||||
private struct Record: Codable {
|
||||
let leadID: String
|
||||
let salesCode: String
|
||||
let documentID: String
|
||||
let fileID: String
|
||||
let version: String
|
||||
let filename: String
|
||||
}
|
||||
|
||||
func save(url: URL, document: LeadDocument, leadID: String, salesCode: String) throws {
|
||||
let record = Record(leadID: leadID, salesCode: salesCode, documentID: document.id,
|
||||
fileID: document.file.id, version: document.file.version,
|
||||
filename: url.lastPathComponent)
|
||||
let data = try JSONEncoder().encode(record)
|
||||
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||
try data.write(to: directory.appending(path: url.deletingPathExtension().lastPathComponent + ".json"),
|
||||
options: .atomic)
|
||||
}
|
||||
|
||||
func savedURLs(documents: [LeadDocument], leadID: String, salesCode: String) -> [String: URL] {
|
||||
guard let files = try? FileManager.default.contentsOfDirectory(
|
||||
at: directory, includingPropertiesForKeys: nil
|
||||
) else { return [:] }
|
||||
var result: [String: URL] = [:]
|
||||
for file in files where file.pathExtension == "json" {
|
||||
guard let data = try? Data(contentsOf: file),
|
||||
let record = try? JSONDecoder().decode(Record.self, from: data),
|
||||
record.leadID == leadID, record.salesCode == salesCode,
|
||||
documents.contains(where: {
|
||||
$0.id == record.documentID && $0.file.id == record.fileID && $0.file.version == record.version
|
||||
}) else { continue }
|
||||
let url = directory.appending(path: record.filename)
|
||||
if FileManager.default.fileExists(atPath: url.path) {
|
||||
result[record.documentID] = url
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user