Skip to content

Commit

Permalink
TW-823: refactor share file flow with video and image
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Oct 17, 2023
1 parent 4ed5210 commit 52c1ed8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
10 changes: 0 additions & 10 deletions lib/data/network/extensions/file_info_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,4 @@ extension FileInfoExtension on FileInfo {
}
return MessageTypes.File;
}

FileInfo get detectFileType {
if (msgType == MessageTypes.Image) {
return ImageFileInfo(fileName, filePath, fileSize);
}
if (msgType == MessageTypes.Video) {
return VideoFileInfo(fileName, filePath, fileSize);
}
return FileInfo(fileName, filePath, fileSize, readStream: readStream);
}
}
12 changes: 12 additions & 0 deletions lib/domain/model/room/room_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ extension RoomExtension on Room {
Future<void> unmute() async {
await setPushRuleState(PushRuleState.notify);
}

String storePlaceholderFileInMem({
required FileInfo fileInfo,
String? txid,
}) {
txid ??= client.generateUniqueTransactionId();
final matrixFile = MatrixFile.fromFileInfo(
fileInfo: fileInfo,
);
sendingFilePlaceholders[txid] = matrixFile;
return txid;
}
}
18 changes: 2 additions & 16 deletions lib/domain/usecase/send_file_interactor.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fluffychat/data/network/extensions/file_info_extension.dart';
import 'package:fluffychat/domain/model/room/room_extension.dart';
import 'package:fluffychat/presentation/extensions/send_file_extension.dart';
import 'package:matrix/matrix.dart';

Expand All @@ -14,9 +15,8 @@ class SendFileInteractor {
}) async {
try {
for (final fileInfo in fileInfos) {
final txid = _storePlaceholderFileInMem(
final txid = room.storePlaceholderFileInMem(
fileInfo: fileInfo,
room: room,
);
await room.sendFileEvent(
fileInfo,
Expand All @@ -31,18 +31,4 @@ class SendFileInteractor {
Logs().d("SendFileInteractor: execute(): $error");
}
}

String _storePlaceholderFileInMem({
required Room room,
required FileInfo fileInfo,
}) {
final txid = room.client.generateUniqueTransactionId();
final matrixFile = MatrixFile.fromMimeType(
name: fileInfo.fileName,
filePath: fileInfo.filePath,
mimeType: fileInfo.mimeType,
);
room.sendingFilePlaceholders[txid] = matrixFile;
return txid;
}
}
6 changes: 3 additions & 3 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:debounce_throttle/debounce_throttle.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:emoji_picker_flutter/emoji_picker_flutter.dart';
import 'package:fluffychat/data/network/extensions/file_info_extension.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_failure.dart';
import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_loading.dart';
Expand Down Expand Up @@ -322,8 +321,9 @@ class ChatController extends State<Chat>
sendFileInteractor.execute(
room: room!,
fileInfos: [
FileInfo(shareFile!.name, shareFile!.filePath!, shareFile!.size)
.detectFileType,
FileInfo.fromMatrixFile(
shareFile!,
)
],
);
}
Expand Down
15 changes: 8 additions & 7 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ class _MessageImageBuilder extends StatelessWidget {
file.height?.toDouble() ?? MessageContentStyle.imageHeight(context),
).getDisplayImageInfo(context);
return SendingImageInfoWidget(
key: ValueKey(event.eventId),
matrixFile: file,
event: event,
onTapPreview: onTapPreview,
Expand Down Expand Up @@ -387,10 +388,12 @@ class _MessageVideoBuilder extends StatelessWidget {
}
if (isSendingVideo(matrixFile)) {
final file = matrixFile as MatrixVideoFile;
displayImageInfo = Size(
file.width!.toDouble(),
file.height!.toDouble(),
).getDisplayImageInfo(context);
if (file.width != null && file.height != null) {
displayImageInfo = Size(
file.width!.toDouble(),
file.height!.toDouble(),
).getDisplayImageInfo(context);
}
return SendingVideoWidget(
key: ValueKey(event.eventId),
event: event,
Expand All @@ -413,8 +416,6 @@ class _MessageVideoBuilder extends StatelessWidget {
}

bool isSendingVideo(MatrixFile? matrixFile) {
return matrixFile is MatrixVideoFile &&
matrixFile.width != null &&
matrixFile.height != null;
return matrixFile is MatrixVideoFile && matrixFile.bytes != null;
}
}
2 changes: 1 addition & 1 deletion lib/pages/chat_list/receive_sharing_intent_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mixin ReceiveSharingIntentMixin<T extends StatefulWidget> on State<T> {
'file': MatrixFile(
name: file.path,
filePath: file.path,
).detectFileType,
).fromSharedMediaFile(shareFile),
};
TwakeApp.router.go('/share');
}
Expand Down
29 changes: 29 additions & 0 deletions lib/utils/matrix_sdk_extensions/matrix_file_extension.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
Expand All @@ -8,6 +9,7 @@ import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:image_picker/image_picker.dart';
import 'package:matrix/matrix.dart';
import 'package:mime/mime.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:share_plus/share_plus.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/size_string.dart';
Expand Down Expand Up @@ -105,5 +107,32 @@ extension MatrixFileExtension on MatrixFile {
return this;
}

MatrixFile fromSharedMediaFile(SharedMediaFile file) {
if (file.type == SharedMediaType.IMAGE) {
return MatrixImageFile(
bytes: null,
name: file.path.split("/").last,
filePath: file.path,
);
}
if (file.type == SharedMediaType.VIDEO) {
Uint8List? thumbnailBytes;
if (file.thumbnail != null) {
thumbnailBytes = File(file.thumbnail!).readAsBytesSync();
}
return MatrixVideoFile(
bytes: thumbnailBytes,
name: file.path.split("/").last,
filePath: file.path,
duration: file.duration,
);
}
return MatrixFile(
bytes: null,
name: file.path.split("/").last,
filePath: file.path,
);
}

String get sizeString => size.sizeString;
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "feat/file-info-from-mime-type"
resolved-ref: "47cc0a78152ed1c8e429bbb752bd084781737bd4"
ref: "feat/add-factory-method-for-file-info-and-matrix-file"
resolved-ref: bc620ba66667c7540ae6c2e49480c96e7a654185
url: "[email protected]:linagora/matrix-dart-sdk.git"
source: git
version: "0.22.4"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dependencies:
matrix:
git:
url: [email protected]:linagora/matrix-dart-sdk.git
ref: feat/file-info-from-mime-type
ref: feat/add-factory-method-for-file-info-and-matrix-file
matrix_homeserver_recommendations: ^0.3.0
matrix_link_text: ^2.0.0
native_imaging: ^0.1.0
Expand Down

0 comments on commit 52c1ed8

Please sign in to comment.