From dfaef13235f10aaf8d5239cd40f4fe7d83698f10 Mon Sep 17 00:00:00 2001 From: Greg Combs Date: Fri, 6 Jan 2017 11:00:19 -0600 Subject: [PATCH] Fix static analyzer issues and a crash when the user has dismissed everything --- SLToastKit/SLToastKit/SLToast.m | 5 +++++ SLToastKit/SLToastKit/SLToastManager.m | 30 +++++++++++--------------- SLToastKit/SLToastKit/SLToastView.h | 10 ++++----- SLToastKit/SLToastKit/SLToastView.m | 6 +++--- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/SLToastKit/SLToastKit/SLToast.m b/SLToastKit/SLToastKit/SLToast.m index 1ac5bb1..58f5e64 100644 --- a/SLToastKit/SLToastKit/SLToast.m +++ b/SLToastKit/SLToastKit/SLToast.m @@ -86,6 +86,9 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary struct SLToastKeys keys = SLToastKeys; NSString *toastId = SLTypeNonEmptyStringOrNil(dictionary[keys.identifier]); + if (!toastId) + return nil; + NSString *title = SLTypeStringOrNil(dictionary[keys.title]); NSString *subtitle = SLTypeStringOrNil(dictionary[keys.subtitle]); UIImage *image = SLTypeImageOrNil(dictionary[keys.image]); @@ -105,6 +108,8 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary subtitle:subtitle image:image duration:duration]; + if (self) + self.status = status; return self; } diff --git a/SLToastKit/SLToastKit/SLToastManager.m b/SLToastKit/SLToastKit/SLToastManager.m index 99f4e53..84b39c8 100644 --- a/SLToastKit/SLToastKit/SLToastManager.m +++ b/SLToastKit/SLToastKit/SLToastManager.m @@ -81,18 +81,6 @@ - (void)dealloc [_nagLimitTimer invalidate]; _nagLimitTimer = nil; #endif - - // Just in case consumers retained their toasts and need status updates... - - for (SLToast *toast in _store) - { - if (toast.status != SLToastStatusQueued - && toast.status != SLToastStatusFinished - && toast.status != SLToastStatusSkipped) - { - toast.status = SLToastStatusUnknown; - } - } } - (NSUInteger)totalToastCount @@ -207,22 +195,28 @@ - (nullable SLToast *)pullNext if (self.store.count) { - NSMutableIndexSet *toastsToRemove = [NSMutableIndexSet indexSet]; + NSMutableArray *toastsToRemove = [[NSMutableArray alloc] init]; + + //NSMutableIndexSet *toastsToRemove = [NSMutableIndexSet indexSet]; [self.store enumerateObjectsUsingBlock:^(SLToast * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - if (toast.status != SLToastStatusSkipped) + if (toast.status == SLToastStatusSkipped) + { + [toastsToRemove addObject:obj]; + return; // keep iterating until we find one that isn't skipped + } + else { - toast = obj; - [toastsToRemove addIndex:idx]; // popping this (unskipped) toast from the queue + toast = [obj copy]; + [toastsToRemove addObject:obj]; *stop = YES; return; } - [toastsToRemove addIndex:idx]; }]; if (!toastsToRemove.count) return toast; - [self.store removeObjectsAtIndexes:toastsToRemove]; + [self.store removeObjectsInArray:toastsToRemove]; } return toast; diff --git a/SLToastKit/SLToastKit/SLToastView.h b/SLToastKit/SLToastKit/SLToastView.h index f716d88..9ab345c 100644 --- a/SLToastKit/SLToastKit/SLToastView.h +++ b/SLToastKit/SLToastKit/SLToastView.h @@ -16,12 +16,12 @@ NS_ASSUME_NONNULL_BEGIN @interface SLToastView : UIView -+ (instancetype)showToastInView:(UIView *)parentView - toast:(SLToast *)toast; ++ (nullable instancetype)showToastInView:(UIView *)parentView + toast:(SLToast *)toast; -+ (instancetype)showToastInWindow:(UIWindow *)parentWindow - statusBarFrame:(CGRect)statusBarFrame - toast:(SLToast *)toast; ++ (nullable instancetype)showToastInWindow:(UIWindow *)parentWindow + statusBarFrame:(CGRect)statusBarFrame + toast:(SLToast *)toast; - (BOOL)showToast:(SLToast *)toast; diff --git a/SLToastKit/SLToastKit/SLToastView.m b/SLToastKit/SLToastKit/SLToastView.m index 28f7163..7ea876a 100644 --- a/SLToastKit/SLToastKit/SLToastView.m +++ b/SLToastKit/SLToastKit/SLToastView.m @@ -50,7 +50,7 @@ + (instancetype)showToastInView:(UIView *)parentView [parentView addSubview:toastView]; NSTimeInterval duration = toast.duration; - if (duration > 0) + if (duration >= 0) { __weak SLToastView *wView = toastView; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ @@ -82,7 +82,7 @@ + (instancetype)showToastInWindow:(UIWindow *)parentWindow [parentWindow addSubview:toastView]; NSTimeInterval duration = toast.duration; - if (duration > 0) + if (duration >= 0) { __weak SLToastView *wView = toastView; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ @@ -110,7 +110,7 @@ - (BOOL)showToast:(SLToast *)toast toast.status = SLToastStatusShowing; NSTimeInterval duration = toast.duration; - if (duration > 0) + if (duration >= 0) { __weak SLToastView *wView = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{