// // LGAlertViewWindowsObserver.m // LGAlertView // // // The MIT License (MIT) // // Copyright © 2015 Grigory Lutkov // (https://github.com/Friend-LGA/LGAlertView) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // #import "LGAlertViewWindowsObserver.h" #import "LGAlertViewWindow.h" #import "LGAlertViewWindowContainer.h" #import "LGAlertViewHelper.h" @interface LGAlertViewWindowsObserver () @property (strong, nonatomic) NSMutableArray *windowsArray; @end @implementation LGAlertViewWindowsObserver + (instancetype)sharedInstance { static LGAlertViewWindowsObserver *sharedInstance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [LGAlertViewWindowsObserver new]; }); return sharedInstance; } - (instancetype)init { self = [super init]; if (self) { self.windowsArray = [NSMutableArray new]; } return self; } - (void)startObserving { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeVisibleNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeHiddenNotification object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil]; } - (void)windowVisibleChanged:(NSNotification *)notification { UIWindow *window = notification.object; __weak UIWindow *weakWindow = window; NSString *windowClassName = NSStringFromClass([window class]); if (![windowClassName containsString:@"Alert"]) { return; } // ----- UIWindow *lastWindow = [self lastWindow]; if (notification.name == UIWindowDidBecomeVisibleNotification) { if ([self isWindowsArrayContains:window]) { if (lastWindow && window != lastWindow) { window.hidden = YES; [lastWindow makeKeyAndVisible]; } } else { if (lastWindow && ![window isKindOfClass:[LGAlertViewWindow class]]) { lastWindow.hidden = YES; } LGAlertViewWindowContainer *container = [LGAlertViewWindowContainer containerWithWindow:window]; [self.windowsArray addObject:container]; } } else if (notification.name == UIWindowDidBecomeHiddenNotification) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (!weakWindow) { UIWindow *lastWindow = [self lastWindow]; if (lastWindow) { [lastWindow makeKeyAndVisible]; } } }); } } - (UIWindow *)lastWindow { NSMutableArray *newArray = [NSMutableArray new]; UIWindow *lastWindow; for (LGAlertViewWindowContainer *container in self.windowsArray) { if (container.window) { [newArray addObject:container]; lastWindow = container.window; } } self.windowsArray = newArray; return lastWindow; } - (BOOL)isWindowsArrayContains:(UIWindow *)window { for (LGAlertViewWindowContainer *container in self.windowsArray) { if (container.window == window) { return YES; } } return NO; } @end