2024-06-29 12:25:29 +00:00
|
|
|
import 'dart:async';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math' as math;
|
2024-06-29 12:25:29 +00:00
|
|
|
import 'dart:typed_data';
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-07-07 05:16:08 +00:00
|
|
|
import 'package:desktop_drop/desktop_drop.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2024-07-07 05:16:08 +00:00
|
|
|
import 'package:pasteboard/pasteboard.dart';
|
2024-06-29 13:03:15 +00:00
|
|
|
import 'package:path/path.dart' show basename;
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:solian/exts.dart';
|
|
|
|
import 'package:solian/models/attachment.dart';
|
2024-06-29 13:03:15 +00:00
|
|
|
import 'package:solian/platform.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-22 15:18:01 +00:00
|
|
|
import 'package:solian/providers/content/attachment.dart';
|
2024-07-07 05:38:43 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_item.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-07-30 12:49:01 +00:00
|
|
|
class AttachmentEditorPopup extends StatefulWidget {
|
2024-05-19 16:08:20 +00:00
|
|
|
final String usage;
|
2024-05-20 15:11:26 +00:00
|
|
|
final List<int> current;
|
|
|
|
final void Function(List<int> data) onUpdate;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-07-30 12:49:01 +00:00
|
|
|
const AttachmentEditorPopup({
|
2024-05-19 16:08:20 +00:00
|
|
|
super.key,
|
|
|
|
required this.usage,
|
|
|
|
required this.current,
|
|
|
|
required this.onUpdate,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
2024-07-30 12:49:01 +00:00
|
|
|
State<AttachmentEditorPopup> createState() => _AttachmentEditorPopupState();
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 12:49:01 +00:00
|
|
|
class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
|
2024-05-19 16:08:20 +00:00
|
|
|
final _imagePicker = ImagePicker();
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
bool _isBusy = false;
|
|
|
|
bool _isFirstTimeBusy = true;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
List<Attachment?> _attachments = List.empty(growable: true);
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
Future<void> pickPhotoToUpload() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
final medias = await _imagePicker.pickMultiImage();
|
|
|
|
if (medias.isEmpty) return;
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
for (final media in medias) {
|
|
|
|
final file = File(media.path);
|
|
|
|
try {
|
2024-05-22 15:18:01 +00:00
|
|
|
await uploadAttachment(
|
2024-06-29 12:25:29 +00:00
|
|
|
await file.readAsBytes(),
|
|
|
|
file.path,
|
2024-07-29 10:06:38 +00:00
|
|
|
null,
|
2024-05-22 15:18:01 +00:00
|
|
|
);
|
2024-05-19 16:08:20 +00:00
|
|
|
} catch (err) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> pickVideoToUpload() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
final media = await _imagePicker.pickVideo(source: ImageSource.gallery);
|
|
|
|
if (media == null) return;
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
final file = File(media.path);
|
|
|
|
|
|
|
|
try {
|
2024-07-29 10:06:38 +00:00
|
|
|
await uploadAttachment(await file.readAsBytes(), file.path, null);
|
2024-05-19 16:08:20 +00:00
|
|
|
} catch (err) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> pickFileToUpload() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-07-19 15:56:59 +00:00
|
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles(
|
|
|
|
allowMultiple: true,
|
|
|
|
);
|
2024-05-19 16:08:20 +00:00
|
|
|
if (result == null) return;
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
List<File> files = result.paths.map((path) => File(path!)).toList();
|
|
|
|
|
2024-05-19 16:08:20 +00:00
|
|
|
for (final file in files) {
|
|
|
|
try {
|
2024-07-29 10:06:38 +00:00
|
|
|
await uploadAttachment(await file.readAsBytes(), file.path, null);
|
2024-05-19 16:08:20 +00:00
|
|
|
} catch (err) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> takeMediaToUpload(bool isVideo) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
XFile? media;
|
|
|
|
if (isVideo) {
|
|
|
|
media = await _imagePicker.pickVideo(source: ImageSource.camera);
|
|
|
|
} else {
|
|
|
|
media = await _imagePicker.pickImage(source: ImageSource.camera);
|
|
|
|
}
|
|
|
|
if (media == null) return;
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-05-19 16:08:20 +00:00
|
|
|
|
|
|
|
final file = File(media.path);
|
|
|
|
try {
|
2024-07-29 10:06:38 +00:00
|
|
|
await uploadAttachment(await file.readAsBytes(), file.path, null);
|
2024-05-19 16:08:20 +00:00
|
|
|
} catch (err) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(err);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 13:03:15 +00:00
|
|
|
void pasteFileToUpload() async {
|
2024-07-07 05:16:08 +00:00
|
|
|
final data = await Pasteboard.image;
|
2024-07-07 05:38:43 +00:00
|
|
|
if (data == null) return;
|
2024-06-29 13:03:15 +00:00
|
|
|
|
2024-07-07 05:16:08 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-06-30 09:43:36 +00:00
|
|
|
|
2024-07-29 10:06:38 +00:00
|
|
|
uploadAttachment(data, 'Pasted Image', null);
|
2024-07-07 05:16:08 +00:00
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
2024-06-29 13:03:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 10:06:38 +00:00
|
|
|
Future<void> uploadAttachment(
|
|
|
|
Uint8List data, String path, Map<String, dynamic>? metadata) async {
|
2024-05-22 15:18:01 +00:00
|
|
|
final AttachmentProvider provider = Get.find();
|
|
|
|
try {
|
2024-06-29 13:03:15 +00:00
|
|
|
context.showSnackbar((PlatformInfo.isWeb
|
|
|
|
? 'attachmentUploadingWebMode'
|
|
|
|
: 'attachmentUploading')
|
|
|
|
.trParams({'name': basename(path)}));
|
2024-05-22 15:18:01 +00:00
|
|
|
final resp = await provider.createAttachment(
|
2024-06-29 12:25:29 +00:00
|
|
|
data,
|
|
|
|
path,
|
2024-05-22 15:18:01 +00:00
|
|
|
widget.usage,
|
2024-07-19 15:56:59 +00:00
|
|
|
metadata,
|
2024-05-22 15:18:01 +00:00
|
|
|
);
|
2024-05-19 16:08:20 +00:00
|
|
|
var result = Attachment.fromJson(resp.body);
|
|
|
|
setState(() => _attachments.add(result));
|
2024-05-20 15:11:26 +00:00
|
|
|
widget.onUpdate(_attachments.map((e) => e!.id).toList());
|
2024-06-29 13:03:15 +00:00
|
|
|
context.clearSnackbar();
|
2024-06-02 15:17:15 +00:00
|
|
|
} catch (err) {
|
2024-05-22 15:18:01 +00:00
|
|
|
rethrow;
|
2024-05-19 16:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:20:11 +00:00
|
|
|
String _formatBytes(int bytes, {int decimals = 2}) {
|
2024-05-19 16:08:20 +00:00
|
|
|
if (bytes == 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
|
|
final dm = decimals < 0 ? 0 : decimals;
|
2024-05-21 16:05:03 +00:00
|
|
|
final sizes = [
|
|
|
|
'Bytes',
|
|
|
|
'KiB',
|
|
|
|
'MiB',
|
|
|
|
'GiB',
|
|
|
|
'TiB',
|
|
|
|
'PiB',
|
|
|
|
'EiB',
|
|
|
|
'ZiB',
|
|
|
|
'YiB'
|
|
|
|
];
|
2024-05-19 16:08:20 +00:00
|
|
|
final i = (math.log(bytes) / math.log(k)).floor().toInt();
|
|
|
|
return '${(bytes / math.pow(k, i)).toStringAsFixed(dm)} ${sizes[i]}';
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
void revertMetadataList() {
|
2024-05-22 15:18:01 +00:00
|
|
|
final AttachmentProvider provider = Get.find();
|
2024-05-20 15:11:26 +00:00
|
|
|
|
|
|
|
if (widget.current.isEmpty) {
|
|
|
|
_isFirstTimeBusy = false;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_attachments = List.filled(widget.current.length, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
int progress = 0;
|
|
|
|
for (var idx = 0; idx < widget.current.length; idx++) {
|
|
|
|
provider.getMetadata(widget.current[idx]).then((resp) {
|
|
|
|
progress++;
|
2024-07-29 09:56:36 +00:00
|
|
|
_attachments[idx] = resp;
|
2024-05-20 15:11:26 +00:00
|
|
|
if (progress == widget.current.length) {
|
|
|
|
setState(() {
|
|
|
|
_isBusy = false;
|
|
|
|
_isFirstTimeBusy = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 05:38:43 +00:00
|
|
|
void showEdit(Attachment element, int index) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return AttachmentEditorDialog(
|
|
|
|
item: element,
|
|
|
|
onDelete: () {
|
|
|
|
setState(() => _attachments.removeAt(index));
|
|
|
|
widget.onUpdate(_attachments.map((e) => e!.id).toList());
|
|
|
|
},
|
|
|
|
onUpdate: (item) {
|
|
|
|
setState(() => _attachments[index] = item);
|
|
|
|
widget.onUpdate(_attachments.map((e) => e!.id).toList());
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
revertMetadataList();
|
|
|
|
}
|
|
|
|
|
2024-06-29 12:25:29 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:08:20 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
const density = VisualDensity(horizontal: 0, vertical: 0);
|
|
|
|
|
|
|
|
return SafeArea(
|
|
|
|
child: SizedBox(
|
|
|
|
height: MediaQuery.of(context).size.height * 0.85,
|
2024-07-07 05:16:08 +00:00
|
|
|
child: DropTarget(
|
|
|
|
onDragDone: (detail) async {
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
for (final file in detail.files) {
|
|
|
|
final data = await file.readAsBytes();
|
2024-07-29 10:06:38 +00:00
|
|
|
uploadAttachment(data, file.path, null);
|
2024-06-29 12:25:29 +00:00
|
|
|
}
|
2024-07-07 05:16:08 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-06-29 12:25:29 +00:00
|
|
|
},
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
'attachmentAdd'.tr,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
).paddingOnly(left: 24, right: 24, top: 32, bottom: 16),
|
|
|
|
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
|
|
|
|
Expanded(
|
|
|
|
child: Builder(builder: (context) {
|
|
|
|
if (_isFirstTimeBusy && _isBusy) {
|
|
|
|
return const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListView.builder(
|
|
|
|
itemCount: _attachments.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final element = _attachments[index];
|
|
|
|
var fileType = element!.mimetype.split('/').firstOrNull;
|
|
|
|
fileType ??= 'unknown';
|
|
|
|
return Container(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 16, right: 8, bottom: 16),
|
2024-07-07 05:38:43 +00:00
|
|
|
child: Card(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
height: 280,
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.only(
|
|
|
|
topLeft: Radius.circular(8),
|
|
|
|
topRight: Radius.circular(8),
|
2024-06-29 12:25:29 +00:00
|
|
|
),
|
2024-07-07 05:38:43 +00:00
|
|
|
child: AttachmentItem(
|
|
|
|
parentId: 'attachment-editor',
|
|
|
|
item: element,
|
|
|
|
showBadge: false,
|
|
|
|
showHideButton: false,
|
2024-06-29 12:25:29 +00:00
|
|
|
),
|
2024-07-07 05:38:43 +00:00
|
|
|
),
|
2024-06-29 12:25:29 +00:00
|
|
|
),
|
2024-07-07 05:38:43 +00:00
|
|
|
SizedBox(
|
|
|
|
height: 54,
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment:
|
|
|
|
CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
element.alt,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
style: const TextStyle(
|
2024-07-19 15:56:59 +00:00
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontFamily: 'monospace'),
|
2024-07-07 05:38:43 +00:00
|
|
|
),
|
|
|
|
Text(
|
2024-07-26 16:20:11 +00:00
|
|
|
'${fileType[0].toUpperCase()}${fileType.substring(1)} · ${_formatBytes(element.size)}',
|
2024-07-07 05:38:43 +00:00
|
|
|
style:
|
|
|
|
const TextStyle(fontSize: 12),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
style: TextButton.styleFrom(
|
|
|
|
shape: const CircleBorder(),
|
|
|
|
foregroundColor:
|
|
|
|
Theme.of(context).primaryColor,
|
|
|
|
),
|
|
|
|
icon: const Icon(Icons.more_horiz),
|
|
|
|
onPressed: () => showEdit(element, index),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(vertical: 8, horizontal: 16),
|
2024-06-29 12:25:29 +00:00
|
|
|
),
|
2024-07-07 05:38:43 +00:00
|
|
|
],
|
|
|
|
),
|
2024-06-29 12:25:29 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
2024-05-19 16:08:20 +00:00
|
|
|
),
|
2024-06-29 12:25:29 +00:00
|
|
|
const Divider(thickness: 0.3, height: 0.3),
|
|
|
|
SizedBox(
|
|
|
|
height: 64,
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
child: Wrap(
|
|
|
|
spacing: 8,
|
|
|
|
runSpacing: 0,
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
runAlignment: WrapAlignment.center,
|
|
|
|
children: [
|
2024-07-30 03:50:26 +00:00
|
|
|
if (PlatformInfo.isDesktop || PlatformInfo.isIOS || PlatformInfo.isWeb)
|
2024-06-29 13:03:15 +00:00
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.paste),
|
|
|
|
label: Text('attachmentAddClipboard'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => pasteFileToUpload(),
|
|
|
|
),
|
2024-06-29 12:25:29 +00:00
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.add_photo_alternate),
|
|
|
|
label: Text('attachmentAddGalleryPhoto'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => pickPhotoToUpload(),
|
|
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.add_road),
|
|
|
|
label: Text('attachmentAddGalleryVideo'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => pickVideoToUpload(),
|
|
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.photo_camera_back),
|
|
|
|
label: Text('attachmentAddCameraPhoto'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => takeMediaToUpload(false),
|
|
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.video_camera_back_outlined),
|
|
|
|
label: Text('attachmentAddCameraVideo'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => takeMediaToUpload(true),
|
|
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: const Icon(Icons.file_present_rounded),
|
|
|
|
label: Text('attachmentAddFile'.tr),
|
|
|
|
style: const ButtonStyle(visualDensity: density),
|
|
|
|
onPressed: () => pickFileToUpload(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingSymmetric(horizontal: 12),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2024-05-19 16:08:20 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-05-20 15:11:26 +00:00
|
|
|
|
2024-06-27 03:05:15 +00:00
|
|
|
class AttachmentEditorDialog extends StatefulWidget {
|
2024-05-20 15:11:26 +00:00
|
|
|
final Attachment item;
|
|
|
|
final Function onDelete;
|
|
|
|
final Function(Attachment item) onUpdate;
|
|
|
|
|
2024-06-27 03:05:15 +00:00
|
|
|
const AttachmentEditorDialog(
|
2024-05-21 16:05:03 +00:00
|
|
|
{super.key,
|
|
|
|
required this.item,
|
|
|
|
required this.onDelete,
|
|
|
|
required this.onUpdate});
|
2024-05-20 15:11:26 +00:00
|
|
|
|
|
|
|
@override
|
2024-06-29 12:25:29 +00:00
|
|
|
State<AttachmentEditorDialog> createState() => _AttachmentEditorDialogState();
|
2024-05-20 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 03:05:15 +00:00
|
|
|
class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
|
2024-05-20 15:11:26 +00:00
|
|
|
final _altController = TextEditingController();
|
|
|
|
|
|
|
|
bool _isBusy = false;
|
|
|
|
bool _isMature = false;
|
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
Future<Attachment?> updateAttachment() async {
|
|
|
|
final AttachmentProvider provider = Get.find();
|
2024-05-20 15:11:26 +00:00
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
2024-05-22 15:18:01 +00:00
|
|
|
try {
|
|
|
|
final resp = await provider.updateAttachment(
|
|
|
|
widget.item.id,
|
|
|
|
_altController.value.text,
|
|
|
|
widget.item.usage,
|
|
|
|
isMature: _isMature,
|
|
|
|
);
|
2024-06-01 13:39:28 +00:00
|
|
|
|
|
|
|
Get.find<AttachmentProvider>().clearCache(id: widget.item.id);
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
2024-05-20 15:11:26 +00:00
|
|
|
return Attachment.fromJson(resp.body);
|
2024-05-22 15:18:01 +00:00
|
|
|
} catch (e) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(e);
|
2024-06-01 13:39:28 +00:00
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-06-01 13:39:28 +00:00
|
|
|
return null;
|
2024-05-20 15:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAttachment() async {
|
|
|
|
setState(() => _isBusy = true);
|
2024-05-22 15:18:01 +00:00
|
|
|
try {
|
|
|
|
final AttachmentProvider provider = Get.find();
|
|
|
|
await provider.deleteAttachment(widget.item.id);
|
2024-05-20 15:11:26 +00:00
|
|
|
widget.onDelete();
|
2024-05-22 15:18:01 +00:00
|
|
|
} catch (e) {
|
2024-05-28 16:14:41 +00:00
|
|
|
context.showErrorDialog(e);
|
2024-05-22 15:18:01 +00:00
|
|
|
} finally {
|
|
|
|
setState(() => _isBusy = false);
|
2024-05-20 15:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncWidget() {
|
|
|
|
_isMature = widget.item.isMature;
|
|
|
|
_altController.text = widget.item.alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
syncWidget();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text('attachmentSetting'.tr),
|
|
|
|
content: Container(
|
|
|
|
constraints: const BoxConstraints(minWidth: 400),
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2024-05-21 16:05:03 +00:00
|
|
|
if (_isBusy)
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
|
|
child: const LinearProgressIndicator().animate().scaleX(),
|
|
|
|
),
|
2024-05-20 15:11:26 +00:00
|
|
|
const SizedBox(height: 18),
|
|
|
|
TextField(
|
|
|
|
controller: _altController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
isDense: true,
|
|
|
|
prefixIcon: const Icon(Icons.image_not_supported),
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'attachmentAlt'.tr,
|
|
|
|
),
|
2024-05-21 16:05:03 +00:00
|
|
|
onTapOutside: (_) =>
|
|
|
|
FocusManager.instance.primaryFocus?.unfocus(),
|
2024-05-20 15:11:26 +00:00
|
|
|
),
|
|
|
|
Card(
|
|
|
|
child: CheckboxListTile(
|
2024-05-21 16:05:03 +00:00
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(10))),
|
2024-05-20 15:11:26 +00:00
|
|
|
title: Text('matureContent'.tr),
|
|
|
|
secondary: const Icon(Icons.visibility_off),
|
|
|
|
value: _isMature,
|
|
|
|
onChanged: (newValue) {
|
|
|
|
setState(() => _isMature = newValue ?? false);
|
|
|
|
},
|
|
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actionsAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
2024-05-21 16:05:03 +00:00
|
|
|
style: TextButton.styleFrom(
|
|
|
|
foregroundColor: Theme.of(context).colorScheme.error),
|
2024-05-20 15:11:26 +00:00
|
|
|
onPressed: () {
|
|
|
|
deleteAttachment().then((_) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Text('delete'.tr),
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
2024-05-21 16:05:03 +00:00
|
|
|
style: TextButton.styleFrom(
|
|
|
|
foregroundColor:
|
|
|
|
Theme.of(context).colorScheme.onSurfaceVariant),
|
2024-05-20 15:11:26 +00:00
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
child: Text('apply'.tr),
|
|
|
|
onPressed: () {
|
2024-05-22 15:18:01 +00:00
|
|
|
updateAttachment().then((value) {
|
2024-05-20 15:11:26 +00:00
|
|
|
if (value != null) {
|
|
|
|
widget.onUpdate(value);
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|