Files
sizzletracker-android/ios/app/Host/Screens/Setup/SetupModel.swift
T
ReactorcoremeltdownandClaude Opus 5.5 64e21e0bcc iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder
- CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports;
  platform-free parser/module/clock in core (midi_test)
- Hardware keyboard and gamepads via GameController; rebindable keyboard,
  gamepad and MIDI CC actions; per-parameter MIDI learn
- Colour themes (.szt), project bundles (.szgz), backup & restore;
  ZIP in core with zlib (zip_test against jar and unzip)
- Persistent app settings; audio pauses in the background unless sound module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:26:01 +01:00

125 lines
4.6 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
/// What a picked file is for.
enum Format {
case sng, project, theme, backup
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 {
if format == .project {
// A bundle (.szgz, a ZIP) or a plain project (.szg).
let data = try SetupFiles.readData(url, limit: 64 << 20)
if SizzleSession.isZip(data) { try session.importProjectBundleData(data) }
else if let text = String(data: data, encoding: .utf8) { try session.importProjectText(text) }
else { throw SetupFileError.notText }
refresh()
return
}
let text = try SetupFiles.readText(url)
switch format {
case .sng: try session.importSngText(text)
default: try session.importProjectText(text)
}
refresh()
}
/// The open song as a bundle, "<song>.szgz" in a temporary folder.
func exportBundle() throws -> URL {
guard let data = session.projectBundleData() else { throw SetupFileError.notText }
return try SetupFiles.export(data, name: SetupFiles.fileStem(songName), ext: "szgz")
}
/// Everything as "sizzle-backup-yyyyMMdd-HHmm.zip" (Android names it the same way).
func backupFile() throws -> URL {
guard let data = session.backupData(withSettings: AppSettings.shared.exportText()) else { throw SetupFileError.notText }
let f = DateFormatter()
f.locale = Locale(identifier: "en_US_POSIX")
f.dateFormat = "yyyyMMdd-HHmm"
return try SetupFiles.export(data, name: "sizzle-backup-" + f.string(from: Date()), ext: "zip")
}
/// Restore a backup, then apply its settings, bindings and theme.
func restoreBackup(_ url: URL) throws {
let data = try SetupFiles.readData(url, limit: 1 << 30)
let settings = try session.restoreBackupData(data)
if !settings.isEmpty { AppSettings.shared.importText(settings) }
InputBindings.shared.reload()
ThemeLibrary.shared.refresh()
ThemeLibrary.shared.select(AppSettings.shared.paletteName)
session.recordTailBars = AppSettings.shared.recordTailBars
refresh()
}
}