Skip to content

Commit 76915ec

Browse files
authored
Modified logic to handle previous chat ended events (#42)
1 parent 06875c3 commit 76915ec

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

chat-sdk/src/main/java/com/amazon/connect/chat/sdk/network/WebSocketManager.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class WebSocketManagerImpl @Inject constructor(
6262
) : WebSocketManager {
6363

6464
private val coroutineScope = CoroutineScope(dispatcher + SupervisorJob())
65-
private var latestParticipantJoinedTimestamp: String? = null
6665

6766
private val client: OkHttpClient = OkHttpClient.Builder()
6867
.pingInterval(60, TimeUnit.SECONDS)
@@ -287,14 +286,7 @@ class WebSocketManagerImpl @Inject constructor(
287286
WebSocketMessageType.EVENT -> {
288287
val eventTypeString = jsonObject.optString("ContentType")
289288
when (val eventType = ContentType.fromType(eventTypeString)) {
290-
ContentType.JOINED -> {
291-
val timestamp = jsonObject.getString("AbsoluteTime")
292-
if (latestParticipantJoinedTimestamp == null
293-
|| timestamp > (latestParticipantJoinedTimestamp ?: "")) {
294-
latestParticipantJoinedTimestamp = timestamp
295-
}
296-
handleParticipantEvent(jsonObject, jsonString)
297-
}
289+
ContentType.JOINED -> handleParticipantEvent(jsonObject, jsonString)
298290
ContentType.LEFT -> handleParticipantEvent(jsonObject, jsonString)
299291
ContentType.TYPING -> handleTyping(jsonObject, jsonString)
300292
ContentType.ENDED -> handleChatEnded(jsonObject, jsonString)
@@ -443,11 +435,9 @@ class WebSocketManagerImpl @Inject constructor(
443435
private suspend fun handleChatEnded(innerJson: JSONObject, rawData: String): TranscriptItem {
444436
val time = innerJson.getString("AbsoluteTime")
445437
val eventId = innerJson.getString("Id")
438+
val isFromPastSession = innerJson.optBoolean("isFromPastSession", false)
446439

447-
// Check if the event belongs to a previous transcript
448-
val isOlderEvent = latestParticipantJoinedTimestamp?.let { time < it } == true
449-
450-
if (!isOlderEvent) {
440+
if (!isFromPastSession) {
451441
// Current session event: Reset state and update session
452442
resetHeartbeatManagers()
453443
this._eventPublisher.emit(ChatEvent.ChatEnded)

chat-sdk/src/main/java/com/amazon/connect/chat/sdk/repository/ChatService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ class ChatServiceImpl @Inject constructor(
416416

417417
override suspend fun disconnectChatSession(): Result<Boolean> {
418418
return runCatching {
419+
// Check if the chat session is active
420+
if (!connectionDetailsProvider.isChatSessionActive()) {
421+
webSocketManager.disconnect("Session inactive")
422+
clearSubscriptionsAndPublishers()
423+
SDKLogger.logger.logDebug { "Chat session is inactive. Disconnecting websocket and clearing resources." }
424+
return Result.success(true) // Successfully handled the inactive session case
425+
}
426+
427+
// Proceed with the disconnection logic
419428
val connectionDetails = connectionDetailsProvider.getConnectionDetails()
420429
?: throw Exception("No connection details available")
421430
awsClient.disconnectParticipantConnection(connectionDetails.connectionToken)

chat-sdk/src/main/java/com/amazon/connect/chat/sdk/utils/TranscriptItemUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ object TranscriptItemUtils {
8484
"Content" to (item.content ?: ""),
8585
"Type" to item.type,
8686
"DisplayName" to (item.displayName ?: ""),
87-
"Attachments" to attachmentsArray
87+
"Attachments" to attachmentsArray,
88+
"isFromPastSession" to true // Mark all these items as coming from a past session
8889
)
8990

9091
// Serialize the dictionary to JSON string

chat-sdk/version.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
sdkVersion=1.0.4
1+
sdkVersion=1.0.5
22
groupId=software.aws.connect
33
artifactId=amazon-connect-chat-android

0 commit comments

Comments
 (0)