Lock screen rotation for the camera (#2353)

This commit is contained in:
Mauro
2024-01-18 14:24:15 +01:00
committed by GitHub
parent 65e9c3baab
commit 3ae33f1309
19 changed files with 180 additions and 53 deletions
@@ -57,13 +57,14 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private var clientProxyObserver: AnyCancellable?
private var cancellables = Set<AnyCancellable>()
let windowManager: WindowManagerProtocol = WindowManager()
let windowManager: WindowManagerProtocol
let notificationManager: NotificationManagerProtocol
private let appRouteURLParser: AppRouteURLParser
@Consumable private var storedAppRoute: AppRoute?
init(appDelegate: AppDelegate) {
windowManager = WindowManager(appDelegate: appDelegate)
Self.setupEnvironmentVariables()
let appSettings = AppSettings()
@@ -22,8 +22,9 @@ enum AppDelegateCallback {
case failedToRegisteredNotifications(error: Error)
}
class AppDelegate: NSObject, UIApplicationDelegate {
final class AppDelegate: NSObject, UIApplicationDelegate {
let callbacks = PassthroughSubject<AppDelegateCallback, Never>()
var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Add a SceneDelegate to the SwiftUI scene so that we can connect up the WindowManager.
@@ -44,4 +45,8 @@ class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
callbacks.send(.failedToRegisteredNotifications(error: error))
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
orientationLock
}
}
@@ -25,9 +25,9 @@ struct Application: App {
init() {
if ProcessInfo.isRunningUITests {
appCoordinator = UITestsAppCoordinator()
appCoordinator = UITestsAppCoordinator(appDelegate: appDelegate)
} else if ProcessInfo.isRunningUnitTests {
appCoordinator = UnitTestsAppCoordinator()
appCoordinator = UnitTestsAppCoordinator(appDelegate: appDelegate)
} else {
appCoordinator = AppCoordinator(appDelegate: appDelegate)
}
@@ -0,0 +1,27 @@
//
// Copyright 2024 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
// sourcery: AutoMockable
protocol OrientationManagerProtocol {
/// Forces the current orientation for the main window, works only on iOS
func setOrientation(_ orientation: UIInterfaceOrientationMask)
/// Locks the current orientation for the main window, works only on iOS
func lockOrientation(_ orientation: UIInterfaceOrientationMask)
}
@@ -18,12 +18,14 @@ import Combine
import SwiftUI
class WindowManager: WindowManagerProtocol {
private let appDelegate: AppDelegate
weak var windowScene: UIWindowScene?
weak var delegate: WindowManagerDelegate?
private(set) var mainWindow: UIWindow!
private(set) var overlayWindow: UIWindow!
private(set) var alternateWindow: UIWindow!
var windows: [UIWindow] {
[mainWindow, overlayWindow, alternateWindow]
}
@@ -34,7 +36,12 @@ class WindowManager: WindowManagerProtocol {
/// A duration that allows window switching to wait a couple of frames to avoid a transition through black.
private let windowHideDelay = Duration.milliseconds(33)
init(appDelegate: AppDelegate) {
self.appDelegate = appDelegate
}
func configure(with windowScene: UIWindowScene) {
self.windowScene = windowScene
mainWindow = windowScene.keyWindow
mainWindow.tintColor = .compound.textActionPrimary
@@ -80,6 +87,14 @@ class WindowManager: WindowManagerProtocol {
mainWindow.isHidden = true
}
}
func setOrientation(_ orientation: UIInterfaceOrientationMask) {
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
}
func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
appDelegate.orientationLock = orientation
}
}
private class PassthroughWindow: UIWindow {
@@ -25,7 +25,7 @@ protocol WindowManagerDelegate: AnyObject {
/// A window manager that supports switching between a main app window with an overlay and
/// an alternate window to switch contexts whilst also preserving the main view hierarchy.
/// Heavily inspired by https://www.fivestars.blog/articles/swiftui-windows/
protocol WindowManagerProtocol: AnyObject {
protocol WindowManagerProtocol: AnyObject, OrientationManagerProtocol {
var delegate: WindowManagerDelegate? { get set }
/// The app's main window (we only support a single scene).
@@ -40,6 +40,7 @@ enum RoomFlowCoordinatorAction: Equatable {
// swiftlint:disable file_length
class RoomFlowCoordinator: FlowCoordinatorProtocol {
private let orientationManager: OrientationManagerProtocol
private let userSession: UserSessionProtocol
private let roomTimelineControllerFactory: RoomTimelineControllerFactoryProtocol
private let navigationStackCoordinator: NavigationStackCoordinator
@@ -73,7 +74,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
emojiProvider: EmojiProviderProtocol,
appSettings: AppSettings,
analytics: AnalyticsService,
userIndicatorController: UserIndicatorControllerProtocol) {
userIndicatorController: UserIndicatorControllerProtocol,
orientationManager: OrientationManagerProtocol) {
self.userSession = userSession
self.roomTimelineControllerFactory = roomTimelineControllerFactory
self.navigationStackCoordinator = navigationStackCoordinator
@@ -82,6 +84,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
self.appSettings = appSettings
self.analytics = analytics
self.userIndicatorController = userIndicatorController
self.orientationManager = orientationManager
setupStateMachine()
}
@@ -606,7 +609,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
mediaProvider: userSession.mediaProvider,
navigationStackCoordinator: stackCoordinator,
roomProxy: roomProxy,
userIndicatorController: userIndicatorController)
userIndicatorController: userIndicatorController,
orientationManager: orientationManager)
let roomDetailsEditCoordinator = RoomDetailsEditScreenCoordinator(parameters: roomDetailsEditParameters)
roomDetailsEditCoordinator.actions.sink { [weak self] action in
@@ -660,12 +664,17 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
private func presentMediaUploadPickerWithSource(_ source: MediaPickerScreenSource) {
let stackCoordinator = NavigationStackCoordinator()
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: userIndicatorController, source: source) { [weak self] action in
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: userIndicatorController,
source: source,
orientationManager: orientationManager) { [weak self] action in
guard let self else {
return
}
switch action {
case .cancel:
self?.navigationStackCoordinator.setSheetCoordinator(nil)
navigationStackCoordinator.setSheetCoordinator(nil)
case .selectMediaAtURL(let url):
self?.stateMachine.tryEvent(.presentMediaUploadPreview(fileURL: url))
stateMachine.tryEvent(.presentMediaUploadPreview(fileURL: url))
}
}
@@ -152,7 +152,8 @@ class SettingsFlowCoordinator: FlowCoordinatorProtocol {
}
private func presentUserDetailsEditScreen() {
let coordinator = UserDetailsEditScreenCoordinator(parameters: .init(clientProxy: parameters.userSession.clientProxy,
let coordinator = UserDetailsEditScreenCoordinator(parameters: .init(orientationManager: parameters.windowManager,
clientProxy: parameters.userSession.clientProxy,
mediaProvider: parameters.userSession.mediaProvider,
navigationStackCoordinator: navigationStackCoordinator,
userIndicatorController: parameters.userIndicatorController))
@@ -25,6 +25,7 @@ enum UserSessionFlowCoordinatorAction {
}
class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
private let windowManager: WindowManagerProtocol
private let userSession: UserSessionProtocol
private let navigationSplitCoordinator: NavigationSplitCoordinator
private let bugReportService: BugReportServiceProtocol
@@ -60,6 +61,7 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
self.navigationSplitCoordinator = navigationSplitCoordinator
self.bugReportService = bugReportService
self.appSettings = appSettings
self.windowManager = windowManager
sidebarNavigationStackCoordinator = NavigationStackCoordinator(navigationSplitCoordinator: navigationSplitCoordinator)
detailNavigationStackCoordinator = NavigationStackCoordinator(navigationSplitCoordinator: navigationSplitCoordinator)
@@ -73,7 +75,8 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
emojiProvider: EmojiProvider(),
appSettings: appSettings,
analytics: analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
orientationManager: windowManager)
settingsFlowCoordinator = SettingsFlowCoordinator(parameters: .init(userSession: userSession,
windowManager: windowManager,
@@ -455,7 +458,8 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
let startChatNavigationStackCoordinator = NavigationStackCoordinator()
let userDiscoveryService = UserDiscoveryService(clientProxy: userSession.clientProxy)
let parameters = StartChatScreenCoordinatorParameters(userSession: userSession,
let parameters = StartChatScreenCoordinatorParameters(orientationManager: windowManager,
userSession: userSession,
userIndicatorController: ServiceLocator.shared.userIndicatorController,
navigationStackCoordinator: startChatNavigationStackCoordinator,
userDiscoveryService: userDiscoveryService)
@@ -1632,6 +1632,41 @@ class NotificationSettingsProxyMock: NotificationSettingsProxyProtocol {
}
}
}
class OrientationManagerMock: OrientationManagerProtocol {
//MARK: - setOrientation
var setOrientationCallsCount = 0
var setOrientationCalled: Bool {
return setOrientationCallsCount > 0
}
var setOrientationReceivedOrientation: UIInterfaceOrientationMask?
var setOrientationReceivedInvocations: [UIInterfaceOrientationMask] = []
var setOrientationClosure: ((UIInterfaceOrientationMask) -> Void)?
func setOrientation(_ orientation: UIInterfaceOrientationMask) {
setOrientationCallsCount += 1
setOrientationReceivedOrientation = orientation
setOrientationReceivedInvocations.append(orientation)
setOrientationClosure?(orientation)
}
//MARK: - lockOrientation
var lockOrientationCallsCount = 0
var lockOrientationCalled: Bool {
return lockOrientationCallsCount > 0
}
var lockOrientationReceivedOrientation: UIInterfaceOrientationMask?
var lockOrientationReceivedInvocations: [UIInterfaceOrientationMask] = []
var lockOrientationClosure: ((UIInterfaceOrientationMask) -> Void)?
func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
lockOrientationCallsCount += 1
lockOrientationReceivedOrientation = orientation
lockOrientationReceivedInvocations.append(orientation)
lockOrientationClosure?(orientation)
}
}
class PollInteractionHandlerMock: PollInteractionHandlerProtocol {
//MARK: - sendPollResponse
@@ -28,13 +28,18 @@ enum MediaPickerScreenCoordinatorAction {
}
class MediaPickerScreenCoordinator: CoordinatorProtocol {
private let orientationManager: OrientationManagerProtocol
private let userIndicatorController: UserIndicatorControllerProtocol
private let source: MediaPickerScreenSource
private let callback: ((MediaPickerScreenCoordinatorAction) -> Void)?
private let callback: (MediaPickerScreenCoordinatorAction) -> Void
init(userIndicatorController: UserIndicatorControllerProtocol, source: MediaPickerScreenSource, callback: @escaping (MediaPickerScreenCoordinatorAction) -> Void) {
init(userIndicatorController: UserIndicatorControllerProtocol,
source: MediaPickerScreenSource,
orientationManager: OrientationManagerProtocol,
callback: @escaping (MediaPickerScreenCoordinatorAction) -> Void) {
self.userIndicatorController = userIndicatorController
self.source = source
self.orientationManager = orientationManager
self.callback = callback
}
@@ -42,6 +47,23 @@ class MediaPickerScreenCoordinator: CoordinatorProtocol {
AnyView(mediaPicker)
}
func start() {
guard source == .camera else {
return
}
orientationManager.setOrientation(.portrait)
orientationManager.lockOrientation(.portrait)
}
func stop() {
guard source == .camera else {
return
}
orientationManager.lockOrientation(.all)
}
@ViewBuilder
private var mediaPicker: some View {
switch source {
@@ -51,12 +73,12 @@ class MediaPickerScreenCoordinator: CoordinatorProtocol {
PhotoLibraryPicker(userIndicatorController: userIndicatorController) { [weak self] action in
switch action {
case .cancel:
self?.callback?(.cancel)
self?.callback(.cancel)
case .error(let error):
MXLog.error("Failed selecting media from the photo library with error: \(error)")
self?.showError()
case .selectFile(let url):
self?.callback?(.selectMediaAtURL(url))
self?.callback(.selectMediaAtURL(url))
}
}
case .documents:
@@ -65,12 +87,12 @@ class MediaPickerScreenCoordinator: CoordinatorProtocol {
DocumentPicker(userIndicatorController: userIndicatorController) { action in
switch action {
case .cancel:
self.callback?(.cancel)
self.callback(.cancel)
case .error(let error):
MXLog.error("Failed selecting media from the document picker with error: \(error)")
self.showError()
case .selectFile(let url):
self.callback?(.selectMediaAtURL(url))
self.callback(.selectMediaAtURL(url))
}
}
}
@@ -80,12 +102,12 @@ class MediaPickerScreenCoordinator: CoordinatorProtocol {
CameraPicker(userIndicatorController: userIndicatorController) { [weak self] action in
switch action {
case .cancel:
self?.callback?(.cancel)
self?.callback(.cancel)
case .error(let error):
MXLog.error("Failed selecting media from the camera picker with error: \(error)")
self?.showError()
case .selectFile(let url):
self?.callback?(.selectMediaAtURL(url))
self?.callback(.selectMediaAtURL(url))
}
}
.background(.black, ignoresSafeAreaEdges: .bottom)
@@ -23,6 +23,7 @@ struct RoomDetailsEditScreenCoordinatorParameters {
weak var navigationStackCoordinator: NavigationStackCoordinator?
let roomProxy: RoomProxyProtocol
let userIndicatorController: UserIndicatorControllerProtocol
let orientationManager: OrientationManagerProtocol
}
enum RoomDetailsEditScreenCoordinatorAction {
@@ -72,7 +73,9 @@ final class RoomDetailsEditScreenCoordinator: CoordinatorProtocol {
private func displayMediaPickerWithSource(_ source: MediaPickerScreenSource) {
let stackCoordinator = NavigationStackCoordinator()
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController, source: source) { [weak self] action in
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController,
source: source,
orientationManager: parameters.orientationManager) { [weak self] action in
guard let self else { return }
switch action {
case .cancel:
@@ -18,6 +18,7 @@ import Combine
import SwiftUI
struct UserDetailsEditScreenCoordinatorParameters {
let orientationManager: OrientationManagerProtocol
let clientProxy: ClientProxyProtocol
let mediaProvider: MediaProviderProtocol
weak var navigationStackCoordinator: NavigationStackCoordinator?
@@ -61,7 +62,7 @@ final class UserDetailsEditScreenCoordinator: CoordinatorProtocol {
private func displayMediaPickerWithSource(_ source: MediaPickerScreenSource) {
let stackCoordinator = NavigationStackCoordinator()
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController, source: source) { [weak self] action in
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController, source: source, orientationManager: parameters.orientationManager) { [weak self] action in
guard let self else { return }
switch action {
case .cancel:
@@ -18,6 +18,7 @@ import Combine
import SwiftUI
struct StartChatScreenCoordinatorParameters {
let orientationManager: OrientationManagerProtocol
let userSession: UserSessionProtocol
let userIndicatorController: UserIndicatorControllerProtocol
weak var navigationStackCoordinator: NavigationStackCoordinator?
@@ -146,7 +147,7 @@ final class StartChatScreenCoordinator: CoordinatorProtocol {
private func displayMediaPickerWithSource(_ source: MediaPickerScreenSource) {
let stackCoordinator = NavigationStackCoordinator()
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController, source: source) { [weak self] action in
let mediaPickerCoordinator = MediaPickerScreenCoordinator(userIndicatorController: parameters.userIndicatorController, source: source, orientationManager: parameters.orientationManager) { [weak self] action in
guard let self else { return }
switch action {
case .cancel:
@@ -28,9 +28,10 @@ class UITestsAppCoordinator: AppCoordinatorProtocol, WindowManagerDelegate {
// periphery:ignore - retaining purpose
private var alternateWindowMockScreen: MockScreen?
let windowManager: WindowManagerProtocol = WindowManager()
let windowManager: WindowManagerProtocol
init() {
init(appDelegate: AppDelegate) {
windowManager = WindowManager(appDelegate: appDelegate)
// disabling View animations
UIView.setAnimationsEnabled(false)
@@ -685,7 +686,8 @@ class MockScreen: Identifiable {
mediaProvider: MockMediaProvider(),
navigationStackCoordinator: navigationStackCoordinator,
roomProxy: roomProxy,
userIndicatorController: ServiceLocator.shared.userIndicatorController))
userIndicatorController: ServiceLocator.shared.userIndicatorController,
orientationManager: OrientationManagerMock()))
navigationStackCoordinator.setRootCoordinator(coordinator)
return navigationStackCoordinator
case .roomMembersListScreen:
@@ -735,7 +737,8 @@ class MockScreen: Identifiable {
userDiscoveryMock.fetchSuggestionsReturnValue = .success([.mockAlice, .mockBob, .mockCharlie])
userDiscoveryMock.searchProfilesWithReturnValue = .success([])
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "@mock:client.com"), mediaProvider: MockMediaProvider(), voiceMessageMediaManager: VoiceMessageMediaManagerMock())
let parameters: StartChatScreenCoordinatorParameters = .init(userSession: userSession,
let parameters: StartChatScreenCoordinatorParameters = .init(orientationManager: OrientationManagerMock(),
userSession: userSession,
userIndicatorController: UserIndicatorControllerMock(),
navigationStackCoordinator: navigationStackCoordinator,
userDiscoveryService: userDiscoveryMock)
@@ -749,7 +752,8 @@ class MockScreen: Identifiable {
userDiscoveryMock.fetchSuggestionsReturnValue = .success([])
userDiscoveryMock.searchProfilesWithReturnValue = .success([.mockBob, .mockBobby])
let userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider(), voiceMessageMediaManager: VoiceMessageMediaManagerMock())
let coordinator = StartChatScreenCoordinator(parameters: .init(userSession: userSession,
let coordinator = StartChatScreenCoordinator(parameters: .init(orientationManager: OrientationManagerMock(),
userSession: userSession,
userIndicatorController: UserIndicatorControllerMock(),
navigationStackCoordinator: navigationStackCoordinator,
userDiscoveryService: userDiscoveryMock))
@@ -17,9 +17,10 @@
import SwiftUI
class UnitTestsAppCoordinator: AppCoordinatorProtocol {
let windowManager: WindowManagerProtocol = WindowManager()
let windowManager: WindowManagerProtocol
init() {
init(appDelegate: AppDelegate) {
windowManager = WindowManager(appDelegate: appDelegate)
ServiceLocator.shared.register(userIndicatorController: UserIndicatorControllerMock.default)
AppSettings.configureWithSuiteName("io.element.elementx.unittests")