diff options
Diffstat (limited to 'octosentry/PersistenceStore.swift')
| -rw-r--r-- | octosentry/PersistenceStore.swift | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/octosentry/PersistenceStore.swift b/octosentry/PersistenceStore.swift new file mode 100644 index 0000000..ca86eec --- /dev/null +++ b/octosentry/PersistenceStore.swift @@ -0,0 +1,46 @@ +// +// PersistenceStore.swift +// octosentry +// +// Loads and saves PersistedState as JSON in the app's Application Support +// container. No entitlement needed — sandboxed apps always get a private +// Application Support directory in their own container. +// + +import Foundation + +actor PersistenceStore { + private let fileURL: URL + + private static let decoder: JSONDecoder = { + let decoder = JSONDecoder() + decoder.dateDecodingStrategy = .iso8601 + return decoder + }() + + private static let encoder: JSONEncoder = { + let encoder = JSONEncoder() + encoder.dateEncodingStrategy = .iso8601 + return encoder + }() + + init() { + let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0] + let directory = appSupport.appendingPathComponent("octosentry", isDirectory: true) + try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + fileURL = directory.appendingPathComponent("state.json") + } + + func load() -> PersistedState { + guard let data = try? Data(contentsOf: fileURL), + let state = try? Self.decoder.decode(PersistedState.self, from: data) else { + return .placeholder + } + return state + } + + func save(_ state: PersistedState) { + guard let data = try? Self.encoder.encode(state) else { return } + try? data.write(to: fileURL, options: .atomic) + } +} |
