diff --git a/lib/main.dart b/lib/main.dart index 76c5301..fb6d154 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -45,6 +45,7 @@ class GoMixHome extends StatefulWidget { class _GoMixHomeState extends State { int currentPageIndex = 0; + bool wsConnected = false; mixer.MixerState mixerState = mixer.MixerState(inputs: [], outputs: []); WebSocketChannel? ws; @@ -57,6 +58,7 @@ class _GoMixHomeState extends State { void initWs() { setState(() { connectionError.value = ""; + wsConnected = false; }); var connections = jsonDecode(prefs.getString("connections") ?? "{}") @@ -65,8 +67,10 @@ class _GoMixHomeState extends State { var wsUrl = (connections[currentConnection] ?? {"url": ""})["url"]; if (wsUrl == "") { - connectionError.value = - "No GoMix connection available! Please add a connection in the server selector."; + setState(() { + connectionError.value = + "No GoMix connection available! Please add a connection in the server selector."; + }); return; } @@ -75,6 +79,8 @@ class _GoMixHomeState extends State { Uri.parse(wsUrl), ); + ws?.ready.whenComplete(() => wsConnected = true); + // Listen to incoming websocket messages ws?.stream.listen((message) { final data = jsonDecode(message) as Map; @@ -104,8 +110,16 @@ class _GoMixHomeState extends State { } }); } + }, onDone: () { + wsConnected = false; + if (connectionError.value != "") return; + Future.delayed(const Duration(seconds: 2), () { + initWs(); + }); }, onError: (err) { - connectionError.value = err.toString(); + setState(() { + connectionError.value = err.toString(); + }); }); // Request the current port list @@ -161,12 +175,26 @@ class _GoMixHomeState extends State { appBar: AppBar( title: const Text('go-mix Audio Mixer'), centerTitle: true, - leading: IconButton( - icon: const Icon(Icons.check_circle_outline), - color: theme.colorScheme.inversePrimary, - tooltip: 'Connected', - onPressed: () {}, - ), + leading: connectionError.value == "" + ? (wsConnected + ? IconButton( + icon: const Icon(Icons.check_circle_outline), + color: Colors.green, + tooltip: 'Connected', + onPressed: () {}, + ) + : IconButton( + icon: const Icon(Icons.change_circle_outlined), + color: Colors.orange, + tooltip: 'Connecting', + onPressed: () {}, + )) + : IconButton( + icon: const Icon(Icons.remove_circle_outline_outlined), + color: Colors.red, + tooltip: 'Not connected', + onPressed: () {}, + ), actions: [ Padding( padding: const EdgeInsets.all(8),