Skip to content

Commit

Permalink
Added widget test for message_avatar_mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim committed Dec 17, 2024
1 parent f7a7f81 commit 6cbdae8
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 44 deletions.
52 changes: 9 additions & 43 deletions lib/pages/chat/events/message/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import 'package:fluffychat/utils/date_time_extension.dart';
import 'package:fluffychat/utils/extension/event_status_custom_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/filtered_timeline_extension.dart';
import 'package:fluffychat/presentation/mixins/message_avatar_mixin.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:fluffychat/widgets/context_menu/context_menu_action.dart';
import 'package:fluffychat/widgets/swipeable.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -99,7 +99,7 @@ class Message extends StatefulWidget {
State<Message> createState() => _MessageState();
}

class _MessageState extends State<Message> {
class _MessageState extends State<Message> with MessageAvatarMixin {
InViewState? inViewState;

final inviewNotifier = ValueNotifier(false);
Expand Down Expand Up @@ -187,10 +187,13 @@ class _MessageState extends State<Message> {
: MainAxisAlignment.start;

final rowChildren = <Widget>[
_placeHolderWidget(
widget.event.isSameSenderWith(widget.previousEvent),
widget.event.isOwnMessage,
widget.event,
placeHolderWidget(
widget.onAvatarTap,
event: widget.event,
sameSender: widget.event.isSameSenderWith(widget.previousEvent),
ownMessage: widget.event.isOwnMessage,
context: context,
selectMode: widget.selectMode,
),
Expanded(
child: MessageContentWithTimestampBuilder(
Expand Down Expand Up @@ -311,43 +314,6 @@ class _MessageState extends State<Message> {
);
}

bool shouldDisplayAvatar(bool sameSender, bool ownMessage) {
return sameSender &&
(!ownMessage || !Message.responsiveUtils.isMobile(context));
}

Widget _placeHolderWidget(bool sameSender, bool ownMessage, Event event) {
if (widget.selectMode ||
(event.room.isDirectChat &&
Message.responsiveUtils.isMobile(context))) {
return const SizedBox();
}

if (shouldDisplayAvatar(sameSender, ownMessage)) {
return Padding(
padding: MessageStyle.paddingAvatar,
child: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final user = snapshot.data ?? event.senderFromMemoryOrFallback;
return Avatar(
size: MessageStyle.avatarSize,
fontSize: MessageStyle.fontSize,
mxContent: user.avatarUrl,
name: user.calcDisplayname(),
onTap: () => widget.onAvatarTap!(event),
);
},
),
);
}

return const Padding(
padding: MessageStyle.paddingAvatar,
child: SizedBox(width: MessageStyle.avatarSize),
);
}

Widget _messageSelectedWidget(
BuildContext context,
Widget child,
Expand Down
56 changes: 56 additions & 0 deletions lib/presentation/mixins/message_avatar_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/pages/chat/events/message/message_style.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';

mixin MessageAvatarMixin {
ResponsiveUtils responsive = getIt.get<ResponsiveUtils>();

bool _shouldDisplayAvatar(
bool sameSender,
bool ownMessage,
BuildContext context,
) {
return sameSender && !(ownMessage && responsive.isMobile(context));
}

Widget placeHolderWidget(
Function(Event)? onAvatarTap, {
required bool sameSender,
required bool ownMessage,
required Event event,
required BuildContext context,
required bool selectMode,
}) {
if (selectMode ||
(event.room.isDirectChat && responsive.isMobile(context))) {
return const SizedBox();
}

if (_shouldDisplayAvatar(sameSender, ownMessage, context)) {
return Padding(
padding: MessageStyle.paddingAvatar,
child: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final user = snapshot.data ?? event.senderFromMemoryOrFallback;
return Avatar(
size: MessageStyle.avatarSize,
fontSize: MessageStyle.fontSize,
mxContent: user.avatarUrl,
name: user.calcDisplayname(),
onTap: () => onAvatarTap!(event),
);
},
),
);
}

return const Padding(
padding: MessageStyle.paddingAvatar,
child: SizedBox(width: MessageStyle.avatarSize),
);
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ packages:
description:
path: "."
ref: "twake-supported-0.22.6"
resolved-ref: "04ec6f3b9ece8e64e031cbe5c6e8164dc2ec7f2d"
resolved-ref: "58585a19abc4693d96eab4f8dad9c56bf8699cc1"
url: "[email protected]:linagora/matrix-dart-sdk.git"
source: git
version: "0.22.6"
Expand Down
Loading

0 comments on commit 6cbdae8

Please sign in to comment.