diff --git a/lib/api.dart b/lib/api.dart index 0476f601..3f676d09 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -297,7 +297,6 @@ class InvenTreeAPI { var response = await request.send(); return response; - } // Perform a POST request diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 0252f43e..f3c05996 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -5,6 +5,9 @@ import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; import 'model.dart'; +import 'dart:async'; +import 'dart:io'; + import 'package:InvenTree/api.dart'; @@ -129,12 +132,12 @@ class InvenTreeStockItem extends InvenTreeModel { }); } - Future uploadTestResult(BuildContext context, String testName, bool result, {String value, String notes}) async { + Future uploadTestResult(BuildContext context, String testName, bool result, {String value, String notes, File attachment}) async { - Map data = { - "stock_item": pk, + Map data = { + "stock_item": pk.toString(), "test": testName, - "result": result, + "result": result.toString(), }; if (value != null && !value.isEmpty) { @@ -145,15 +148,23 @@ class InvenTreeStockItem extends InvenTreeModel { data["notes"] = notes; } - bool _result = false; + /* + * 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(context, data); - await InvenTreeStockItemTestResult().create(context, data).then((InvenTreeModel model) { + return (_result != null) && (_result is InvenTreeStockItemTestResult); + } else { + var url = InvenTreeStockItemTestResult().URL; + http.StreamedResponse _uploadResponse = await InvenTreeAPI().uploadFile(url, attachment, fields: data); - _result = model != null && model is InvenTreeStockItemTestResult; + // Check that the HTTP status code is HTTP_201_CREATED + return _uploadResponse.statusCode == 201; + } - }); - - return _result; + return false; } int get partId => jsondata['part'] ?? -1; diff --git a/lib/widget/stock_item_test_results.dart b/lib/widget/stock_item_test_results.dart index 3e761ac3..8c0ba25d 100644 --- a/lib/widget/stock_item_test_results.dart +++ b/lib/widget/stock_item_test_results.dart @@ -12,6 +12,7 @@ import 'package:flutter/material.dart'; import 'package:InvenTree/widget/refreshable_state.dart'; import 'package:flutter_speed_dial/flutter_speed_dial.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:image_picker/image_picker.dart'; class StockItemTestResultsWidget extends StatefulWidget { @@ -41,7 +42,7 @@ class _StockItemTestResultDisplayState extends RefreshableState _attachment = attachment, + ), StringField( allowEmpty: true, label: "Notes",