From f027dff2af18cb0dcd1c895b3adc4b00f34ba3f8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 16 Aug 2021 20:27:44 +1000 Subject: [PATCH] Refactor upload test result --- lib/inventree/stock.dart | 47 ++++-------- lib/widget/fields.dart | 95 ++++-------------------- lib/widget/stock_item_test_results.dart | 96 ++++--------------------- 3 files changed, 39 insertions(+), 199 deletions(-) diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index c83cba08..9acc7059 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -17,6 +17,20 @@ class InvenTreeStockItemTestResult extends InvenTreeModel { @override String get URL => "stock/test/"; + @override + Map formFields() { + return { + "stock_item": { + "hidden": true + }, + "test": {}, + "result": {}, + "value": {}, + "notes": {}, + "attachment": {}, + }; + } + String get key => jsondata['key'] ?? ''; String get testName => jsondata['test'] ?? ''; @@ -190,39 +204,6 @@ class InvenTreeStockItem extends InvenTreeModel { }); } - Future uploadTestResult(BuildContext context, String testName, bool result, {String? value, String? notes, File? attachment}) async { - - Map data = { - "stock_item": pk.toString(), - "test": testName, - "result": result.toString(), - }; - - if (value != null && value.isNotEmpty) { - data["value"] = value; - } - - if (notes != null && notes.isNotEmpty) { - data["notes"] = notes; - } - - /* - * Upload is performed in different ways, depending if an attachment is provided. - * TODO: Is there a nice way to refactor this one? - */ - if (attachment == null) { - var _result = await InvenTreeStockItemTestResult().create(data); - - return (_result != null) && (_result is InvenTreeStockItemTestResult); - } else { - var url = InvenTreeStockItemTestResult().URL; - http.StreamedResponse _uploadResponse = await InvenTreeAPI().uploadFile(url, attachment, fields: data); - - // Check that the HTTP status code is HTTP_201_CREATED - return _uploadResponse.statusCode == 201; - } - } - String get uid => jsondata['uid'] ?? ''; int get status => jsondata['status'] ?? -1; diff --git a/lib/widget/fields.dart b/lib/widget/fields.dart index 42ca8e90..f8016075 100644 --- a/lib/widget/fields.dart +++ b/lib/widget/fields.dart @@ -56,7 +56,7 @@ class FilePickerDialog { } // Present a dialog to pick a file, either from local file system or from camera - static Future pickFile({bool allowImages = true, bool allowFiles = true, Function(File)? onPicked}) async { + static Future pickFile({String message = "", bool allowImages = true, bool allowFiles = true, Function(File)? onPicked}) async { String title = ""; @@ -67,7 +67,17 @@ class FilePickerDialog { } // Construct actions - List actions = []; + List actions = [ + + ]; + + if (message.isNotEmpty) { + actions.add( + ListTile( + title: Text(message) + ) + ); + } actions.add( SimpleDialogOption( @@ -132,87 +142,6 @@ class FilePickerDialog { } -/* - * Form field for selecting an image file, - * either from the gallery, or from the camera. - */ -class ImagePickerField extends FormField { - - static void _selectFromGallery(FormFieldState field) { - _getImageFromGallery(field); - } - - static void _selectFromCamera(FormFieldState field) { - _getImageFromCamera(field); - } - - static Future _getImageFromGallery(FormFieldState field) async { - - final picker = ImagePicker(); - - final pickedImage = await picker.getImage(source: ImageSource.gallery); - - if (pickedImage != null) - { - field.didChange(File(pickedImage.path)); - } - } - - static Future _getImageFromCamera(FormFieldState field) async { - - final picker = ImagePicker(); - - final pickedImage = await picker.getImage(source: ImageSource.camera); - - if (pickedImage != null) - { - field.didChange(File(pickedImage.path)); - } - - } - - ImagePickerField(BuildContext context, {String? label, Function(File?)? onSaved, bool required = false}) : - super( - onSaved: onSaved, - validator: (File? img) { - if (required && (img == null)) { - return L10().required; - } - - return null; - }, - builder: (FormFieldState state) { - - String _label = label ?? L10().attachImage; - - return InputDecorator( - decoration: InputDecoration( - errorText: state.errorText, - labelText: required ? _label + "*" : _label, - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - TextButton( - child: Text(L10().selectImage), - onPressed: () { - _selectFromGallery(state); - }, - ), - TextButton( - child: Text(L10().takePicture), - onPressed: () { - _selectFromCamera(state); - }, - ) - ], - ), - ); - } - ); -} - - class CheckBoxField extends FormField { CheckBoxField({ String? label, bool initial = false, Function(bool?)? onSaved, diff --git a/lib/widget/stock_item_test_results.dart b/lib/widget/stock_item_test_results.dart index bdefdc2b..bb189980 100644 --- a/lib/widget/stock_item_test_results.dart +++ b/lib/widget/stock_item_test_results.dart @@ -1,3 +1,4 @@ +import 'package:inventree/api_form.dart'; import 'package:inventree/app_colors.dart'; import 'package:inventree/inventree/part.dart'; import 'package:inventree/inventree/stock.dart'; @@ -46,73 +47,19 @@ class _StockItemTestResultDisplayState extends RefreshableState[ - StringField( - label: L10().testName, - initial: name, - isEnabled: nameIsEditable, - onSaved: (value) => _name = value ?? '', - ), - CheckBoxField( - label: L10().result, - helperText: L10().testPassedOrFailed, - initial: true, - onSaved: (value) => _result = value ?? false, - ), - StringField( - label: L10().value, - initial: value, - allowEmpty: true, - onSaved: (value) => _value = value ?? '', - validator: (String value) { - if (valueRequired && value.isEmpty) { - return L10().valueRequired; - } - return null; - }, - ), - ImagePickerField( - context, - label: L10().attachImage, - required: attachmentRequired, - onSaved: (attachment) => _attachment = attachment, - ), - StringField( - allowEmpty: true, - label: L10().notes, - onSaved: (value) => _notes = value ?? '', - ), - ] + onSuccess: (data) { + refresh(); + }, + fileField: "attachment", ); } @@ -274,29 +221,12 @@ class _StockItemTestResultDisplayState extends RefreshableState actionButtons() { - - var buttons = List(); - - buttons.add(SpeedDialChild( - child: Icon(FontAwesomeIcons.plusCircle), - label: L10().testResultAdd, - onTap: () { - addTestResult(); - }, - )); - - return buttons; - } - */ - @override Widget getFab(BuildContext context) { return FloatingActionButton( child: Icon(FontAwesomeIcons.plus), onPressed: () { - addTestResult(); + addTestResult(context); }, ); }