Skip to content

Commit b65c6b8

Browse files
committed
Pull request #482: Jdzubak MCA-4524 lc threads api
Merge in MML/infobip-mobile-messaging-android from jdzubak-MCA-4524-lc-threads-api to master Squashed commit of the following: commit 83220945c4c1692f4976ae2decfbfe9e559b88b1 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Wed Mar 19 13:49:31 2025 +0100 MCA-4534 Fixed comments type commit d618547e3d413fa2e85397420191eacbbe8e3689 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Wed Mar 19 13:09:05 2025 +0100 MCA-4534 Lifecycle methods fix commit 9683933cd487c6ae77c5703c6f5a462e12834ef2 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Wed Mar 19 09:04:30 2025 +0100 MCA-4534 Imports revert commit 6348a9bfc8b8b66a9b4b3b5b420be6b096610e4b Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Tue Mar 18 13:17:04 2025 +0100 MCA-4534 Delegation commit 82fdfb096ec02bfbd3cb83d63fe0b3a968c583ff Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Tue Mar 18 12:37:02 2025 +0100 MCA-4534 Fixed issue in InAppChatActivityResultDelegate.kt commit d87da94c82eb8f6b651f1568c070d53e32f24982 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 15:18:46 2025 +0100 MCA-4534 Added missing Fragment public functions commit 6b987f5deab87f76a0e810e36661fd45a35dd68d Merge: 326823af 67d7e09 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 14:39:14 2025 +0100 MCA-4534 Merge master commit 326823af18fa2bcd62c34051284fdc8f42d30faa Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 13:43:46 2025 +0100 MCA-4534 DefaultInAppChatErrorHandler commit d162f281675e3fb0974a8623ba2c264da4777fa7 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 13:38:45 2025 +0100 MCA-4534 Added default implementations commit 82d9e4ecede5a9b8632d7f2d340b7280a0e93f62 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 12:10:59 2025 +0100 MCA-4534 Small fix commit 0b75d98e2b1a7841134467cf3b9dfbb98cb93949 Merge: 044ac6d0 e55ce6a Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 12:00:39 2025 +0100 Merge remote-tracking branch 'origin/master' into jdzubak-MCA-4524-lc-threads-api commit 044ac6d042eebfb2ee92891d4003fbea01da45f1 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 17 11:56:02 2025 +0100 MCA-4534 LivechatWidgetApi added threads API functions commit 4c5231816a64763b17c2dbd80b05d93f49dddb6d Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Fri Mar 14 16:01:55 2025 +0100 MCA-4534 LivechatWidgetApi added threads management functions, migration in fragment missing commit 0e59299aaa4c5ac8034155faa5e882cd17b6215e Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Fri Mar 14 10:12:02 2025 +0100 MCA-4534 Threads API unfinished commit eec23f9920d36a5748eda913672459f659d88d12 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Thu Mar 13 08:36:02 2025 +0100 MCA-4534 getThreads commit ce2c94e77f05e07d1b23db11a81b9df387bd264e Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 10 15:06:02 2025 +0100 MCA-4477 Few improvements commit f285f6813ffc506a339fef61583ac41b71029ac1 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Mon Mar 10 14:01:12 2025 +0100 MCA-4477 Null safe commit a1590d5862a888cd23d0db6f545b92291d738562 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Fri Mar 7 16:17:43 2025 +0100 MCA-4477 Little update commit 57f278c19861bb4c1ef5553ce6066847d8dfc374 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Fri Mar 7 10:18:02 2025 +0100 MCA-4450 Doco review updates commit 1d8d1f04d349b6de94f1859f80a50d53d1a59bc3 Author: Jakub Dzubak <Jakub.Dzubak@infobip.com> Date: Wed Mar 5 16:34:42 2025 +0100 MCA-4450 Little updates ... and 4 more commits
1 parent 67d7e09 commit b65c6b8

25 files changed

+1589
-375
lines changed

infobip-mobile-messaging-android-chat-sdk/src/main/assets/livechat-widget.html

+67-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
const widgetTheme = getQueryParameter('widgetTheme');
2626

2727
let uri = 'https://livechat.infobip.com/widget.js';
28+
let livechatSdk = null;
2829

2930
let decodedWidgetUri = null;
3031
if (!isNullOrBlank(widgetUri)) {
@@ -77,16 +78,17 @@
7778

7879
// If all is OK -> initialize widget, enable native controls and show widget
7980
function init() {
80-
let initParams = null;
81-
if (domain != null) {
82-
initParams = { widgetId: widgetId, domain: domain };
83-
}
84-
liveChat('init', initParams, function (error, result) {
81+
const callback = function (error, result) {
8582
widgetApiCallback('initWidget', error, result);
86-
if(!error) {
87-
show();
83+
if (!error) {
84+
show();
8885
}
89-
});
86+
};
87+
if (domain != null) {
88+
liveChat('init', { widgetId: widgetId, domain: domain }, callback);
89+
} else {
90+
liveChat('init', widgetId, callback);
91+
}
9092
}
9193

9294
function show() {
@@ -106,10 +108,13 @@
106108
});
107109

108110
function addLiveChatEventsListener() {
109-
const liveChatSDK = new LiveChatSDK();
110-
liveChatSDK.on('messageReceived', (payload) => {
111-
InAppChatMobile.onRawMessageReceived(stringify(payload));
112-
});
111+
try {
112+
getLivechatSdk().on('messageReceived', (payload) => {
113+
InAppChatMobile.onRawMessageReceived(stringify(payload));
114+
});
115+
} catch (e) {
116+
console.error('Could not register to observe messageReceived event: ', e);
117+
}
113118
}
114119

115120
//Widget API
@@ -164,6 +169,45 @@
164169
liveChat('set_theme', theme, (error, result) => widgetApiCallback('setTheme', error, result));
165170
}
166171

172+
async function getThreads() {
173+
try {
174+
const response = await getLivechatSdk().getThreads();
175+
if (response.success) {
176+
widgetApiCallback('getThreads', null, response.data);
177+
} else {
178+
widgetApiCallback('getThreads', response.error, null);
179+
}
180+
} catch (e) {
181+
widgetApiCallback('getThreads', `Could not get threads ${e.message}`, null);
182+
}
183+
}
184+
185+
async function showThread(threadId) {
186+
try {
187+
const response = await getLivechatSdk().getWidget().showThread(threadId);
188+
if (response.success) {
189+
widgetApiCallback('showThread', null, response.data);
190+
} else {
191+
widgetApiCallback('showThread', response.error, null);
192+
}
193+
} catch (e) {
194+
widgetApiCallback('showThread', `Could not show thread ${e.message}`, null);
195+
}
196+
}
197+
198+
async function getActiveThread() {
199+
try {
200+
const response = await getLivechatSdk().getActiveThread();
201+
if (response.success) {
202+
widgetApiCallback('getActiveThread', null, response.data);
203+
} else {
204+
widgetApiCallback('getActiveThread', response.error, null);
205+
}
206+
} catch (e) {
207+
widgetApiCallback('getActiveThread', `Could not show thread ${e.message}`, null);
208+
}
209+
}
210+
167211
function getQueryParameter(name) {
168212
if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search)) {
169213
return decodeURIComponent(name[1]);
@@ -197,6 +241,17 @@
197241
}
198242
}
199243

244+
function getLivechatSdk() {
245+
if (!livechatSdk) {
246+
try {
247+
livechatSdk = new LiveChatSDK();
248+
} catch (e) {
249+
throw e;
250+
}
251+
}
252+
return livechatSdk;
253+
}
254+
200255
</script>
201256
</body>
202257

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/InAppChatImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void applicationInForeground() {
189189
// must be done on separate thread if it's not invoked by UI thread
190190
private void cleanupWidgetData() {
191191
sessionStorage().setConfigSyncResult(null);
192-
livechatWidgetApi().reset();
192+
getLivechatWidgetApi().reset();
193193
}
194194

195195
@Override

infobip-mobile-messaging-android-chat-sdk/src/main/java/org/infobip/mobile/messaging/chat/core/widget/LivechatWidgetApi.kt

+22-98
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ package org.infobip.mobile.messaging.chat.core.widget
33
import org.infobip.mobile.messaging.chat.attachments.InAppChatMobileAttachment
44
import org.infobip.mobile.messaging.chat.core.JwtProvider
55
import org.infobip.mobile.messaging.chat.core.MultithreadStrategy
6-
import org.infobip.mobile.messaging.chat.core.widget.LivechatWidgetApi.ExecutionListener
76

87
/**
98
* The `LivechatWidgetApi` interface provides methods to interact with the Livechat widget.
109
* It allows you to perform various actions such as sending messages, setting themes, and managing the widget's state.
1110
* It creates queue for widget functions execution using Kotlin Mutex, single function is executed at a time.
1211
*
13-
* ### Important Note on `ExecutionListener`
14-
*
15-
* The [ExecutionListener] used in the `LivechatWidgetApi` methods does not return the actual return value from the Livechat widget.
16-
* Instead, it provides a way to listen for the completion of the execution of a method.
17-
* If you need to get the real return value from the Livechat widget, you should use the [LivechatWidgetEventsListener].
18-
*
1912
* ### Using `LivechatWidgetEventsListener`
2013
* To get the real return value from the Livechat widget, you should use the [LivechatWidgetEventsListener].
2114
* This listener provides events that you can handle to get the actual data from the widget.
@@ -104,23 +97,8 @@ interface LivechatWidgetApi {
10497
* Use [resumeConnection] to reestablish connection.
10598
*
10699
* To detect if the connection is paused use [LivechatWidgetEventsListener.onConnectionPaused] event.
107-
*
108-
* @param listener listen for the completion of the livechat widget method execution
109100
*/
110-
fun pauseConnection(listener: ExecutionListener<String>? = null)
111-
112-
/**
113-
* Pauses livechat widget connection, but widget stay loaded in WebView.
114-
* Widget connection and loaded state are independent.
115-
*
116-
* By the connection you can control push notifications.
117-
* Push notifications are active only when the connection is not active.
118-
*
119-
* Use [resumeConnection] to reestablish connection.
120-
*
121-
* To detect if the connection is paused use [LivechatWidgetEventsListener.onConnectionPaused] event.
122-
*/
123-
fun pauseConnection() = pauseConnection(listener = null)
101+
fun pauseConnection()
124102

125103
/**
126104
* Resumes livechat widget connection when you previously called [pauseConnection].
@@ -131,22 +109,8 @@ interface LivechatWidgetApi {
131109
* Use [pauseConnection] to pause connection.
132110
*
133111
* To detect if the connection is resumed use [LivechatWidgetEventsListener.onConnectionResumed] event.
134-
*
135-
* @param listener listen for the completion of the livechat widget method execution
136112
*/
137-
fun resumeConnection(listener: ExecutionListener<String>? = null)
138-
139-
/**
140-
* Resumes livechat widget connection when you previously called [pauseConnection].
141-
*
142-
* By the connection you can control push notifications.
143-
* Push notifications are active only when the connection is not active.
144-
*
145-
* Use [pauseConnection] to pause connection.
146-
*
147-
* To detect if the connection is resumed use [LivechatWidgetEventsListener.onConnectionResumed] event.
148-
*/
149-
fun resumeConnection() = resumeConnection(listener = null)
113+
fun resumeConnection()
150114

151115
/**
152116
* Sends a message with optional [InAppChatMobileAttachment].
@@ -155,19 +119,8 @@ interface LivechatWidgetApi {
155119
*
156120
* @param message message to be send, max length allowed is 4096 characters
157121
* @param attachment to create attachment use [InAppChatMobileAttachment]'s constructor where you provide attachment's mimeType, base64 and filename
158-
* @param listener listen for the completion of the livechat widget method execution
159-
*/
160-
fun sendMessage(message: String?, attachment: InAppChatMobileAttachment? = null, listener: ExecutionListener<String>? = null)
161-
162-
/**
163-
* Sends a message.
164-
*
165-
* You can observe result by [LivechatWidgetEventsListener.onMessageSent] event.
166-
*
167-
* @param message message to be send, max length allowed is 4096 characters
168-
* @param listener listen for the completion of the livechat widget method execution
169122
*/
170-
fun sendMessage(message: String?, listener: ExecutionListener<String>? = null) = sendMessage(message = message, attachment = null, listener = listener)
123+
fun sendMessage(message: String?, attachment: InAppChatMobileAttachment? = null)
171124

172125
/**
173126
* Sends a message
@@ -176,17 +129,7 @@ interface LivechatWidgetApi {
176129
*
177130
* @param message message to be send, max length allowed is 4096 characters
178131
*/
179-
fun sendMessage(message: String?) = sendMessage(message = message, attachment = null, listener = null)
180-
181-
/**
182-
* Sends a draft message.
183-
*
184-
* You can observe result by [LivechatWidgetEventsListener.onDraftSent] event.
185-
*
186-
* @param draft draft message to be send
187-
* @param listener listen for the completion of the livechat widget method execution
188-
*/
189-
fun sendDraft(draft: String, listener: ExecutionListener<String>? = null)
132+
fun sendMessage(message: String) = sendMessage(message = message, attachment = null)
190133

191134
/**
192135
* Sends a draft message.
@@ -195,7 +138,7 @@ interface LivechatWidgetApi {
195138
*
196139
* @param draft draft message to be send
197140
*/
198-
fun sendDraft(draft: String) = sendDraft(draft = draft, listener = null)
141+
fun sendDraft(draft: String)
199142

200143
/**
201144
* Sends contextual data.
@@ -204,44 +147,38 @@ interface LivechatWidgetApi {
204147
*
205148
* @param data contextual data in JSON format
206149
* @param multiThreadFlag multithread strategy flag
207-
* @param listener listen for the completion of the livechat widget method execution
208150
*/
209-
fun sendContextualData(data: String, multiThreadFlag: MultithreadStrategy, listener: ExecutionListener<String>? = null)
151+
fun sendContextualData(data: String, multiThreadFlag: MultithreadStrategy)
210152

211153
/**
212-
* Sends contextual data.
213-
*
214-
* You can observe result by [LivechatWidgetEventsListener.onContextualDataSent] event.
154+
* Requests current threads from livechat widget.
215155
*
216-
* @param data contextual data in JSON format
217-
* @param multiThreadFlag multithread strategy flag
156+
* You can observe result by [LivechatWidgetEventsListener.onThreadsReceived] event.
218157
*/
219-
fun sendContextualData(data: String, multiThreadFlag: MultithreadStrategy) = sendContextualData(data = data, multiThreadFlag = multiThreadFlag, listener = null)
158+
fun getThreads()
220159

221160
/**
222-
* Navigates livechat widget from [LivechatWidgetView.THREAD] back to [LivechatWidgetView.THREAD_LIST] destination in multithread widget. It does nothing if widget is not multithread.
161+
* Requests shown thread - active from livechat widget.
223162
*
224-
* You can observe result by [LivechatWidgetEventsListener.onWidgetViewChanged] event.
225-
* @param listener listen for the completion of the livechat widget method execution
163+
* You can observe result by [LivechatWidgetEventsListener.onActiveThreadReceived] event.
226164
*/
227-
fun showThreadList(listener: ExecutionListener<String>? = null)
165+
fun getActiveThread()
228166

229167
/**
230-
* Navigates livechat widget from [LivechatWidgetView.THREAD] back to [LivechatWidgetView.THREAD_LIST] destination in multithread widget. It does nothing if widget is not multithread.
168+
* Navigates livechat widget to thread specified by provided [threadId].
169+
*
170+
* You can observe result by [LivechatWidgetEventsListener.onThreadShown] event.
231171
*
232-
* You can observe result by [LivechatWidgetEventsListener.onWidgetViewChanged] event.
172+
* @param threadId thread to be shown
233173
*/
234-
fun showThreadList() = showThreadList(listener = null)
174+
fun showThread(threadId: String)
235175

236176
/**
237-
* Sets a livechat widget's language.
238-
*
239-
* You can observe result by [LivechatWidgetEventsListener.onLanguageChanged] event.
177+
* Navigates livechat widget from [LivechatWidgetView.THREAD] back to [LivechatWidgetView.THREAD_LIST] destination in multithread widget. It does nothing if widget is not multithread.
240178
*
241-
* @param language language to be set
242-
* @param listener listen for the completion of the livechat widget method execution
179+
* You can observe result by [LivechatWidgetEventsListener.onThreadListShown] or [LivechatWidgetEventsListener.onWidgetViewChanged] event.
243180
*/
244-
fun setLanguage(language: LivechatWidgetLanguage, listener: ExecutionListener<String>? = null)
181+
fun showThreadList()
245182

246183
/**
247184
* Sets a livechat widget's language.
@@ -250,20 +187,7 @@ interface LivechatWidgetApi {
250187
*
251188
* @param language language to be set
252189
*/
253-
fun setLanguage(language: LivechatWidgetLanguage) = setLanguage(language = language, listener = null)
254-
255-
/**
256-
* Sets a livechat widget's theme.
257-
*
258-
* You can observe result by [LivechatWidgetEventsListener.onThemeChanged] event.
259-
*
260-
* You can define widget themes in <a href="https://portal.infobip.com/apps/livechat/widgets">Live chat widget setup page</a> in Infobip Portal, section `Advanced customization`.
261-
* Please check widget <a href="https://www.infobip.com/docs/live-chat/widget-customization">documentation</a> for more details.
262-
*
263-
* @param themeName theme name to be set
264-
* @param listener listen for the completion of the livechat widget method execution
265-
*/
266-
fun setTheme(themeName: String, listener: ExecutionListener<String>? = null)
190+
fun setLanguage(language: LivechatWidgetLanguage)
267191

268192
/**
269193
* Sets a livechat widget's theme.
@@ -275,7 +199,7 @@ interface LivechatWidgetApi {
275199
*
276200
* @param themeName theme name to be set
277201
*/
278-
fun setTheme(themeName: String) = setTheme(themeName = themeName, listener = null)
202+
fun setTheme(themeName: String)
279203

280204
/**
281205
* Resets livechat widget state and loads blank page.

0 commit comments

Comments
 (0)