Add save file option for profile sharing

Add "Save File" and "Save Content JSON" options to profile share menu,
allowing users to save profiles directly to a chosen location using
fileExporter instead of the system share sheet.
This commit is contained in:
世界
2026-01-01 20:25:28 +08:00
parent 55e01a89b5
commit 313cb5d213
11 changed files with 1674 additions and 26 deletions
@@ -109,3 +109,61 @@ public struct TypedProfile: Transferable, Codable {
public extension UTType {
static var profile: UTType { .init(exportedAs: "io.nekohasekai.sfavt.profile") }
}
// MARK: - FileDocument for Export
public struct ProfileExportDocument: FileDocument {
public static var readableContentTypes: [UTType] { [.profile] }
public let data: Data
public let filename: String
public init(content: LibboxProfileContent) throws {
guard let encoded = content.encode() else {
throw NSError(domain: "ProfileExportDocument", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to encode profile"])
}
data = encoded
filename = "\(content.name).bpf"
}
public init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents else {
throw CocoaError(.fileReadCorruptFile)
}
self.data = data
filename = "profile.bpf"
}
public func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
FileWrapper(regularFileWithContents: data)
}
}
public struct ProfileJSONExportDocument: FileDocument {
public static var readableContentTypes: [UTType] { [.json] }
public let content: String
public let filename: String
public init(jsonContent: String, name: String) {
content = jsonContent
filename = "\(name).json"
}
public init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let content = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
self.content = content
filename = "profile.json"
}
public func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
guard let data = content.data(using: .utf8) else {
throw CocoaError(.fileWriteInapplicableStringEncoding)
}
return FileWrapper(regularFileWithContents: data)
}
}