Add a protocol for the WindowManager

This commit is contained in:
Stefan Ceriu
2024-01-05 14:06:04 +02:00
parent 8e4e9603e4
commit 9a2966dacf
10 changed files with 71 additions and 27 deletions
@@ -57,7 +57,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
private var clientProxyObserver: AnyCancellable?
private var cancellables = Set<AnyCancellable>()
let windowManager = WindowManager()
let windowManager: WindowManagerProtocol = WindowManager()
let notificationManager: NotificationManagerProtocol
private let appRouteURLParser: AppRouteURLParser
@@ -218,7 +218,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
// MARK: - WindowManagerDelegate
func windowManagerDidConfigureWindows(_ windowManager: WindowManager) {
func windowManagerDidConfigureWindows(_ windowManager: WindowManagerProtocol) {
windowManager.alternateWindow.rootViewController = UIHostingController(rootView: appLockFlowCoordinator.toPresentable())
ServiceLocator.shared.userIndicatorController.window = windowManager.overlayWindow
}
@@ -17,6 +17,6 @@
import Foundation
protocol AppCoordinatorProtocol: CoordinatorProtocol {
var windowManager: WindowManager { get }
var windowManager: WindowManagerProtocol { get }
@discardableResult func handleDeepLink(_ url: URL) -> Bool
}
@@ -20,7 +20,7 @@ import SwiftUI
///
/// We don't support multiple scenes right now, so the implementation is pretty basic.
class SceneDelegate: NSObject, UIWindowSceneDelegate {
weak static var windowManager: WindowManager!
weak static var windowManager: WindowManagerProtocol!
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
@@ -17,23 +17,11 @@
import Combine
import SwiftUI
protocol WindowManagerDelegate: AnyObject {
/// The window manager has configured its windows.
func windowManagerDidConfigureWindows(_ windowManager: WindowManager)
}
@MainActor
/// 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/
class WindowManager {
class WindowManager: WindowManagerProtocol {
weak var delegate: WindowManagerDelegate?
/// The app's main window (we only support a single scene).
private(set) var mainWindow: UIWindow!
/// Presented on top of the main window, to display e.g. user indicators.
private(set) var overlayWindow: UIWindow!
/// A secondary window that can be presented instead of the main/overlay window combo.
private(set) var alternateWindow: UIWindow!
var windows: [UIWindow] {
@@ -46,7 +34,6 @@ class WindowManager {
/// A duration that allows window switching to wait a couple of frames to avoid a transition through black.
private let windowHideDelay = Duration.milliseconds(33)
/// Configures the window manager to operate on the supplied scene.
func configure(with windowScene: UIWindowScene) {
mainWindow = windowScene.keyWindow
mainWindow.tintColor = .compound.textActionPrimary
@@ -62,7 +49,6 @@ class WindowManager {
delegate?.windowManagerDidConfigureWindows(self)
}
/// Shows the main and overlay window combo, hiding the alternate window.
func switchToMain() {
mainWindow.isHidden = false
overlayWindow.isHidden = false
@@ -75,7 +61,6 @@ class WindowManager {
}
}
/// Shows the alternate window, hiding the main and overlay combo.
func switchToAlternate() {
alternateWindow.isHidden = false
@@ -0,0 +1,49 @@
//
// 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 SwiftUI
protocol WindowManagerDelegate: AnyObject {
/// The window manager has configured its windows.
func windowManagerDidConfigureWindows(_ windowManager: WindowManagerProtocol)
}
@MainActor
/// 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 {
var delegate: WindowManagerDelegate? { get set }
/// The app's main window (we only support a single scene).
var mainWindow: UIWindow! { get }
/// Presented on top of the main window, to display e.g. user indicators.
var overlayWindow: UIWindow! { get }
/// A secondary window that can be presented instead of the main/overlay window combo.
var alternateWindow: UIWindow! { get }
/// All the windows being managed
var windows: [UIWindow] { get }
/// Configures the window manager to operate on the supplied scene.
func configure(with windowScene: UIWindowScene)
/// Shows the main and overlay window combo, hiding the alternate window.
func switchToMain()
/// Shows the alternate window, hiding the main and overlay combo.
func switchToAlternate()
}
@@ -28,7 +28,7 @@ enum SettingsFlowCoordinatorAction {
struct SettingsFlowCoordinatorParameters {
let userSession: UserSessionProtocol
let windowManager: WindowManager
let windowManager: WindowManagerProtocol
let appLockService: AppLockServiceProtocol
let bugReportService: BugReportServiceProtocol
let notificationSettings: NotificationSettingsProxyProtocol
@@ -49,7 +49,7 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
init(userSession: UserSessionProtocol,
navigationSplitCoordinator: NavigationSplitCoordinator,
windowManager: WindowManager,
windowManager: WindowManagerProtocol,
appLockService: AppLockServiceProtocol,
bugReportService: BugReportServiceProtocol,
roomTimelineControllerFactory: RoomTimelineControllerFactoryProtocol,
@@ -28,7 +28,7 @@ class UITestsAppCoordinator: AppCoordinatorProtocol, WindowManagerDelegate {
// periphery:ignore - retaining purpose
private var alternateWindowMockScreen: MockScreen?
let windowManager = WindowManager()
let windowManager: WindowManagerProtocol = WindowManager()
init() {
// disabling View animations
@@ -65,7 +65,7 @@ class UITestsAppCoordinator: AppCoordinatorProtocol, WindowManagerDelegate {
fatalError("Not implemented.")
}
func windowManagerDidConfigureWindows(_ windowManager: WindowManager) {
func windowManagerDidConfigureWindows(_ windowManager: WindowManagerProtocol) {
ServiceLocator.shared.userIndicatorController.window = windowManager.overlayWindow
// Set up the alternate window for the App Lock flow coordinator tests.
@@ -79,12 +79,12 @@ class UITestsAppCoordinator: AppCoordinatorProtocol, WindowManagerDelegate {
@MainActor
class MockScreen: Identifiable {
let id: UITestsScreenIdentifier
let windowManager: WindowManager
let windowManager: WindowManagerProtocol
private var retainedState = [Any]()
private var cancellables = Set<AnyCancellable>()
init(id: UITestsScreenIdentifier, windowManager: WindowManager) {
init(id: UITestsScreenIdentifier, windowManager: WindowManagerProtocol) {
self.id = id
self.windowManager = windowManager
}
@@ -17,7 +17,7 @@
import SwiftUI
class UnitTestsAppCoordinator: AppCoordinatorProtocol {
let windowManager = WindowManager()
let windowManager: WindowManagerProtocol = WindowManager()
init() {
ServiceLocator.shared.register(userIndicatorController: UserIndicatorControllerMock.default)