Fix profile save file not working

SwiftUI only supports one fileExporter modifier per view hierarchy.
When multiple fileExporter modifiers are attached, only the last one
takes effect. Unified ProfileExportDocument and ProfileJSONExportDocument
into a single ProfileAnyExportDocument to use one fileExporter for both
file types.
This commit is contained in:
世界
2026-01-13 17:05:12 +08:00
parent a393ff4ca8
commit 42d1107c53
3 changed files with 81 additions and 116 deletions
+34 -1
View File
@@ -115,7 +115,7 @@ public struct TypedProfile: Transferable, Codable {
}
public extension UTType {
static var profile: UTType { .init(exportedAs: AppConfiguration.profileUTType) }
static let profile = UTType(exportedAs: AppConfiguration.profileUTType)
}
#if !os(tvOS)
@@ -177,4 +177,37 @@ public extension UTType {
return FileWrapper(regularFileWithContents: data)
}
}
public struct ProfileAnyExportDocument: FileDocument {
public static var readableContentTypes: [UTType] { [.data, .json] }
public let data: Data
public let filename: String
public let contentType: UTType
public init(profile: ProfileExportDocument) {
data = profile.data
filename = profile.filename
contentType = .data
}
public init(json: ProfileJSONExportDocument) {
data = json.content.data(using: .utf8) ?? Data()
filename = json.filename
contentType = .json
}
public init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents else {
throw CocoaError(.fileReadCorruptFile)
}
self.data = data
filename = "profile"
contentType = .data
}
public func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
FileWrapper(regularFileWithContents: data)
}
}
#endif