Skip to content

Commit

Permalink
TW-131: send-one-image-without-caption
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Jun 18, 2023
1 parent c1d00f0 commit e55dd51
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 29 deletions.
4 changes: 3 additions & 1 deletion lib/config/routes.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/di/send_image/send_image_di.dart';
import 'package:fluffychat/pages/add_story/add_story.dart';
import 'package:fluffychat/pages/archive/archive.dart';
import 'package:fluffychat/pages/chat/chat.dart';
Expand Down Expand Up @@ -101,8 +102,9 @@ class AppRoutes {
stackedRoutes: _chatDetailsRoutes,
buildTransition: _rightToLeftTransition,
),
VWidget(
VWidgetWithDependency(
path: '/rooms/:roomid',
di: SendImageDi(),
widget: const Chat(),
buildTransition: _rightToLeftTransition,
stackedRoutes: [
Expand Down
13 changes: 13 additions & 0 deletions lib/di/send_image/send_image_di.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:fluffychat/di/base_di.dart';
import 'package:fluffychat/domain/usecase/send_image_interactor.dart';
import 'package:get_it/get_it.dart';

class SendImageDi extends BaseDI {
@override
String get scopeName => "Send image";

@override
void setUp(GetIt get) {
get.registerFactory<SendImageInteractor>(() => SendImageInteractor());
}
}
35 changes: 35 additions & 0 deletions lib/domain/usecase/send_image_interactor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import 'package:fluffychat/presentation/extensions/room_extension.dart';
import 'package:matrix/matrix.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:fluffychat/presentation/extensions/asset_entity_extension.dart';

class SendImageInteractor {
Future<void> execute({
required Room room,
required AssetEntity entity,
String? txId,
Event? inReplyTo,
String? editEventId,
int? shrinkImageMaxDimension,
MatrixImageFile? thumbnail,
Map<String, dynamic>? extraContent,
}) async {
final matrixFile = await entity.toMatrixFile();
if (matrixFile != null) {
try {
final mxcUri = await room.sendImageFileEvent(
matrixFile,
txid: txId,
editEventId: editEventId,
inReplyTo: inReplyTo,
shrinkImageMaxDimension: shrinkImageMaxDimension,
thumbnail: MatrixImageFile(bytes: matrixFile.bytes, name: matrixFile.name),
extraContent: extraContent,
);
} catch(error) {
Logs().d("SendImageInteractor: execute(): $error");
}
}
}
}
41 changes: 19 additions & 22 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'dart:async';
import 'dart:io';

import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/usecase/send_image_interactor.dart';
import 'package:fluffychat/pages/forward/forward.dart';
import 'package:fluffychat/utils/network_connection_service.dart';
import 'package:fluffychat/presentation/extensions/asset_entity_extension.dart';
import 'package:fluffychat/utils/voip/permission_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
Expand All @@ -16,16 +16,17 @@ import 'package:device_info_plus/device_info_plus.dart';
import 'package:emoji_picker_flutter/emoji_picker_flutter.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:image_picker/image_picker.dart';
import 'package:linagora_design_flutter/images_picker/images_picker.dart' hide ImagePicker;
import 'package:matrix/matrix.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:record/record.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/presentation/extensions/room_extension.dart';

import 'package:fluffychat/pages/chat/chat_view.dart';
import 'package:fluffychat/pages/chat/event_info_dialog.dart';
Expand All @@ -36,7 +37,6 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/ios_badge_client_extensio
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:photo_manager/photo_manager.dart';
import '../../utils/account_bundles.dart';
import '../../utils/localized_exception_extension.dart';
import '../../utils/matrix_sdk_extensions/matrix_file_extension.dart';
Expand Down Expand Up @@ -398,28 +398,25 @@ class ChatController extends State<Chat> {
);
}

Future<void> sendImage(IndexedAssetEntity entity) async {
final matrixFile = await entity.toMatrixFile();
if (matrixFile != null) {
await room!.sendFileEvent(
matrixFile,
thumbnail: null,
).catchError((e) {
Fluttertoast.showToast(
msg: "error: $e",
gravity: ToastGravity.BOTTOM,
);
return null;
});
void sendImage() {
final assetEntity = imagePickerController.selectedAssets.first;
final sendImageInteractor = getIt.get<SendImageInteractor>();
if (assetEntity.asset.type == AssetType.image) {
sendImageInteractor.execute(room: room!, entity: assetEntity.asset);
imagePickerController.clearAssetCounter();
numberSelectedImagesNotifier.value = 0;
}
}

Future<void> sendImages() async {
final selectedAssets = imagePickerController.sortedSelectedAssets;
for (final entity in selectedAssets) {
await sendImage(entity);
}
}
// Future<void> sendImages() async {
// final selectedAssets = imagePickerController.sortedSelectedAssets;
// for (final entity in selectedAssets) {
// await sendImage();
// }

// imagePickerController.clearAssetCounter();
// numberSelectedImagesNotifier.value = 0;
// }

void openCameraAction() async {
// Make sure the textfield is unfocused before opening the camera
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/chat/chat_input_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Future<void> showImagesPickerBottomSheet({
InkWell(
borderRadius: const BorderRadius.all(Radius.circular(100)),
onTap: () {
controller.sendImages();
controller.sendImage();
Navigator.of(context).pop();
},
child: SvgPicture.asset(
Expand Down
16 changes: 16 additions & 0 deletions lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:fluffychat/pages/chat/events/sending_image_widget.dart';
import 'package:fluffychat/widgets/twake_link_text.dart';
import 'package:flutter/material.dart';

Expand All @@ -9,6 +10,7 @@ import 'package:fluffychat/pages/chat/events/video_player.dart';
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
import 'package:fluffychat/utils/date_time_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../../../config/app_config.dart';
Expand Down Expand Up @@ -112,6 +114,20 @@ class MessageContent extends StatelessWidget {
case EventTypes.Sticker:
switch (event.messageType) {
case MessageTypes.Image:
if (event.status == EventStatus.error && event.messageType == MessageTypes.Image) {
return SizedBox(
width: MessageContentStyle.imageBubbleWidth,
height: MessageContentStyle.imageBubbleHeight,
child: const Center(
child: Icon(Icons.error, color: Colors.red,),
),
);
}

final sendingImageData = event.getSendingImageData();
if (sendingImageData != null) {
return SendingImageWidget(sendingImageData: sendingImageData);
}
return ImageBubble(
event,
width: MessageContentStyle.imageBubbleWidth,
Expand Down
27 changes: 27 additions & 0 deletions lib/pages/chat/events/sending_image_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:typed_data';

import 'package:fluffychat/pages/chat/events/message_content_style.dart';
import 'package:flutter/material.dart';

class SendingImageWidget extends StatelessWidget {
const SendingImageWidget({
super.key,
required this.sendingImageData,
});

final Uint8List sendingImageData;

@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(12.0),
child: Image.memory(
sendingImageData,
width: MessageContentStyle.imageBubbleWidth,
height: MessageContentStyle.imageBubbleHeight,
fit: BoxFit.cover,
filterQuality: FilterQuality.medium,
),
);
}
}
8 changes: 4 additions & 4 deletions lib/presentation/extensions/asset_entity_extension.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:linagora_design_flutter/images_picker/images_picker.dart';
import 'package:matrix/matrix.dart';
import 'package:photo_manager/photo_manager.dart';

extension AssetEntityExtension on IndexedAssetEntity {
extension AssetEntityExtension on AssetEntity {
Future<MatrixFile?> toMatrixFile() async {
final bytes = await asset.originBytes;
final bytes = await originBytes;
if (bytes != null && bytes.isNotEmpty) {
return MatrixFile(bytes: bytes, name: await asset.titleAsync);
return MatrixFile(bytes: bytes, name: title ?? await titleAsync);
}
return null;
}
Expand Down
Loading

0 comments on commit e55dd51

Please sign in to comment.