2024-09-10 13:36:10 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/exts.dart';
|
|
|
|
import 'package:solian/models/attachment.dart';
|
|
|
|
import 'package:solian/providers/content/attachment.dart';
|
|
|
|
import 'package:solian/widgets/attachments/attachment_editor.dart';
|
|
|
|
|
|
|
|
class AttachmentEditorThumbnailDialog extends StatefulWidget {
|
|
|
|
final Attachment item;
|
|
|
|
final String pool;
|
|
|
|
final String? initialItem;
|
|
|
|
final Function(String? id) onUpdate;
|
|
|
|
|
|
|
|
const AttachmentEditorThumbnailDialog({
|
|
|
|
super.key,
|
|
|
|
required this.item,
|
|
|
|
required this.pool,
|
|
|
|
required this.initialItem,
|
|
|
|
required this.onUpdate,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<AttachmentEditorThumbnailDialog> createState() =>
|
|
|
|
_AttachmentEditorThumbnailDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AttachmentEditorThumbnailDialogState
|
|
|
|
extends State<AttachmentEditorThumbnailDialog> {
|
|
|
|
bool _isLoading = false;
|
|
|
|
|
|
|
|
final TextEditingController _attachmentController = TextEditingController();
|
|
|
|
|
|
|
|
void _promptUploadNewAttachment() {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AttachmentEditorPopup(
|
|
|
|
pool: widget.pool,
|
|
|
|
singleMode: true,
|
|
|
|
imageOnly: true,
|
|
|
|
autoUpload: true,
|
|
|
|
onAdd: (value) {
|
|
|
|
widget.onUpdate(value);
|
|
|
|
_attachmentController.text = value;
|
|
|
|
},
|
|
|
|
initialAttachments: const [],
|
|
|
|
onRemove: (_) {},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _updateAttachment() async {
|
|
|
|
setState(() => _isLoading = true);
|
|
|
|
|
|
|
|
final AttachmentProvider attach = Get.find();
|
|
|
|
|
|
|
|
widget.item.metadata ??= {};
|
2024-09-10 13:53:05 +00:00
|
|
|
if (_attachmentController.text.isNotEmpty) {
|
|
|
|
widget.item.metadata!['thumbnail'] = _attachmentController.text;
|
|
|
|
} else {
|
|
|
|
widget.item.metadata!['thumbnail'] = null;
|
|
|
|
}
|
2024-09-10 13:36:10 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await attach.updateAttachment(
|
|
|
|
widget.item.id,
|
|
|
|
widget.item.alt,
|
|
|
|
isMature: widget.item.isMature,
|
|
|
|
metadata: widget.item.metadata!,
|
|
|
|
);
|
|
|
|
|
|
|
|
Get.find<AttachmentProvider>().clearCache(id: widget.item.rid);
|
|
|
|
} catch (e) {
|
|
|
|
context.showErrorDialog(e);
|
|
|
|
} finally {
|
|
|
|
setState(() => _isLoading = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
if (widget.initialItem != null) {
|
|
|
|
_attachmentController.text = widget.initialItem!;
|
|
|
|
}
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_attachmentController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
2024-09-16 12:33:34 +00:00
|
|
|
title: Text('attachmentThumbnail'.tr),
|
2024-09-10 13:36:10 +00:00
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Card(
|
|
|
|
margin: EdgeInsets.zero,
|
|
|
|
child: ListTile(
|
2024-09-16 12:33:34 +00:00
|
|
|
title: Text('attachmentThumbnailAttachmentNew'.tr),
|
2024-09-10 13:36:10 +00:00
|
|
|
contentPadding: const EdgeInsets.only(left: 12, right: 9),
|
|
|
|
trailing: const Icon(Icons.chevron_right),
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(8)),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
_promptUploadNewAttachment();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const Row(children: <Widget>[
|
|
|
|
Expanded(child: Divider()),
|
|
|
|
Text('OR'),
|
|
|
|
Expanded(child: Divider()),
|
|
|
|
]).paddingOnly(top: 12, bottom: 16, left: 16, right: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _attachmentController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
isDense: true,
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
prefixText: '#',
|
2024-09-16 12:33:34 +00:00
|
|
|
labelText: 'attachmentThumbnailAttachment'.tr,
|
2024-09-10 13:36:10 +00:00
|
|
|
),
|
|
|
|
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isLoading
|
|
|
|
? null
|
|
|
|
: () {
|
|
|
|
_updateAttachment().then((_) {
|
|
|
|
widget.onUpdate(_attachmentController.text);
|
|
|
|
if (mounted) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Text('confirm'.tr),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|