Skip to content

Commit

Permalink
Add pending secure messaging logic to EntryWidget
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Egor Egorov authored and EgorovEI committed Nov 22, 2024
1 parent 78a8175 commit 7b2be9e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 53 deletions.
3 changes: 2 additions & 1 deletion GliaWidgets/Public/Glia/Glia+EntryWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extension EntryWidget {
var theme: Theme
var log: CoreSdkClient.Logger
var isAuthenticated: () -> Bool
var pendingSecureConversationStatusUpdates: CoreSdkClient.PendingSecureConversationStatusUpdates
}
}

Expand All @@ -23,7 +24,8 @@ extension EntryWidget.Environment {
engagementLauncher: engagementLauncher,
theme: .mock(),
log: .mock,
isAuthenticated: { true }
isAuthenticated: { true },
pendingSecureConversationStatusUpdates: { _ in }
)
}
}
Expand Down
16 changes: 14 additions & 2 deletions GliaWidgets/Sources/EntryWidget/EntryWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
87 changes: 38 additions & 49 deletions GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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<Int?, Error>) -> 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())
Expand All @@ -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))])
}
}

0 comments on commit 7b2be9e

Please sign in to comment.