Attachment can link exists things

 Optimize upload progress
This commit is contained in:
2024-08-02 15:49:32 +08:00
parent 98cc313a91
commit 11fb79623e
7 changed files with 111 additions and 24 deletions

View File

@ -110,6 +110,63 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
);
}
Future<void> _linkAttachments() async {
final controller = TextEditingController();
final input = await showDialog<String?>(
context: context,
builder: (context) {
return AlertDialog(
title: Text('attachmentAddLink'.tr),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('attachmentAddLinkHint'.tr, textAlign: TextAlign.left),
const SizedBox(height: 18),
TextField(
controller: controller,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'attachmentAddLinkInput'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
],
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
foregroundColor:
Theme.of(context).colorScheme.onSurface.withOpacity(0.8),
),
onPressed: () => Navigator.pop(context),
child: Text('cancel'.tr),
),
TextButton(
child: Text('next'.tr),
onPressed: () {
Navigator.pop(context, controller.text);
},
),
],
);
},
);
WidgetsBinding.instance.addPostFrameCallback((_) => controller.dispose());
if (input == null || input.isEmpty) return;
final value = int.tryParse(input);
if (value == null) return;
final AttachmentProvider attach = Get.find();
final result = await attach.getMetadata(value);
if (result != null) {
widget.onAdd(result.id);
setState(() => _attachments.add(result));
}
}
void _pasteFileToUpload() async {
final data = await Pasteboard.image;
if (data == null) return;
@ -150,7 +207,7 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
}
void _revertMetadataList() {
final AttachmentProvider provider = Get.find();
final AttachmentProvider attach = Get.find();
if (widget.initialAttachments.isEmpty) {
_isFirstTimeBusy = false;
@ -167,7 +224,7 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
int progress = 0;
for (var idx = 0; idx < widget.initialAttachments.length; idx++) {
provider.getMetadata(widget.initialAttachments[idx]).then((resp) {
attach.getMetadata(widget.initialAttachments[idx]).then((resp) {
progress++;
_attachments[idx] = resp;
if (progress == widget.initialAttachments.length) {
@ -425,6 +482,19 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
});
},
),
PopupMenuItem(
child: ListTile(
title: Text('unlink'.tr),
leading: const Icon(Icons.link_off),
contentPadding: const EdgeInsets.symmetric(
horizontal: 8,
),
),
onTap: () {
widget.onRemove(element.id);
setState(() => _attachments.removeAt(index));
},
),
],
),
],
@ -661,6 +731,12 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
style: const ButtonStyle(visualDensity: density),
onPressed: () => _pickFileToUpload(),
),
ElevatedButton.icon(
icon: const Icon(Icons.link),
label: Text('attachmentAddFile'.tr),
style: const ButtonStyle(visualDensity: density),
onPressed: () => _linkAttachments(),
),
],
).paddingSymmetric(horizontal: 12),
),

View File

@ -24,7 +24,7 @@ class ChatEventList extends StatelessWidget {
required this.onReply,
});
bool checkMessageMergeable(Event? a, Event? b) {
bool _checkMessageMergeable(Event? a, Event? b) {
if (a == null || b == null) return false;
if (a.sender.account.id != b.sender.account.id) return false;
return a.createdAt.difference(b.createdAt).inMinutes <= 3;
@ -42,13 +42,13 @@ class ChatEventList extends StatelessWidget {
itemBuilder: (context, index) {
bool isMerged = false, hasMerged = false;
if (index > 0) {
hasMerged = checkMessageMergeable(
hasMerged = _checkMessageMergeable(
chatController.currentEvents[index - 1].data,
chatController.currentEvents[index].data,
);
}
if (index + 1 < chatController.currentEvents.length) {
isMerged = checkMessageMergeable(
isMerged = _checkMessageMergeable(
chatController.currentEvents[index].data,
chatController.currentEvents[index + 1].data,
);