From 7a87b5f04c3c0e59ec83d4d8324101e9007f73b5 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:22:22 +0200 Subject: [PATCH] Fix a crash when opening the camera while it's unavailable. (#6388) * Fix a crash when opening the camera while it's unavailable. The app set the image picker's source to the camera without checking it could be used. iOS throws when it can't: when Screen Time or a device management profile blocks the camera, or while it's already in use. Check AppMediator.isCameraAvailable when Camera or Take photo is tapped in the room and thread composers, profile editing, room editing and room creation. If it's unavailable, show a toast instead of opening the picker. Also handle Cancel in the camera picker so the sheet closes through the coordinator, like the photo library and document pickers. Sentry: ELEMENT-X-IOS-SP * Update the Enterprise submodule. Picks up the create room hook test fix that passes the new appMediator argument. * Use the Localazy string for the camera unavailable toast. The copy is now in Localazy as error_camera_unavailable ("The camera is not available"), so it replaces the untranslated string. * Order appMediator consistently with CommonFlowParameters. Move appMediator before analytics and userIndicatorController in the create room, room details edit and user details edit screens, their call sites and tests. Co-authored-by: Velin92 <34335419+Velin92@users.noreply.github.com> * Update the Enterprise submodule. Picks up the create room hook test passing appMediator before analytics, to match the new init order. * Update the Enterprise submodule. Points at Enterprise main now that element-ios-enterprise#42 is merged. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .../en-US.lproj/Localizable.strings | 1 + .../en.lproj/Localizable.strings | 1 + .../Sources/Application/AppMediator.swift | 4 ++ .../Application/AppMediatorProtocol.swift | 3 ++ .../RoomFlowCoordinator.swift | 4 +- .../SettingsFlowCoordinator.swift | 4 +- .../SpaceSettingsFlowCoordinator.swift | 4 +- .../StartChatFlowCoordinator.swift | 5 ++- ElementX/Sources/Generated/Strings.swift | 2 + ElementX/Sources/Mocks/AppMediatorMock.swift | 2 + .../Mocks/Generated/GeneratedMocks.swift | 5 +++ .../CreateRoomScreenCoordinator.swift | 4 +- .../CreateRoomScreenViewModel.swift | 8 ++++ .../View/CreateRoomScreen.swift | 1 + .../View/CreateRoomSpaceSelectionSheet.swift | 1 + .../MediaPickerScreen/CameraPicker.swift | 4 ++ .../RoomDetailsEditScreenCoordinator.swift | 5 ++- .../RoomDetailsEditScreenViewModel.swift | 8 ++++ .../View/RoomDetailsEditScreen.swift | 1 + .../UserDetailsEditScreenCoordinator.swift | 5 ++- .../UserDetailsEditScreenViewModel.swift | 8 ++++ .../View/UserDetailsEditScreen.swift | 1 + .../Screens/Timeline/TimelineViewModel.swift | 5 +++ Enterprise | 2 +- .../Sources/CreateRoomViewModelTests.swift | 37 ++++++++++++++++++- .../RoomDetailsEditScreenViewModelTests.swift | 24 ++++++++++++ .../Sources/TimelineViewModelTests.swift | 36 +++++++++++++++++- .../UserDetailsEditScreenViewModelTests.swift | 25 ++++++++++++- 28 files changed, 192 insertions(+), 18 deletions(-) diff --git a/ElementX/Resources/Localizations/en-US.lproj/Localizable.strings b/ElementX/Resources/Localizations/en-US.lproj/Localizable.strings index 0895f59c2..5eab03ae5 100644 --- a/ElementX/Resources/Localizations/en-US.lproj/Localizable.strings +++ b/ElementX/Resources/Localizations/en-US.lproj/Localizable.strings @@ -503,6 +503,7 @@ "emoji_picker_search_placeholder" = "Search emojis"; "error_account_already_logged_in_title" = "Duplicate sign-in"; "error_account_creation_not_possible" = "Your account provider needs to be upgraded to support Matrix Authentication Service and account creation."; +"error_camera_unavailable" = "The camera is not available"; "error_failed_creating_the_permalink" = "Failed creating the permalink"; "error_failed_loading_map" = "%1$@ could not load the map. Please try again later."; "error_failed_loading_messages" = "Failed loading messages"; diff --git a/ElementX/Resources/Localizations/en.lproj/Localizable.strings b/ElementX/Resources/Localizations/en.lproj/Localizable.strings index 3c0184f27..ec3324ba5 100644 --- a/ElementX/Resources/Localizations/en.lproj/Localizable.strings +++ b/ElementX/Resources/Localizations/en.lproj/Localizable.strings @@ -503,6 +503,7 @@ "emoji_picker_search_placeholder" = "Search emojis"; "error_account_already_logged_in_title" = "Duplicate sign-in"; "error_account_creation_not_possible" = "Your account provider needs to be upgraded to support Matrix Authentication Service and account creation."; +"error_camera_unavailable" = "The camera is not available"; "error_failed_creating_the_permalink" = "Failed creating the permalink"; "error_failed_loading_map" = "%1$@ could not load the map. Please try again later."; "error_failed_loading_messages" = "Failed loading messages"; diff --git a/ElementX/Sources/Application/AppMediator.swift b/ElementX/Sources/Application/AppMediator.swift index 207fc4d0e..544e390c1 100644 --- a/ElementX/Sources/Application/AppMediator.swift +++ b/ElementX/Sources/Application/AppMediator.swift @@ -36,6 +36,10 @@ class AppMediator: AppMediatorProtocol { } } + var isCameraAvailable: Bool { + UIImagePickerController.isSourceTypeAvailable(.camera) + } + func beginBackgroundTask(expirationHandler handler: (@MainActor @Sendable () -> Void)?) -> UIBackgroundTaskIdentifier { application.beginBackgroundTask(expirationHandler: handler) } diff --git a/ElementX/Sources/Application/AppMediatorProtocol.swift b/ElementX/Sources/Application/AppMediatorProtocol.swift index 93c44b7de..461b21ccf 100644 --- a/ElementX/Sources/Application/AppMediatorProtocol.swift +++ b/ElementX/Sources/Application/AppMediatorProtocol.swift @@ -16,6 +16,9 @@ protocol AppMediatorProtocol { var appState: UIApplication.State { get } + /// Changes at runtime (e.g. while the camera is in use), so check it before each use. + var isCameraAvailable: Bool { get } + func open(_ url: URL) func openAppSettings() diff --git a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift index c5097e570..6bf4195fe 100644 --- a/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/RoomFlowCoordinator.swift @@ -1016,8 +1016,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol { userSession: userSession, mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: userSession.userSettings), navigationStackCoordinator: stackCoordinator, - userIndicatorController: flowParameters.userIndicatorController, - orientationManager: flowParameters.appMediator.windowManager) + appMediator: flowParameters.appMediator, + userIndicatorController: flowParameters.userIndicatorController) let roomDetailsEditCoordinator = RoomDetailsEditScreenCoordinator(parameters: roomDetailsEditParameters) roomDetailsEditCoordinator.actions.sink { [weak self] action in diff --git a/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift index b838b0044..c097e085a 100644 --- a/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/SettingsFlowCoordinator.swift @@ -190,10 +190,10 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol { } private func presentUserDetailsEditScreen() { - let coordinator = UserDetailsEditScreenCoordinator(parameters: .init(orientationManager: flowParameters.windowManager, - userSession: flowParameters.userSession, + let coordinator = UserDetailsEditScreenCoordinator(parameters: .init(userSession: flowParameters.userSession, mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: userSettings), navigationStackCoordinator: navigationStackCoordinator, + appMediator: flowParameters.appMediator, userIndicatorController: flowParameters.userIndicatorController)) coordinator.actions .sink { [weak self] action in diff --git a/ElementX/Sources/FlowCoordinators/SpaceSettingsFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/SpaceSettingsFlowCoordinator.swift index 5e0ee73de..2985df624 100644 --- a/ElementX/Sources/FlowCoordinators/SpaceSettingsFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/SpaceSettingsFlowCoordinator.swift @@ -252,8 +252,8 @@ final class SpaceSettingsFlowCoordinator: FlowCoordinatorProtocol { userSession: flowParameters.userSession, mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: flowParameters.userSession.userSettings), navigationStackCoordinator: stackCoordinator, - userIndicatorController: flowParameters.userIndicatorController, - orientationManager: flowParameters.appMediator.windowManager) + appMediator: flowParameters.appMediator, + userIndicatorController: flowParameters.userIndicatorController) let coordinator = RoomDetailsEditScreenCoordinator(parameters: parameters) diff --git a/ElementX/Sources/FlowCoordinators/StartChatFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/StartChatFlowCoordinator.swift index d62bf5eba..223829a72 100644 --- a/ElementX/Sources/FlowCoordinators/StartChatFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/StartChatFlowCoordinator.swift @@ -213,8 +213,9 @@ class StartChatFlowCoordinator: FlowCoordinatorProtocol { spaceSelectionMode: spaceSelectionMode, shouldShowCancelButton: isRoot, userSession: flowParameters.userSession, - userIndicatorController: flowParameters.userIndicatorController, - analytics: flowParameters.analytics) + appMediator: flowParameters.appMediator, + analytics: flowParameters.analytics, + userIndicatorController: flowParameters.userIndicatorController) let coordinator = CreateRoomScreenCoordinator(parameters: createParameters) coordinator.actions.sink { [weak self] action in guard let self else { return } diff --git a/ElementX/Sources/Generated/Strings.swift b/ElementX/Sources/Generated/Strings.swift index 42d126cd3..159bdfe87 100644 --- a/ElementX/Sources/Generated/Strings.swift +++ b/ElementX/Sources/Generated/Strings.swift @@ -1156,6 +1156,8 @@ internal nonisolated enum L10n { internal static var errorAccountAlreadyLoggedInTitle: String { return L10n.tr("Localizable", "error_account_already_logged_in_title") } /// Your account provider needs to be upgraded to support Matrix Authentication Service and account creation. internal static var errorAccountCreationNotPossible: String { return L10n.tr("Localizable", "error_account_creation_not_possible") } + /// The camera is not available + internal static var errorCameraUnavailable: String { return L10n.tr("Localizable", "error_camera_unavailable") } /// Failed creating the permalink internal static var errorFailedCreatingThePermalink: String { return L10n.tr("Localizable", "error_failed_creating_the_permalink") } /// %1$@ could not load the map. Please try again later. diff --git a/ElementX/Sources/Mocks/AppMediatorMock.swift b/ElementX/Sources/Mocks/AppMediatorMock.swift index 9f5a19d10..7d8bcd504 100644 --- a/ElementX/Sources/Mocks/AppMediatorMock.swift +++ b/ElementX/Sources/Mocks/AppMediatorMock.swift @@ -12,6 +12,7 @@ extension AppMediatorMock { struct Configuration { var appState = UIApplication.State.active var isCameraAuthorized = true + var isCameraAvailable = true var networkMonitor = NetworkMonitorMock(.init()) } @@ -20,6 +21,7 @@ extension AppMediatorMock { appState = configuration.appState requestAuthorizationIfNeededReturnValue = configuration.isCameraAuthorized + isCameraAvailable = configuration.isCameraAvailable networkMonitor = configuration.networkMonitor let windowManagerMock = WindowManagerMock() diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index 684e55d30..451ae6629 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -1044,6 +1044,11 @@ nonisolated class AppMediatorMock: AppMediatorProtocol, @unchecked Sendable { set(value) { underlyingAppState = value } } nonisolated(unsafe) var underlyingAppState: UIApplication.State! + var isCameraAvailable: Bool { + get { return underlyingIsCameraAvailable } + set(value) { underlyingIsCameraAvailable = value } + } + nonisolated(unsafe) var underlyingIsCameraAvailable: Bool! //MARK: - open diff --git a/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenCoordinator.swift b/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenCoordinator.swift index ebb78e390..9a85a2998 100644 --- a/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenCoordinator.swift +++ b/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenCoordinator.swift @@ -14,8 +14,9 @@ struct CreateRoomScreenCoordinatorParameters { let spaceSelectionMode: CreateRoomScreenSpaceSelectionMode let shouldShowCancelButton: Bool let userSession: UserSessionProtocol - let userIndicatorController: UserIndicatorControllerProtocol + let appMediator: AppMediatorProtocol let analytics: AnalyticsServiceProtocol + let userIndicatorController: UserIndicatorControllerProtocol } enum CreateRoomScreenCoordinatorAction { @@ -38,6 +39,7 @@ final class CreateRoomScreenCoordinator: CoordinatorProtocol { spaceSelectionMode: parameters.spaceSelectionMode, shouldShowCancelButton: parameters.shouldShowCancelButton, userSession: parameters.userSession, + appMediator: parameters.appMediator, analytics: parameters.analytics, userIndicatorController: parameters.userIndicatorController) } diff --git a/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenViewModel.swift b/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenViewModel.swift index 1df5e21be..692bcf663 100644 --- a/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/CreateRoomScreen/CreateRoomScreenViewModel.swift @@ -15,6 +15,7 @@ typealias CreateRoomScreenViewModelType = StateStoreViewModel = .init() private let roomProxy: JoinedRoomProxyProtocol private let clientProxy: ClientProxyProtocol + private let appMediator: AppMediatorProtocol private let userIndicatorController: UserIndicatorControllerProtocol private let mediaUploadingPreprocessor: MediaUploadingPreprocessor @@ -25,11 +26,13 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe init(roomProxy: JoinedRoomProxyProtocol, userSession: UserSessionProtocol, mediaUploadingPreprocessor: MediaUploadingPreprocessor, + appMediator: AppMediatorProtocol, userIndicatorController: UserIndicatorControllerProtocol) { self.roomProxy = roomProxy clientProxy = userSession.clientProxy self.mediaUploadingPreprocessor = mediaUploadingPreprocessor self.userIndicatorController = userIndicatorController + self.appMediator = appMediator let roomAvatar = roomProxy.infoPublisher.value.avatarURL let roomName = roomProxy.infoPublisher.value.displayName @@ -66,6 +69,11 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe case .presentMediaSource: state.bindings.showMediaSheet = true case .displayCameraPicker: + guard appMediator.isCameraAvailable else { + MXLog.info("Camera unavailable, not presenting the picker.") + userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorCameraUnavailable)) + return + } actionsSubject.send(.displayCameraPicker) case .displayMediaPicker: actionsSubject.send(.displayMediaPicker) diff --git a/ElementX/Sources/Screens/RoomDetailsEditScreen/View/RoomDetailsEditScreen.swift b/ElementX/Sources/Screens/RoomDetailsEditScreen/View/RoomDetailsEditScreen.swift index a3aa07276..79cad1230 100644 --- a/ElementX/Sources/Screens/RoomDetailsEditScreen/View/RoomDetailsEditScreen.swift +++ b/ElementX/Sources/Screens/RoomDetailsEditScreen/View/RoomDetailsEditScreen.swift @@ -178,6 +178,7 @@ struct RoomDetailsEditScreen_Previews: PreviewProvider, TestablePreview { return RoomDetailsEditScreenViewModel(roomProxy: roomProxy, userSession: UserSessionMock(.init()), mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: .mock()), + appMediator: AppMediatorMock(.init()), userIndicatorController: UserIndicatorControllerMock()) } } diff --git a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenCoordinator.swift b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenCoordinator.swift index af26667ee..add087f06 100644 --- a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenCoordinator.swift +++ b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenCoordinator.swift @@ -10,10 +10,10 @@ import Combine import SwiftUI struct UserDetailsEditScreenCoordinatorParameters { - let orientationManager: OrientationManagerProtocol let userSession: UserSessionProtocol let mediaUploadingPreprocessor: MediaUploadingPreprocessor weak var navigationStackCoordinator: NavigationStackCoordinator? + let appMediator: AppMediatorProtocol let userIndicatorController: UserIndicatorControllerProtocol } @@ -36,6 +36,7 @@ final class UserDetailsEditScreenCoordinator: CoordinatorProtocol { viewModel = UserDetailsEditScreenViewModel(userSession: parameters.userSession, mediaUploadingPreprocessor: parameters.mediaUploadingPreprocessor, + appMediator: parameters.appMediator, userIndicatorController: parameters.userIndicatorController) } @@ -69,7 +70,7 @@ final class UserDetailsEditScreenCoordinator: CoordinatorProtocol { let mediaPickerCoordinator = MediaPickerScreenCoordinator(mode: mode, userIndicatorController: parameters.userIndicatorController, - orientationManager: parameters.orientationManager) { [weak self] action in + orientationManager: parameters.appMediator.windowManager) { [weak self] action in guard let self else { return } switch action { case .cancel: diff --git a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenViewModel.swift b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenViewModel.swift index 1987156db..7cd4b0283 100644 --- a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenViewModel.swift +++ b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/UserDetailsEditScreenViewModel.swift @@ -14,6 +14,7 @@ typealias UserDetailsEditScreenViewModelType = StateStoreViewModelV2 = .init() private let clientProxy: ClientProxyProtocol + private let appMediator: AppMediatorProtocol private let userIndicatorController: UserIndicatorControllerProtocol private let mediaUploadingPreprocessor: MediaUploadingPreprocessor @@ -23,10 +24,12 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe init(userSession: UserSessionProtocol, mediaUploadingPreprocessor: MediaUploadingPreprocessor, + appMediator: AppMediatorProtocol, userIndicatorController: UserIndicatorControllerProtocol) { clientProxy = userSession.clientProxy self.mediaUploadingPreprocessor = mediaUploadingPreprocessor self.userIndicatorController = userIndicatorController + self.appMediator = appMediator let profile = clientProxy.userProfilePublisher.value super.init(initialViewState: UserDetailsEditScreenViewState(currentUserProfile: profile, @@ -63,6 +66,11 @@ class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDe case .presentMediaSource: state.bindings.showMediaSheet = true case .displayCameraPicker: + guard appMediator.isCameraAvailable else { + MXLog.info("Camera unavailable, not presenting the picker.") + userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorCameraUnavailable)) + return + } actionsSubject.send(.displayCameraPicker) case .displayMediaPicker: actionsSubject.send(.displayMediaPicker) diff --git a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/View/UserDetailsEditScreen.swift b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/View/UserDetailsEditScreen.swift index 20725ad83..9d6b50210 100644 --- a/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/View/UserDetailsEditScreen.swift +++ b/ElementX/Sources/Screens/Settings/UserDetailsEditScreen/View/UserDetailsEditScreen.swift @@ -146,6 +146,7 @@ struct UserDetailsEditScreen_Previews: PreviewProvider, TestablePreview { return UserDetailsEditScreenViewModel(userSession: userSession, mediaUploadingPreprocessor: .init(userSettings: .mock()), + appMediator: AppMediatorMock(.init()), userIndicatorController: UserIndicatorControllerMock()) } } diff --git a/ElementX/Sources/Screens/Timeline/TimelineViewModel.swift b/ElementX/Sources/Screens/Timeline/TimelineViewModel.swift index 11f09f913..0506a4c5d 100644 --- a/ElementX/Sources/Screens/Timeline/TimelineViewModel.swift +++ b/ElementX/Sources/Screens/Timeline/TimelineViewModel.swift @@ -372,6 +372,11 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol { private func attach(_ attachment: ComposerAttachmentType) { switch attachment { case .camera: + guard appMediator.isCameraAvailable else { + MXLog.info("Camera unavailable, not presenting the picker.") + displayErrorToast(L10n.errorCameraUnavailable) + return + } actionsSubject.send(.displayCameraPicker) case .photoLibrary: actionsSubject.send(.displayMediaPicker) diff --git a/Enterprise b/Enterprise index 6394f5118..85dfc4de4 160000 --- a/Enterprise +++ b/Enterprise @@ -1 +1 @@ -Subproject commit 6394f5118907ffe9795a769415b4119d73d9b006 +Subproject commit 85dfc4de4da939bf3c9031063b38c92e833acbf5 diff --git a/UnitTests/Sources/CreateRoomViewModelTests.swift b/UnitTests/Sources/CreateRoomViewModelTests.swift index b88bf50de..b129955d6 100644 --- a/UnitTests/Sources/CreateRoomViewModelTests.swift +++ b/UnitTests/Sources/CreateRoomViewModelTests.swift @@ -16,6 +16,8 @@ final class CreateRoomScreenViewModelTests { var clientProxy: ClientProxyMock! var spaceService: SpaceServiceProxyMock! var userSession: UserSessionMock! + var appMediator: AppMediatorMock! + var userIndicatorController: UserIndicatorControllerMock! private let userSettings: UserSettings @@ -32,6 +34,8 @@ final class CreateRoomScreenViewModelTests { clientProxy = nil spaceService = nil userSession = nil + userIndicatorController = nil + appMediator = nil } @Test @@ -90,6 +94,7 @@ final class CreateRoomScreenViewModelTests { spaceSelectionMode: .none, shouldShowCancelButton: false, userSession: userSession, + appMediator: AppMediatorMock(.init()), analytics: AnalyticsServiceMock(.init()), userIndicatorController: UserIndicatorControllerMock()) self.viewModel = viewModel @@ -326,6 +331,33 @@ final class CreateRoomScreenViewModelTests { #expect(context.viewState.canSelectSpace) } + @Test + func cameraPickerOpensWhenTheCameraIsAvailable() async throws { + setup() + + let deferred = deferFulfillment(viewModel.actions) { action in + guard case .displayCameraPicker = action else { return false } + return true + } + context.send(viewAction: .displayCameraPicker) + try await deferred.fulfill() + } + + @Test + func cameraPickerShowsAnErrorWhenTheCameraIsUnavailable() async throws { + setup() + appMediator.isCameraAvailable = false + + let deferredFailure = deferFailure(viewModel.actions, timeout: .seconds(1)) { action in + guard case .displayCameraPicker = action else { return false } + return true + } + context.send(viewAction: .displayCameraPicker) + try await deferredFailure.fulfill() + + #expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.errorCameraUnavailable) + } + private func setup(isSpace: Bool = false, spacesSelectionMode: CreateRoomScreenSpaceSelectionMode = .editableSpacesList(preSelectedSpace: nil)) { spaceService = SpaceServiceProxyMock(.init(editableSpaces: .mockJoinedSpaces2, spaceRoomLists: ["1": .init()])) @@ -335,12 +367,15 @@ final class CreateRoomScreenViewModelTests { clientProxy.roomForIdentifierClosure = { roomID in .joined(JoinedRoomProxyMock(.init(id: roomID))) } userSession = UserSessionMock(.init(clientProxy: clientProxy, userSettings: userSettings)) userSettings.app.knockingEnabled = true + userIndicatorController = UserIndicatorControllerMock() + appMediator = AppMediatorMock(.init()) let viewModel = CreateRoomScreenViewModel(isSpace: isSpace, spaceSelectionMode: spacesSelectionMode, shouldShowCancelButton: false, userSession: userSession, + appMediator: appMediator, analytics: AnalyticsServiceMock(.init()), - userIndicatorController: UserIndicatorControllerMock()) + userIndicatorController: userIndicatorController) self.viewModel = viewModel } } diff --git a/UnitTests/Sources/RoomDetailsEditScreenViewModelTests.swift b/UnitTests/Sources/RoomDetailsEditScreenViewModelTests.swift index 27cceaec0..75c9fa73d 100644 --- a/UnitTests/Sources/RoomDetailsEditScreenViewModelTests.swift +++ b/UnitTests/Sources/RoomDetailsEditScreenViewModelTests.swift @@ -15,6 +15,7 @@ struct RoomDetailsEditScreenViewModelTests { var viewModel: RoomDetailsEditScreenViewModel! var userIndicatorController: UserIndicatorControllerMock! + var appMediator: AppMediatorMock! var context: RoomDetailsEditScreenViewModelType.Context { viewModel.context @@ -161,13 +162,36 @@ struct RoomDetailsEditScreenViewModelTests { #expect(context.viewState.avatarURL == nil) } + @Test + mutating func cameraPickerOpensWhenTheCameraIsAvailable() async throws { + setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin])) + + let deferred = deferFulfillment(viewModel.actions) { $0 == .displayCameraPicker } + context.send(viewAction: .displayCameraPicker) + try await deferred.fulfill() + } + + @Test + mutating func cameraPickerShowsAnErrorWhenTheCameraIsUnavailable() async throws { + setupViewModel(roomProxyConfiguration: .init(name: "Some room", members: [.mockMeAdmin])) + appMediator.isCameraAvailable = false + + let deferredFailure = deferFailure(viewModel.actions, timeout: .seconds(1)) { $0 == .displayCameraPicker } + context.send(viewAction: .displayCameraPicker) + try await deferredFailure.fulfill() + + #expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.errorCameraUnavailable) + } + // MARK: - Private private mutating func setupViewModel(roomProxyConfiguration: JoinedRoomProxyMockConfiguration) { userIndicatorController = UserIndicatorControllerMock() + appMediator = AppMediatorMock(.init()) viewModel = .init(roomProxy: JoinedRoomProxyMock(roomProxyConfiguration), userSession: UserSessionMock(.init()), mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: .mock()), + appMediator: appMediator, userIndicatorController: userIndicatorController) } } diff --git a/UnitTests/Sources/TimelineViewModelTests.swift b/UnitTests/Sources/TimelineViewModelTests.swift index 81ce0dae7..d3ee1df10 100644 --- a/UnitTests/Sources/TimelineViewModelTests.swift +++ b/UnitTests/Sources/TimelineViewModelTests.swift @@ -879,6 +879,37 @@ final class TimelineViewModelTests { try await deferred.fulfill() } + // MARK: - Attachments + + @Test + func cameraAttachmentOpensThePickerWhenTheCameraIsAvailable() async throws { + let viewModel = makeViewModel(timelineController: TimelineControllerMock(.init())) + + let deferred = deferFulfillment(viewModel.actions) { action in + guard case .displayCameraPicker = action else { return false } + return true + } + viewModel.process(composerAction: .attach(.camera)) + try await deferred.fulfill() + } + + @Test + func cameraAttachmentShowsAnErrorWhenTheCameraIsUnavailable() async throws { + let userIndicatorController = UserIndicatorControllerMock() + let viewModel = makeViewModel(timelineController: TimelineControllerMock(.init()), + userIndicatorController: userIndicatorController, + appMediator: AppMediatorMock(.init(isCameraAvailable: false))) + + let deferredFailure = deferFailure(viewModel.actions, timeout: .seconds(1)) { action in + guard case .displayCameraPicker = action else { return false } + return true + } + viewModel.process(composerAction: .attach(.camera)) + try await deferredFailure.fulfill() + + #expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.errorCameraUnavailable) + } + // MARK: - Helpers private func makeViewModel(items: [RoomTimelineItemProtocol], @@ -890,14 +921,15 @@ final class TimelineViewModelTests { private func makeViewModel(roomProxy: JoinedRoomProxyProtocol? = nil, focussedEventID: String? = nil, timelineController: TimelineControllerProtocol, - userIndicatorController: UserIndicatorControllerProtocol = UserIndicatorControllerMock()) -> TimelineViewModel { + userIndicatorController: UserIndicatorControllerProtocol = UserIndicatorControllerMock(), + appMediator: AppMediatorProtocol = AppMediatorMock(.init())) -> TimelineViewModel { TimelineViewModel(roomProxy: roomProxy ?? JoinedRoomProxyMock(.init(name: "")), focussedEventID: focussedEventID, timelineController: timelineController, userSession: UserSessionMock(.init()), mediaPlayerProvider: MediaPlayerProviderMock(), userIndicatorController: userIndicatorController, - appMediator: AppMediatorMock(.init()), + appMediator: appMediator, analyticsService: AnalyticsServiceMock(.init()), emojiProvider: EmojiProvider(userSettings: .mock()), linkMetadataProvider: LinkMetadataProvider(), diff --git a/UnitTests/Sources/UserDetailsEditScreenViewModelTests.swift b/UnitTests/Sources/UserDetailsEditScreenViewModelTests.swift index c0832ff7f..c3e7f0149 100644 --- a/UnitTests/Sources/UserDetailsEditScreenViewModelTests.swift +++ b/UnitTests/Sources/UserDetailsEditScreenViewModelTests.swift @@ -13,15 +13,20 @@ import Testing @MainActor struct UserDetailsEditScreenViewModelTests { private var viewModel: UserDetailsEditScreenViewModel! + private var userIndicatorController: UserIndicatorControllerMock! + private var appMediator: AppMediatorMock! private var context: UserDetailsEditScreenViewModelType.Context { viewModel.context } init() { + userIndicatorController = UserIndicatorControllerMock() + appMediator = AppMediatorMock(.init()) viewModel = .init(userSession: UserSessionMock(.init()), mediaUploadingPreprocessor: MediaUploadingPreprocessor(userSettings: .mock()), - userIndicatorController: UserIndicatorControllerMock()) + appMediator: appMediator, + userIndicatorController: userIndicatorController) } @Test @@ -89,4 +94,22 @@ struct UserDetailsEditScreenViewModelTests { context.alertInfo?.primaryButton.action?() // Save try await deferred.fulfill() } + + @Test + func cameraPickerOpensWhenTheCameraIsAvailable() async throws { + let deferred = deferFulfillment(viewModel.actions) { $0 == .displayCameraPicker } + context.send(viewAction: .displayCameraPicker) + try await deferred.fulfill() + } + + @Test + func cameraPickerShowsAnErrorWhenTheCameraIsUnavailable() async throws { + appMediator.isCameraAvailable = false + + let deferredFailure = deferFailure(viewModel.actions, timeout: .seconds(1)) { $0 == .displayCameraPicker } + context.send(viewAction: .displayCameraPicker) + try await deferredFailure.fulfill() + + #expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.errorCameraUnavailable) + } }