From 7a6a95488bc8d1ff8ced54f5c4a7fcef17cfe8f7 Mon Sep 17 00:00:00 2001 From: "qianyuan.wqy" Date: Mon, 2 Dec 2019 15:29:43 +0800 Subject: [PATCH] [iOS] Do not use queue for iOS toast. --- .../WeexSDK/Sources/Module/WXModalUIModule.m | 43 ++++++------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Module/WXModalUIModule.m b/ios/sdk/WeexSDK/Sources/Module/WXModalUIModule.m index 408898e7cd..48a3f4d738 100644 --- a/ios/sdk/WeexSDK/Sources/Module/WXModalUIModule.m +++ b/ios/sdk/WeexSDK/Sources/Module/WXModalUIModule.m @@ -44,7 +44,6 @@ @implementation WXToastInfo @interface WXToastManager : NSObject -@property (strong, nonatomic) NSMutableArray *toastQueue; @property (strong, nonatomic) UIView *toastingView; + (WXToastManager *)sharedManager; @@ -58,7 +57,6 @@ + (WXToastManager *)sharedManager{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shareInstance = [[WXToastManager alloc] init]; - shareInstance.toastQueue = [NSMutableArray new]; }); return shareInstance; } @@ -122,16 +120,13 @@ - (void)toast:(NSString *)message duration:(double)duration superView = self.weexInstance.rootView; } UIView *toastView = [self toastViewForMessage:message superView:superView]; - WXToastInfo *info = [WXToastInfo new]; - info.instance = self.weexInstance; - info.toastView = toastView; - info.superView = superView; - info.duration = duration; - [[WXToastManager sharedManager].toastQueue addObject:info]; - if (![WXToastManager sharedManager].toastingView) { - [self showToast:toastView superView:superView duration:duration]; + UIView* toastingView = [WXToastManager sharedManager].toastingView; + if (toastingView) { + [toastingView removeFromSuperview]; + [WXToastManager sharedManager].toastingView = nil; } + [self showToast:toastView superView:superView duration:duration]; } - (UIView *)toastViewForMessage:(NSString *)message superView:(UIView *)superView @@ -156,7 +151,10 @@ - (UIView *)toastViewForMessage:(NSString *)message superView:(UIView *)superVie )]; CGPoint point = CGPointZero; - UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; + UIWindow* window = [UIApplication sharedApplication].delegate.window; + if (window == NULL) { + window = [[[UIApplication sharedApplication] windows] firstObject]; + } // adjust to screen orientation UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIApplication sharedApplication] statusBarOrientation]; @@ -191,7 +189,7 @@ - (UIView *)toastViewForMessage:(NSString *)message superView:(UIView *)superVie [toastView addSubview:messageLabel]; toastView.layer.cornerRadius = 7; - toastView.backgroundColor=[UIColor colorWithWhite:0 alpha:0.7]; + toastView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7]; return toastView; } @@ -204,7 +202,6 @@ - (void)showToast:(UIView *)toastView superView:(UIView *)superView duration:(do [WXToastManager sharedManager].toastingView = toastView; [superView addSubview:toastView]; - __weak typeof(self) weakSelf = self; [UIView animateWithDuration:0.2 delay:duration options:UIViewAnimationOptionCurveEaseInOut animations:^{ toastView.transform = CGAffineTransformConcat(toastView.transform, CGAffineTransformMakeScale(0.8, 0.8)) ; } completion:^(BOOL finished) { @@ -212,24 +209,8 @@ - (void)showToast:(UIView *)toastView superView:(UIView *)superView duration:(do toastView.alpha = 0; } completion:^(BOOL finished){ [toastView removeFromSuperview]; - [WXToastManager sharedManager].toastingView = nil; - - NSMutableArray *queue = [WXToastManager sharedManager].toastQueue; - if (queue.count > 0) { - [queue removeObjectAtIndex:0]; - - // remove invalid toasts - for (NSInteger i = [queue count] - 1; i >= 0; i --) { - WXToastInfo *info = queue[i]; - if (info.instance == nil) { - [queue removeObjectAtIndex:i]; - } - } - - if (queue.count > 0) { - WXToastInfo *info = [queue firstObject]; - [weakSelf showToast:info.toastView superView:info.superView duration:info.duration]; - } + if ([WXToastManager sharedManager].toastingView == toastView) { + [WXToastManager sharedManager].toastingView = nil; } }]; }];