@@ -11,6 +11,7 @@ import './conversation.dart';
1111import './chatoptions.dart' ;
1212import './user.dart' ;
1313import './message.dart' ;
14+ import './predicate.dart' ;
1415
1516typedef SendMessageHandler = void Function (SendMessageEvent event);
1617typedef TranslationToggledHandler = void Function (TranslationToggledEvent event);
@@ -49,6 +50,7 @@ class ChatBox extends StatefulWidget {
4950 final String ? theme;
5051 final TranslateConversations ? translateConversations;
5152 final List <String > highlightedWords = const < String > [];
53+ final MessagePredicate messageFilter;
5254
5355 final Conversation ? conversation;
5456 final bool ? asGuest;
@@ -66,6 +68,7 @@ class ChatBox extends StatefulWidget {
6668 this .theme,
6769 this .translateConversations,
6870 //this.highlightedWords = const <String>[], // Commented out due to bug #1953
71+ this .messageFilter = const MessagePredicate (),
6972 this .conversation,
7073 this .asGuest,
7174 this .onSendMessage,
@@ -104,6 +107,7 @@ class ChatBoxState extends State<ChatBox> {
104107 /// Objects stored for comparing changes
105108 ChatBoxOptions ? _oldOptions;
106109 List <String > _oldHighlightedWords = [];
110+ MessagePredicate _oldMessageFilter = const MessagePredicate ();
107111 bool ? _oldAsGuest;
108112 Conversation ? _oldConversation;
109113
@@ -121,7 +125,7 @@ class ChatBoxState extends State<ChatBox> {
121125
122126 _createSession ();
123127 _createChatBox ();
124- _setHighlightedWords ();
128+ // messageFilter and highlightedWords are set as options for the chatbox
125129 _createConversation ();
126130
127131 execute ('chatBox.mount(document.getElementById("talkjs-container"));' );
@@ -134,9 +138,10 @@ class ChatBoxState extends State<ChatBox> {
134138 final chatBoxRecreated = _checkRecreateChatBox ();
135139
136140 if (chatBoxRecreated) {
137- _setHighlightedWords ();
141+ // messageFilter and highlightedWords are set as options for the chatbox
138142 _createConversation ();
139143 } else {
144+ _checkMessageFilter ();
140145 _checkHighlightedWords ();
141146 _checkRecreateConversation ();
142147 }
@@ -187,6 +192,9 @@ class ChatBoxState extends State<ChatBox> {
187192 translateConversations: widget.translateConversations,
188193 );
189194
195+ _oldHighlightedWords = List <String >.of (widget.highlightedWords);
196+ _oldMessageFilter = MessagePredicate .of (widget.messageFilter);
197+
190198 execute ('chatBox = session.createChatbox(${_oldOptions !.getJsonString (this )});' );
191199
192200 execute ('chatBox.on("sendMessage", (event) => JSCSendMessage.postMessage(JSON.stringify(event)));' );
@@ -259,6 +267,22 @@ class ChatBoxState extends State<ChatBox> {
259267 return false ;
260268 }
261269
270+ void _setMessageFilter () {
271+ _oldMessageFilter = MessagePredicate .of (widget.messageFilter);
272+
273+ execute ('chatBox.setMessageFilter(${json .encode (_oldMessageFilter )});' );
274+ }
275+
276+ bool _checkMessageFilter () {
277+ if (widget.messageFilter != _oldMessageFilter) {
278+ _setMessageFilter ();
279+
280+ return true ;
281+ }
282+
283+ return false ;
284+ }
285+
262286 void _webViewCreatedCallback (WebViewController webViewController) async {
263287 if (kDebugMode) {
264288 print ('📗 chatbox._webViewCreatedCallback' );
@@ -420,6 +444,15 @@ class ChatBoxState extends State<ChatBox> {
420444 }
421445 }
422446
447+ /// For internal use only. Implementation detail that may change anytime.
448+ ///
449+ /// Sets the options for ChatBoxOptions for the properties where there exists
450+ /// both a declarative option and an imperative method
451+ void setExtraOptions (Map <String , dynamic > result) {
452+ result['highlightedWords' ] = widget.highlightedWords;
453+ result['messageFilter' ] = widget.messageFilter;
454+ }
455+
423456 /// For internal use only. Implementation detail that may change anytime.
424457 ///
425458 /// Evaluates the JavaScript statement given.
@@ -438,32 +471,3 @@ class ChatBoxState extends State<ChatBox> {
438471 }
439472}
440473
441- /// Encapsulates the message entry field tied to the currently selected conversation.
442- class MessageField {
443- /// The ChatBox associated with this message field
444- ChatBoxState chatbox;
445-
446- /// The JavaScript variable name for this object.
447- String variableName;
448-
449- MessageField ({required this .chatbox, required this .variableName});
450-
451- /// Focuses the message entry field.
452- ///
453- /// Note that on mobile devices, this will cause the on-screen keyboard to pop up, obscuring part
454- /// of the screen.
455- void focus () {
456- chatbox.execute ('$variableName .focus();' );
457- }
458-
459- /// Sets the message field to `text` .
460- ///
461- /// Useful if you want to guide your user with message suggestions. If you want to start a UI
462- /// with a given text showing immediately, call this method before calling Inbox.mount
463- void setText (String text) {
464- chatbox.execute ('$variableName .setText("$text ");' );
465- }
466-
467- /// TODO: setVisible(visible: boolean | ConversationPredicate): void;
468- }
469-
0 commit comments