Refactor iOS version to use a bunch more shared code

This commit is contained in:
James Babcock committed 2026-09-22 21:26:51 -07:00
1 parent c3a0ba9f56
commit 4a194e37bb
40 files changed
+1874 -2605

No files matched your search

+8 -5
View File
@@ -62,11 +62,14 @@ that remains: the sherpa-onnx classes, which are the vendored Android JNI ABI.
PNG/GIF adapters retain only Android storage responsibilities; their codecs are
shared.
On `ios-port`, platform adapters route the existing TypeScript session and UI
logic through shared Kotlin for protocol builders, CRC/framing/reassembly,
event decoding, CFW compression/ACKs, changed-image regions, surface composition,
and LVGL glyphs. The original pure TypeScript implementations remain as Node
test references; NativeScript selects the `.ios.ts` native adapters.
On iOS (since 2026-09-22) `iosMain` also hosts the platform actuals for the shared
cores: `ble/IosBleCentral` (CoreBluetooth, implements `SessionLink` and `StockLink`
plus scanning and ANCS authorization), `ble/IosSessionHost`, `ble/IosGlassesSession`
(the facade `app/native/faceclaw-communicator.ios.ts` wraps), `ble/IosStockFlows`
(device-info probe, flash prompt, OTA flasher), `net/IosRemoteInput`,
`net/IosSseRequest`, `net/IosModelDownloader` and `IosFrameTimings`. Callbacks into
TypeScript are always dispatched to the main queue. The former pure-TypeScript
session/protocol duplicates were deleted; their coverage lives in `tests/kotlin`.
Texture rendering uses the same `GlyphAtlas`, `ImageAtlas`, `TextureCacheState`
and `TexturePlanner` on both platforms. iOS's `IosTextureAtlas` and
@@ -182,8 +182,14 @@ class IosBleCentral(private val platform: ProtocolPlatform = IosProtocolPlatform
central = CBCentralManager(delegate = delegate, queue = queue)
}
/**
* Registers the scan/state listener and creates the central if needed, so a listener that
* only waits for readiness (no scan) still receives the state report; an existing central's
* current state is re-reported to the new listener on the main queue.
*/
fun setScanListener(listener: IosBleScanListener?) {
scanListener = listener
if (listener != null && !closed) ensureCentral()
}
/** Starts (or keeps) scanning for all peripherals with duplicates; events reach the scan listener on main. */
@@ -15,6 +15,11 @@ interface IosAncsListener {
fun onRelayFrame(data: NSData)
}
/** Audio packets for the NativeScript side as NSData (a Kotlin ByteArray crosses per byte). Main queue. */
interface IosAudioPacketListener {
fun onAudioPacket(data: NSData, arm: String, arrivalMs: Long)
}
/**
* ObjC-facing facade over the shared [GlassesSessionCore] for the NativeScript app: the iOS
* counterpart of Android's FaceclawBleCommunicator with the same method names. GATT goes
@@ -119,6 +124,16 @@ class IosGlassesSession internal constructor(
fun stopG2AudioCapture() = core.stopG2AudioCapture()
/** As [startG2AudioCapture], with packets delivered as NSData on the main queue. */
fun startG2AudioCaptureNsData(listener: IosAudioPacketListener): Boolean =
core.startG2AudioCapture(NsDataAudioListener(listener))
/** The right arm's usable write payload (ATT MTU minus the 3-byte header), or 20 when unknown. */
fun rightWriteLimit(): Int {
val mtu = try { central?.negotiatedMtu(rightIdentifier) ?: 0 } catch (t: Throwable) { 0 }
return if (mtu > 3) mtu - 3 else 20
}
fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive()
fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled)
@@ -237,6 +252,14 @@ class IosGlassesSession internal constructor(
internal fun coreForTests(): GlassesSessionCore = core
private class NsDataAudioListener(private val delegate: IosAudioPacketListener) : FaceclawAudioPacketListener {
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
val packet = (data ?: ByteArray(0)).data()
val label = arm ?: ""
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(packet, label, arrivalMs) }
}
}
private class MainQueueAudioListener(private val delegate: FaceclawAudioPacketListener) : FaceclawAudioPacketListener {
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(data, arm, arrivalMs) }