2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-29 14:06:47 +00:00

Add server status icon

This commit is contained in:
Oliver Walters 2020-04-05 13:06:59 +10:00
parent d38ff5ad7f
commit 2cef9e8190

View File

@ -118,8 +118,30 @@ class _MyHomePageState extends State<MyHomePage> {
bool _serverConnection = false; bool _serverConnection = false;
FaIcon _serverIcon = new FaIcon(FontAwesomeIcons.spinner);
Color _serverStatusColor = Color.fromARGB(255, 50, 50, 250); 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 * Test the server connection
*/ */
@ -127,40 +149,32 @@ class _MyHomePageState extends State<MyHomePage> {
var prefs = await SharedPreferences.getInstance(); var prefs = await SharedPreferences.getInstance();
print("Checking server connection");
_serverAddress = prefs.getString("server"); _serverAddress = prefs.getString("server");
InvenTreeAPI().connect().then((bool result) { InvenTreeAPI().connect().then((bool result) {
print("Connection status: $result");
_serverConnection = result;
if (_serverConnection) { if (result) {
_serverStatus = "Connected to server: $_serverAddress"; onConnectSuccess("");
_serverMessage = "";
_serverStatusColor = Color.fromARGB(255, 50, 250, 50);
} else { } else {
_serverStatus = "Could not connect to server: $_serverAddress"; onConnectFailure("Could not connect to server");
_serverStatusColor = Color.fromARGB(255, 250, 50, 50);
} }
setState(() {});
}).catchError((e) { }).catchError((e) {
String fault = "Connection error";
_serverConnection = false; _serverConnection = false;
_serverStatusColor = Color.fromARGB(255, 250, 50, 50); _serverStatusColor = Color.fromARGB(255, 250, 50, 50);
_serverStatus = "Error connecting to $_serverAddress"; _serverStatus = "Error connecting to $_serverAddress";
if (e is TimeoutException) { if (e is TimeoutException) {
_serverMessage = "No response from server"; fault = "Timeout: No response from server";
} else { } else {
_serverMessage = e.toString(); fault = e.toString();
} }
print("Server error: $_serverMessage"); onConnectFailure(fault);
setState(() {});
}); });
} }
@ -280,23 +294,14 @@ class _MyHomePageState extends State<MyHomePage> {
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Expanded( Expanded(
child: Card( child: ListTile(
child: Column( title: Text("$_serverStatus",
mainAxisAlignment: MainAxisAlignment.center, style: TextStyle(color: _serverStatusColor),
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('$_serverStatus',
style: TextStyle(
color: _serverStatusColor,
),
),
Text('$_serverMessage',
style: TextStyle(
color: _serverStatusColor,
),
), ),
], subtitle: Text("$_serverMessage",
style: TextStyle(color: _serverStatusColor),
), ),
leading: _serverIcon,
), ),
), ),
], ],