// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// Android's MidiLearnPopup: every parameter of the slot with its MIDI CC. LEARN, /// then move a knob or fader on a MIDI controller; ✕ removes the binding. The /// bindings are saved in the song (`midicc@` slot values), as on Android. /// EXPORT shares them as a .szm text file; IMPORT takes one for this device type. struct MidiLearnSheet: View { let slot: Int let type: String @ObservedObject private var input = InputController.shared @ObservedObject private var watch = MixSongWatch.shared @Environment(\.dismiss) private var dismiss @State private var importing = false @State private var importError: String? var body: some View { let session = SizzleSession.shared VStack(alignment: .leading, spacing: 8) { HStack { Text("MIDI LEARN — \(session.name(ofSlot: slot))").font(Theme.mono(14)).foregroundColor(Theme.accent) Spacer() Text("EXPORT").font(Theme.mono(13)).foregroundColor(Theme.accent) .contentShape(Rectangle()).onTapGesture(perform: export) .padding(.trailing, 14) Text("IMPORT").font(Theme.mono(13)).foregroundColor(Theme.accent) .contentShape(Rectangle()).onTapGesture { importing = true } .padding(.trailing, 14) Text("DONE").font(Theme.mono(13)).foregroundColor(Theme.dim) .contentShape(Rectangle()).onTapGesture { dismiss() } } Text("Tap LEARN, then move a knob/fader on your MIDI controller.").font(Theme.mono(10)).foregroundColor(Theme.dim) SnapScroll { VStack(spacing: 6) { ForEach(SizzleSession.paramSpecs(forType: type), id: \.key) { spec in let arming = input.paramLearnSlot == slot && input.paramLearnKey == spec.key let cc = input.paramCc(slot: slot, key: spec.key) HStack(spacing: 8) { Text(spec.label).font(Theme.mono(12)).foregroundColor(Theme.text) .frame(maxWidth: .infinity, alignment: .leading) Text(arming ? "waiting…" : cc.map { "CC \($0)" } ?? "—").font(Theme.mono(11)) .foregroundColor(arming ? Theme.accent : Theme.dim).frame(width: 72, alignment: .leading) MixKit.Button(label: arming ? "ARMING" : "LEARN", active: arming, width: 78) { if arming { input.cancelParamLearn() } else { input.beginParamLearn(slot: slot, key: spec.key) } } MixKit.Button(label: "✕") { input.clearParamCc(slot: slot, key: spec.key) } } } } } } .padding(16) .background(Theme.surface.ignoresSafeArea()) .preferredColorScheme(.dark) .onDisappear { input.cancelParamLearn() } // The completion needs nothing but the URL (iOS 15 resets the binding first). .fileImporter(isPresented: $importing, allowedContentTypes: [.item]) { result in guard case .success(let url) = result else { return } do { let data = try SetupFiles.readData(url, limit: 1 << 20) guard let text = String(data: data, encoding: .utf8) else { throw SetupFileError.notText } try SizzleSession.shared.importMidiMapText(text, intoSlot: slot) } catch { importError = error.localizedDescription } } .alert("Import failed", isPresented: Binding(get: { importError != nil }, set: { if !$0 { importError = nil } }), actions: { Button("OK") { importError = nil } }, message: { Text(importError ?? "") }) } /// The mapping as " MIDI.szm", through the share sheet. private func export() { let session = SizzleSession.shared guard let text = session.midiMapText(ofSlot: slot) else { return } let name = session.name(ofSlot: slot).trimmingCharacters(in: .whitespaces) let base = SetupFiles.fileStem(name.isEmpty ? type : name) + " MIDI" do { let url = try SetupFiles.export(Data(text.utf8), name: base, ext: "szm") ShareSheet.present(url) { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) } } catch { importError = error.localizedDescription } } }