import Foundation import CBORCoding protocol HistoryManagerProtocol { func loadEntries() -> [HistoryItem] func save(item: HistoryItem) throws } final class HistoryManager: HistoryManagerProtocol { private let encoder = CBOREncoder(dateEncodingStrategy: .secondsSince1970) private var fm: FileManager { .default } static var documentDirectory: URL { try! FileManager.default.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) } private let fileUrl: URL init() { self.fileUrl = HistoryManager.documentDirectory.appendingPathComponent("history2.bin") } func loadEntries() -> [HistoryItem] { guard fm.fileExists(atPath: fileUrl.path) else { print("No history data found") return [] } let content: Data do { content = try Data(contentsOf: fileUrl) } catch { print("Failed to read history data: \(error)") return [] } let decoder = CBORDecoder() var index = 0 var entries = [HistoryItem]() while index < content.count { let length = Int(content[index]) index += 1 if index + length > content.count { print("Missing bytes in history file: needed \(length), has only \(content.count - index)") return entries } let entryData = content[index.. [HistoryItem] { [.mock] } func save(item: HistoryItem) throws { } }