From 8cdf33e27c633ac30e8851c4617f6063c141660d Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sat, 07 Mar 2026 17:53:05 +0100
Subject: [PATCH] fix: audio routing, WebSocket reconnection, inverted chat list

---
 services/audio.ts |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/services/audio.ts b/services/audio.ts
index 188164e..fc7099c 100644
--- a/services/audio.ts
+++ b/services/audio.ts
@@ -11,6 +11,22 @@
   durationMs: number;
 }
 
+// --- Audio mode (set once at import time) ---
+// Setting this on every playback causes iOS to renegotiate the audio route,
+// which disconnects Bluetooth/CarPlay. Set it once and leave it alone.
+let _audioModeReady = false;
+async function ensureAudioMode(): Promise<void> {
+  if (_audioModeReady) return;
+  _audioModeReady = true;
+  try {
+    await setAudioModeAsync({ playsInSilentMode: true });
+  } catch {
+    _audioModeReady = false; // retry next time
+  }
+}
+// Eagerly initialize on module load
+ensureAudioMode();
+
 // --- Autoplay suppression ---
 // Don't autoplay voice messages when the app is in the background
 // or when the user is on a phone call (detected via audio interruption).
@@ -153,7 +169,7 @@
     let player: ReturnType<typeof createAudioPlayer> | null = null;
 
     try {
-      await setAudioModeAsync({ playsInSilentMode: true });
+      await ensureAudioMode();
 
       player = createAudioPlayer(uri);
       currentPlayer = player;

--
Gitblit v1.3.1