feat: change logic of registration on shaking motion

This commit is contained in:
Nikita Semenov 2022-11-08 17:34:55 +03:00
parent 4ac0eace66
commit 69c2a85718
3 changed files with 66 additions and 28 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}
}
}