1// Copyright 2015-present 650 Industries. All rights reserved.
2
3#import <React/RCTUtils.h>
4
5#import "EXDevMenuMotionInterceptor.h"
6#import "EXDevMenuManager.h"
7
8@import UIKit;
9
10static BOOL isInstalled = NO;
11
12@implementation UIWindow (EXDevMenuMotionInterceptor)
13
14- (void)EX_motionEnded:(__unused UIEventSubtype)motion withEvent:(UIEvent *)event
15{
16  if (event.subtype == UIEventSubtypeMotionShake) {
17    [[EXDevMenuManager sharedInstance] toggle];
18  }
19}
20
21@end
22
23@implementation EXDevMenuMotionInterceptor
24
25+ (void)install
26{
27  if (!isInstalled) {
28    // Capture shake gesture from any window by swapping default implementation from UIWindow.
29    RCTSwapInstanceMethods([UIWindow class], @selector(motionEnded:withEvent:), @selector(EX_motionEnded:withEvent:));
30    isInstalled = YES;
31  }
32}
33
34+ (void)uninstall
35{
36  if (isInstalled) {
37    // Bring back the original method.
38    RCTSwapInstanceMethods([UIWindow class], @selector(motionEnded:withEvent:), @selector(EX_motionEnded:withEvent:));
39    isInstalled = NO;
40  }
41}
42
43+ (BOOL)isInstalled
44{
45  return isInstalled;
46}
47
48@end
49