From 7b2be9ec841a986f940315853e6f6cb1b14538b4 Mon Sep 17 00:00:00 2001 From: Egor Egorov Date: Tue, 19 Nov 2024 16:24:52 +0200 Subject: [PATCH] Add pending secure messaging logic to EntryWidget This commit adds next logic into EntryWidget.show(in:) method implementation: - if pending secure messaging exists ChatTranscript screen will be opened. - otherwise EntryWidget Bottom Sheet will be shown. Commit also adds unit test. MOB-3653 --- .../Public/Glia/Glia+EntryWidget.swift | 3 +- .../EntryWidget/EntryWidget.Environment.swift | 4 +- .../Sources/EntryWidget/EntryWidget.swift | 16 +++- .../EntryWidget/EntryWidgetTests.swift | 87 ++++++++----------- 4 files changed, 57 insertions(+), 53 deletions(-) diff --git a/GliaWidgets/Public/Glia/Glia+EntryWidget.swift b/GliaWidgets/Public/Glia/Glia+EntryWidget.swift index 33626a741..e2ee406c6 100644 --- a/GliaWidgets/Public/Glia/Glia+EntryWidget.swift +++ b/GliaWidgets/Public/Glia/Glia+EntryWidget.swift @@ -18,7 +18,8 @@ extension Glia { engagementLauncher: try getEngagementLauncher(queueIds: queueIds), theme: theme, log: loggerPhase.logger, - isAuthenticated: environment.isAuthenticated + isAuthenticated: environment.isAuthenticated, + pendingSecureConversationStatusUpdates: environment.coreSdk.pendingSecureConversationStatusUpdates ) ) } diff --git a/GliaWidgets/Sources/EntryWidget/EntryWidget.Environment.swift b/GliaWidgets/Sources/EntryWidget/EntryWidget.Environment.swift index 6159867f3..7997c6a54 100644 --- a/GliaWidgets/Sources/EntryWidget/EntryWidget.Environment.swift +++ b/GliaWidgets/Sources/EntryWidget/EntryWidget.Environment.swift @@ -9,6 +9,7 @@ extension EntryWidget { var theme: Theme var log: CoreSdkClient.Logger var isAuthenticated: () -> Bool + var pendingSecureConversationStatusUpdates: CoreSdkClient.PendingSecureConversationStatusUpdates } } @@ -23,7 +24,8 @@ extension EntryWidget.Environment { engagementLauncher: engagementLauncher, theme: .mock(), log: .mock, - isAuthenticated: { true } + isAuthenticated: { true }, + pendingSecureConversationStatusUpdates: { _ in } ) } } diff --git a/GliaWidgets/Sources/EntryWidget/EntryWidget.swift b/GliaWidgets/Sources/EntryWidget/EntryWidget.swift index 18fa187d4..33469fec6 100644 --- a/GliaWidgets/Sources/EntryWidget/EntryWidget.swift +++ b/GliaWidgets/Sources/EntryWidget/EntryWidget.swift @@ -61,11 +61,23 @@ public final class EntryWidget: NSObject { // MARK: - Public methods public extension EntryWidget { - /// Displays the widget as a modal sheet in the given view controller. + /// Displays the widget as a modal sheet in the given view controller. If there is an ongoing secure conversation, the Chat Transcript screen will be opened instead. /// /// - Parameter viewController: The `UIViewController` in which the widget will be shown as a sheet. func show(in viewController: UIViewController) { - showSheet(in: viewController) + var pendingSecureConversationExists = false + environment.pendingSecureConversationStatusUpdates { hasPendingSecureConversation in + pendingSecureConversationExists = hasPendingSecureConversation + } + if pendingSecureConversationExists { + do { + try environment.engagementLauncher.startSecureMessaging() + } catch { + viewState = .error + } + } else { + showSheet(in: viewController) + } } /// Embeds the widget as a view in the given parent view. diff --git a/GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift b/GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift index 5e81a2101..369896e21 100644 --- a/GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift +++ b/GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift @@ -5,8 +5,9 @@ import GliaCoreSDK @testable import GliaWidgets class EntryWidgetTests: XCTestCase { - private enum Call { + private enum Call: Equatable { case observeSecureUnreadMessageCount + case start(EngagementKind) } func test_secureMessagingIsHiddenWhenUserIsNotAuthenticated() { @@ -21,22 +22,13 @@ class EntryWidgetTests: XCTestCase { completion(.success(mockQueue)) return UUID.mock.uuidString } - let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) - - let engagementLauncher = EngagementLauncher { _, _ in } - let isAuthenticated: () -> Bool = { false } + var environment = EntryWidget.Environment.mock() + environment.isAuthenticated = { false } + environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) let entryWidget = EntryWidget( queueIds: [mockQueueId], - environment: .init( - observeSecureUnreadMessageCount: { _ in UUID.mock.uuidString }, - unsubscribeFromUpdates: { _, _ in }, - queuesMonitor: queuesMonitor, - engagementLauncher: engagementLauncher, - theme: .mock(), - log: .mock, - isAuthenticated: isAuthenticated - ) + environment: environment ) entryWidget.show(in: .init()) @@ -60,21 +52,12 @@ class EntryWidgetTests: XCTestCase { completion(.success(mockQueue)) return UUID.mock.uuidString } - let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) - let engagementLauncher = EngagementLauncher { _, _ in } - let isAuthenticated: () -> Bool = { true } + var environment = EntryWidget.Environment.mock() + environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) let entryWidget = EntryWidget( queueIds: [mockQueueId], - environment: .init( - observeSecureUnreadMessageCount: { _ in UUID.mock.uuidString }, - unsubscribeFromUpdates: { _, _ in }, - queuesMonitor: queuesMonitor, - engagementLauncher: engagementLauncher, - theme: .mock(), - log: .mock, - isAuthenticated: isAuthenticated - ) + environment: environment ) entryWidget.show(in: .init()) @@ -97,27 +80,18 @@ class EntryWidgetTests: XCTestCase { completion(.success(mockQueue)) return UUID.mock.uuidString } - let queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) - - let engagementLauncher = EngagementLauncher { _, _ in } - let isAuthenticated: () -> Bool = { true } - let observeMessageCount: (_ completion: @escaping (Result) -> Void) -> String? = { completion in completion(.success(5)) return UUID.mock.uuidString } + var environment = EntryWidget.Environment.mock() + environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment) + environment.observeSecureUnreadMessageCount = observeMessageCount + let entryWidget = EntryWidget( queueIds: [mockQueueId], - environment: .init( - observeSecureUnreadMessageCount: observeMessageCount, - unsubscribeFromUpdates: { _, _ in }, - queuesMonitor: queuesMonitor, - engagementLauncher: engagementLauncher, - theme: .mock(), - log: .mock, - isAuthenticated: isAuthenticated - ) + environment: environment ) entryWidget.show(in: .init()) @@ -138,20 +112,35 @@ class EntryWidgetTests: XCTestCase { return mockSubscriptionId } + var environment = EntryWidget.Environment.mock() + environment.observeSecureUnreadMessageCount = observeMessageCount + let entryWidget = EntryWidget( queueIds: [UUID.mock.uuidString], - environment: .init( - observeSecureUnreadMessageCount: observeMessageCount, - unsubscribeFromUpdates: { _, _ in }, - queuesMonitor: .mock(), - engagementLauncher: EngagementLauncher { _, _ in }, - theme: .mock(), - log: .mock, - isAuthenticated: { true } - ) + environment: environment ) XCTAssertEqual(envCalls, [.observeSecureUnreadMessageCount]) XCTAssertEqual(entryWidget.unreadSecureMessageSubscriptionId, mockSubscriptionId) } + + func test_secureMessagingIsOpenedIfPendingSecureConversationExists() { + var envCalls: [Call] = [] + + let engagementLauncher = EngagementLauncher { engagementKind, _ in + envCalls.append(.start(engagementKind)) + } + var environment = EntryWidget.Environment.mock() + environment.pendingSecureConversationStatusUpdates = { $0(true) } + environment.engagementLauncher = engagementLauncher + + let entryWidget = EntryWidget( + queueIds: [UUID.mock.uuidString], + environment: environment + ) + + entryWidget.show(in: .init()) + + XCTAssertEqual(envCalls, [.start(.messaging(.welcome))]) + } }