diff --git a/TILogging/Sources/LoggingPresenter.swift b/TILogging/Sources/LoggingPresenter.swift index 5a743238..9427184c 100644 --- a/TILogging/Sources/LoggingPresenter.swift +++ b/TILogging/Sources/LoggingPresenter.swift @@ -41,26 +41,30 @@ final public class LoggingPresenter { private init() { } - /// binds openning and closing of logging list view to a shaking motion. + /// Binds openning and closing of logging list view to a shaking motion. public func displayOnShakeEvent(of scene: UIWindowScene? = nil) { + showWindow(withScene: scene) + loggingViewController.setVisible(isVisible: false) + loggingViewController.setRegistrationForShacking(isShackingEventAllowed: true) + } + + /// Shows the UIWindow with a button that opens a logging list view. + public func addLogsButton(to scene: UIWindowScene? = nil, isShakingMotionAllowed isShaking: Bool = true) { + showWindow(withScene: scene) + loggingViewController.setVisible(isVisible: true) + loggingViewController.setRegistrationForShacking(isShackingEventAllowed: isShaking) + } + + /// Shows the UIWindow + public func showWindow(withScene scene: UIWindowScene? = nil) { if let scene = scene { window.windowScene = scene } window.makeKeyAndVisible() - loggingViewController.setVisible(isVisible: false) } - /// shows the UIWindow with a button that opens a logging list view. - public func addLogsButton(to scene: UIWindowScene? = nil) { - if let scene = scene { - window.windowScene = scene - } - - window.isHidden = false - } - - /// hides the UIWindow. + /// Hides the UIWindow. public func hide() { window.isHidden = true loggingViewController.setVisible(isVisible: true) diff --git a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingViewController.swift b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingViewController.swift index c7f4e42f..23b4b531 100644 --- a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingViewController.swift +++ b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingViewController.swift @@ -28,9 +28,7 @@ open class LoggingTogglingViewController: BaseInitializeableViewController { private var initialCenter = CGPoint() - private var isRegisteredForShakingEvent: Bool { - !isVisibleState - } + private(set) public var isRegisteredForShakingEvent: Bool = false private(set) public var isLogsPresented = false @@ -95,6 +93,11 @@ open class LoggingTogglingViewController: BaseInitializeableViewController { // MARK: - Public methods + public func setRegistrationForShacking(isShackingEventAllowed: Bool) { + isRegisteredForShakingEvent = isShackingEventAllowed + } + + /// Hides a button that opens logs list view controller. public func setVisible(isVisible: Bool) { isVisibleState = isVisible } diff --git a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift index ff3b1995..7c036bba 100644 --- a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift +++ b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift @@ -23,7 +23,7 @@ import UIKit @available(iOS 15, *) -class LoggingTogglingWindow: UIWindow { +final class LoggingTogglingWindow: UIWindow { override init(frame: CGRect) { super.init(frame: frame) @@ -37,18 +37,6 @@ class LoggingTogglingWindow: UIWindow { fatalError("init(coder:) has not been implemented") } - open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { - guard let rootView = rootViewController else { return } - - guard let loggingController = rootView.presentedViewController as? LoggingTogglingViewController else { - return - } - - if motion == .motionShake { - loggingController.openLoggingScreen() - } - } - override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { guard let rootView = rootViewController else { return false } @@ -64,3 +52,46 @@ class LoggingTogglingWindow: UIWindow { return false } } + +// MARK: - Registration for shaking event + +public extension UIWindow { + override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { + if #available(iOS 15, *) { + guard let window = getLoggingWindow() else { + super.motionEnded(motion, with: event) + return + } + + checkForShakingMotion(motion, forWindow: window, with: event) + + } else { + super.motionEnded(motion, with: event) + } + } + + @available(iOS 15, *) + private func getLoggingWindow() -> LoggingTogglingWindow? { + guard let windows = windowScene?.windows else { + return nil + } + + return windows.compactMap { $0 as? LoggingTogglingWindow }.first + } + + @available(iOS 15, *) + private func checkForShakingMotion(_ motion: UIEvent.EventSubtype, + forWindow window: LoggingTogglingWindow, + with event: UIEvent?) { + guard let loggingController = window.rootViewController as? LoggingTogglingViewController else { + super.motionEnded(motion, with: event) + return + } + + if motion == .motionShake, loggingController.isRegisteredForShakingEvent { + loggingController.openLoggingScreen() + } else { + super.motionEnded(motion, with: event) + } + } +}