diff --git a/lib/main.dart b/lib/main.dart index 148056d0..f738531c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -118,7 +118,29 @@ class _MyHomePageState extends State { bool _serverConnection = false; + FaIcon _serverIcon = new FaIcon(FontAwesomeIcons.spinner); + Color _serverStatusColor = Color.fromARGB(255, 50, 50, 250); + + void onConnectSuccess(String msg) { + _serverConnection = true; + _serverMessage = msg; + _serverStatus = "Connected to $_serverAddress"; + _serverStatusColor = Color.fromARGB(255, 50, 250, 50); + _serverIcon = new FaIcon(FontAwesomeIcons.checkCircle, color: _serverStatusColor); + + setState(() {}); + } + + void onConnectFailure(String msg) { + _serverConnection = false; + _serverMessage = msg; + _serverStatus = "Could not connect to $_serverAddress"; + _serverStatusColor = Color.fromARGB(255, 250, 50, 50); + _serverIcon = new FaIcon(FontAwesomeIcons.timesCircle, color: _serverStatusColor); + + setState(() {}); + } /* * Test the server connection @@ -127,40 +149,32 @@ class _MyHomePageState extends State { var prefs = await SharedPreferences.getInstance(); - print("Checking server connection"); - _serverAddress = prefs.getString("server"); InvenTreeAPI().connect().then((bool result) { - print("Connection status: $result"); - _serverConnection = result; - if (_serverConnection) { - _serverStatus = "Connected to server: $_serverAddress"; - _serverMessage = ""; - _serverStatusColor = Color.fromARGB(255, 50, 250, 50); + if (result) { + onConnectSuccess(""); } else { - _serverStatus = "Could not connect to server: $_serverAddress"; - _serverStatusColor = Color.fromARGB(255, 250, 50, 50); + onConnectFailure("Could not connect to server"); } - setState(() {}); - }).catchError((e) { + + String fault = "Connection error"; + _serverConnection = false; _serverStatusColor = Color.fromARGB(255, 250, 50, 50); _serverStatus = "Error connecting to $_serverAddress"; if (e is TimeoutException) { - _serverMessage = "No response from server"; + fault = "Timeout: No response from server"; } else { - _serverMessage = e.toString(); + fault = e.toString(); } - print("Server error: $_serverMessage"); - - setState(() {}); + onConnectFailure(fault); }); } @@ -280,23 +294,14 @@ class _MyHomePageState extends State { crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( - child: Card( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - Text('$_serverStatus', - style: TextStyle( - color: _serverStatusColor, - ), - ), - Text('$_serverMessage', - style: TextStyle( - color: _serverStatusColor, - ), + child: ListTile( + title: Text("$_serverStatus", + style: TextStyle(color: _serverStatusColor), ), - ], + subtitle: Text("$_serverMessage", + style: TextStyle(color: _serverStatusColor), ), + leading: _serverIcon, ), ), ],