Ported from the Android tabs by parallel agents (each screen with its own SizzleSession category); slot editors use the card editor with a preset library; songs load/save in the Android project format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
2.3 KiB
Swift
50 lines
2.3 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import Foundation
|
|
|
|
/// Export and import files for the Setup tab (Foundation only).
|
|
enum SetupFiles {
|
|
/// Writes [text] to a fresh temporary folder as "<song name>.<ext>" (the name
|
|
/// the share sheet and Files show), clearing earlier exports first.
|
|
static func export(_ text: String, songName: String, ext: String) throws -> URL {
|
|
let root = FileManager.default.temporaryDirectory.appendingPathComponent("Exports", isDirectory: true)
|
|
try? FileManager.default.removeItem(at: root)
|
|
let dir = root.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
let url = dir.appendingPathComponent(fileStem(songName)).appendingPathExtension(ext)
|
|
try text.write(to: url, atomically: true, encoding: .utf8)
|
|
return url
|
|
}
|
|
|
|
/// The song name as a file name: path separators replaced, blank → "untitled".
|
|
static func fileStem(_ name: String) -> String {
|
|
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let safe = trimmed.components(separatedBy: CharacterSet(charactersIn: "/\\:")).joined(separator: "_")
|
|
return safe.isEmpty || safe.hasPrefix(".") ? "untitled" + safe : safe
|
|
}
|
|
|
|
/// Reads a picked file as text (security-scoped, as .fileImporter hands it over).
|
|
static func readText(_ url: URL) throws -> String {
|
|
let scoped = url.startAccessingSecurityScopedResource()
|
|
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
|
let data = try Data(contentsOf: url)
|
|
// Song files are small text; refuse anything huge before decoding.
|
|
guard data.count <= 32 << 20 else { throw SetupFileError.tooLarge }
|
|
guard let text = String(data: data, encoding: .utf8) ?? String(data: data, encoding: .isoLatin1) else {
|
|
throw SetupFileError.notText
|
|
}
|
|
return text
|
|
}
|
|
}
|
|
|
|
enum SetupFileError: LocalizedError {
|
|
case tooLarge, notText
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .tooLarge: return "The file is too large to be a Sizzle song."
|
|
case .notText: return "Couldn't read the selected file."
|
|
}
|
|
}
|
|
}
|