✨ Make status indicator work
- Display current state (Connecting, Error, Connected) - Try to reconnect after disconnect
This commit is contained in:
@ -45,6 +45,7 @@ class GoMixHome extends StatefulWidget {
|
|||||||
|
|
||||||
class _GoMixHomeState extends State<GoMixHome> {
|
class _GoMixHomeState extends State<GoMixHome> {
|
||||||
int currentPageIndex = 0;
|
int currentPageIndex = 0;
|
||||||
|
bool wsConnected = false;
|
||||||
mixer.MixerState mixerState = mixer.MixerState(inputs: [], outputs: []);
|
mixer.MixerState mixerState = mixer.MixerState(inputs: [], outputs: []);
|
||||||
|
|
||||||
WebSocketChannel? ws;
|
WebSocketChannel? ws;
|
||||||
@ -57,6 +58,7 @@ class _GoMixHomeState extends State<GoMixHome> {
|
|||||||
void initWs() {
|
void initWs() {
|
||||||
setState(() {
|
setState(() {
|
||||||
connectionError.value = "";
|
connectionError.value = "";
|
||||||
|
wsConnected = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
var connections = jsonDecode(prefs.getString("connections") ?? "{}")
|
var connections = jsonDecode(prefs.getString("connections") ?? "{}")
|
||||||
@ -65,8 +67,10 @@ class _GoMixHomeState extends State<GoMixHome> {
|
|||||||
var wsUrl = (connections[currentConnection] ?? {"url": ""})["url"];
|
var wsUrl = (connections[currentConnection] ?? {"url": ""})["url"];
|
||||||
|
|
||||||
if (wsUrl == "") {
|
if (wsUrl == "") {
|
||||||
connectionError.value =
|
setState(() {
|
||||||
"No GoMix connection available! Please add a connection in the server selector.";
|
connectionError.value =
|
||||||
|
"No GoMix connection available! Please add a connection in the server selector.";
|
||||||
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -75,6 +79,8 @@ class _GoMixHomeState extends State<GoMixHome> {
|
|||||||
Uri.parse(wsUrl),
|
Uri.parse(wsUrl),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ws?.ready.whenComplete(() => wsConnected = true);
|
||||||
|
|
||||||
// Listen to incoming websocket messages
|
// Listen to incoming websocket messages
|
||||||
ws?.stream.listen((message) {
|
ws?.stream.listen((message) {
|
||||||
final data = jsonDecode(message) as Map<String, dynamic>;
|
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) {
|
}, onError: (err) {
|
||||||
connectionError.value = err.toString();
|
setState(() {
|
||||||
|
connectionError.value = err.toString();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Request the current port list
|
// Request the current port list
|
||||||
@ -161,12 +175,26 @@ class _GoMixHomeState extends State<GoMixHome> {
|
|||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('go-mix Audio Mixer'),
|
title: const Text('go-mix Audio Mixer'),
|
||||||
centerTitle: true,
|
centerTitle: true,
|
||||||
leading: IconButton(
|
leading: connectionError.value == ""
|
||||||
icon: const Icon(Icons.check_circle_outline),
|
? (wsConnected
|
||||||
color: theme.colorScheme.inversePrimary,
|
? IconButton(
|
||||||
tooltip: 'Connected',
|
icon: const Icon(Icons.check_circle_outline),
|
||||||
onPressed: () {},
|
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>[
|
actions: <Widget>[
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8),
|
||||||
|
Reference in New Issue
Block a user