Last active
January 10, 2025 22:39
-
-
Save colinfwren/27146251776f3fe945403aacbfe0023a to your computer and use it in GitHub Desktop.
Generating and Saving doc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import SwiftData | |
struct ContentView: View { | |
@Environment(\.modelContext) var modelContext | |
var body: some View { | |
Button("Export your data") { | |
exportData() | |
} | |
} | |
func getMarkdown(records: [Record]) -> String { | |
var markdown: String = "# Data Export\n\n" | |
for record in records { | |
exportData += "## Header\n\(record.somePropertyToInclude)\n" | |
} | |
return markdown | |
} | |
func dataExportUrl() { | |
let descriptor = FetchDescriptor<Record>() | |
let fetchedRecords = try? modelContext.fetch(descriptor) | |
if let records = fetchedRecords { | |
let markdown = getMarkdown(records) | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd" | |
let url = FileManager.default.temporaryDirectory.appendingPathComponent("export-\(dateFormatter.string(from: Date.now))").appendingPathExtension("md") | |
try? markdown.write(to: url, atomically: true, encoding: .utf8) | |
// do something the URL | |
} else { | |
// something when there's no records | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment