2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00

Refactor PartAttachmentWidget

- Now displays generic attachments
- Augment the InvenTreeAttachment class to "know" about its model reference
This commit is contained in:
Oliver Walters 2022-06-04 00:12:44 +10:00
parent c8fa6bd992
commit 10783cd1c4
4 changed files with 67 additions and 49 deletions

View File

@ -636,6 +636,9 @@ class InvenTreeAttachment extends InvenTreeModel {
InvenTreeAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json);
// Override this reference field for any subclasses
String get REFERENCE_FIELD => "";
String get attachment => (jsondata["attachment"] ?? "") as String;
// Return the filename of the attachment
@ -684,19 +687,27 @@ class InvenTreeAttachment extends InvenTreeModel {
}
}
Future<bool> uploadAttachment(File attachment, {String comment = "", Map<String, String> fields = const {}}) async {
Future<bool> uploadAttachment(File attachment, int parentId, {String comment = "", Map<String, String> fields = const {}}) async {
// Ensure that the correct reference field is set
Map<String, String> data = Map<String, String>.from(fields);
data[REFERENCE_FIELD] = parentId.toString();
final APIResponse response = await InvenTreeAPI().uploadFile(
URL,
attachment,
method: "POST",
name: "attachment",
fields: fields
fields: data
);
return response.successful();
}
/*
* Download this attachment file
*/
Future<void> downloadAttachment() async {
await InvenTreeAPI().downloadFile(attachment);

View File

@ -422,6 +422,9 @@ class InvenTreePartAttachment extends InvenTreeAttachment {
InvenTreePartAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json);
@override
String get REFERENCE_FIELD => "part";
@override
String get URL => "part/attachment/";

View File

@ -1,33 +1,42 @@
/*
* A generic widget for displaying a list of attachments.
*
* To allow use with different "types" of attachments,
* we pass a subclassed instance of the InvenTreeAttachment model.
*/
import "dart:io";
import "package:flutter/material.dart";
import "package:font_awesome_flutter/font_awesome_flutter.dart";
import "package:inventree/inventree/part.dart";
import "package:inventree/inventree/model.dart";
import "package:inventree/widget/fields.dart";
import "package:inventree/widget/refreshable_state.dart";
import "package:inventree/widget/snacks.dart";
import "package:inventree/api.dart";
import "package:inventree/widget/refreshable_state.dart";
import "package:inventree/l10.dart";
class PartAttachmentsWidget extends StatefulWidget {
class AttachmentWidget extends StatefulWidget {
const PartAttachmentsWidget(this.part, {Key? key}) : super(key: key);
const AttachmentWidget(this.attachment, this.referenceId, this.hasUploadPermission) : super();
final InvenTreePart part;
final InvenTreeAttachment attachment;
final int referenceId;
final bool hasUploadPermission;
@override
_PartAttachmentDisplayState createState() => _PartAttachmentDisplayState(part);
_AttachmentWidgetState createState() => _AttachmentWidgetState(attachment, referenceId, hasUploadPermission);
}
class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget> {
class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
_PartAttachmentDisplayState(this.part);
_AttachmentWidgetState(this.attachment, this.referenceId, this.hasUploadPermission);
final InvenTreePart part;
final InvenTreeAttachment attachment;
final int referenceId;
final bool hasUploadPermission;
List<InvenTreePartAttachment> attachments = [];
List<InvenTreeAttachment> attachments = [];
@override
String getAppBarTitle(BuildContext context) => L10().attachments;
@ -37,20 +46,19 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
List<Widget> actions = [];
if (InvenTreeAPI().checkPermission("part", "change")) {
if (hasUploadPermission) {
// File upload
actions.add(
IconButton(
icon: FaIcon(FontAwesomeIcons.plusCircle),
onPressed: () async {
FilePickerDialog.pickFile(
onPicked: (File file) {
upload(file);
}
);
},
)
IconButton(
icon: FaIcon(FontAwesomeIcons.plusCircle),
onPressed: () async {
FilePickerDialog.pickFile(
onPicked: (File file) {
upload(file);
}
);
},
)
);
}
@ -58,12 +66,8 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
}
Future<void> upload(File file) async {
final bool result = await InvenTreePartAttachment().uploadAttachment(
file,
fields: {
"part": "${part.pk}"
}
);
final bool result = await attachment.uploadAttachment(file, referenceId);
if (result) {
showSnackIcon(L10().uploadSuccess, success: true);
@ -77,32 +81,31 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
@override
Future<void> request(BuildContext context) async {
await InvenTreePartAttachment().list(
await attachment.list(
filters: {
"part": "${part.pk}"
attachment.REFERENCE_FIELD: referenceId.toString()
}
).then((var results) {
attachments.clear();
for (var result in results) {
if (result is InvenTreePartAttachment) {
if (result is InvenTreeAttachment) {
attachments.add(result);
}
}
});
}
@override
Widget getBody(BuildContext context) {
return Center(
child: ListView(
children: ListTile.divideTiles(
context: context,
tiles: attachmentTiles(context)
).toList(),
)
child: ListView(
children: ListTile.divideTiles(
context: context,
tiles: attachmentTiles(context)
).toList(),
)
);
}
@ -126,14 +129,12 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
tiles.add(ListTile(
title: Text(L10().attachmentNone),
subtitle: Text(
L10().attachmentNonePartDetail,
style: TextStyle(fontStyle: FontStyle.italic),
L10().attachmentNonePartDetail,
style: TextStyle(fontStyle: FontStyle.italic),
),
));
}
return tiles;
}
}
}

View File

@ -6,7 +6,7 @@ import "package:inventree/app_colors.dart";
import "package:inventree/inventree/stock.dart";
import "package:inventree/l10.dart";
import "package:inventree/helpers.dart";
import "package:inventree/widget/part_attachments_widget.dart";
import "package:inventree/widget/attachment_widget.dart";
import "package:inventree/widget/part_notes.dart";
import "package:inventree/widget/progress.dart";
import "package:inventree/inventree/part.dart";
@ -410,7 +410,10 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PartAttachmentsWidget(part)
builder: (context) => AttachmentWidget(
InvenTreePartAttachment(),
part.pk,
InvenTreeAPI().checkPermission("part", "change"))
)
);
},