No Description

LGAlertViewWindowsObserver.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // LGAlertViewWindowsObserver.m
  3. // LGAlertView
  4. //
  5. //
  6. // The MIT License (MIT)
  7. //
  8. // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
  9. // (https://github.com/Friend-LGA/LGAlertView)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28. //
  29. #import "LGAlertViewWindowsObserver.h"
  30. #import "LGAlertViewWindow.h"
  31. #import "LGAlertViewWindowContainer.h"
  32. #import "LGAlertViewHelper.h"
  33. @interface LGAlertViewWindowsObserver ()
  34. @property (strong, nonatomic) NSMutableArray *windowsArray;
  35. @end
  36. @implementation LGAlertViewWindowsObserver
  37. + (instancetype)sharedInstance {
  38. static LGAlertViewWindowsObserver *sharedInstance;
  39. static dispatch_once_t onceToken;
  40. dispatch_once(&onceToken, ^{
  41. sharedInstance = [LGAlertViewWindowsObserver new];
  42. });
  43. return sharedInstance;
  44. }
  45. - (instancetype)init {
  46. self = [super init];
  47. if (self) {
  48. self.windowsArray = [NSMutableArray new];
  49. }
  50. return self;
  51. }
  52. - (void)startObserving {
  53. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeVisibleNotification object:nil];
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowVisibleChanged:) name:UIWindowDidBecomeHiddenNotification object:nil];
  55. }
  56. - (void)dealloc {
  57. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
  58. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
  59. }
  60. - (void)windowVisibleChanged:(NSNotification *)notification {
  61. UIWindow *window = notification.object;
  62. __weak UIWindow *weakWindow = window;
  63. NSString *windowClassName = NSStringFromClass([window class]);
  64. if (![windowClassName containsString:@"Alert"]) {
  65. return;
  66. }
  67. // -----
  68. UIWindow *lastWindow = [self lastWindow];
  69. if (notification.name == UIWindowDidBecomeVisibleNotification) {
  70. if ([self isWindowsArrayContains:window]) {
  71. if (lastWindow && window != lastWindow) {
  72. window.hidden = YES;
  73. [lastWindow makeKeyAndVisible];
  74. }
  75. }
  76. else {
  77. if (lastWindow && ![window isKindOfClass:[LGAlertViewWindow class]]) {
  78. lastWindow.hidden = YES;
  79. }
  80. LGAlertViewWindowContainer *container = [LGAlertViewWindowContainer containerWithWindow:window];
  81. [self.windowsArray addObject:container];
  82. }
  83. }
  84. else if (notification.name == UIWindowDidBecomeHiddenNotification) {
  85. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  86. if (!weakWindow) {
  87. UIWindow *lastWindow = [self lastWindow];
  88. if (lastWindow) {
  89. [lastWindow makeKeyAndVisible];
  90. }
  91. }
  92. });
  93. }
  94. }
  95. - (UIWindow *)lastWindow {
  96. NSMutableArray *newArray = [NSMutableArray new];
  97. UIWindow *lastWindow;
  98. for (LGAlertViewWindowContainer *container in self.windowsArray) {
  99. if (container.window) {
  100. [newArray addObject:container];
  101. lastWindow = container.window;
  102. }
  103. }
  104. self.windowsArray = newArray;
  105. return lastWindow;
  106. }
  107. - (BOOL)isWindowsArrayContains:(UIWindow *)window {
  108. for (LGAlertViewWindowContainer *container in self.windowsArray) {
  109. if (container.window == window) {
  110. return YES;
  111. }
  112. }
  113. return NO;
  114. }
  115. @end