2020-10-03 13:11:07 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2020-09-04 12:56:25 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../../utils/matrix_sdk_extensions.dart/matrix_file_extension.dart';
|
|
|
|
import '../../utils/resize_image.dart';
|
2020-09-04 12:56:25 +02:00
|
|
|
|
|
|
|
class SendFileDialog extends StatefulWidget {
|
|
|
|
final Room room;
|
|
|
|
final MatrixFile file;
|
|
|
|
|
2021-01-18 10:43:00 +01:00
|
|
|
const SendFileDialog({
|
2021-12-01 20:45:07 +01:00
|
|
|
required this.room,
|
|
|
|
required this.file,
|
|
|
|
Key? key,
|
2021-01-18 10:43:00 +01:00
|
|
|
}) : super(key: key);
|
2020-09-04 12:56:25 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
_SendFileDialogState createState() => _SendFileDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SendFileDialogState extends State<SendFileDialog> {
|
|
|
|
bool origImage = false;
|
2020-10-25 17:34:14 +01:00
|
|
|
bool _isSending = false;
|
2021-10-26 18:47:05 +02:00
|
|
|
|
2021-12-01 20:44:59 +01:00
|
|
|
/// Images smaller than 20kb don't need compression.
|
|
|
|
static const int minSizeToCompress = 20 * 1024;
|
|
|
|
|
2020-09-04 12:56:25 +02:00
|
|
|
Future<void> _send() async {
|
|
|
|
var file = widget.file;
|
2022-02-03 07:35:44 +01:00
|
|
|
MatrixImageFile? thumbnail;
|
2021-12-01 20:44:59 +01:00
|
|
|
if (file is MatrixImageFile &&
|
|
|
|
!origImage &&
|
|
|
|
file.bytes.length > minSizeToCompress) {
|
2022-02-03 07:35:44 +01:00
|
|
|
file = await MatrixImageFile.shrink(
|
|
|
|
bytes: file.bytes,
|
|
|
|
name: file.name,
|
|
|
|
compute: widget.room.client.runInBackground,
|
|
|
|
);
|
2020-09-04 12:56:25 +02:00
|
|
|
}
|
2021-12-27 13:01:51 +01:00
|
|
|
if (file is MatrixVideoFile && file.bytes.length > minSizeToCompress) {
|
|
|
|
file = await file.resizeVideo();
|
2022-02-03 07:35:44 +01:00
|
|
|
thumbnail = await file.getVideoThumbnail();
|
2021-12-27 13:01:51 +01:00
|
|
|
}
|
2022-02-03 07:35:44 +01:00
|
|
|
await widget.room.sendFileEvent(file, thumbnail: thumbnail);
|
2020-09-04 12:56:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-12-01 20:45:07 +01:00
|
|
|
var sendStr = L10n.of(context)!.sendFile;
|
2020-09-04 12:56:25 +02:00
|
|
|
if (widget.file is MatrixImageFile) {
|
2021-12-01 20:45:07 +01:00
|
|
|
sendStr = L10n.of(context)!.sendImage;
|
2020-09-04 12:56:25 +02:00
|
|
|
} else if (widget.file is MatrixAudioFile) {
|
2021-12-01 20:45:07 +01:00
|
|
|
sendStr = L10n.of(context)!.sendAudio;
|
2020-09-04 12:56:25 +02:00
|
|
|
} else if (widget.file is MatrixVideoFile) {
|
2021-12-01 20:45:07 +01:00
|
|
|
sendStr = L10n.of(context)!.sendVideo;
|
2020-09-04 12:56:25 +02:00
|
|
|
}
|
|
|
|
Widget contentWidget;
|
|
|
|
if (widget.file is MatrixImageFile) {
|
|
|
|
contentWidget = Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
|
|
|
|
Flexible(
|
|
|
|
child: Image.memory(
|
|
|
|
widget.file.bytes,
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Checkbox(
|
|
|
|
value: origImage,
|
2021-12-01 20:45:07 +01:00
|
|
|
onChanged: (v) => setState(() => origImage = v ?? false),
|
2020-09-04 12:56:25 +02:00
|
|
|
),
|
|
|
|
InkWell(
|
|
|
|
onTap: () => setState(() => origImage = !origImage),
|
2021-12-01 20:45:07 +01:00
|
|
|
child: Text(L10n.of(context)!.sendOriginal +
|
2021-02-24 12:17:23 +01:00
|
|
|
' (${widget.file.sizeString})'),
|
2020-09-04 12:56:25 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
contentWidget = Text('${widget.file.name} (${widget.file.sizeString})');
|
|
|
|
}
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text(sendStr),
|
|
|
|
content: contentWidget,
|
|
|
|
actions: <Widget>[
|
2021-02-27 07:53:34 +01:00
|
|
|
TextButton(
|
2020-09-04 12:56:25 +02:00
|
|
|
onPressed: () {
|
|
|
|
// just close the dialog
|
2021-02-24 12:17:23 +01:00
|
|
|
Navigator.of(context, rootNavigator: false).pop();
|
2020-09-04 12:56:25 +02:00
|
|
|
},
|
2021-12-01 20:45:07 +01:00
|
|
|
child: Text(L10n.of(context)!.cancel),
|
2020-09-04 12:56:25 +02:00
|
|
|
),
|
2021-02-27 07:53:34 +01:00
|
|
|
TextButton(
|
2020-10-25 17:34:14 +01:00
|
|
|
onPressed: _isSending
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
setState(() {
|
|
|
|
_isSending = true;
|
|
|
|
});
|
2020-12-25 09:58:34 +01:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context, future: () => _send());
|
2021-03-04 12:28:06 +01:00
|
|
|
Navigator.of(context, rootNavigator: false).pop();
|
2020-10-25 17:34:14 +01:00
|
|
|
},
|
2021-12-01 20:45:07 +01:00
|
|
|
child: Text(L10n.of(context)!.send),
|
2020-09-04 12:56:25 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|