Skip to content

Commit

Permalink
TW-423: Update UI when send file on web version
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored and hoangdat committed Aug 18, 2023
1 parent 3160796 commit 6efe010
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 76 deletions.
2 changes: 1 addition & 1 deletion lib/di/global/get_it_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class GetItInitializer {
}

void bindingQueue() {
getIt.registerSingleton<Queue>(Queue());
getIt.registerFactory<Queue>(() => Queue());
}

void bindingAPI() {
Expand Down
12 changes: 4 additions & 8 deletions lib/domain/usecase/send_file_on_web_interactor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@ class SendFileOnWebInteractor {
Map<String, dynamic>? extraContent,
}) async {
try {
final fileInfos = filePickerResult.files
final matrixFiles = filePickerResult.files
.map(
(xFile) => MatrixFile(
bytes: xFile.bytes,
name: xFile.name,
filePath: '',
),
)
.toList();

final txIdMapToImageInfo =
await room.sendPlaceholdersWebForImages(entities: fileInfos);

for (final txId in txIdMapToImageInfo.value1.keys) {
for (final matrixFile in matrixFiles) {
await room.sendFileOnWebEvent(
txIdMapToImageInfo.value1[txId]!,
fakeImageEvent: txIdMapToImageInfo.value2[txId],
matrixFile,
txid: txId,
);
room.clearOlderImagesCacheInRoom(txId: txId);
}
} catch (error) {
Logs().d("SendFileInteractor: execute(): $error");
Expand Down
19 changes: 18 additions & 1 deletion lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class ChatController extends State<Chat> with ImagePickerMixin, SendFilesMixin {
prefs.remove('draft_$roomId');
var parseCommands = true;

final commandMatch = RegExp(r'^\/(\w+)').firstMatch(sendController.text);
final commandMatch = RegExp(r'^/(\w+)').firstMatch(sendController.text);
if (commandMatch != null &&
!room!.client.commands.keys.contains(commandMatch[1]!.toLowerCase())) {
final l10n = L10n.of(context)!;
Expand Down Expand Up @@ -1249,6 +1249,23 @@ class ChatController extends State<Chat> with ImagePickerMixin, SendFilesMixin {
editEvent = null;
});

void onSendFileClick() async {
if (PlatformInfos.isMobile) {
showImagesPickerBottomSheetAction(
room: room,
context: context,
onItemAction: (action) => onClickItemAction(
action: action,
room: room,
context: context,
),
onSendTap: () => sendImages(room: room),
);
} else {
sendFileOnWebAction(context, room: room);
}
}

@override
Widget build(BuildContext context) {
return ChatView(this, key: widget.key);
Expand Down
14 changes: 1 addition & 13 deletions lib/pages/chat/chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,7 @@ class ChatInputRow extends StatelessWidget {
tooltip: L10n.of(context)!.more,
margin: const EdgeInsets.only(right: 4.0),
icon: Icons.add_circle_outline,
onPressed: () async {
controller.showImagesPickerBottomSheetAction(
room: controller.room,
context: context,
onItemAction: (action) => controller.onClickItemAction(
action: action,
room: controller.room,
context: context,
),
onSendTap: () =>
controller.sendImages(room: controller.room),
);
},
onPressed: controller.onSendFileClick,
),
if (controller.matrix!.isMultiAccount &&
controller.matrix!.hasComplexBundles &&
Expand Down
23 changes: 12 additions & 11 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:fluffychat/pages/chat/chat.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pages/bootstrap/bootstrap_dialog.dart';
import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:fluffychat/pages/chat/events/sending_image_widget.dart';
import 'package:fluffychat/pages/chat/events/send_image_info_widget.dart';
import 'package:fluffychat/pages/chat/events/sending_video_widget.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/string_extension.dart';
Expand Down Expand Up @@ -131,16 +131,17 @@ class MessageContent extends StatelessWidget {
child: Icon(Icons.error, color: Colors.red),
),
);
}
final matrixFile = event.getMatrixFile();
if (matrixFile != null &&
matrixFile.filePath != null &&
matrixFile is MatrixImageFile) {
return SendingImageWidget(
matrixFile: matrixFile,
event: event,
onTapPreview: onTapPreview,
);
} else if (event.status == EventStatus.sending) {
final matrixFile = event.getMatrixFile();
if (matrixFile != null &&
matrixFile.filePath != null &&
matrixFile is MatrixImageFile) {
return SendImageInfoWidget(
matrixFile: matrixFile,
event: event,
onTapPreview: onTapPreview,
);
}
}
return ImageBubble(
event,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:io';

import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:fluffychat/pages/image_viewer/image_viewer.dart';
import 'package:flutter/material.dart';
import 'package:linagora_design_flutter/colors/linagora_ref_colors.dart';
import 'package:matrix/matrix.dart' hide Visibility;

class SendingImageWidget extends StatelessWidget {
SendingImageWidget({
class SendImageInfoWidget extends StatelessWidget {
SendImageInfoWidget({
super.key,
required this.matrixFile,
required this.event,
Expand Down
47 changes: 8 additions & 39 deletions lib/presentation/extensions/send_file_web_extension.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import 'dart:collection';

import 'package:dartz/dartz.dart' hide id;
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/presentation/extensions/send_file_extension.dart';
import 'package:matrix/matrix.dart';

extension SendFileWebExtension on Room {
Expand Down Expand Up @@ -46,22 +41,26 @@ extension SendFileWebExtension on Room {
}

EncryptedFile? encryptedFile;
MatrixFile? uploadFile;
if (encrypted && client.fileEncryptionEnabled) {
fakeImageEvent.rooms!.join!.values.first.timeline!.events!.first
.unsigned![fileSendingStatusKey] = FileSendingStatus.encrypting.name;
await handleImageFakeSync(fakeImageEvent);
encryptedFile = await file.encrypt();
uploadFile =
MatrixFile.fromMimeType(bytes: encryptedFile!.data, name: 'crypt');
}
Uri? uploadResp;

fakeImageEvent.rooms!.join!.values.first.timeline!.events!.first
.unsigned![fileSendingStatusKey] = FileSendingStatus.uploading.name;
while (uploadResp == null && file.bytes != null) {
while (
uploadResp == null && uploadFile != null && uploadFile.bytes != null) {
try {
uploadResp = await client.uploadContent(
file.bytes!,
filename: file.name,
contentType: file.mimeType,
uploadFile.bytes!,
filename: uploadFile.name,
contentType: uploadFile.mimeType,
);
} on MatrixException catch (e) {
fakeImageEvent.rooms!.join!.values.first.timeline!.events!.first
Expand Down Expand Up @@ -176,34 +175,4 @@ extension SendFileWebExtension on Room {
await client.handleSync(fakeImageEvent, direction: direction);
}
}

void clearOlderImagesCacheInRoom({String? txId}) {
final imageCacheQueue = getIt.get<Queue>();
// clear older image cache
while (imageCacheQueue.length >= maxImagesCacheInRoom) {
if (sendingFilePlaceholders.containsKey(txId)) {
sendingFilePlaceholders.remove(txId);
}
if (sendingFileThumbnails.containsKey(txId)) {
sendingFileThumbnails.remove(txId);
}
}
}

Future<
Tuple2<Map<TransactionId, MatrixFile>,
Map<TransactionId, FakeImageEvent>>> sendPlaceholdersWebForImages({
required List<MatrixFile> entities,
}) async {
// ignore: prefer_const_constructors
final txIdMapToImageFile = Tuple2<Map<TransactionId, MatrixFile>,
Map<TransactionId, FakeImageEvent>>({}, {});
for (final entity in entities) {
final txid = client.generateUniqueTransactionId();
final fakeImageEvent = await sendFakeImageEvent(entity, txid: txid);
txIdMapToImageFile.value1[txid] = entity;
txIdMapToImageFile.value2[txid] = fakeImageEvent;
}
return txIdMapToImageFile;
}
}
15 changes: 15 additions & 0 deletions lib/presentation/mixins/send_files_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:file_picker/file_picker.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/usecase/send_file_interactor.dart';
import 'package:fluffychat/domain/usecase/send_file_on_web_interactor.dart';
import 'package:fluffychat/domain/usecase/send_image_interactor.dart';
import 'package:fluffychat/domain/usecase/send_images_interactor.dart';
import 'package:fluffychat/pages/chat/chat_actions.dart';
Expand Down Expand Up @@ -57,6 +58,20 @@ mixin SendFilesMixin on ImagePickerMixin {
sendFileInteractor.execute(room: room!, filePickerResult: result!);
}

void sendFileOnWebAction(
BuildContext context, {
Room? room,
}) async {
if (room == null) {}
final sendFileOnWebInteractor = getIt.get<SendFileOnWebInteractor>();
final result = await FilePicker.platform.pickFiles(
withData: true,
);
if (result == null && result?.files.isEmpty == true) return;

sendFileOnWebInteractor.execute(room: room!, filePickerResult: result!);
}

void onClickItemAction({
required BuildContext context,
Room? room,
Expand Down

0 comments on commit 6efe010

Please sign in to comment.