diff --git a/Examples/xx-messenger/Sources/AppCore/SharedUI/ShakeViewModifier.swift b/Examples/xx-messenger/Sources/AppCore/SharedUI/ShakeViewModifier.swift new file mode 100644 index 0000000000000000000000000000000000000000..d46cdc6d1a61adde2ace4c7fd6aebb3a1cfedd16 --- /dev/null +++ b/Examples/xx-messenger/Sources/AppCore/SharedUI/ShakeViewModifier.swift @@ -0,0 +1,42 @@ +import SwiftUI + +struct ShakeViewModifier: ViewModifier { + var action: () -> Void + + func body(content: Content) -> some View { + content.onReceive( + NotificationCenter.default.publisher( + for: UIDevice.deviceDidShakeNotification + ), + perform: { _ in + action() + } + ) + } +} + +extension View { + public func onShake(perform action: @escaping () -> Void) -> some View { + modifier(ShakeViewModifier(action: action)) + } +} + +extension UIDevice { + static let deviceDidShakeNotification = Notification.Name( + rawValue: "deviceDidShakeNotification" + ) +} + +extension UIWindow { + open override func motionEnded( + _ motion: UIEvent.EventSubtype, + with event: UIEvent? + ) { + super.motionEnded(motion, with: event) + guard motion == .motionShake else { return } + NotificationCenter.default.post( + name: UIDevice.deviceDidShakeNotification, + object: nil + ) + } +}