Skip to content

Commit

Permalink
Merge pull request cruffenach#12 from dmiedema/remove-by-identifier
Browse files Browse the repository at this point in the history
Remove toasts by identifier
  • Loading branch information
dmiedema committed Jan 7, 2015
2 parents 2f86ea2 + ecfb590 commit 5ee03d4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CRToast/CRToastManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
*/
+ (void)dismissAllNotifications:(BOOL)animated;

/**
Immidiately begins the (un)animated dismisal of a notification and removing of all notifications
with specified identifier
@param identifier `kCRToastIdentiferKey` specified for the toasts in queue. If no toasts are found with that identifier, none will be removed.
@param animated If YES the notification will dismiss with its configure animation, otherwise it will immidiately disappear
*/
+ (void)dismissAllNotificationsWithIdentifier:(NSString *)identifer animated:(BOOL)animated;

/**
Gets the array of notification identifiers currently in the @c CRToastManager notifications queue.
If no identifier is specified for the @c kCRToastIdentifier when created, it will be excluded from this collection.
Expand Down
22 changes: 22 additions & 0 deletions CRToast/CRToastManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ + (void)dismissAllNotifications:(BOOL)animated {
[[self manager] dismissAllNotifications:animated];
}

+ (void)dismissAllNotificationsWithIdentifier:(NSString *)identifer animated:(BOOL)animated {
[[self manager] dismissAllNotificationsWithIdentifier:identifer animated:animated];
}

+ (NSArray *)notificationIdentifiersInQueue {
return [[self manager] notificationIdentifiersInQueue];
}
Expand Down Expand Up @@ -231,6 +235,24 @@ - (void)dismissAllNotifications:(BOOL)animated {
[self.notifications removeAllObjects];
}

- (void)dismissAllNotificationsWithIdentifier:(NSString *)identifer animated:(BOOL)animated {
if (_notifications.count == 0) { return; }
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

__block BOOL callDismiss = NO;
[self.notifications enumerateObjectsUsingBlock:^(CRToast *toast, NSUInteger idx, BOOL *stop) {
NSString *toastIdentifier = toast.options[kCRToastIdentifierKey];
if (toastIdentifier && [toastIdentifier isEqualToString:identifer]) {
if (idx == 0) { callDismiss = YES; }
else {
[indexes addIndex:idx];
}
}
}];
[self.notifications removeObjectsAtIndexes:indexes];
if (callDismiss) { [self dismissNotification:animated]; }
}

- (void)addNotification:(CRToast*)notification {
BOOL showingNotification = self.showingNotification;
[_notifications addObject:notification];
Expand Down
24 changes: 24 additions & 0 deletions Tests/Unit Tests/CRToastManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ - (void)testNotificationIdentifiers {
XCTAssertFalse([identifiers containsObject:@"test"], @"identifiers should not contain an identifier 'test'.");
}

- (void)testDismissByIdentifier {
NSMutableDictionary *options = __TestToastOptionsDictionary();

for (NSString *str in @[@"1", @"2", @"3", @"3", @"4"]) {
options[kCRToastTimeIntervalKey] = @15;
options[kCRToastIdentifierKey] = str;
[CRToastManager showNotificationWithOptions:options completionBlock:nil];
}

// Add another notification without identifier creating 6 items in queue
[CRToastManager showNotificationWithOptions:__TestToastOptionsDictionary() completionBlock:nil];

[CRToastManager dismissAllNotificationsWithIdentifier:@"3" animated:NO];

NSArray *identifiers = [CRToastManager notificationIdentifiersInQueue];

XCTAssertTrue(identifiers.count == 3, @"identifiers should contain 3 items. Instead contains %lu", (long)identifiers.count);

XCTAssertTrue([identifiers containsObject:@"2"], @"identifiers should contain an identifier '2'.");

XCTAssertFalse([identifiers containsObject:@"3"], @"identifiers should not contain an identifier '3'.");

}

#pragma mark - Setup
- (void)setUp {
[super setUp];
Expand Down

0 comments on commit 5ee03d4

Please sign in to comment.