Skip to content

Commit a517f1b

Browse files
author
Franco Bugnano
committed
1 parent 8492b9d commit a517f1b

File tree

10 files changed

+247
-377
lines changed

10 files changed

+247
-377
lines changed

lib/assets/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
}
1212
</style>
1313
<script>
14-
(function(t,a,l,k,j,s){
15-
s=a.createElement('script');s.async=1;s.src="http://192.168.122.116:50000/__assets/cdn/talk-bundle.js";a.head.appendChild(s)
16-
;k=t.Promise;t.Talk={v:3,ready:{then:function(f){if(k)return new k(function(r,e){l.push([f,r,e])});l
17-
.push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);
18-
</script>
14+
(function(t,a,l,k,j,s){
15+
s=a.createElement('script');s.async=1;s.src="https://cdn.talkjs.com/talk.js";a.head.appendChild(s)
16+
;k=t.Promise;t.Talk={v:3,ready:{then:function(f){if(k)return new k(function(r,e){l.push([f,r,e])});l
17+
.push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);
18+
</script>
1919
</head>
2020
<body>
2121
<div id="talkjs-container"></div>

lib/src/chatbox.dart

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ import 'package:flutter/foundation.dart';
66

77
import 'package:webview_flutter/webview_flutter.dart';
88

9-
import 'package:provider/provider.dart';
10-
119
import './session.dart';
1210
import './conversation.dart';
1311
import './chatoptions.dart';
1412
import './user.dart';
1513
import './message.dart';
1614

17-
typedef BlurHandler = void Function();
18-
typedef FocusHandler = void Function();
1915
typedef SendMessageHandler = void Function(SendMessageEvent event);
2016
typedef TranslationToggledHandler = void Function(TranslationToggledEvent event);
2117

@@ -44,43 +40,35 @@ class TranslationToggledEvent {
4440
/// Create a Chatbox through [Session.createChatbox] and then call [mount] to show it.
4541
/// There is no way for the user to switch between conversations
4642
class ChatBox extends StatefulWidget {
47-
final ChatMode? chatSubtitleMode;
48-
final ChatMode? chatTitleMode;
43+
final Session session;
44+
4945
final TextDirection? dir;
5046
final MessageFieldOptions? messageField;
5147
final bool? showChatHeader;
5248
final TranslationToggle? showTranslationToggle;
5349
final String? theme;
5450
final TranslateConversations? translateConversations;
55-
final List<Conversation>? conversationsToTranslate;
56-
final List<String>? conversationIdsToTranslate;
5751

5852
final Conversation? conversation;
5953
final bool? asGuest;
6054

61-
final BlurHandler? onBlur;
62-
final FocusHandler? onFocus;
6355
final SendMessageHandler? onSendMessage;
6456
final TranslationToggledHandler? onTranslationToggled;
6557

66-
const ChatBox({Key? key,
67-
this.chatSubtitleMode,
68-
this.chatTitleMode,
69-
this.dir,
70-
this.messageField,
71-
this.showChatHeader,
72-
this.showTranslationToggle,
73-
this.theme,
74-
this.translateConversations,
75-
this.conversationsToTranslate,
76-
this.conversationIdsToTranslate,
77-
this.conversation,
78-
this.asGuest,
79-
this.onBlur,
80-
this.onFocus,
81-
this.onSendMessage,
82-
this.onTranslationToggled,
83-
}) : super(key: key);
58+
const ChatBox({
59+
Key? key,
60+
required this.session,
61+
this.dir,
62+
this.messageField,
63+
this.showChatHeader,
64+
this.showTranslationToggle,
65+
this.theme,
66+
this.translateConversations,
67+
this.conversation,
68+
this.asGuest,
69+
this.onSendMessage,
70+
this.onTranslationToggled,
71+
}) : super(key: key);
8472

8573
@override
8674
State<ChatBox> createState() => ChatBoxState();
@@ -122,15 +110,13 @@ class ChatBoxState extends State<ChatBox> {
122110
print('📗 chatbox.build (_webViewCreated: $_webViewCreated)');
123111
}
124112

125-
final sessionState = context.read<SessionState>();
126-
127113
if (!_webViewCreated) {
128114
// If it's the first time that the widget is built, then build everything
129115
_webViewCreated = true;
130116

131117
execute('let chatBox;');
132118

133-
_createSession(sessionState);
119+
_createSession();
134120
_createChatBox();
135121
_createConversation();
136122

@@ -162,65 +148,53 @@ class ChatBoxState extends State<ChatBox> {
162148
onWebViewCreated: _webViewCreatedCallback,
163149
onPageFinished: _onPageFinished,
164150
javascriptChannels: <JavascriptChannel>{
165-
JavascriptChannel(name: 'JSCBlur', onMessageReceived: _jscBlur),
166-
JavascriptChannel(name: 'JSCFocus', onMessageReceived: _jscFocus),
167151
JavascriptChannel(name: 'JSCSendMessage', onMessageReceived: _jscSendMessage),
168152
JavascriptChannel(name: 'JSCTranslationToggled', onMessageReceived: _jscTranslationToggled),
169153
});
170154
}
171155

172-
void _createSession(SessionState sessionState) {
156+
void _createSession() {
173157
// Initialize Session object
174158
final options = <String, dynamic>{};
175159

176-
options['appId'] = sessionState.appId;
160+
options['appId'] = widget.session.appId;
177161

178-
if (sessionState.signature != null) {
179-
options["signature"] = sessionState.signature;
162+
if (widget.session.signature != null) {
163+
options["signature"] = widget.session.signature;
180164
}
181165

182166
execute('const options = ${json.encode(options)};');
183167

184-
final variableName = getUserVariableName(sessionState.me);
168+
final variableName = getUserVariableName(widget.session.me);
185169
execute('options["me"] = $variableName;');
186170

187171
execute('const session = new Talk.Session(options);');
188172
}
189173

190174
void _createChatBox() {
191175
_oldOptions = ChatBoxOptions(
192-
chatSubtitleMode: widget.chatSubtitleMode,
193-
chatTitleMode: widget.chatTitleMode,
194176
dir: widget.dir,
195177
messageField: widget.messageField,
196178
showChatHeader: widget.showChatHeader,
197179
showTranslationToggle: widget.showTranslationToggle,
198180
theme: widget.theme,
199181
translateConversations: widget.translateConversations,
200-
conversationsToTranslate: widget.conversationsToTranslate,
201-
conversationIdsToTranslate: widget.conversationIdsToTranslate,
202182
);
203183

204184
execute('chatBox = session.createChatbox(${_oldOptions!.getJsonString(this)});');
205185

206-
execute('chatBox.on("blur", (event) => JSCBlur.postMessage(JSON.stringify(event)));');
207-
execute('chatBox.on("focus", (event) => JSCFocus.postMessage(JSON.stringify(event)));');
208186
execute('chatBox.on("sendMessage", (event) => JSCSendMessage.postMessage(JSON.stringify(event)));');
209187
execute('chatBox.on("translationToggled", (event) => JSCTranslationToggled.postMessage(JSON.stringify(event)));');
210188
}
211189

212190
bool _checkRecreateChatBox() {
213191
final options = ChatBoxOptions(
214-
chatSubtitleMode: widget.chatSubtitleMode,
215-
chatTitleMode: widget.chatTitleMode,
216192
dir: widget.dir,
217193
messageField: widget.messageField,
218194
showChatHeader: widget.showChatHeader,
219195
showTranslationToggle: widget.showTranslationToggle,
220196
theme: widget.theme,
221197
translateConversations: widget.translateConversations,
222-
conversationsToTranslate: widget.conversationsToTranslate,
223-
conversationIdsToTranslate: widget.conversationIdsToTranslate,
224198
);
225199

226200
if (options != _oldOptions) {
@@ -245,8 +219,11 @@ class ChatBoxState extends State<ChatBox> {
245219
if (_oldConversation != null) {
246220
execute('chatBox.select(${getConversationVariableName(_oldConversation!)}, ${json.encode(result)});');
247221
} else {
248-
// TODO: null or undefined?
249-
execute('chatBox.select(null, ${json.encode(result)});');
222+
if (result.isNotEmpty) {
223+
execute('chatBox.select(undefined, ${json.encode(result)});');
224+
} else {
225+
execute('chatBox.select(undefined);');
226+
}
250227
}
251228
}
252229

@@ -300,22 +277,6 @@ class ChatBoxState extends State<ChatBox> {
300277
}
301278
}
302279

303-
void _jscBlur(JavascriptMessage message) {
304-
if (kDebugMode) {
305-
print('📗 chatbox._jscBlur: ${message.message}');
306-
}
307-
308-
widget.onBlur?.call();
309-
}
310-
311-
void _jscFocus(JavascriptMessage message) {
312-
if (kDebugMode) {
313-
print('📗 chatbox._jscFocus: ${message.message}');
314-
}
315-
316-
widget.onFocus?.call();
317-
}
318-
319280
void _jscSendMessage(JavascriptMessage message) {
320281
if (kDebugMode) {
321282
print('📗 chatbox._jscSendMessage: ${message.message}');

0 commit comments

Comments
 (0)