123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // LGAlertViewWindowsObserver.m
- // LGAlertView
- //
- //
- // The MIT License (MIT)
- //
- // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
- // (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
|