// // BackupDocument.swift // OTP // // Created by Shadowfacts on 8/23/21. // import SwiftUI import UniformTypeIdentifiers import OTPKit struct BackupDocument: FileDocument { private static let encoder = PropertyListEncoder() private static let decoder = PropertyListDecoder() static var readableContentTypes: [UTType] { [.propertyList] } private(set) var data: KeyData init(data: KeyData) { self.data = data } init(configuration: ReadConfiguration) throws { if let data = configuration.file.regularFileContents { let store = try BackupDocument.decoder.decode(KeyData.self, from: data) self.data = store } else { self.data = KeyData() } } init(url: URL) throws { let data = try Data(contentsOf: url) let store = try BackupDocument.decoder.decode(KeyData.self, from: data) self.data = store } func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { let data = try! BackupDocument.encoder.encode(data) return FileWrapper(regularFileWithContents: data) } }