Skip to content

Commit

Permalink
fixup! Update SC availability taking into account 'transferring' state
Browse files Browse the repository at this point in the history
Introduce 'transferred' available state.
  • Loading branch information
igorkravchenko committed Dec 9, 2024
1 parent 75698b5 commit 9cdb97d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ extension SecureConversations {
availability.checkSecureConversationsAvailability(for: environment.queueIds) { [weak self] result in
guard let self else { return }
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
self.environment.queueIds = queueIds
self.isSecureConversationsAvailable = true
self.fileUploadListModel.isEnabled = true
case .success(.available(.transferred)):
self.environment.queueIds = []
self.isSecureConversationsAvailable = true
self.fileUploadListModel.isEnabled = true
case .failure, .success(.unavailable(.emptyQueue)), .success(.unavailable(.unauthenticated)):
// For chat screen we no longer show unavailability dialog, but unavailability banner instead.
self.isSecureConversationsAvailable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension SecureConversations {
if let engagement = environment.getCurrentEngagement(),
engagement.status == .transferring,
engagement.capabilities?.text == true {
completion(.success(.available(queueIds: [])))
completion(.success(.available(.transferred)))
return
}

Expand All @@ -57,7 +57,7 @@ extension SecureConversations {
let queueIds = filteredQueues.map { $0.id }

environment.log.info("Secure Messaging is available in queues with IDs: \(queueIds).")
completion(.success(.available(queueIds: queueIds)))
completion(.success(.available(.queues(queueIds: queueIds))))
}

private var defaultPredicate: (CoreSdkClient.Queue) -> Bool {
Expand All @@ -81,7 +81,11 @@ extension SecureConversations.Availability {

extension SecureConversations.Availability {
enum Status: Equatable {
case available(queueIds: [String] = [])
enum Available: Equatable {
case queues(queueIds: [String])
case transferred
}
case available(Available)
case unavailable(UnavailabilityReason)

static func == (lhs: Status, rhs: Status) -> Bool {
Expand All @@ -94,6 +98,15 @@ extension SecureConversations.Availability {
return false
}
}

var isAvailable: Bool {
switch self {
case .available:
true
case .unavailable:
false
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension SecureConversations {
/// Flag indicating attachment(s) availability.
/// By default attachments are not available, until site configurations are fetched.
private(set) var isAttachmentsAvailable: Bool = false { didSet { reportChange() } }
var availabilityStatus: Availability.Status = .available(queueIds: []) { didSet { reportChange() } }
var availabilityStatus: Availability.Status = .available(.queues(queueIds: [])) { didSet { reportChange() } }
var messageInputState: MessageInputState = .normal { didSet { reportChange() } }
var sendMessageRequestState: SendMessageRequestState = .waiting { didSet { reportChange() } }

Expand Down Expand Up @@ -64,9 +64,12 @@ extension SecureConversations {
availability.checkSecureConversationsAvailability(for: environment.queueIds) { [weak self] result in
guard let self else { return }
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
self.environment.queueIds = queueIds
self.availabilityStatus = .available(queueIds: queueIds)
self.availabilityStatus = .available(.queues(queueIds: queueIds))
case .success(.available(.transferred)):
self.environment.queueIds = []
self.availabilityStatus = .available(.transferred)
case .success(.unavailable(.emptyQueue)), .failure:
self.availabilityStatus = .unavailable(.emptyQueue)
self.delegate?(.showAlert(.unavailableMessageCenter()))
Expand Down Expand Up @@ -178,7 +181,7 @@ extension SecureConversations.WelcomeViewModel {
) -> SecureConversations.WelcomeStyle {
var style = instance.environment.welcomeStyle

if instance.availabilityStatus != .available() {
if !instance.availabilityStatus.isAvailable {
style.messageTitleStyle = nil
}

Expand All @@ -191,7 +194,7 @@ extension SecureConversations.WelcomeViewModel {
var filePickerButton: WelcomeViewProps.FilePickerButton?

let isFilePickerEnabled = !instance.fileUploadListModel.isLimitReached
if instance.availabilityStatus == .available(), instance.isAttachmentsAvailable {
if instance.availabilityStatus.isAvailable, instance.isAttachmentsAvailable {
filePickerButton = WelcomeViewProps.FilePickerButton(
isEnabled: isFilePickerEnabled,
tap: Command { originView in
Expand All @@ -203,7 +206,7 @@ extension SecureConversations.WelcomeViewModel {
return filePickerButton
}
static func textViewState(for instance: SecureConversations.WelcomeViewModel) -> TextViewProps? {
guard instance.availabilityStatus == .available() else {
guard instance.availabilityStatus.isAvailable else {
return nil
}

Expand Down Expand Up @@ -254,7 +257,7 @@ extension SecureConversations.WelcomeViewModel {
for instance: SecureConversations.WelcomeViewModel
) -> SendMessageButton? {
// Is service available?
guard instance.availabilityStatus == .available() else {
guard instance.availabilityStatus.isAvailable else {
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions GliaWidgetsTests/SecureConversations/AvailabilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class AvailabilityTests: XCTestCase {
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(.available(queueIds)):
XCTAssertEqual(queueIds, [queueId])
XCTAssertEqual(queueIds, .queues(queueIds: [queueId]))
default:
XCTFail("Result should be `.success(.available)`")
}
Expand Down Expand Up @@ -174,7 +174,7 @@ final class AvailabilityTests: XCTestCase {
}
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
XCTAssertEqual(queueIds, [queueId])
default:
XCTFail("Result should be `.success(.available)`")
Expand Down Expand Up @@ -224,8 +224,8 @@ final class AvailabilityTests: XCTestCase {
}
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(.available(queueIds)):
XCTAssertEqual(queueIds, [])
case let .success(status):
XCTAssertEqual(status, .available(.transferred))
default:
XCTFail("Result should be `.success(.available)`. Got `\(result)` instead.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

viewModel = .init(environment: environment, availability: .mock)
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))

if case .welcome(let props) = viewModel.props() {
XCTAssertNotNil(props.filePickerButton)
Expand Down Expand Up @@ -335,7 +335,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateFileUploads() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
let uploadFile: FileUpload.Environment.UploadFile = .toSecureMessaging { file, progress, completion in
completion(.failure(CoreSdkClient.GliaCoreError(reason: "")))
return .mock
Expand All @@ -354,7 +354,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateInProgressFileUpload() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
let fileUpload: FileUpload = .mock()
fileUpload.startUpload()
viewModel.fileUploadListModel.environment.uploader.uploads = [fileUpload]
Expand All @@ -367,7 +367,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateEmptyText() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
viewModel.messageText = ""

if case .welcome(let props) = viewModel.props() {
Expand All @@ -378,7 +378,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateSuccessfulUpload() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
viewModel.fileUploadListModel.environment.uploader = FileUploader(maximumUploads: 100, environment: .mock)
viewModel.messageText = ""

Expand All @@ -390,7 +390,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateNoQueues() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
viewModel.messageText = "text"
viewModel.fileUploadListModel.environment.uploader = FileUploader(maximumUploads: 100, environment: .mock)
viewModel.environment.queueIds = []
Expand All @@ -403,7 +403,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateLoadingMessageRequestState() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
viewModel.messageText = "text"
viewModel.fileUploadListModel.environment.uploader = FileUploader(maximumUploads: 100, environment: .mock)
viewModel.environment.queueIds = [""]
Expand All @@ -417,7 +417,7 @@ extension SecureConversationsWelcomeViewModelTests {
}

func testSendMessageButtonStateWaitingMessageRequestState() {
viewModel.availabilityStatus = .available()
viewModel.availabilityStatus = .available(.queues(queueIds: []))
viewModel.messageText = "text"
viewModel.fileUploadListModel.environment.uploader = FileUploader(maximumUploads: 100, environment: .mock)
viewModel.environment.queueIds = [""]
Expand Down Expand Up @@ -623,7 +623,7 @@ extension SecureConversationsWelcomeViewModelTests {

viewModel = .init(environment: .mock(queueIds: [uuid]), availability: availability)

XCTAssertEqual(viewModel.availabilityStatus, .available())
XCTAssertEqual(viewModel.availabilityStatus, .available(.queues(queueIds: [])))
}

func testAvailabilityUnavailableEmptyQueues() {
Expand Down

0 comments on commit 9cdb97d

Please sign in to comment.