diff --git a/Thoughts/Model/ComposeModel.swift b/Thoughts/Model/ComposeModel.swift index 49b94a7..78b1653 100644 --- a/Thoughts/Model/ComposeModel.swift +++ b/Thoughts/Model/ComposeModel.swift @@ -39,7 +39,14 @@ class ComposeModel: ObservableObject { .debounce(for: .seconds(0.1), scheduler: DispatchQueue.main) .sink { document in do { - try document.save() + if document.isEmpty { + let fileManager = FileManager.default + if fileManager.fileExists(atPath: document.url.path) { + try fileManager.removeItem(at: document.url) + } + } else { + try document.save() + } } catch { self.error = error } diff --git a/Thoughts/Model/Document.swift b/Thoughts/Model/Document.swift index 8957bf3..c251173 100644 --- a/Thoughts/Model/Document.swift +++ b/Thoughts/Model/Document.swift @@ -29,16 +29,20 @@ struct Document { return "---\ndate: \(dateString)\n---\n\n" } - static func url(for timestamp: Date) -> URL { + var date: Date + var content: String + + var url: URL { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss" formatter.timeZone = .gmt - let filename = formatter.string(from: timestamp) + let filename = formatter.string(from: date) return ApplicationModel.folderURL.appendingPathComponent(filename).appendingPathExtension("md") } - var date: Date - var content: String + var isEmpty: Bool { + return content.isEmpty + } init(date: Date = Date()) { self.date = date @@ -46,7 +50,6 @@ struct Document { } func save() throws { - let url = Self.url(for: date) let content = Self.header(for: date) + self.content try content.write(to: url, atomically: true, encoding: .utf8) }