Skip to content

Commit 2e41637

Browse files
authored
chore: clean up message code (#20)
1 parent 58c897d commit 2e41637

File tree

11 files changed

+375
-415
lines changed

11 files changed

+375
-415
lines changed

lib/chat/chat_room/common/view/chat_room_pinned_events_dialog.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import 'package:flutter/material.dart';
22
import 'package:matrix/matrix.dart';
33
import 'package:watch_it/watch_it.dart';
4+
import 'package:yaru/yaru.dart';
45

6+
import '../../../../common/view/ui_constants.dart';
7+
import '../../../../l10n/l10n.dart';
58
import '../../../common/chat_model.dart';
69
import '../../../events/view/chat_event_tile.dart';
710

@@ -23,10 +26,18 @@ class ChatRoomPinnedEventsDialog extends StatelessWidget with WatchItMixin {
2326
).data ??
2427
[];
2528
return AlertDialog(
29+
titlePadding: EdgeInsets.zero,
30+
title: YaruDialogTitleBar(
31+
title: Text(context.l10n.pin),
32+
backgroundColor: Colors.transparent,
33+
border: BorderSide.none,
34+
),
35+
contentPadding: const EdgeInsets.symmetric(vertical: kBigPadding),
2636
content: SizedBox(
2737
height: 400,
2838
width: 400,
2939
child: ListView.builder(
40+
padding: const EdgeInsets.symmetric(horizontal: kBigPadding),
3041
itemCount: pinnedEvents.length,
3142
itemBuilder: (context, index) => ChatRoomPinnedEventTile(
3243
eventId: pinnedEvents.elementAt(index),

lib/chat/chat_room/common/view/chat_typing_indicator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ class ChatTypingIndicator extends StatelessWidget with WatchItMixin {
4949
),
5050
),
5151
Material(
52-
color: getMonochromeBg(theme: theme, factor: 3, darkFactor: 10),
52+
color: getMonochromeBg(theme: theme, factor: 6, darkFactor: 15),
5353
borderRadius: const BorderRadius.all(
5454
Radius.circular(30),
5555
),
56+
elevation: 0.1,
5657
child: Padding(
5758
padding: const EdgeInsets.symmetric(
5859
horizontal: kSmallPadding,
@@ -107,8 +108,7 @@ class __TypingDotsState extends State<_TypingDots> {
107108
final theme = context.theme;
108109
const size = kTypingAvatarSize / 3;
109110

110-
return Padding(
111-
padding: const EdgeInsets.symmetric(vertical: kSmallPadding),
111+
return Center(
112112
child: Row(
113113
mainAxisSize: MainAxisSize.min,
114114
children: [

lib/chat/chat_room/input/view/chat_input.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class _ChatInputState extends State<ChatInput> {
229229
),
230230
),
231231
Positioned(
232-
top: -kTypingAvatarSize + kSmallPadding,
232+
top: -kTypingAvatarSize - kSmallPadding,
233233
child: ChatTypingIndicator(room: widget.room),
234234
),
235235
],

lib/chat/common/event_x.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ extension EventX on Event {
2525
required bool showAvatarChanges,
2626
required bool showDisplayNameChanges,
2727
}) {
28+
if (type == 'm.room.server_acl') {
29+
return true;
30+
}
2831
if (type == EventTypes.RoomPinnedEvents) {
2932
return true;
3033
}

lib/chat/events/view/chat_event_tile.dart

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import '../../../common/view/ui_constants.dart';
55
import '../../common/event_x.dart';
66
import '../../common/view/chat_avatar.dart';
77
import '../../common/view/chat_profile_dialog.dart';
8-
import 'chat_image.dart';
98
import 'chat_message_badge.dart';
109
import 'chat_message_bubble.dart';
11-
import 'chat_message_image_full_screen_dialog.dart';
1210

1311
class ChatEventTile extends StatelessWidget {
1412
const ChatEventTile({
@@ -26,57 +24,32 @@ class ChatEventTile extends StatelessWidget {
2624

2725
@override
2826
Widget build(BuildContext context) {
29-
if (event.type == EventTypes.RoomMember) {
27+
if (event.showAsBadge) {
3028
return ChatMessageBadge(
3129
displayEvent: event.getDisplayEvent(timeline),
32-
leading: Padding(
33-
padding: const EdgeInsets.only(right: kSmallPadding),
34-
child: ChatAvatar(
35-
fallBackIconSize: 10,
36-
dimension: 15,
37-
avatarUri: event.senderFromMemoryOrFallback.avatarUrl,
38-
onTap: () => showDialog(
39-
context: context,
40-
builder: (context) => ChatProfileDialog(userId: event.senderId),
41-
),
42-
),
43-
),
30+
leading: event.type == EventTypes.RoomMember
31+
? Padding(
32+
padding: const EdgeInsets.only(right: kSmallPadding),
33+
child: ChatAvatar(
34+
fallBackIconSize: 10,
35+
dimension: 15,
36+
avatarUri: event.senderFromMemoryOrFallback.avatarUrl,
37+
onTap: () => showDialog(
38+
context: context,
39+
builder: (context) =>
40+
ChatProfileDialog(userId: event.senderId),
41+
),
42+
),
43+
)
44+
: null,
4445
);
4546
}
46-
if (event.showAsBadge) {
47-
return ChatMessageBadge(displayEvent: event.getDisplayEvent(timeline));
48-
}
49-
return switch (event.messageType) {
50-
MessageTypes.Image => event.isSvgImage
51-
? ChatMessageBubble(
52-
event: event,
53-
timeline: timeline,
54-
onReplyOriginClick: onReplyOriginClick,
55-
)
56-
: ChatImage(
57-
timeline: timeline,
58-
event: event,
59-
onTap: () => showDialog(
60-
context: context,
61-
builder: (context) =>
62-
ChatMessageImageFullScreenDialog(event: event),
63-
),
64-
),
65-
MessageTypes.Location ||
66-
MessageTypes.File ||
67-
MessageTypes.Video ||
68-
MessageTypes.Audio ||
69-
MessageTypes.Text ||
70-
MessageTypes.Notice ||
71-
MessageTypes.Emote ||
72-
MessageTypes.BadEncrypted =>
73-
ChatMessageBubble(
74-
event: event,
75-
timeline: timeline,
76-
onReplyOriginClick: onReplyOriginClick,
77-
partOfMessageCohort: partOfMessageCohort,
78-
),
79-
_ => ChatMessageBadge(displayEvent: event.getDisplayEvent(timeline))
80-
};
47+
48+
return ChatMessageBubble(
49+
event: event,
50+
timeline: timeline,
51+
onReplyOriginClick: onReplyOriginClick,
52+
partOfMessageCohort: partOfMessageCohort,
53+
);
8154
}
8255
}

lib/chat/events/view/chat_image.dart

Lines changed: 53 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import 'dart:typed_data';
22

33
import 'package:flutter/material.dart';
4+
import 'package:flutter_svg/svg.dart';
45
import 'package:matrix/matrix.dart';
56
import 'package:watch_it/watch_it.dart';
67
import 'package:yaru/yaru.dart';
78

89
import '../../../common/view/image_shimmer.dart';
910
import '../../../common/view/ui_constants.dart';
10-
import '../../common/chat_model.dart';
11+
import '../../common/event_x.dart';
1112
import '../../common/local_image_model.dart';
1213
import '../chat_download_model.dart';
13-
import 'chat_event_status_icon.dart';
1414
import 'chat_message_attachment_indicator.dart';
15-
import 'chat_message_menu.dart';
16-
import 'chat_message_reactions.dart';
1715

1816
class ChatImage extends StatelessWidget with WatchItMixin {
1917
const ChatImage({
@@ -45,109 +43,56 @@ class ChatImage extends StatelessWidget with WatchItMixin {
4543
final theHeight = dimension ?? height ?? imageHeight;
4644
final theWidth = dimension ?? width;
4745
final theFit = fit ?? BoxFit.cover;
48-
final isUserMessage = di<ChatModel>().isUserEvent(event);
4946
final maybeImage =
5047
watchPropertyValue((LocalImageModel m) => m.get(event.eventId));
5148

5249
return Padding(
53-
padding: EdgeInsets.only(
54-
top: kBigPadding,
55-
bottom: kBigPadding,
56-
right: kSmallPadding,
57-
left: (isUserMessage ? 0 : kAvatarDefaultSize + kBigPadding),
58-
),
59-
child: Align(
60-
alignment: isUserMessage ? Alignment.centerRight : Alignment.centerLeft,
61-
child: ClipRRect(
62-
borderRadius: const BorderRadius.all(kBigBubbleRadius),
63-
child: Stack(
64-
children: [
65-
ChatMessageMenu(
66-
event: event,
67-
child: SizedBox(
68-
height: theHeight,
69-
width: theWidth,
70-
child: maybeImage != null
71-
? Image.memory(
50+
padding: const EdgeInsets.symmetric(vertical: kSmallPadding),
51+
child: ClipRRect(
52+
borderRadius: const BorderRadius.all(kBigBubbleRadius),
53+
child: Stack(
54+
children: [
55+
SizedBox(
56+
height: theHeight,
57+
width: theWidth,
58+
child: maybeImage != null
59+
? event.isSvgImage
60+
? SvgPicture.memory(
7261
maybeImage,
7362
fit: theFit,
7463
height: theHeight,
7564
width: theWidth,
7665
)
77-
: ChatImageFuture(
78-
event: event,
79-
width: theWidth,
80-
height: theHeight,
66+
: Image.memory(
67+
maybeImage,
8168
fit: theFit,
82-
),
83-
),
84-
),
85-
if (!event.redacted)
86-
Positioned(
87-
left: kSmallPadding,
88-
bottom: kSmallPadding,
89-
child: ChatMessageReactions(
90-
key: ValueKey('${event.eventId}reactions'),
91-
event: event,
92-
timeline: timeline,
93-
),
94-
),
95-
Positioned(
96-
top: kSmallPadding,
97-
right: 10 * kSmallPadding,
98-
child: IconButton(
99-
style: IconButton.styleFrom(
100-
backgroundColor: Colors.black.withValues(alpha: 0.5),
101-
shape: const CircleBorder(),
102-
),
103-
onPressed: () => di<ChatDownloadModel>().safeFile(event),
104-
icon: ChatMessageAttachmentIndicator(
105-
event: event,
106-
iconSize: 22,
107-
color: Colors.white,
108-
),
109-
),
110-
),
111-
if (isUserMessage)
112-
Positioned(
113-
bottom: 0,
114-
right: 0,
115-
child: Container(
116-
padding:
117-
const EdgeInsets.symmetric(horizontal: kSmallPadding),
118-
margin: const EdgeInsets.all(kMediumPadding),
119-
decoration: BoxDecoration(
120-
borderRadius: BorderRadius.circular(kBigPadding),
121-
color: Colors.black.withValues(alpha: 0.5),
122-
),
123-
child: ChatEventStatusIcon(
124-
timeline: timeline,
125-
padding: const EdgeInsets.all(kTinyPadding),
126-
event: event,
127-
foregroundColor: Colors.white,
128-
),
129-
),
130-
)
131-
else
132-
Positioned(
133-
bottom: 0,
134-
right: 0,
135-
child: Container(
136-
padding:
137-
const EdgeInsets.symmetric(horizontal: kSmallPadding),
138-
margin: const EdgeInsets.all(kMediumPadding),
139-
decoration: BoxDecoration(
140-
borderRadius: BorderRadius.circular(kBigPadding),
141-
color: Colors.black.withValues(alpha: 0.5),
142-
),
143-
child: ChatEventStatusIcon(
144-
timeline: timeline,
145-
padding: const EdgeInsets.all(kTinyPadding),
69+
height: theHeight,
70+
width: theWidth,
71+
)
72+
: ChatImageFuture(
14673
event: event,
147-
foregroundColor: Colors.white,
74+
width: theWidth,
75+
height: theHeight,
76+
fit: theFit,
14877
),
149-
),
78+
),
79+
Positioned(
80+
top: kSmallPadding,
81+
right: onTap != null ? 10 * kSmallPadding : kSmallPadding,
82+
child: IconButton(
83+
style: IconButton.styleFrom(
84+
backgroundColor: Colors.black.withValues(alpha: 0.5),
85+
shape: const CircleBorder(),
15086
),
87+
onPressed: () => di<ChatDownloadModel>().safeFile(event),
88+
icon: ChatMessageAttachmentIndicator(
89+
event: event,
90+
iconSize: 22,
91+
color: Colors.white,
92+
),
93+
),
94+
),
95+
if (onTap != null)
15196
Positioned(
15297
top: kSmallPadding,
15398
right: kSmallPadding,
@@ -163,8 +108,7 @@ class ChatImage extends StatelessWidget with WatchItMixin {
163108
),
164109
),
165110
),
166-
],
167-
),
111+
],
168112
),
169113
),
170114
);
@@ -207,6 +151,19 @@ class _ChatImageFutureState extends State<ChatImageFuture> {
207151
builder: (context, snapshot) {
208152
if (snapshot.hasData) {
209153
final data = snapshot.data;
154+
155+
if (widget.event.isSvgImage ||
156+
widget.event.thumbnailMimetype == 'image/svg+xml' ||
157+
widget.event.attachmentMimetype.contains('svg') ||
158+
widget.event.thumbnailMimetype.contains('svg')) {
159+
SvgPicture.memory(
160+
data!,
161+
fit: widget.fit ?? BoxFit.contain,
162+
height: widget.height,
163+
width: widget.width,
164+
);
165+
}
166+
210167
return Image.memory(
211168
data!,
212169
fit: widget.fit,

0 commit comments

Comments
 (0)