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>
88 lines
2.8 KiB
Swift
88 lines
2.8 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import Combine
|
|
import Foundation
|
|
|
|
/// The Setup tab's state: the saved songs (Documents/songs) and the open song's
|
|
/// name, refreshed when the song changes or a file is saved or deleted, plus the
|
|
/// project actions (Android's AppViewModel songNames / saveSong / loadSong /
|
|
/// newProject / deleteSong / importSng / writeSngExport).
|
|
///
|
|
/// Kept apart from AppModel so the screen doesn't re-render (and re-list the
|
|
/// songs folder) at the transport's 30 Hz while playing.
|
|
@MainActor
|
|
final class SetupModel: ObservableObject {
|
|
let session = SizzleSession.shared
|
|
@Published private(set) var songs: [String] = []
|
|
@Published private(set) var songName = ""
|
|
private var observer: NSObjectProtocol?
|
|
|
|
init() {
|
|
refresh()
|
|
observer = NotificationCenter.default.addObserver(forName: .SizzleSessionDidChange, object: nil, queue: .main) { [weak self] _ in
|
|
Task { @MainActor in self?.refresh() }
|
|
}
|
|
}
|
|
|
|
deinit { if let o = observer { NotificationCenter.default.removeObserver(o) } }
|
|
|
|
func refresh() {
|
|
let list = session.songNames, name = session.songName
|
|
if list != songs { songs = list }
|
|
if name != songName { songName = name }
|
|
}
|
|
|
|
/// The save prompt's default: the open song's name, else "untitled".
|
|
var defaultSaveName: String {
|
|
let n = songName.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
return n.isEmpty ? "untitled" : n
|
|
}
|
|
|
|
func save(_ name: String) throws {
|
|
try session.saveSongNamed(name.trimmingCharacters(in: .whitespacesAndNewlines))
|
|
refresh()
|
|
}
|
|
|
|
func load(_ name: String) throws {
|
|
try session.loadSongNamed(name)
|
|
refresh()
|
|
}
|
|
|
|
func delete(_ name: String) throws {
|
|
try session.deleteSongNamed(name)
|
|
refresh()
|
|
}
|
|
|
|
func newSong() {
|
|
session.newSong()
|
|
refresh()
|
|
}
|
|
|
|
func panic() { session.panic() }
|
|
|
|
// MARK: Export / import
|
|
|
|
enum Format {
|
|
case sng, project
|
|
var ext: String { self == .sng ? "sng" : "szg" }
|
|
}
|
|
|
|
/// The open song written to a temporary "<song>.sng" / "<song>.szg" for sharing.
|
|
func exportFile(_ format: Format) throws -> URL {
|
|
let text = format == .sng ? session.sngText() : session.projectText()
|
|
return try SetupFiles.export(text, songName: songName, ext: format.ext)
|
|
}
|
|
|
|
/// Imports a picked file: a .sng into the open song (notes, blocks,
|
|
/// arrangement), or a project replacing it.
|
|
func importFile(_ url: URL, as format: Format) throws {
|
|
let text = try SetupFiles.readText(url)
|
|
switch format {
|
|
case .sng: try session.importSngText(text)
|
|
case .project: try session.importProjectText(text)
|
|
}
|
|
refresh()
|
|
}
|
|
}
|