fluffychat/lib/pages/chat/send_file_dialog.dart

335 lines
11 KiB
Dart
Raw Normal View History

import 'dart:io';
2023-11-01 10:45:21 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2020-09-04 10:56:25 +00:00
import 'package:cross_file/cross_file.dart';
2021-10-26 16:50:34 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
import 'package:mime/mime.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/config/app_config.dart';
2024-09-22 17:18:08 +00:00
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart';
2022-07-10 07:26:16 +00:00
import 'package:fluffychat/utils/size_string.dart';
import '../../utils/resize_video.dart';
2020-09-04 10:56:25 +00:00
class SendFileDialog extends StatefulWidget {
final Room room;
final List<XFile> files;
2024-09-22 17:18:08 +00:00
final BuildContext outerContext;
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,
2024-09-22 17:18:08 +00:00
required this.outerContext,
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 {
2024-09-22 17:18:08 +00:00
final scaffoldMessenger = ScaffoldMessenger.of(widget.outerContext);
final l10n = L10n.of(context);
2024-09-22 17:18:08 +00:00
try {
scaffoldMessenger.showLoadingSnackBar(l10n.prepareSendingAttachment);
Navigator.of(context, rootNavigator: false).pop();
final clientConfig = await widget.room.client.getConfig();
final maxUploadSize = clientConfig.mUploadSize ?? 100 * 1024 * 1024;
2022-07-10 07:26:16 +00:00
2024-09-22 17:18:08 +00:00
for (final xfile in widget.files) {
final MatrixFile file;
MatrixImageFile? thumbnail;
final length = await xfile.length();
final mimeType = xfile.mimeType ?? lookupMimeType(xfile.path);
2024-09-22 17:18:08 +00:00
// If file is a video, shrink it!
if (PlatformInfos.isMobile &&
mimeType != null &&
mimeType.startsWith('video') &&
length > minSizeToCompress &&
!origImage) {
scaffoldMessenger.showLoadingSnackBar(l10n.compressVideo);
file = await xfile.resizeVideo();
scaffoldMessenger.showLoadingSnackBar(l10n.generatingVideoThumbnail);
thumbnail = await xfile.getVideoThumbnail();
} else {
// Else we just create a MatrixFile
file = MatrixFile(
bytes: await xfile.readAsBytes(),
name: xfile.name,
mimeType: xfile.mimeType,
).detectFileType;
}
2024-09-22 17:18:08 +00:00
if (file.bytes.length > maxUploadSize) {
throw FileTooBigMatrixException(length, maxUploadSize);
}
if (widget.files.length > 1) {
scaffoldMessenger.showLoadingSnackBar(
l10n.sendingAttachmentCountOfCount(
widget.files.indexOf(xfile) + 1,
widget.files.length,
),
);
} else {
2024-09-22 17:20:10 +00:00
scaffoldMessenger.clearSnackBars();
2024-09-22 17:18:08 +00:00
}
2024-09-22 17:18:08 +00:00
try {
await widget.room.sendFileEvent(
file,
thumbnail: thumbnail,
shrinkImageMaxDimension: origImage ? null : 1600,
);
} on MatrixException catch (e) {
final retryAfterMs = e.retryAfterMs;
if (e.error != MatrixError.M_LIMIT_EXCEEDED || retryAfterMs == null) {
rethrow;
}
2024-09-22 17:18:08 +00:00
final retryAfterDuration =
Duration(milliseconds: retryAfterMs + 1000);
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text(
l10n.serverLimitReached(retryAfterDuration.inSeconds),
),
),
);
await Future.delayed(retryAfterDuration);
scaffoldMessenger.showLoadingSnackBar(l10n.sendingAttachment);
2024-09-22 17:18:08 +00:00
await widget.room.sendFileEvent(
file,
thumbnail: thumbnail,
shrinkImageMaxDimension: origImage ? null : 1600,
);
}
2024-09-22 17:18:08 +00:00
}
2024-09-22 17:47:47 +00:00
scaffoldMessenger.clearSnackBars();
2024-09-22 17:18:08 +00:00
} catch (e) {
scaffoldMessenger.clearSnackBars();
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text(e.toLocalizedString(widget.outerContext)),
duration: const Duration(seconds: 30),
showCloseIcon: true,
),
);
rethrow;
}
2022-03-30 09:46:24 +00:00
return;
2020-09-04 10:56:25 +00:00
}
Future<String> _calcCombinedFileSize() async {
final lengths =
await Future.wait(widget.files.map((file) => file.length()));
return lengths.fold<double>(0, (p, length) => p + length).sizeString;
}
2020-09-04 10:56:25 +00:00
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
var sendStr = L10n.of(context).sendFile;
final uniqueMimeType = widget.files
.map((file) => file.mimeType ?? lookupMimeType(file.path))
.toSet()
.singleOrNull;
2022-07-10 07:26:16 +00:00
final fileName = widget.files.length == 1
? widget.files.single.name
: L10n.of(context).countFiles(widget.files.length.toString());
2022-07-10 07:26:16 +00:00
if (uniqueMimeType?.startsWith('image') ?? false) {
sendStr = L10n.of(context).sendImage;
} else if (uniqueMimeType?.startsWith('audio') ?? false) {
sendStr = L10n.of(context).sendAudio;
} else if (uniqueMimeType?.startsWith('video') ?? false) {
sendStr = L10n.of(context).sendVideo;
2020-09-04 10:56:25 +00:00
}
return FutureBuilder<String>(
future: _calcCombinedFileSize(),
builder: (context, snapshot) {
final sizeString =
snapshot.data ?? L10n.of(context).calculatingFileSize;
Widget contentWidget;
if (uniqueMimeType?.startsWith('image') ?? false) {
contentWidget = Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Material(
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
elevation: theme.appBarTheme.scrolledUnderElevation ?? 4,
shadowColor: theme.appBarTheme.shadowColor,
clipBehavior: Clip.hardEdge,
child: kIsWeb
? Image.network(
widget.files.first.path,
fit: BoxFit.contain,
height: 256,
)
: Image.file(
File(widget.files.first.path),
fit: BoxFit.contain,
height: 256,
),
),
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 {
final fileNameParts = fileName.split('.');
contentWidget = SizedBox(
width: 256,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
2023-11-01 10:45:21 +00:00
children: [
Icon(
uniqueMimeType == null
? Icons.description_outlined
: uniqueMimeType.startsWith('video')
? Icons.video_file_outlined
: uniqueMimeType.startsWith('audio')
? Icons.audio_file_outlined
: Icons.description_outlined,
2023-11-01 10:45:21 +00:00
),
const SizedBox(width: 8),
Expanded(
child: Text(
fileNameParts.first,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
if (fileNameParts.length > 1)
Text('.${fileNameParts.last}'),
Text(' ($sizeString)'),
2023-11-01 10:45:21 +00:00
],
),
// Workaround for SwitchListTile.adaptive crashes in CupertinoDialog
if (uniqueMimeType != null &&
uniqueMimeType.startsWith('video') &&
PlatformInfos.isMobile)
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),
],
),
),
],
),
],
),
);
}
return AlertDialog.adaptive(
title: Text(sendStr),
content: contentWidget,
actions: <Widget>[
TextButton(
onPressed: () {
// just close the dialog
Navigator.of(context, rootNavigator: false).pop();
},
child: Text(L10n.of(context).cancel),
),
TextButton(
onPressed: _send,
child: Text(L10n.of(context).send),
),
],
);
},
2020-09-04 10:56:25 +00:00
);
}
}
2024-09-22 17:18:08 +00:00
extension on ScaffoldMessengerState {
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showLoadingSnackBar(
String title,
) {
clearSnackBars();
return showSnackBar(
SnackBar(
duration: const Duration(minutes: 5),
dismissDirection: DismissDirection.none,
content: Row(
children: [
const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
),
),
const SizedBox(width: 16),
Text(title),
],
),
),
);
}
}