I am trying to allow users to pick folders using the DocumentGroup context in SwiftUI, having defined a SourceFolderDocument class inheriting from ReferenceFileDocument.
However, when launching the app, only elements conforming to .sourceCode are pickable in the file picker. What would be the correct way to pick folders to open in SwiftUI?
Thank you in advance!
Here is the class in question:
final class SourceFolderDocument: ReferenceFileDocument{
static var readableContentTypes: [UTType] {[.folder, .sourceCode, .directory]}
init(configuration: ReadConfiguration) throws {
var documents : [SourceFile] = []
if(configuration.file.isDirectory){
guard let wrappers = configuration.file.fileWrappers
else{
throw CocoaError(.fileReadCorruptFile)
}
var documents : [SourceFile] = []
for document in wrappers {
if let fileContents = document.value.regularFileContents{
documents.append(SourceFile(name: document.key, contents: String(data: fileContents, encoding: .utf8)!, fileUTType: .sourceCode))
}
}
}else{
guard let data = configuration.file.regularFileContents
else{
throw CocoaError(.fileReadCorruptFile)
}
documents.append(SourceFile(name: configuration.file.filename ?? "blank", contents: String(data: data, encoding: .utf8) ?? "", fileUTType: .sourceCode))
}
self.sourceFolder = SourceFolder(name: configuration.file.filename!, documents: documents)
}
func snapshot(contentType: UTType) throws -> SourceFolder {
sourceFolder
}
func fileWrapper(snapshot: SourceFolder, configuration: WriteConfiguration) throws -> FileWrapper {
var fileWrappers: [String : FileWrapper] = [:]
for document in snapshot.documents {
fileWrappers[document.name] = FileWrapper(regularFileWithContents: document.contents.data(using: .utf8)!)
}
let fileWrapper = FileWrapper(directoryWithFileWrappers: fileWrappers)
return fileWrapper
}
init(){
sourceFolder = .defaultFolder
}
typealias Snapshot = SourceFolder
@Published var sourceFolder : SourceFolder
}