From 37cdf23ae1631eb9514ac39c9aa22a4703089153 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 6 Apr 2020 22:33:38 +1000 Subject: [PATCH] Add a connection checker --- lib/api.dart | 35 +++++++++++++++++++++++++++++++++++ lib/main.dart | 9 +++++++++ lib/widget/drawer.dart | 7 +++++++ 3 files changed, 51 insertions(+) diff --git a/lib/api.dart b/lib/api.dart index 069a6fb2..885fd145 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -72,6 +72,41 @@ class InvenTreeAPI { // Authentication token (initially empty, must be requested) String _token = ""; + bool isConnected() { + return _token.isNotEmpty; + } + + /* + * Check server connection and display messages if not connected. + * Useful as a precursor check before performing operations. + */ + bool checkConnection(BuildContext context) { + + // Firstly, is the server connected? + if (!isConnected()) { + showDialog( + context: context, + child: new SimpleDialog( + title: new Text("Not Connected"), + children: [ + ListTile( + title: Text("Server not connected"), + ) + ] + ) + ); + + return false; + } + + // Is the server version too old? + // TODO + + // Finally + return true; + + } + // Server instance information String instance = ''; diff --git a/lib/main.dart b/lib/main.dart index 121fa93a..ac1cc7a2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -179,18 +179,27 @@ class _MyHomePageState extends State { } void _search() { + if (!InvenTreeAPI().checkConnection(context)) return; + // TODO } void _scan() { + if (!InvenTreeAPI().checkConnection(context)) return; + scanQrCode(context); } void _parts() { + if (!InvenTreeAPI().checkConnection(context)) return; + Navigator.push(context, MaterialPageRoute(builder: (context) => CategoryDisplayWidget(null))); } void _stock() { + + if (!InvenTreeAPI().checkConnection(context)) return; + Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); } diff --git a/lib/widget/drawer.dart b/lib/widget/drawer.dart index 03095ce1..ef031280 100644 --- a/lib/widget/drawer.dart +++ b/lib/widget/drawer.dart @@ -1,6 +1,9 @@ +import 'package:InvenTree/api.dart'; import 'package:InvenTree/barcode.dart'; import 'package:flutter/material.dart'; +import 'package:InvenTree/api.dart'; + import 'package:InvenTree/widget/category_display.dart'; import 'package:InvenTree/widget/location_display.dart'; @@ -33,6 +36,7 @@ class InvenTreeDrawer extends StatelessWidget { * Upon successful scan, data are passed off to be decoded. */ void _scan() async { + if (!InvenTreeAPI().checkConnection(context)) return; _closeDrawer(); scanQrCode(context); @@ -42,6 +46,7 @@ class InvenTreeDrawer extends StatelessWidget { * Display the top-level PartCategory list */ void _showParts() { + if (!InvenTreeAPI().checkConnection(context)) return; _closeDrawer(); Navigator.push(context, MaterialPageRoute(builder: (context) => CategoryDisplayWidget(null))); @@ -51,6 +56,7 @@ class InvenTreeDrawer extends StatelessWidget { * Display the top-level StockLocation list */ void _showStock() { + if (!InvenTreeAPI().checkConnection(context)) return; _closeDrawer(); Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); } @@ -72,6 +78,7 @@ class InvenTreeDrawer extends StatelessWidget { leading: new Image.asset( "assets/image/icon.png", fit: BoxFit.scaleDown, + width: 40, ), title: new Text("InvenTree"), onTap: _home,