From 8c1df0eb88c85c663e0e0f4f28c015b297ff4533 Mon Sep 17 00:00:00 2001 From: Kelrap Date: Tue, 23 Jul 2024 12:35:53 -0400 Subject: [PATCH] Move calculations to state --- lib/pages/chat/input_bar.dart | 13 ++++--------- lib/pangea/widgets/chat/input_bar_wrapper.dart | 13 +++++++++---- lib/pangea/widgets/igc/pangea_text_controller.dart | 5 +++++ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/pages/chat/input_bar.dart b/lib/pages/chat/input_bar.dart index 4ffde1f42..54dba8674 100644 --- a/lib/pages/chat/input_bar.dart +++ b/lib/pages/chat/input_bar.dart @@ -21,7 +21,6 @@ class InputBar extends StatelessWidget { final ValueChanged? onSubmitImage; final FocusNode? focusNode; // #Pangea - final Function? updateBar; // final TextEditingController? controller; final PangeaTextController? controller; // Pangea# @@ -39,7 +38,6 @@ class InputBar extends StatelessWidget { this.onSubmitImage, this.focusNode, this.controller, - this.updateBar, this.decoration, this.onChanged, this.autofocus, @@ -404,7 +402,7 @@ class InputBar extends StatelessWidget { Widget build(BuildContext context) { final useShortCuts = (AppConfig.sendOnEnter ?? !PlatformInfos.isMobile); // #Pangea - final bool maxLength = controller?.text.length == 1000; + controller?.currentlyMaxLength = controller?.isMaxLength ?? false; // Pangea# return Shortcuts( shortcuts: !useShortCuts @@ -509,7 +507,9 @@ class InputBar extends StatelessWidget { onSubmitted!(text); }, // #Pangea - style: maxLength ? const TextStyle(color: Colors.red) : null, + style: controller?.isMaxLength ?? false + ? const TextStyle(color: Colors.red) + : null, onTap: () { controller!.onInputTap( context, @@ -522,11 +522,6 @@ class InputBar extends StatelessWidget { // fix for the library for now // it sets the types for the callback incorrectly onChanged!(text); - // #Pangea - if (maxLength != (controller?.text.length == 1000)) { - updateBar!(); - } - // Pangea# }, textCapitalization: TextCapitalization.sentences, ), diff --git a/lib/pangea/widgets/chat/input_bar_wrapper.dart b/lib/pangea/widgets/chat/input_bar_wrapper.dart index 5aa813bbe..1e8cd4727 100644 --- a/lib/pangea/widgets/chat/input_bar_wrapper.dart +++ b/lib/pangea/widgets/chat/input_bar_wrapper.dart @@ -61,8 +61,14 @@ class InputBarWrapperState extends State { super.dispose(); } - void refreshOnChange() { - setState(() {}); + void refreshOnChange(String text) { + if (widget.onChanged != null) { + widget.onChanged!(text); + } + if (widget.controller?.currentlyMaxLength != + widget.controller?.isMaxLength) { + setState(() {}); + } } @override @@ -77,8 +83,7 @@ class InputBarWrapperState extends State { focusNode: widget.focusNode, controller: widget.controller, decoration: widget.decoration, - updateBar: refreshOnChange, - onChanged: widget.onChanged, + onChanged: refreshOnChange, autofocus: widget.autofocus, textInputAction: widget.textInputAction, readOnly: widget.readOnly, diff --git a/lib/pangea/widgets/igc/pangea_text_controller.dart b/lib/pangea/widgets/igc/pangea_text_controller.dart index b91186c22..d7f24a7e2 100644 --- a/lib/pangea/widgets/igc/pangea_text_controller.dart +++ b/lib/pangea/widgets/igc/pangea_text_controller.dart @@ -25,6 +25,11 @@ class PangeaTextController extends TextEditingController { text ??= ''; this.text = text; } + + bool get isMaxLength => text.length == 1000; + + bool currentlyMaxLength = false; + bool forceKeepOpen = false; setSystemText(String text, EditType type) {