Skip to content

Commit 33f91dc

Browse files
committed
fix: reduce cpu usage
1 parent cfa9414 commit 33f91dc

16 files changed

+150
-165
lines changed

lib/chat/bootstrap/view/key_verification_dialog.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,17 @@ class KeyVerificationPageState extends State<KeyVerificationDialog> {
364364
child: Text(
365365
l10n.close,
366366
),
367-
onPressed: () => Navigator.of(context, rootNavigator: false).pop(),
367+
onPressed: () {
368+
Navigator.of(context, rootNavigator: false).pop();
369+
Navigator.of(context).pushAndRemoveUntil(
370+
MaterialPageRoute(
371+
builder: (_) => const ChatMasterDetailPage(
372+
checkBootstrap: false,
373+
),
374+
),
375+
(route) => false,
376+
);
377+
},
368378
),
369379
);
370380
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ChatRoomCreateOrEditAvatar extends StatelessWidget with WatchItMixin {
3232
(ChatModel m) => m.getJoinedRoomAvatarStream(room),
3333
initialValue: room?.avatar,
3434
).data;
35-
final foreGroundColor = yaru ? Colors.white : null;
36-
35+
final foreGroundColor =
36+
context.colorScheme.isLight ? Colors.black : Colors.white;
3737
final attachingAvatar =
3838
watchPropertyValue((DraftModel m) => m.attachingAvatar);
3939

@@ -101,9 +101,9 @@ class ChatRoomCreateOrEditAvatar extends StatelessWidget with WatchItMixin {
101101
color: foreGroundColor,
102102
),
103103
)
104-
: Icon(
104+
: const Icon(
105105
YaruIcons.pen,
106-
color: foreGroundColor,
106+
color: Colors.white,
107107
),
108108
),
109109
),

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'chat_room_info_drawer_topic.dart';
1717
import 'chat_room_users_list.dart';
1818
import '../../titlebar/chat_room_join_or_leave_button.dart';
1919

20-
class ChatRoomInfoDrawer extends StatelessWidget with WatchItMixin {
20+
class ChatRoomInfoDrawer extends StatelessWidget {
2121
const ChatRoomInfoDrawer({super.key, required this.room});
2222

2323
final Room room;
@@ -28,13 +28,7 @@ class ChatRoomInfoDrawer extends StatelessWidget with WatchItMixin {
2828
final theme = context.theme;
2929
final textTheme = theme.textTheme;
3030

31-
final ava = watchStream(
32-
(ChatModel m) => m.getJoinedRoomAvatarStream(room),
33-
initialValue: room.avatar,
34-
).data;
35-
3631
return Drawer(
37-
key: ValueKey(ava.toString()),
3832
child: SizedBox(
3933
width: kSideBarWith,
4034
child: Column(
@@ -44,9 +38,11 @@ class ChatRoomInfoDrawer extends StatelessWidget with WatchItMixin {
4438
AppBar(
4539
leadingWidth: 80,
4640
leading: Center(
47-
child: ChatAvatar(
48-
avatarUri: room.avatar,
49-
fallBackIcon: YaruIcons.users,
41+
child: ChatRoomInfoDrawerAvatar(
42+
key: ValueKey(
43+
'${room.id}${room.avatar}${room.isDirectChat},',
44+
),
45+
room: room,
5046
),
5147
),
5248
title: Center(
@@ -106,7 +102,12 @@ class ChatRoomInfoDrawer extends StatelessWidget with WatchItMixin {
106102
spacing: kBigPadding,
107103
mainAxisSize: MainAxisSize.min,
108104
children: [
109-
_Ava(room: room),
105+
ChatRoomInfoDrawerAvatar(
106+
key: ValueKey('${room.id}${room.avatar}'),
107+
dimension: 150.0,
108+
fallBackIconSize: 80,
109+
room: room,
110+
),
110111
SizedBox(
111112
width: 300,
112113
child: Column(
@@ -219,24 +220,29 @@ class ChatRoomInfoDrawer extends StatelessWidget with WatchItMixin {
219220
}
220221
}
221222

222-
class _Ava extends StatelessWidget with WatchItMixin {
223-
const _Ava({
223+
class ChatRoomInfoDrawerAvatar extends StatelessWidget with WatchItMixin {
224+
const ChatRoomInfoDrawerAvatar({
225+
super.key,
224226
required this.room,
227+
this.dimension,
228+
this.fallBackIconSize,
225229
});
226230

227231
final Room room;
232+
final double? dimension;
233+
final double? fallBackIconSize;
228234

229235
@override
230236
Widget build(BuildContext context) {
231237
final avatar = watchStream(
232238
(ChatModel m) => m.getJoinedRoomAvatarStream(room),
233239
initialValue: room.avatar,
234-
preserveState: false,
235240
).data;
236241
return ChatAvatar(
237242
avatarUri: avatar,
238-
dimension: 150,
239-
fallBackIconSize: 80,
243+
fallBackIcon: YaruIcons.users,
244+
dimension: dimension ?? kAvatarDefaultSize,
245+
fallBackIconSize: fallBackIconSize,
240246
);
241247
}
242248
}

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

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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';
54

65
import '../../../../common/view/build_context_x.dart';
76
import '../../../../common/view/common_widgets.dart';
@@ -10,12 +9,11 @@ import '../../../../common/view/snackbars.dart';
109
import '../../../../common/view/ui_constants.dart';
1110
import '../../../../l10n/l10n.dart';
1211
import '../../../common/chat_model.dart';
12+
import '../../input/view/chat_input.dart';
13+
import '../../titlebar/chat_room_title_bar.dart';
1314
import '../timeline_model.dart';
14-
import 'chat_room_default_background.dart';
1515
import 'chat_room_info_drawer.dart';
1616
import 'chat_room_timeline_list.dart';
17-
import '../../input/view/chat_input.dart';
18-
import '../../titlebar/chat_room_title_bar.dart';
1917

2018
final GlobalKey<ScaffoldState> chatRoomScaffoldKey = GlobalKey();
2119

@@ -47,17 +45,18 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
4745
@override
4846
Widget build(BuildContext context) {
4947
final l10n = context.l10n;
50-
final archiveActive = watchPropertyValue((ChatModel m) => m.archiveActive);
51-
final loadingArchive =
52-
watchPropertyValue((ChatModel m) => m.loadingArchive);
48+
final colorScheme = context.colorScheme;
49+
5350
final updating = watchPropertyValue(
5451
(TimelineModel m) => m.getUpdatingTimeline(widget.room.id),
5552
);
5653

5754
registerStreamHandler(
5855
select: (ChatModel m) => m.getLeftRoomStream(widget.room.id),
5956
handler: (context, leftRoomUpdate, cancel) {
60-
if (!archiveActive && !loadingArchive && leftRoomUpdate.hasData) {
57+
if (!di<ChatModel>().archiveActive &&
58+
!di<ChatModel>().loadingArchive &&
59+
leftRoomUpdate.hasData) {
6160
di<ChatModel>().leaveSelectedRoom(
6261
onFail: (error) => showSnackBar(
6362
context,
@@ -81,11 +80,9 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
8180
return Stack(
8281
alignment: Alignment.center,
8382
children: [
84-
const ChatRoomDefaultBackground(),
8583
Scaffold(
8684
key: chatRoomScaffoldKey,
8785
endDrawer: ChatRoomInfoDrawer(room: widget.room),
88-
backgroundColor: Colors.transparent,
8986
appBar: ChatRoomTitleBar(room: widget.room),
9087
bottomNavigationBar: widget.room.isArchived
9188
? null
@@ -96,44 +93,36 @@ class _ChatRoomPageState extends State<ChatRoomPage> {
9693
body: FutureBuilder<Timeline>(
9794
key: ValueKey(widget.room.id),
9895
future: _timelineFuture,
99-
builder: (context, snapshot) {
100-
if (!snapshot.hasData) {
101-
return const Center(
102-
child: Progress(),
103-
);
104-
}
105-
106-
return Padding(
107-
padding: const EdgeInsets.only(bottom: kMediumPadding),
108-
child: ChatRoomTimelineList(
109-
timeline: snapshot.data!,
110-
listKey: _roomListKey,
111-
),
112-
);
113-
},
96+
builder: (context, snapshot) => snapshot.hasData
97+
? Padding(
98+
padding: const EdgeInsets.only(bottom: kMediumPadding),
99+
child: ChatRoomTimelineList(
100+
timeline: snapshot.data!,
101+
listKey: _roomListKey,
102+
),
103+
)
104+
: const Center(
105+
child: Progress(),
106+
),
114107
),
115108
),
116-
Positioned(
117-
top: 3 * kBigPadding,
118-
child: IgnorePointer(
119-
child: AnimatedOpacity(
120-
duration: const Duration(milliseconds: 200),
121-
opacity: updating ? 1 : 0,
122-
child: FloatingActionButton.small(
123-
backgroundColor: context.colorScheme.isLight
124-
? Colors.white
125-
: Colors.black.scale(lightness: 0.09),
126-
onPressed: () {},
127-
child: const SizedBox.square(
128-
dimension: 20,
109+
if (updating)
110+
Positioned(
111+
top: 3 * kBigPadding,
112+
child: RepaintBoundary(
113+
child: CircleAvatar(
114+
backgroundColor: colorScheme.surface,
115+
maxRadius: 15,
116+
child: SizedBox.square(
117+
dimension: 18,
129118
child: Progress(
130119
strokeWidth: 2,
120+
color: colorScheme.onSurface,
131121
),
132122
),
133123
),
134124
),
135125
),
136-
),
137126
],
138127
);
139128
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,15 @@ class _ChatRoomTimelineListState extends State<ChatRoomTimelineList> {
108108
textAlign: TextAlign.center,
109109
style: theme.textTheme.labelSmall,
110110
),
111-
ChatEventTile(
112-
key: ValueKey('${event.eventId}column'),
113-
event: event,
114-
partOfMessageCohort:
115-
event.partOfMessageCohort(previous),
116-
onReplyOriginClick: (event) => _jump(event),
117-
timeline: widget.timeline,
111+
RepaintBoundary(
112+
child: ChatEventTile(
113+
key: ValueKey('${event.eventId}column'),
114+
event: event,
115+
partOfMessageCohort:
116+
event.partOfMessageCohort(previous),
117+
onReplyOriginClick: (event) => _jump(event),
118+
timeline: widget.timeline,
119+
),
118120
),
119121
if (i == 0)
120122
ChatEventSeenByIndicator(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ChatTypingIndicator extends StatelessWidget with WatchItMixin {
4444
(e) => ChatAvatar(
4545
key: ValueKey(e.id),
4646
dimension: kTypingAvatarSize,
47+
fallBackIconSize: kTypingAvatarSize / 2,
4748
avatarUri: e.avatarUrl,
4849
),
4950
),

lib/chat/chat_room/titlebar/chat_room_encryption_status_button.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import 'package:flutter/material.dart';
22
import 'package:matrix/matrix.dart';
3-
import 'package:watch_it/watch_it.dart';
43
import 'package:yaru/yaru.dart';
4+
55
import '../../../common/view/build_context_x.dart';
66
import '../../../l10n/l10n.dart';
7-
import '../../common/chat_model.dart';
87

9-
class ChatRoomEncryptionStatusButton extends StatelessWidget with WatchItMixin {
8+
class ChatRoomEncryptionStatusButton extends StatelessWidget {
109
const ChatRoomEncryptionStatusButton({
1110
super.key,
1211
required this.room,
@@ -18,8 +17,9 @@ class ChatRoomEncryptionStatusButton extends StatelessWidget with WatchItMixin {
1817
Widget build(BuildContext context) {
1918
final l10n = context.l10n;
2019
final colorScheme = context.colorScheme;
21-
watchStream((ChatModel m) => m.getJoinedRoomUpdate(room.id)).data;
20+
2221
return IconButton(
22+
key: ValueKey('${room.id}${room.encrypted}'),
2323
onPressed: null,
2424
tooltip: room.encrypted ? l10n.encrypted : l10n.encryptionNotEnabled,
2525
icon: !room.encrypted

lib/chat/chat_room/titlebar/chat_room_pin_button.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import 'package:flutter/material.dart';
22
import 'package:matrix/matrix.dart';
3-
import 'package:watch_it/watch_it.dart';
43
import 'package:yaru/yaru.dart';
4+
55
import '../../../common/view/build_context_x.dart';
6-
import '../../common/chat_model.dart';
76

8-
class ChatRoomPinButton extends StatelessWidget with WatchItMixin {
7+
class ChatRoomPinButton extends StatelessWidget {
98
const ChatRoomPinButton({
109
super.key,
1110
required this.room,
@@ -15,9 +14,8 @@ class ChatRoomPinButton extends StatelessWidget with WatchItMixin {
1514

1615
@override
1716
Widget build(BuildContext context) {
18-
watchStream((ChatModel m) => m.getJoinedRoomUpdate(room.id)).data;
19-
2017
return IconButton(
18+
key: ValueKey('${room.id}fav'),
2119
onPressed: () => room.setFavourite(!room.isFavourite),
2220
icon: Icon(
2321
YaruIcons.pin,

0 commit comments

Comments
 (0)