Skip to content

Commit

Permalink
Fixed access to badge to be atomic
Browse files Browse the repository at this point in the history
Fix bug for "Unable to set badgeOriginX and badgeOriginY (swift) #9"
  • Loading branch information
mike authored and mike committed Mar 4, 2015
1 parent 0883851 commit 35590f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion UIBarButtonItem+Badge.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@interface UIBarButtonItem (Badge)

@property (strong, nonatomic) UILabel *badge;
@property (strong, atomic) UILabel *badge;

// Badge value to be display
@property (nonatomic) NSString *badgeValue;
Expand Down
41 changes: 25 additions & 16 deletions UIBarButtonItem+Badge.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ - (void)refreshBadge
self.badge.textColor = self.badgeTextColor;
self.badge.backgroundColor = self.badgeBGColor;
self.badge.font = self.badgeFont;

if (!self.badgeValue || [self.badgeValue isEqualToString:@""] || ([self.badgeValue isEqualToString:@"0"] && self.shouldHideBadgeAtZero)) {
self.badge.hidden = YES;
} else {
self.badge.hidden = NO;
[self updateBadgeValueAnimated:YES];
}

}

- (CGSize) badgeExpectedSize
Expand Down Expand Up @@ -132,7 +140,15 @@ - (void)removeBadge

#pragma mark - getters/setters
-(UILabel*) badge {
return objc_getAssociatedObject(self, &UIBarButtonItem_badgeKey);
UILabel* lbl = objc_getAssociatedObject(self, &UIBarButtonItem_badgeKey);
if(lbl==nil) {
lbl = [[UILabel alloc] initWithFrame:CGRectMake(self.badgeOriginX, self.badgeOriginY, 20, 20)];
[self setBadge:lbl];
[self badgeInit];
[self.customView addSubview:lbl];
lbl.textAlignment = NSTextAlignmentCenter;
}
return lbl;
}
-(void)setBadge:(UILabel *)badgeLabel
{
Expand All @@ -148,21 +164,8 @@ -(void) setBadgeValue:(NSString *)badgeValue
objc_setAssociatedObject(self, &UIBarButtonItem_badgeValueKey, badgeValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

// When changing the badge value check if we need to remove the badge
if (!badgeValue || [badgeValue isEqualToString:@""] || ([badgeValue isEqualToString:@"0"] && self.shouldHideBadgeAtZero)) {
[self removeBadge];
} else if (!self.badge) {
// Create a new badge because not existing
self.badge = [[UILabel alloc] initWithFrame:CGRectMake(self.badgeOriginX, self.badgeOriginY, 20, 20)];
self.badge.textColor = self.badgeTextColor;
self.badge.backgroundColor = self.badgeBGColor;
self.badge.font = self.badgeFont;
self.badge.textAlignment = NSTextAlignmentCenter;
[self badgeInit];
[self.customView addSubview:self.badge];
[self updateBadgeValueAnimated:NO];
} else {
[self updateBadgeValueAnimated:YES];
}
[self updateBadgeValueAnimated:YES];
[self refreshBadge];
}

// Badge background color
Expand Down Expand Up @@ -265,6 +268,9 @@ - (void)setShouldHideBadgeAtZero:(BOOL)shouldHideBadgeAtZero
{
NSNumber *number = [NSNumber numberWithBool:shouldHideBadgeAtZero];
objc_setAssociatedObject(self, &UIBarButtonItem_shouldHideBadgeAtZeroKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if(self.badge) {
[self refreshBadge];
}
}

// Badge has a bounce animation when value changes
Expand All @@ -276,6 +282,9 @@ - (void)setShouldAnimateBadge:(BOOL)shouldAnimateBadge
{
NSNumber *number = [NSNumber numberWithBool:shouldAnimateBadge];
objc_setAssociatedObject(self, &UIBarButtonItem_shouldAnimateBadgeKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if(self.badge) {
[self refreshBadge];
}
}


Expand Down

0 comments on commit 35590f4

Please sign in to comment.