mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-24 18:59:36 +00:00
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>
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ typealias CreateRoomScreenViewModelType = StateStoreViewModel<CreateRoomScreenVi
|
||||
class CreateRoomScreenViewModel: CreateRoomScreenViewModelType, CreateRoomScreenViewModelProtocol {
|
||||
private let userSession: UserSessionProtocol
|
||||
private let mediaUploadingPreprocessor: MediaUploadingPreprocessor
|
||||
private let appMediator: AppMediatorProtocol
|
||||
private let analytics: AnalyticsServiceProtocol
|
||||
private let userIndicatorController: UserIndicatorControllerProtocol
|
||||
|
||||
@@ -31,12 +32,14 @@ class CreateRoomScreenViewModel: CreateRoomScreenViewModelType, CreateRoomScreen
|
||||
spaceSelectionMode: CreateRoomScreenSpaceSelectionMode,
|
||||
shouldShowCancelButton: Bool,
|
||||
userSession: UserSessionProtocol,
|
||||
appMediator: AppMediatorProtocol,
|
||||
analytics: AnalyticsServiceProtocol,
|
||||
userIndicatorController: UserIndicatorControllerProtocol) {
|
||||
self.userSession = userSession
|
||||
mediaUploadingPreprocessor = MediaUploadingPreprocessor(userSettings: userSession.userSettings)
|
||||
self.analytics = analytics
|
||||
self.userIndicatorController = userIndicatorController
|
||||
self.appMediator = appMediator
|
||||
|
||||
var selectedSpace: SpaceServiceRoom?
|
||||
let canSelectSpace: Bool
|
||||
@@ -88,6 +91,11 @@ class CreateRoomScreenViewModel: CreateRoomScreenViewModelType, CreateRoomScreen
|
||||
case .createRoom:
|
||||
Task { await createRoom() }
|
||||
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)
|
||||
|
||||
@@ -465,6 +465,7 @@ struct CreateRoom_Previews: PreviewProvider, TestablePreview {
|
||||
spaceSelectionMode: selectionMode,
|
||||
shouldShowCancelButton: isSpace,
|
||||
userSession: userSession,
|
||||
appMediator: AppMediatorMock(.init()),
|
||||
analytics: AnalyticsServiceMock(.init()),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ struct CreateRoomSpaceSelectionSheet_Previews: PreviewProvider, TestablePreview
|
||||
spaceSelectionMode: .editableSpacesList(preSelectedSpace: nil),
|
||||
shouldShowCancelButton: false,
|
||||
userSession: userSession,
|
||||
appMediator: AppMediatorMock(.init()),
|
||||
analytics: AnalyticsServiceMock(.init()),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}()
|
||||
|
||||
@@ -87,5 +87,9 @@ struct CameraPicker: UIViewControllerRepresentable {
|
||||
cameraPicker.callback(.error(.invalidOriginalImage))
|
||||
}
|
||||
}
|
||||
|
||||
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
||||
cameraPicker.callback(.cancel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -14,8 +14,8 @@ struct RoomDetailsEditScreenCoordinatorParameters {
|
||||
let userSession: UserSessionProtocol
|
||||
let mediaUploadingPreprocessor: MediaUploadingPreprocessor
|
||||
weak var navigationStackCoordinator: NavigationStackCoordinator?
|
||||
let appMediator: AppMediatorProtocol
|
||||
let userIndicatorController: UserIndicatorControllerProtocol
|
||||
let orientationManager: OrientationManagerProtocol
|
||||
}
|
||||
|
||||
enum RoomDetailsEditScreenCoordinatorAction {
|
||||
@@ -38,6 +38,7 @@ final class RoomDetailsEditScreenCoordinator: CoordinatorProtocol {
|
||||
viewModel = RoomDetailsEditScreenViewModel(roomProxy: parameters.roomProxy,
|
||||
userSession: parameters.userSession,
|
||||
mediaUploadingPreprocessor: parameters.mediaUploadingPreprocessor,
|
||||
appMediator: parameters.appMediator,
|
||||
userIndicatorController: parameters.userIndicatorController)
|
||||
}
|
||||
|
||||
@@ -69,7 +70,7 @@ final class RoomDetailsEditScreenCoordinator: 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:
|
||||
|
||||
@@ -15,6 +15,7 @@ class RoomDetailsEditScreenViewModel: RoomDetailsEditScreenViewModelType, RoomDe
|
||||
private let actionsSubject: PassthroughSubject<RoomDetailsEditScreenViewModelAction, Never> = .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)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -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:
|
||||
|
||||
+8
@@ -14,6 +14,7 @@ typealias UserDetailsEditScreenViewModelType = StateStoreViewModelV2<UserDetails
|
||||
class UserDetailsEditScreenViewModel: UserDetailsEditScreenViewModelType, UserDetailsEditScreenViewModelProtocol {
|
||||
private let actionsSubject: PassthroughSubject<UserDetailsEditScreenViewModelAction, Never> = .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)
|
||||
|
||||
+1
@@ -146,6 +146,7 @@ struct UserDetailsEditScreen_Previews: PreviewProvider, TestablePreview {
|
||||
|
||||
return UserDetailsEditScreenViewModel(userSession: userSession,
|
||||
mediaUploadingPreprocessor: .init(userSettings: .mock()),
|
||||
appMediator: AppMediatorMock(.init()),
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
+1
-1
Submodule Enterprise updated: 6394f51189...85dfc4de4d
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user