-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Isolate pending interaction logic in separate model
Introduce 'PendingInteraction' model to determine pending interaction for SC 2.0. MOB-3850
1 parent
90246b6
commit d507d17
Showing
19 changed files
with
460 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
GliaWidgets/SecureConversations/SecureConversations.PendingInteraction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import Combine | ||
|
||
extension SecureConversations { | ||
final class PendingInteraction: ObservableObject { | ||
@Published private(set) var pendingStatus = false | ||
@Published private(set) var unreadMessageCount = 0 | ||
@Published private(set) var hasPendingInteraction = false | ||
private let environment: Environment | ||
private(set) var pendingStatusCancellationToken: String? | ||
private(set) var unreadMessageCountCancellationToken: String? | ||
|
||
init(environment: Environment) { | ||
self.environment = environment | ||
self.pendingStatusCancellationToken = environment.observePendingSecureConversationsStatus { [weak self] result in | ||
guard let self else { return } | ||
// At this point it is enough to know if there is a pending conversation, | ||
// so no need to handle error. | ||
pendingStatus = (try? result.get()) ?? false | ||
} | ||
self.unreadMessageCountCancellationToken = environment.observeSecureConversationsUnreadMessageCount { [weak self] result in | ||
guard let self else { return } | ||
// At this point it is enough to know if there is an unread message count, | ||
// so no need to handle error. | ||
unreadMessageCount = (try? result.get()) ?? 0 | ||
} | ||
|
||
$pendingStatus.combineLatest($unreadMessageCount) | ||
.map { hasPending, unreadCount in | ||
hasPending || unreadCount > 0 | ||
} | ||
.assign(to: &$hasPendingInteraction) | ||
} | ||
|
||
deinit { | ||
if let unreadMessageCountCancellationToken { | ||
environment.unsubscribeFromUnreadCount(unreadMessageCountCancellationToken) | ||
} | ||
|
||
if let pendingStatusCancellationToken { | ||
environment.unsubscribeFromPendingStatus(pendingStatusCancellationToken) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension SecureConversations.PendingInteraction { | ||
struct Environment { | ||
var observePendingSecureConversationsStatus: CoreSdkClient.ObservePendingSecureConversationStatus | ||
var observeSecureConversationsUnreadMessageCount: CoreSdkClient.SubscribeForUnreadSCMessageCount | ||
var unsubscribeFromUnreadCount: CoreSdkClient.UnsubscribeFromUnreadCount | ||
var unsubscribeFromPendingStatus: CoreSdkClient.UnsubscribeFromPendingSCStatus | ||
} | ||
} | ||
|
||
extension SecureConversations.PendingInteraction.Environment { | ||
init(with client: CoreSdkClient) { | ||
self.observePendingSecureConversationsStatus = client.observePendingSecureConversationStatus | ||
self.observeSecureConversationsUnreadMessageCount = client.subscribeForUnreadSCMessageCount | ||
self.unsubscribeFromPendingStatus = client.unsubscribeFromPendingSecureConversationStatus | ||
self.unsubscribeFromUnreadCount = client.unsubscribeFromUnreadCount | ||
} | ||
} | ||
|
||
#if DEBUG | ||
extension SecureConversations.PendingInteraction.Environment { | ||
static let mock = Self( | ||
observePendingSecureConversationsStatus: { _ in nil }, | ||
observeSecureConversationsUnreadMessageCount: { _ in nil }, | ||
unsubscribeFromUnreadCount: { _ in }, | ||
unsubscribeFromPendingStatus: { _ in } | ||
) | ||
} | ||
|
||
extension SecureConversations.PendingInteraction { | ||
static func mock(environment: Environment = .mock) -> Self { | ||
.init(environment: environment) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
GliaWidgetsTests/SecureConversations/SecureConversations.PendingInteraction.Failing.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@testable import GliaWidgets | ||
|
||
extension SecureConversations.PendingInteraction.Environment { | ||
static let failing = Self( | ||
observePendingSecureConversationsStatus: { _ in | ||
fail("\(Self.self).observePendingSecureConversationsStatus") | ||
return nil | ||
}, | ||
observeSecureConversationsUnreadMessageCount: { _ in | ||
fail("\(Self.self).observeSecureConversationsUnreadMessageCount") | ||
return nil | ||
}, | ||
unsubscribeFromUnreadCount: { _ in | ||
fail("\(Self.self).unsubscribeFromUnreadCount") | ||
}, | ||
unsubscribeFromPendingStatus: { _ in | ||
fail("\(Self.self).unsubscribeFromPendingStatus") | ||
} | ||
) | ||
} | ||
|
||
extension SecureConversations.PendingInteraction { | ||
static func failing() -> Self { | ||
.init(environment: .failing) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
GliaWidgetsTests/SecureConversations/SecureConversations.PendingInteractionTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
@testable import GliaWidgets | ||
import XCTest | ||
|
||
final class SecureConversationsPendingInteractionTests: XCTestCase { | ||
func test_hasPendingInteractionGetChangedBasedOnUnreadCountAndPendingStatus() throws { | ||
var environment = SecureConversations.PendingInteraction.Environment.failing | ||
let uuidGen = UUID.incrementing | ||
var pendingCallback: ((Result<Bool, Error>) -> Void)? | ||
environment.observePendingSecureConversationsStatus = { callback in | ||
pendingCallback = callback | ||
return uuidGen().uuidString | ||
} | ||
var unreadCountCallback: ((Result<Int?, Error>) -> Void)? | ||
environment.observeSecureConversationsUnreadMessageCount = { callback in | ||
unreadCountCallback = callback | ||
return uuidGen().uuidString | ||
} | ||
environment.unsubscribeFromPendingStatus = { _ in } | ||
environment.unsubscribeFromUnreadCount = { _ in } | ||
|
||
let pendingInteraction = SecureConversations.PendingInteraction(environment: environment) | ||
// Assert initial pending interaction is false. | ||
XCTAssertFalse(pendingInteraction.hasPendingInteraction) | ||
// Affect pending secure conversations value by setting it to `true` and assert `hasPendingInteraction`, | ||
// then set it back to `false` and assert again. | ||
try XCTUnwrap(pendingCallback)(.success(true)) | ||
XCTAssertTrue(pendingInteraction.hasPendingInteraction) | ||
try XCTUnwrap(pendingCallback)(.success(false)) | ||
XCTAssertFalse(pendingInteraction.hasPendingInteraction) | ||
// Affect unread count value by setting it to `1` and assert `hasPendingInteraction`, | ||
// then set it back to `0` and `nil` and assert again. | ||
try XCTUnwrap(unreadCountCallback)(.success(1)) | ||
XCTAssertTrue(pendingInteraction.hasPendingInteraction) | ||
try XCTUnwrap(unreadCountCallback)(.success(0)) | ||
try XCTUnwrap(unreadCountCallback)(.success(nil)) | ||
XCTAssertFalse(pendingInteraction.hasPendingInteraction) | ||
} | ||
|
||
func test_unsubscribeIsCalledOnDeinit() { | ||
enum Call { | ||
case unsubscribeFromPendingStatus | ||
case unsubscribeFromUnreadCount | ||
} | ||
var calls: [Call] = [] | ||
var environment = SecureConversations.PendingInteraction.Environment.failing | ||
let uuidGen = UUID.incrementing | ||
environment.observePendingSecureConversationsStatus = { _ in uuidGen().uuidString } | ||
environment.observeSecureConversationsUnreadMessageCount = { _ in uuidGen().uuidString } | ||
environment.unsubscribeFromPendingStatus = { _ in | ||
calls.append(.unsubscribeFromPendingStatus) | ||
} | ||
environment.unsubscribeFromUnreadCount = { _ in | ||
calls.append(.unsubscribeFromUnreadCount) | ||
} | ||
var pendingInteraction = SecureConversations.PendingInteraction(environment: environment) | ||
pendingInteraction = .mock() | ||
_ = pendingInteraction | ||
XCTAssertEqual(calls, [.unsubscribeFromUnreadCount, .unsubscribeFromPendingStatus]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.