Make status indicator work

- Display current state (Connecting, Error, Connected)
- Try to reconnect after disconnect
This commit is contained in:
2024-02-29 20:21:25 +01:00
parent 8e82ad36cb
commit f91cdc3827

View File

@ -45,6 +45,7 @@ class GoMixHome extends StatefulWidget {
class _GoMixHomeState extends State<GoMixHome> {
int currentPageIndex = 0;
bool wsConnected = false;
mixer.MixerState mixerState = mixer.MixerState(inputs: [], outputs: []);
WebSocketChannel? ws;
@ -57,6 +58,7 @@ class _GoMixHomeState extends State<GoMixHome> {
void initWs() {
setState(() {
connectionError.value = "";
wsConnected = false;
});
var connections = jsonDecode(prefs.getString("connections") ?? "{}")
@ -65,8 +67,10 @@ class _GoMixHomeState extends State<GoMixHome> {
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<GoMixHome> {
Uri.parse(wsUrl),
);
ws?.ready.whenComplete(() => wsConnected = true);
// Listen to incoming websocket messages
ws?.stream.listen((message) {
final data = jsonDecode(message) as Map<String, dynamic>;
@ -104,8 +110,16 @@ class _GoMixHomeState extends State<GoMixHome> {
}
});
}
}, 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<GoMixHome> {
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: <Widget>[
Padding(
padding: const EdgeInsets.all(8),