mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 13:36:50 +00:00
Refactor upload test result
This commit is contained in:
parent
8bca501fc4
commit
f027dff2af
@ -17,6 +17,20 @@ class InvenTreeStockItemTestResult extends InvenTreeModel {
|
||||
@override
|
||||
String get URL => "stock/test/";
|
||||
|
||||
@override
|
||||
Map<String, dynamic> 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<bool> uploadTestResult(BuildContext context, String testName, bool result, {String? value, String? notes, File? attachment}) async {
|
||||
|
||||
Map<String, String> 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;
|
||||
|
@ -56,7 +56,7 @@ class FilePickerDialog {
|
||||
}
|
||||
|
||||
// Present a dialog to pick a file, either from local file system or from camera
|
||||
static Future<void> pickFile({bool allowImages = true, bool allowFiles = true, Function(File)? onPicked}) async {
|
||||
static Future<void> pickFile({String message = "", bool allowImages = true, bool allowFiles = true, Function(File)? onPicked}) async {
|
||||
|
||||
String title = "";
|
||||
|
||||
@ -67,7 +67,17 @@ class FilePickerDialog {
|
||||
}
|
||||
|
||||
// Construct actions
|
||||
List<Widget> actions = [];
|
||||
List<Widget> 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<File> {
|
||||
|
||||
static void _selectFromGallery(FormFieldState<File> field) {
|
||||
_getImageFromGallery(field);
|
||||
}
|
||||
|
||||
static void _selectFromCamera(FormFieldState<File> field) {
|
||||
_getImageFromCamera(field);
|
||||
}
|
||||
|
||||
static Future<void> _getImageFromGallery(FormFieldState<File> field) async {
|
||||
|
||||
final picker = ImagePicker();
|
||||
|
||||
final pickedImage = await picker.getImage(source: ImageSource.gallery);
|
||||
|
||||
if (pickedImage != null)
|
||||
{
|
||||
field.didChange(File(pickedImage.path));
|
||||
}
|
||||
}
|
||||
|
||||
static Future<void> _getImageFromCamera(FormFieldState<File> 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<File> state) {
|
||||
|
||||
String _label = label ?? L10().attachImage;
|
||||
|
||||
return InputDecorator(
|
||||
decoration: InputDecoration(
|
||||
errorText: state.errorText,
|
||||
labelText: required ? _label + "*" : _label,
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
TextButton(
|
||||
child: Text(L10().selectImage),
|
||||
onPressed: () {
|
||||
_selectFromGallery(state);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: Text(L10().takePicture),
|
||||
onPressed: () {
|
||||
_selectFromCamera(state);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
class CheckBoxField extends FormField<bool> {
|
||||
CheckBoxField({
|
||||
String? label, bool initial = false, Function(bool?)? onSaved,
|
||||
|
@ -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<StockItemTestRes
|
||||
|
||||
_StockItemTestResultDisplayState(this.item);
|
||||
|
||||
void uploadTestResult(String name, bool result, String value, String notes, File? attachment) async {
|
||||
void addTestResult(BuildContext context, {String name = '', bool nameIsEditable = true, bool result = false, String value = '', bool valueRequired = false, bool attachmentRequired = false}) async {
|
||||
|
||||
final success = await item.uploadTestResult(
|
||||
context, name, result,
|
||||
value: value,
|
||||
notes: notes,
|
||||
attachment: attachment
|
||||
);
|
||||
|
||||
showSnackIcon(
|
||||
success ? L10().testResultUploadPass : L10().testResultUploadFail,
|
||||
success: success
|
||||
);
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
void addTestResult({String name = '', bool nameIsEditable = true, bool result = false, String value = '', bool valueRequired = false, bool attachmentRequired = false}) async {
|
||||
|
||||
String _name = "";
|
||||
bool _result = false;
|
||||
String _value = "";
|
||||
String _notes = "";
|
||||
File? _attachment;
|
||||
|
||||
showFormDialog(L10().testResultAdd,
|
||||
key: _addResultKey,
|
||||
callback: () {
|
||||
uploadTestResult(_name, _result, _value, _notes, _attachment);
|
||||
InvenTreeStockItemTestResult().createForm(
|
||||
context,
|
||||
L10().testResultAdd,
|
||||
data: {
|
||||
"stock_item": "${item.pk}",
|
||||
"test": "${name}",
|
||||
},
|
||||
fields: <Widget>[
|
||||
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<StockItemTestRes
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
List<SpeedDialChild> actionButtons() {
|
||||
|
||||
var buttons = List<SpeedDialChild>();
|
||||
|
||||
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);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user