refactor: Remove unnecessary setState in ChatPage for better performance

pull/738/head
krille-chan 2 years ago
parent a615de889e
commit 63d7bef515
No known key found for this signature in database

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

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

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

Loading…
Cancel
Save