fluffychat/lib/pages/chat/send_file_dialog.dart

183 lines
5.7 KiB
Dart
Raw Normal View History

2023-11-01 10:45:21 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2020-09-04 10:56:25 +00:00
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
2022-07-10 07:26:16 +00:00
import 'package:fluffychat/utils/size_string.dart';
2021-11-09 20:32:16 +00:00
import '../../utils/resize_image.dart';
2020-09-04 10:56:25 +00:00
class SendFileDialog extends StatefulWidget {
final Room room;
2022-07-10 07:26:16 +00:00
final List<MatrixFile> files;
2020-09-04 10:56:25 +00:00
2021-01-18 09:43:00 +00:00
const SendFileDialog({
required this.room,
2022-07-10 07:26:16 +00:00
required this.files,
super.key,
});
2020-09-04 10:56:25 +00:00
@override
2022-08-14 14:59:21 +00:00
SendFileDialogState createState() => SendFileDialogState();
2020-09-04 10:56:25 +00:00
}
2022-08-14 14:59:21 +00:00
class SendFileDialogState extends State<SendFileDialog> {
2020-09-04 10:56:25 +00:00
bool origImage = false;
2021-10-26 16:47:05 +00:00
/// Images smaller than 20kb don't need compression.
static const int minSizeToCompress = 20 * 1024;
2020-09-04 10:56:25 +00:00
Future<void> _send() async {
2022-07-10 07:26:16 +00:00
for (var file in widget.files) {
MatrixImageFile? thumbnail;
if (file is MatrixVideoFile && file.bytes.length > minSizeToCompress) {
await showFutureLoadingDialog(
context: context,
future: () async {
file = origImage ? file : await file.resizeVideo();
thumbnail = await file.getVideoThumbnail();
},
);
2022-07-10 07:26:16 +00:00
}
final scaffoldMessenger = ScaffoldMessenger.of(context);
widget.room
.sendFileEvent(
file,
thumbnail: thumbnail,
shrinkImageMaxDimension: origImage ? null : 1600,
)
.catchError((e) {
scaffoldMessenger.showSnackBar(
SnackBar(content: Text((e as Object).toLocalizedString(context))),
2022-07-10 07:26:16 +00:00
);
2023-01-26 08:47:30 +00:00
return null;
2022-07-10 07:26:16 +00:00
});
}
Navigator.of(context, rootNavigator: false).pop();
2022-07-10 07:26:16 +00:00
2022-03-30 09:46:24 +00:00
return;
2020-09-04 10:56:25 +00:00
}
@override
Widget build(BuildContext context) {
var sendStr = L10n.of(context)!.sendFile;
2022-07-10 07:26:16 +00:00
final bool allFilesAreImages =
widget.files.every((file) => file is MatrixImageFile);
final sizeString = widget.files
.fold<double>(0, (p, file) => p + file.bytes.length)
.sizeString;
final fileName = widget.files.length == 1
? widget.files.single.name
: L10n.of(context)!.countFiles(widget.files.length.toString());
if (allFilesAreImages) {
sendStr = L10n.of(context)!.sendImage;
2022-07-10 07:26:16 +00:00
} else if (widget.files.every((file) => file is MatrixAudioFile)) {
sendStr = L10n.of(context)!.sendAudio;
2022-07-10 07:26:16 +00:00
} else if (widget.files.every((file) => file is MatrixVideoFile)) {
sendStr = L10n.of(context)!.sendVideo;
2020-09-04 10:56:25 +00:00
}
Widget contentWidget;
2022-07-10 07:26:16 +00:00
if (allFilesAreImages) {
contentWidget = Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Material(
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
elevation:
Theme.of(context).appBarTheme.scrolledUnderElevation ?? 4,
shadowColor: Theme.of(context).appBarTheme.shadowColor,
2023-10-29 08:33:41 +00:00
clipBehavior: Clip.hardEdge,
child: Image.memory(
widget.files.first.bytes,
fit: BoxFit.contain,
height: 256,
),
2020-09-04 10:56:25 +00:00
),
),
2023-11-01 10:45:21 +00:00
const SizedBox(height: 16),
// Workaround for SwitchListTile.adaptive crashes in CupertinoDialog
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CupertinoSwitch(
value: origImage,
onChanged: (v) => setState(() => origImage = v),
),
const SizedBox(width: 16),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
L10n.of(context)!.sendOriginal,
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(sizeString),
],
),
),
],
),
],
);
} else if (widget.files.every((file) => file is MatrixVideoFile)) {
contentWidget = Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(fileName),
const SizedBox(height: 16),
// Workaround for SwitchListTile.adaptive crashes in CupertinoDialog
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CupertinoSwitch(
value: origImage,
onChanged: (v) => setState(() => origImage = v),
),
const SizedBox(width: 16),
Expanded(
child: Column(
2023-11-01 10:45:21 +00:00
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
L10n.of(context)!.sendOriginal,
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(sizeString),
],
),
),
],
2023-08-18 05:24:31 +00:00
),
],
);
2020-09-04 10:56:25 +00:00
} else {
2022-07-10 07:26:16 +00:00
contentWidget = Text('$fileName ($sizeString)');
2020-09-04 10:56:25 +00:00
}
return AlertDialog.adaptive(
2020-09-04 10:56:25 +00:00
title: Text(sendStr),
content: contentWidget,
actions: <Widget>[
2021-02-27 06:53:34 +00:00
TextButton(
2020-09-04 10:56:25 +00:00
onPressed: () {
// just close the dialog
2021-02-24 11:17:23 +00:00
Navigator.of(context, rootNavigator: false).pop();
2020-09-04 10:56:25 +00:00
},
child: Text(L10n.of(context)!.cancel),
2020-09-04 10:56:25 +00:00
),
2021-02-27 06:53:34 +00:00
TextButton(
onPressed: _send,
child: Text(L10n.of(context)!.send),
2020-09-04 10:56:25 +00:00
),
],
);
}
}