2022-07-15 15:01:42 -07:00
|
|
|
//
|
|
|
|
|
// CsvDocument.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 7/15/22.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
import UniformTypeIdentifiers
|
|
|
|
|
|
|
|
|
|
struct CsvDocument: FileDocument {
|
|
|
|
|
|
|
|
|
|
static var readableContentTypes = [UTType.commaSeparatedText]
|
|
|
|
|
|
|
|
|
|
@State var csvData: String
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
init(emptyCsv: String = "" ) {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
csvData = emptyCsv
|
|
|
|
|
}
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
init(configuration: ReadConfiguration) throws {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
if let data = configuration.file.regularFileContents {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
csvData = String(decoding: data, as: UTF8.self)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
} else {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
throw CocoaError(.fileReadCorruptFile)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
|
|
|
|
|
let data = Data(csvData.utf8)
|
|
|
|
|
return FileWrapper(regularFileWithContents: data)
|
|
|
|
|
}
|
|
|
|
|
}
|