From f5b8311428bb6b2d746a55a3e6638ac3caac60f4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 9 Apr 2020 23:41:16 +1000 Subject: [PATCH] Action to count stock items now works --- lib/inventree/model.dart | 8 ++- lib/widget/stock_detail.dart | 112 ++++++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 24 deletions(-) diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index a0e2c639..c47f19b7 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -83,20 +83,22 @@ class InvenTreeModel { /* * Reload this object, by requesting data from the server */ - void reload() async { + Future reload() async { print("Reloading data from $url"); - var response = await api.get(url); + var response = await api.get(url, params: defaultGetFilters()); if (response.statusCode != 200) { print("Error retrieving data"); - return; + return false; } final Map data = json.decode(response.body); jsondata = data; + + return true; } // Return the detail view for the associated pk diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index cc5f782a..7e88d375 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -28,6 +28,9 @@ class StockDetailWidget extends StatefulWidget { class _StockItemDisplayState extends State { final _addStockKey = GlobalKey(); + final _takeStockKey = GlobalKey(); + final _countStockKey = GlobalKey(); + final _moveStockKey = GlobalKey(); _StockItemDisplayState(this.item) { // TODO @@ -39,7 +42,22 @@ class _StockItemDisplayState extends State { // TODO - Form for editing stock item } - void _addStock() { + void _addStock(double quantity) async { + + Navigator.of(context).pop(); + + // Await response to prevent the button from being pressed multiple times + var response = await item.addStock(quantity); + + // TODO - Handle error cases + + await item.reload(); + + setState(() {}); + + } + + void _addStockDialog() async { showDialog(context: context, builder: (BuildContext context) { return AlertDialog( @@ -55,31 +73,26 @@ class _StockItemDisplayState extends State { content: Form( key: _addStockKey, child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ + Text("Current Quantity: ${item.quantity}"), TextFormField( - decoration: InputDecoration(labelText: "Stock Quantity"), + decoration: InputDecoration( + labelText: "Add stock", + ), keyboardType: TextInputType.numberWithOptions(signed:false, decimal:true), validator: (value) { - if (value.isEmpty) { - return "Value cannot be empty"; - } + if (value.isEmpty) return "Value cannot be empty"; double quantity = double.tryParse(value); + if (quantity == null) return "Value cannot be converted to a number"; + if (quantity <= 0) return "Value must be positive"; - if (quantity == null) { - return "Value cannot be converted to a number"; - } + _addStock(quantity); - if (quantity <= 0) { - return "Value must be positive"; - } - - print("Adding stock!"); - - item.addStock(quantity).then((var response) { - print("added stock"); - }); + return null; }, ), ], @@ -95,8 +108,65 @@ class _StockItemDisplayState extends State { // TODO - Form for removing stock } - void _countStock() { - // TODO - Form for counting stock + void _countStock(double quantity) async { + + Navigator.of(context).pop(); + + var response = await item.countStock(quantity); + + // TODO - Handle error cases + + await item.reload(); + + setState(() {}); + + } + + void _countStockDialog() async { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text("Count Stock"), + actions: [ + FlatButton( + child: Text("Count"), + onPressed: () { + _countStockKey.currentState.validate(); + }, + ) + ], + content: Form( + key: _countStockKey, + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + TextFormField( + decoration: InputDecoration( + labelText: "Count stock", + hintText: "${item.quantity}", + ), + keyboardType: TextInputType.numberWithOptions(signed: false, decimal: true), + validator: (value) { + if (value.isEmpty) return "Value cannot be empty"; + + double quantity = double.tryParse(value); + if (quantity == null) return "Value cannot be converted to a number"; + if (quantity < 0) return "Value cannot be negative"; + + _countStock(quantity); + + return null; + }, + ) + ], + ) + ) + ); + } + ); } void _transferStock() { @@ -249,7 +319,7 @@ class _StockItemDisplayState extends State { buttons.add(SpeedDialChild( child: Icon(FontAwesomeIcons.plusCircle), label: "Add Stock", - onTap: _addStock, + onTap: _addStockDialog, ) ); @@ -263,7 +333,7 @@ class _StockItemDisplayState extends State { buttons.add(SpeedDialChild( child: Icon(FontAwesomeIcons.checkCircle), label: "Count Stock", - onTap: _countStock, + onTap: _countStockDialog, )); }