refactor: Remove unnecessary setState in ChatPage for better performance

This commit is contained in:
krille-chan 2023-12-23 07:38:45 +01:00
parent a615de889e
commit 63d7bef515
No known key found for this signature in database
3 changed files with 222 additions and 215 deletions

View file

@ -187,8 +187,6 @@ class ChatController extends State<ChatPageWithRoom> {
final int _loadHistoryCount = 100;
String inputText = '';
String pendingText = '';
bool showEmojiPicker = false;
@ -260,7 +258,6 @@ class ChatController extends State<ChatPageWithRoom> {
if (!mounted) {
return;
}
setReadMarker();
if (!scrollController.hasClients) return;
if (timeline?.allowNewEvent == false ||
scrollController.position.pixels > 0 && _scrolledUp == false) {
@ -285,7 +282,6 @@ class ChatController extends State<ChatPageWithRoom> {
final draft = prefs.getString('draft_$roomId');
if (draft != null && draft.isNotEmpty) {
sendController.text = draft;
setState(() => inputText = draft);
}
}
@ -472,7 +468,7 @@ class ChatController extends State<ChatPageWithRoom> {
);
setState(() {
inputText = pendingText;
sendController.text = pendingText;
replyEvent = null;
editEvent = null;
pendingText = '';
@ -938,7 +934,7 @@ class ChatController extends State<ChatPageWithRoom> {
);
});
await loadTimelineFuture;
setReadMarker(eventId: timeline!.events.first.eventId);
setReadMarker();
}
scrollController.jumpTo(0);
}
@ -1040,7 +1036,7 @@ class ChatController extends State<ChatPageWithRoom> {
setState(() {
pendingText = sendController.text;
editEvent = selectedEvents.first;
inputText = sendController.text =
sendController.text =
editEvent!.getDisplayEvent(timeline!).calcLocalizedBodyFallback(
MatrixLocals(L10n.of(context)!),
withSenderNamePrefix: false,
@ -1187,7 +1183,6 @@ class ChatController extends State<ChatPageWithRoom> {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('draft_$roomId', text);
});
setReadMarker();
if (text.endsWith(' ') && Matrix.of(context).hasComplexBundles) {
final clients = currentRoomBundle;
for (final client in clients) {
@ -1196,7 +1191,6 @@ class ChatController extends State<ChatPageWithRoom> {
text.toLowerCase() == '${prefix.toLowerCase()} ') {
setSendingClient(client);
setState(() {
inputText = '';
sendController.text = '';
});
return;
@ -1222,8 +1216,14 @@ class ChatController extends State<ChatPageWithRoom> {
);
}
}
setState(() => inputText = text);
if (_inputTextIsEmpty != text.isEmpty) {
setState(() {
_inputTextIsEmpty = text.isEmpty;
});
}
}
bool _inputTextIsEmpty = true;
bool get isArchived =>
{Membership.leave, Membership.ban}.contains(room.membership);
@ -1291,7 +1291,7 @@ class ChatController extends State<ChatPageWithRoom> {
void cancelReplyEventAction() => setState(() {
if (editEvent != null) {
inputText = sendController.text = pendingText;
sendController.text = pendingText;
pendingText = '';
}
replyEvent = null;

View file

@ -105,7 +105,7 @@ class ChatInputRow extends StatelessWidget {
duration: FluffyThemes.animationDuration,
curve: FluffyThemes.animationCurve,
height: 56,
width: controller.inputText.isEmpty ? 56 : 0,
width: controller.sendController.text.isEmpty ? 56 : 0,
alignment: Alignment.center,
clipBehavior: Clip.hardEdge,
decoration: const BoxDecoration(),
@ -268,7 +268,7 @@ class ChatInputRow extends StatelessWidget {
),
),
if (PlatformInfos.platformCanRecord &&
controller.inputText.isEmpty)
controller.sendController.text.isEmpty)
Container(
height: 56,
alignment: Alignment.center,
@ -278,7 +278,8 @@ class ChatInputRow extends StatelessWidget {
onPressed: controller.voiceMessageAction,
),
),
if (!PlatformInfos.isMobile || controller.inputText.isNotEmpty)
if (!PlatformInfos.isMobile ||
controller.sendController.text.isNotEmpty)
Container(
height: 56,
alignment: Alignment.center,

View file

@ -149,6 +149,8 @@ class ChatView extends StatelessWidget {
child: GestureDetector(
onTapDown: (_) => controller.setReadMarker(),
behavior: HitTestBehavior.opaque,
child: MouseRegion(
onEnter: (_) => controller.setReadMarker(),
child: StreamBuilder(
stream: controller.room.onUpdate.stream
.rateLimit(const Duration(seconds: 1)),
@ -227,7 +229,8 @@ class ChatView extends StatelessWidget {
icon: const Icon(Icons.close),
tooltip: L10n.of(context)!.close,
onPressed: () {
controller.discardScrollUpBannerEventId();
controller
.discardScrollUpBannerEventId();
controller.setReadMarker();
},
),
@ -241,7 +244,8 @@ class ChatView extends StatelessWidget {
controller.scrollToEventId(
scrollUpBannerEventId,
);
controller.discardScrollUpBannerEventId();
controller
.discardScrollUpBannerEventId();
},
child: Text(L10n.of(context)!.jump),
),
@ -255,13 +259,12 @@ class ChatView extends StatelessWidget {
builder: (context) {
if (controller.timeline == null) {
return const Center(
child:
CircularProgressIndicator.adaptive(
child: CircularProgressIndicator
.adaptive(
strokeWidth: 2,
),
);
}
return ChatEventList(
controller: controller,
);
@ -283,10 +286,12 @@ class ChatView extends StatelessWidget {
alignment: Alignment.center,
child: Material(
borderRadius: const BorderRadius.only(
bottomLeft:
Radius.circular(AppConfig.borderRadius),
bottomRight:
Radius.circular(AppConfig.borderRadius),
bottomLeft: Radius.circular(
AppConfig.borderRadius,
),
bottomRight: Radius.circular(
AppConfig.borderRadius,
),
),
elevation: 4,
shadowColor: Colors.black.withAlpha(64),
@ -368,6 +373,7 @@ class ChatView extends StatelessWidget {
),
),
),
),
);
}
}