diff --git a/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+Networking.swift b/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+Networking.swift deleted file mode 100644 index 5988aae01..000000000 --- a/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+Networking.swift +++ /dev/null @@ -1,67 +0,0 @@ -import BitwardenKit -import BitwardenResources -import BitwardenSdk -import Foundation - -// MARK: Alert+Networking - -extension Alert { - /// An alert shown to the user when they aren't connected to the internet. - /// - /// - Parameter tryAgain: An action allowing the user to retry the request. - /// - /// - Returns: An alert notifying the user that they aren't connected to the internet. - /// - static func internetConnectionError(_ tryAgain: (() async -> Void)? = nil) -> Alert { - Alert( - title: Localizations.internetConnectionRequiredTitle, - message: Localizations.internetConnectionRequiredMessage, - alertActions: [ - AlertAction(title: Localizations.tryAgain, style: .default) { _ in - if let tryAgain { - await tryAgain() - } - }, - AlertAction(title: Localizations.cancel, style: .cancel), - ], - ) - } - - /// Creates an alert for the networking error that was received. - /// - /// - Parameters: - /// - error: The networking error that occurred. - /// - tryAgain: An action allowing the user to retry the request. - /// - /// - Returns: An alert notifying the user that a networking error occurred. - /// - static func networkResponseError( - _ error: Error, - _ tryAgain: (() async -> Void)? = nil, - ) -> Alert { - switch error { - case let serverError as ServerError: - defaultAlert( - title: Localizations.anErrorHasOccurred, - message: serverError.message, - ) - case let error as URLError where error.code == .notConnectedToInternet || error.code == .networkConnectionLost: - internetConnectionError(tryAgain) - case let error as URLError where error.code == .timedOut: - defaultAlert( - title: Localizations.anErrorHasOccurred, - message: error.localizedDescription, - alertActions: [ - AlertAction(title: Localizations.tryAgain, style: .default) { _ in - if let tryAgain { - await tryAgain() - } - }, - AlertAction(title: Localizations.cancel, style: .cancel), - ], - ) - default: - defaultAlert(title: Localizations.anErrorHasOccurred) - } - } -} diff --git a/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift b/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift deleted file mode 100644 index e7d9da5e0..000000000 --- a/AuthenticatorShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift +++ /dev/null @@ -1,102 +0,0 @@ -// swiftlint:disable:this file_name - -import BitwardenKit -import BitwardenKitMocks -import BitwardenResources -import Foundation -import Networking -import TestHelpers -import XCTest - -@testable import AuthenticatorShared - -// MARK: - Alert+NetworkingTests - -class AlertNetworkingTests: BitwardenTestCase { - // MARK: Tests - - /// Tests the `networkConnectionLostError` alert contains the correct properties. - func test_networkConnectionLost() { - let urlError = URLError(.networkConnectionLost) - let subject = Alert.networkResponseError(urlError) {} - - XCTAssertEqual(subject.title, Localizations.internetConnectionRequiredTitle) - XCTAssertEqual(subject.message, Localizations.internetConnectionRequiredMessage) - XCTAssertEqual(subject.preferredStyle, .alert) - XCTAssertEqual(subject.alertActions.count, 2) - - let action = subject.alertActions[0] - XCTAssertEqual(action.title, Localizations.tryAgain) - XCTAssertEqual(action.style, .default) - XCTAssertNotNil(action.handler) - } - - /// `.networkResponseError` builds an alert to display a server error message. - func test_networkResponseError_serverError() throws { - let response = HTTPResponse.failure(statusCode: 400, body: APITestData.bitwardenErrorMessage.data) - let error = try ServerError.error(errorResponse: ErrorResponseModel(response: response)) - let subject = Alert.networkResponseError(error) - - XCTAssertEqual(subject.title, Localizations.anErrorHasOccurred) - XCTAssertEqual(subject.message, "You do not have permissions to edit this.") - XCTAssertEqual(subject.preferredStyle, .alert) - XCTAssertEqual(subject.alertActions.count, 1) - - let action = subject.alertActions[0] - XCTAssertEqual(action.title, Localizations.ok) - XCTAssertEqual(action.style, .cancel) - XCTAssertNil(action.handler) - } - - /// Tests the `internetConnectionError` alert contains the correct properties. - func test_noInternetConnection() { - let urlError = URLError(.notConnectedToInternet) - let subject = Alert.networkResponseError(urlError) {} - - XCTAssertEqual(subject.title, Localizations.internetConnectionRequiredTitle) - XCTAssertEqual(subject.message, Localizations.internetConnectionRequiredMessage) - XCTAssertEqual(subject.preferredStyle, .alert) - XCTAssertEqual(subject.alertActions.count, 2) - - let action = subject.alertActions[0] - XCTAssertEqual(action.title, Localizations.tryAgain) - XCTAssertEqual(action.style, .default) - XCTAssertNotNil(action.handler) - } - - /// `ResponseValidationError` builds an alert to display a server error message. - func test_responseValidationError() throws { - let response = HTTPResponse.failure(statusCode: 400, body: APITestData.responseValidationError.data) - let error = try ServerError.validationError( - validationErrorResponse: ResponseValidationErrorModel( - response: response, - ), - ) - let subject = Alert.networkResponseError(error) - - XCTAssertEqual(subject.title, Localizations.anErrorHasOccurred) - XCTAssertEqual(subject.message, "Username or password is incorrect. Try again.") - XCTAssertEqual(subject.preferredStyle, .alert) - XCTAssertEqual(subject.alertActions.count, 1) - - let action = subject.alertActions[0] - XCTAssertEqual(action.title, Localizations.ok) - XCTAssertEqual(action.style, .cancel) - XCTAssertNil(action.handler) - } - - /// Tests the `timeoutError` alert contains the correct properties. - func test_timeoutError() { - let urlError = URLError(.timedOut) - let subject = Alert.networkResponseError(urlError) {} - - XCTAssertEqual(subject.message, urlError.localizedDescription) - XCTAssertEqual(subject.preferredStyle, .alert) - XCTAssertEqual(subject.alertActions.count, 2) - - let action = subject.alertActions[0] - XCTAssertEqual(action.title, Localizations.tryAgain) - XCTAssertEqual(action.style, .default) - XCTAssertNotNil(action.handler) - } -} diff --git a/BitwardenShared/UI/Platform/Application/Extensions/Alert+Networking.swift b/BitwardenKit/UI/Platform/Application/Extensions/Alert+Networking.swift similarity index 98% rename from BitwardenShared/UI/Platform/Application/Extensions/Alert+Networking.swift rename to BitwardenKit/UI/Platform/Application/Extensions/Alert+Networking.swift index 9e7fbc671..87b13db35 100644 --- a/BitwardenShared/UI/Platform/Application/Extensions/Alert+Networking.swift +++ b/BitwardenKit/UI/Platform/Application/Extensions/Alert+Networking.swift @@ -1,11 +1,9 @@ -import BitwardenKit import BitwardenResources -import BitwardenSdk import Foundation // MARK: Alert+Networking -extension Alert { +public extension Alert { /// An alert shown to the user when they aren't connected to the internet. /// /// - Parameter tryAgain: An action allowing the user to retry the request. diff --git a/BitwardenShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift b/BitwardenKit/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift similarity index 99% rename from BitwardenShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift rename to BitwardenKit/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift index a7b22d2fd..0d2cadb69 100644 --- a/BitwardenShared/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift +++ b/BitwardenKit/UI/Platform/Application/Extensions/Alert+NetworkingTests.swift @@ -6,8 +6,6 @@ import Networking import TestHelpers import XCTest -@testable import BitwardenShared - // MARK: - Alert+NetworkingTests class AlertNetworkingTests: BitwardenTestCase {