Files
app/ios/DModem/Sources/AppModel.swift
T
Claude 133d5acb61 iOS app: SwiftUI on DModemKit and the shared terminal
ios/DModem (XcodeGen's project.yml, iOS 16): Home (the modem's state, the
rates and what the CONNECT was, the call's time, start, hang up, stop, an
AT command line and its last words), the terminal (the shared Kotlin
emulator through TerminalBridge, drawn run by run in a Canvas; CP437 and
the VGA colours; the keyboard and a hardware one's arrows and Esc; Esc,
a sticky Ctrl, Tab and the arrows on a bar; pinch for the font), the log
(shared as text) and the settings (the SIP account or user:password@host,
incoming calls, T.38, the speaker, the AT commands at start, the log
levels). The modem's speaker on AVAudioEngine; the audio session (its
sound, or quiet) keeps a call going in the background. The DNS servers
from the resolver's state (SystemDNS.c: iOS has no resolv.conf for the
engine).

shared: TerminalBridge, the terminal for Swift (octets as Latin-1 strings
both ways, rows as runs, keys, replies, the title, the look and the
character set), tested on the JVM and natively; the framework also as an
XCFramework (assembleDModemSharedXCFramework).

CI: the macOS job builds the shared XCFramework, runs DModemKit's tests
and builds the app for the simulator; the Linux job runs DModemKit's
tests on the host engine. Not built here: no Xcode on Linux (DModemKit,
TerminalBridge and SystemDNS.c are).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-18 02:15:16 +00:00

87 lines
3.0 KiB
Swift

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
import DModemKit
import Foundation
import SwiftUI
/// The app's state for its views: the modem session (DModemKit) brought to
/// the main thread, the settings kept, the speaker and the background
/// audio that keeps a call going when the app isn't in front.
@MainActor
final class AppModel: ObservableObject {
@Published private(set) var run = ModemSession.Run.stopped
@Published private(set) var status = ModemStatus()
@Published private(set) var log: [String] = []
@Published private(set) var console = ""
@Published private(set) var error: String?
/// when the call came online (for its time on Home)
@Published private(set) var onlineSince: Date?
@Published var settings: ModemSettings {
didSet { store.settings = settings }
}
let session = ModemSession()
let terminal = TerminalModel()
private let store = SettingsStore()
private let audio = ModemAudio()
private var pending = false
init() {
settings = store.settings
session.onChange = { [weak self] in
// (many changes at once, one update)
Task { @MainActor [weak self] in self?.refresh() }
}
session.onData = { [weak self] data in
Task { @MainActor [weak self] in self?.terminal.feed(data) }
}
session.onSpeaker = { [audio] level in audio.speaker(level) }
session.onSound = { [audio] pcm in audio.play(pcm) }
terminal.send = { [weak self] data in self?.session.send(data) }
}
var connectLabel: String { status.link == .online ? ConnectInfo.label(console) : "" }
func start() {
error = nil
audio.enabled = settings.speaker
let dataFile = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
.map { url -> String in
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
return url.appendingPathComponent("modem-data").path
} ?? ""
let s = settings
Task.detached { [session, audio] in
// (the background audio first: the call keeps the app running)
audio.begin()
if !session.start(s, dns: SystemDNS.servers(), dataFile: dataFile) { audio.end() }
}
}
func stop() {
Task.detached { [session, audio] in
session.stop()
audio.end()
}
}
func hangUp() {
Task.detached { [session] in session.hangUp() }
}
/// an AT command (Home's line)
func command(_ cmd: String) { session.command(cmd) }
private func refresh() {
let wasOnline = status.link == .online
run = session.run
status = session.status
log = session.log
console = session.console
error = session.error
if status.link == .online && !wasOnline { onlineSince = Date() }
if status.link != .online { onlineSince = nil }
if run == .stopped { audio.end() }
}
}