Skip to content

Commit

Permalink
Fix keeping wrong interactor state after CV engagement
Browse files Browse the repository at this point in the history
MOB-3901
  • Loading branch information
Egor Egorov authored and EgorovEI committed Dec 23, 2024
1 parent 6472f01 commit 22ff2eb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
14 changes: 12 additions & 2 deletions GliaWidgets/Sources/CallVisualizer/CallVisualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ extension CallVisualizer {

func endSession() {
coordinator.end()
delegate?(.engagementEnded)
environment.interactorProviding()?.endSession(
success: { [weak self] in
self?.delegate?(.engagementEnded)
},
failure: { [weak self] _ in
self?.delegate?(.engagementEnded)
}
)
}

func handleRestoredEngagement() {
Expand All @@ -124,9 +131,12 @@ extension CallVisualizer {
extension CallVisualizer {
func startObservingInteractorEvents() {
environment.interactorProviding()?.addObserver(self) { [weak self] event in
if case .stateChanged(.ended(.byOperator)) = event {
if case .stateChanged(.ended(.byOperator)) = event,
let endedEngagement = self?.environment.interactorProviding()?.currentEngagement,
endedEngagement.source == .callVisualizer {
self?.endSession()
self?.environment.log.prefixed(Self.self).info("Call visualizer engagement ended")
return
}

guard
Expand Down
10 changes: 9 additions & 1 deletion GliaWidgets/Sources/Interactor/Interactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Interactor {
}

let visitorContext: Configuration.VisitorContext?
@Published var currentEngagement: CoreSdkClient.Engagement?
@Published private(set) var currentEngagement: CoreSdkClient.Engagement?

private var observers = [() -> (AnyObject?, EventHandler)]()

Expand Down Expand Up @@ -417,3 +417,11 @@ extension InteractorState: Equatable {
}
}
}

#if DEBUG
extension Interactor {
func setCurrentEngagement(_ engagement: CoreSdkClient.Engagement?) {
currentEngagement = engagement
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EngagementCoordinatorSurveyTests: XCTestCase {
media: .init(audio: .none, video: .oneWay)
)
let interactor = Interactor.mock(environment: .init(coreSdk: coreSdkClient, queuesMonitor: .mock(), gcd: .failing, log: .failing))
interactor.currentEngagement = engagement
interactor.setCurrentEngagement(engagement)
var alertManagerEnv = AlertManager.Environment.failing()
var log = CoreSdkClient.Logger.failing
log.prefixedClosure = { _ in log }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ final class EngagementCoordinatorTests: XCTestCase {

func test_endChatWithSurvey() throws {
let survey: CoreSdkClient.Survey = try .mock()
coordinator.interactor.currentEngagement = .mock(fetchSurvey: { _, completion in completion(.success(survey)) })
let engagement: CoreSdkClient.Engagement = .mock(fetchSurvey: { _, completion in completion(.success(survey)) })
coordinator.interactor.setCurrentEngagement(engagement)
coordinator.end()

let surveyViewController = coordinator.gliaPresenter.topMostViewController as? Survey.ViewController
Expand All @@ -131,7 +132,8 @@ final class EngagementCoordinatorTests: XCTestCase {
let survey: CoreSdkClient.Survey = try .mock()
coordinator.start()

coordinator.interactor.currentEngagement = .mock(fetchSurvey: { _, completion in completion(.success(survey)) })
let engagement: CoreSdkClient.Engagement = .mock(fetchSurvey: { _, completion in completion(.success(survey)) })
coordinator.interactor.setCurrentEngagement(engagement)
coordinator.end(surveyPresentation: .doNotPresentSurvey)

XCTAssertEqual(coordinator.navigationPresenter.viewControllers.count, 0)
Expand Down
10 changes: 5 additions & 5 deletions GliaWidgetsTests/Sources/EntryWidget/EntryWidgetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class EntryWidgetTests: XCTestCase {
environment.isAuthenticated = { false }
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let interactor: Interactor = .mock()
interactor.currentEngagement = .mock()
interactor.setCurrentEngagement(.mock())
environment.currentInteractor = { interactor }

let entryWidget = EntryWidget(
Expand Down Expand Up @@ -278,7 +278,7 @@ class EntryWidgetTests: XCTestCase {
environment.isAuthenticated = { false }
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let interactor: Interactor = .mock()
interactor.currentEngagement = .mock(media: .init(audio: .twoWay, video: nil))
interactor.setCurrentEngagement(.mock(media: .init(audio: .twoWay, video: nil)))
environment.currentInteractor = { interactor }

let entryWidget = EntryWidget(
Expand Down Expand Up @@ -308,7 +308,7 @@ class EntryWidgetTests: XCTestCase {
environment.isAuthenticated = { false }
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let interactor: Interactor = .mock()
interactor.currentEngagement = .mock(media: .init(audio: .twoWay, video: .twoWay))
interactor.setCurrentEngagement(.mock(media: .init(audio: .twoWay, video: .twoWay)))
environment.currentInteractor = { interactor }

let entryWidget = EntryWidget(
Expand Down Expand Up @@ -338,7 +338,7 @@ class EntryWidgetTests: XCTestCase {
environment.isAuthenticated = { false }
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let interactor: Interactor = .mock()
interactor.currentEngagement = .mock(source: .callVisualizer)
interactor.setCurrentEngagement(.mock(source: .callVisualizer))
environment.currentInteractor = { interactor }

let entryWidget = EntryWidget(
Expand Down Expand Up @@ -371,7 +371,7 @@ class EntryWidgetTests: XCTestCase {
var environment = EntryWidget.Environment.mock()
environment.queuesMonitor = QueuesMonitor(environment: queueMonitorEnvironment)
let interactor: Interactor = .mock()
interactor.currentEngagement = .mock(source: .callVisualizer)
interactor.setCurrentEngagement(.mock(source: .callVisualizer))
environment.currentInteractor = { interactor }
environment.onCallVisualizerResume = {
calls.append(.onCallVisualizerResume)
Expand Down
10 changes: 5 additions & 5 deletions GliaWidgetsTests/Sources/Glia/GliaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ final class GliaTests: XCTestCase {
theme: .mock()
) { _ in }

sdk.environment.coreSdk.getCurrentEngagement = { .mock(source: .callVisualizer) }
sdk.interactor?.setCurrentEngagement(.mock(source: .callVisualizer))
sdk.interactor?.state = .ended(.byOperator)

XCTAssertEqual(calls, [.onEvent(.ended)])
Expand Down Expand Up @@ -202,7 +202,7 @@ final class GliaTests: XCTestCase {

XCTAssertEqual(calls, [])

sdk.environment.coreSdk.getCurrentEngagement = { .mock(source: .callVisualizer) }
sdk.interactor?.setCurrentEngagement(.mock(source: .callVisualizer))
sdk.interactor?.state = .ended(.byOperator)

/// Since interactor is created only after visitor code is requested,
Expand Down Expand Up @@ -247,7 +247,7 @@ final class GliaTests: XCTestCase {
with: .mock(),
theme: .mock()
) { _ in }
sdk.environment.coreSdk.getCurrentEngagement = { .mock(source: .callVisualizer) }
sdk.interactor?.setCurrentEngagement(.mock(source: .callVisualizer))

sdk.callVisualizer.coordinator.showEndScreenSharingViewController()
sdk.interactor?.state = .ended(.byOperator)
Expand Down Expand Up @@ -295,7 +295,7 @@ final class GliaTests: XCTestCase {
with: .mock(),
theme: .mock()
) { _ in }
sdk.environment.coreSdk.getCurrentEngagement = { .mock(source: .callVisualizer) }
sdk.interactor?.setCurrentEngagement(.mock(source: .callVisualizer))

sdk.callVisualizer.coordinator.showVideoCallViewController()
sdk.interactor?.state = .ended(.byOperator)
Expand Down Expand Up @@ -654,7 +654,7 @@ final class GliaTests: XCTestCase {
with: .mock(),
theme: .mock()
) { _ in }
sdk.environment.coreSdk.getCurrentEngagement = { .mock(source: .callVisualizer) }
sdk.interactor?.setCurrentEngagement(.mock(source: .callVisualizer))
sdk.onEvent = { event in
switch event {
case .ended:
Expand Down

0 comments on commit 22ff2eb

Please sign in to comment.