Skip to content

Commit

Permalink
support send image/video/data without sound (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
boyan01 authored Jul 13, 2023
1 parent 1e03aae commit 35128c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 50 deletions.
54 changes: 34 additions & 20 deletions lib/account/account_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class AccountServer {
String? conversationId,
String? recipientId,
String? quoteMessageId,
bool silent = false,
}) async =>
_sendMessageHelper.sendImageMessage(
conversationId: await _initConversation(conversationId, recipientId),
Expand All @@ -431,21 +432,28 @@ class AccountServer {
category: encryptCategory.toCategory(MessageCategory.plainImage,
MessageCategory.signalImage, MessageCategory.encryptedImage),
quoteMessageId: quoteMessageId,
silent: silent,
);

// NOTE: Send video as DataMessage, cause we can not retriever video metadata
// from video file.
Future<void> sendVideoMessage(XFile video, EncryptCategory encryptCategory,
{String? conversationId,
String? recipientId,
String? quoteMessageId}) async =>
Future<void> sendVideoMessage(
XFile video,
EncryptCategory encryptCategory, {
String? conversationId,
String? recipientId,
String? quoteMessageId,
bool silent = false,
}) async =>
_sendMessageHelper.sendDataMessage(
await _initConversation(conversationId, recipientId),
userId,
video,
encryptCategory.toCategory(MessageCategory.plainData,
MessageCategory.signalData, MessageCategory.encryptedData),
quoteMessageId);
await _initConversation(conversationId, recipientId),
userId,
video,
encryptCategory.toCategory(MessageCategory.plainData,
MessageCategory.signalData, MessageCategory.encryptedData),
quoteMessageId,
silent: silent,
);

Future<void> sendAudioMessage(
XFile audio,
Expand All @@ -467,17 +475,23 @@ class AccountServer {
mediaWaveform: waveform,
);

Future<void> sendDataMessage(XFile file, EncryptCategory encryptCategory,
{String? conversationId,
String? recipientId,
String? quoteMessageId}) async =>
Future<void> sendDataMessage(
XFile file,
EncryptCategory encryptCategory, {
String? conversationId,
String? recipientId,
String? quoteMessageId,
bool silent = false,
}) async =>
_sendMessageHelper.sendDataMessage(
await _initConversation(conversationId, recipientId),
userId,
file,
encryptCategory.toCategory(MessageCategory.plainData,
MessageCategory.signalData, MessageCategory.encryptedData),
quoteMessageId);
await _initConversation(conversationId, recipientId),
userId,
file,
encryptCategory.toCategory(MessageCategory.plainData,
MessageCategory.signalData, MessageCategory.encryptedData),
quoteMessageId,
silent: silent,
);

Future<void> sendStickerMessage(
String stickerId,
Expand Down
18 changes: 15 additions & 3 deletions lib/account/send_message_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class SendMessageHelper {
required String category,
String? quoteMessageId,
AttachmentResult? attachmentResult,
bool silent = false,
}) async {
final messageId = const Uuid().v4();
final _bytes = bytes ?? await file!.readAsBytes();
Expand Down Expand Up @@ -203,7 +204,8 @@ class SendMessageHelper {
attachmentResult.keys,
attachmentResult.digest,
);
_addSendingJob(await _jobDao.createSendingJob(messageId, conversationId));
_addSendingJob(await _jobDao.createSendingJob(messageId, conversationId,
silent: silent));
}

Future<void> sendVideoMessage(
Expand All @@ -217,6 +219,7 @@ class SendMessageHelper {
int? mediaHeight,
String? thumbImage,
String? mediaDuration,
bool silent = false,
}) async {
final messageId = const Uuid().v4();
final mimeType = file.mimeType ?? lookupMimeType(file.path) ?? 'video/mp4';
Expand Down Expand Up @@ -281,7 +284,11 @@ class SendMessageHelper {
attachmentResult.keys,
attachmentResult.digest,
);
_addSendingJob(await _jobDao.createSendingJob(messageId, conversationId));
_addSendingJob(await _jobDao.createSendingJob(
messageId,
conversationId,
silent: silent,
));
}

Future<void> sendStickerMessage(String conversationId, String senderId,
Expand Down Expand Up @@ -314,6 +321,7 @@ class SendMessageHelper {
String? quoteMessageId, {
AttachmentResult? attachmentResult,
String? name,
bool silent = false,
}) async {
final messageId = const Uuid().v4();
final mimeType = file.mimeType ??
Expand Down Expand Up @@ -377,7 +385,11 @@ class SendMessageHelper {
attachmentResult.keys,
attachmentResult.digest,
);
_addSendingJob(await _jobDao.createSendingJob(messageId, conversationId));
_addSendingJob(await _jobDao.createSendingJob(
messageId,
conversationId,
silent: silent,
));
}

Future<void> sendContactMessage(
Expand Down
52 changes: 37 additions & 15 deletions lib/ui/home/chat/files_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import '../../../widgets/buttons.dart';
import '../../../widgets/cache_image.dart';
import '../../../widgets/dash_path_border.dart';
import '../../../widgets/dialog.dart';
import '../../../widgets/menu.dart';
import '../bloc/conversation_cubit.dart';
import '../bloc/quote_message_cubit.dart';
import 'image_editor.dart';
Expand Down Expand Up @@ -127,11 +128,15 @@ class _FilesPreviewDialog extends HookWidget {
showAsBigImage.value = hasImage && currentTab.value == _TabType.image;
}, [hasImage, currentTab.value]);

Future<void> send() async {
Future<void> send(bool silent) async {
if (currentTab.value != _TabType.zip) {
for (final file in files.value) {
unawaited(
_sendFile(context, file, quoteMessageCubit?.state?.messageId));
unawaited(_sendFile(
context,
file,
quoteMessageCubit?.state?.messageId,
silent: silent,
));
}
quoteMessageCubit?.emit(null);
Navigator.pop(context);
Expand All @@ -144,6 +149,7 @@ class _FilesPreviewDialog extends HookWidget {
context,
await _File.createFromPath(zipFilePath),
quoteMessageCubit?.state?.messageId,
silent: silent,
));
quoteMessageCubit?.emit(null);
Navigator.pop(context);
Expand Down Expand Up @@ -193,7 +199,7 @@ class _FilesPreviewDialog extends HookWidget {
const SizedBox(height: 4),
Expanded(
child: _FileInputOverlay(
onSend: send,
onSend: () => send(false),
onFileAdded: (fileAdded) {
final currentFiles =
files.value.map((e) => e.path).toSet();
Expand Down Expand Up @@ -239,16 +245,25 @@ class _FilesPreviewDialog extends HookWidget {
),
const SizedBox(height: 32),
Align(
child: ElevatedButton(
onPressed: send,
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.only(
left: 32, top: 18, bottom: 18, right: 32),
backgroundColor: context.theme.accent,
),
child: Text(
context.l10n.send.toUpperCase(),
style: const TextStyle(color: Colors.white),
child: ContextMenuPortalEntry(
buildMenus: () => [
ContextMenu(
icon: Resources.assetsImagesContextMenuMuteSvg,
title: context.l10n.sendWithoutSound,
onTap: () => send(true),
)
],
child: ElevatedButton(
onPressed: () => send(false),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.only(
left: 32, top: 18, bottom: 18, right: 32),
backgroundColor: context.theme.accent,
),
child: Text(
context.l10n.send.toUpperCase(),
style: const TextStyle(color: Colors.white),
),
),
),
),
Expand Down Expand Up @@ -289,7 +304,11 @@ Future<String> _archiveFiles(List<String> paths) async {
}

Future<void> _sendFile(
BuildContext context, _File file, String? quoteMessageId) async {
BuildContext context,
_File file,
String? quoteMessageId, {
required bool silent,
}) async {
final conversationItem = context.read<ConversationCubit>().state;
if (conversationItem == null) return;
final xFile = file.file;
Expand All @@ -300,6 +319,7 @@ Future<void> _sendFile(
conversationId: conversationItem.conversationId,
recipientId: conversationItem.userId,
quoteMessageId: quoteMessageId,
silent: silent,
);
} else if (xFile.isVideo) {
return context.accountServer.sendVideoMessage(
Expand All @@ -308,6 +328,7 @@ Future<void> _sendFile(
conversationId: conversationItem.conversationId,
recipientId: conversationItem.userId,
quoteMessageId: quoteMessageId,
silent: silent,
);
}
await context.accountServer.sendDataMessage(
Expand All @@ -316,6 +337,7 @@ Future<void> _sendFile(
conversationId: conversationItem.conversationId,
recipientId: conversationItem.userId,
quoteMessageId: quoteMessageId,
silent: silent,
);
}

Expand Down
24 changes: 12 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.2"
common_crypto:
dependency: "direct main"
description:
Expand Down Expand Up @@ -908,10 +908,10 @@ packages:
dependency: "direct main"
description:
name: intl
sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.0"
version: "0.18.1"
intl_phone_number_input:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1052,18 +1052,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.16"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -1556,10 +1556,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
sprintf:
dependency: transitive
description:
Expand Down Expand Up @@ -1668,10 +1668,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.0"
timezone:
dependency: transitive
description:
Expand Down

0 comments on commit 35128c4

Please sign in to comment.