Skip to content

Commit 454a821

Browse files
committed
Cache the application badge number
1 parent 24aa1f3 commit 454a821

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

Carthage/Checkouts/Bolts-ObjC

Parse/Internal/PFApplication.m

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#elif PF_TARGET_OS_OSX
1515
#import <AppKit/AppKit.h>
1616
#endif
17+
@interface PFApplication() {
18+
NSUInteger _iconBadgeNumber;
19+
}
20+
@end
1721

1822
@implementation PFApplication
1923

@@ -30,6 +34,19 @@ + (instancetype)currentApplication {
3034
return application;
3135
}
3236

37+
- (id)init {
38+
self = [super init];
39+
if (self) {
40+
#if TARGET_OS_IOS
41+
[self.systemApplication addObserver:self forKeyPath:@"applicationIconBadgeNumber" options:NSKeyValueObservingOptionNew context:nil];
42+
_iconBadgeNumber = self.systemApplication.applicationIconBadgeNumber;
43+
#elif PF_TARGET_OS_OSX
44+
45+
#endif
46+
}
47+
return self;
48+
}
49+
3350
///--------------------------------------
3451
#pragma mark - Accessors
3552
///--------------------------------------
@@ -50,7 +67,7 @@ - (NSInteger)iconBadgeNumber {
5067
#if TARGET_OS_WATCH || TARGET_OS_TV
5168
return 0;
5269
#elif TARGET_OS_IOS
53-
return self.systemApplication.applicationIconBadgeNumber;
70+
return _iconBadgeNumber;
5471
#elif PF_TARGET_OS_OSX
5572
// Make sure not to use `NSApp` here, because it doesn't work sometimes,
5673
// `NSApplication +sharedApplication` does though.
@@ -74,20 +91,27 @@ - (NSInteger)iconBadgeNumber {
7491
- (void)setIconBadgeNumber:(NSInteger)iconBadgeNumber {
7592
if (self.iconBadgeNumber != iconBadgeNumber) {
7693
#if TARGET_OS_IOS
94+
_iconBadgeNumber = iconBadgeNumber;
7795
dispatch_block_t block = ^{
7896
self.systemApplication.applicationIconBadgeNumber = iconBadgeNumber;
7997
};
8098
if ([NSThread currentThread].isMainThread) {
8199
block();
82100
} else {
83-
dispatch_sync(dispatch_get_main_queue(), block);
101+
dispatch_async(dispatch_get_main_queue(), block);
84102
}
85103
#elif PF_TARGET_OS_OSX
86104
[[NSApplication sharedApplication] dockTile].badgeLabel = [@(iconBadgeNumber) stringValue];
87105
#endif
88106
}
89107
}
90108

109+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
110+
if ([keyPath isEqualToString:@"applicationIconBadgeNumber"] && change) {
111+
_iconBadgeNumber = [change[NSKeyValueChangeNewKey] integerValue];
112+
}
113+
}
114+
91115
- (UIApplication *)systemApplication {
92116
#if TARGET_OS_WATCH
93117
return nil;
@@ -97,4 +121,10 @@ - (UIApplication *)systemApplication {
97121
#endif
98122
}
99123

124+
- (void)dealloc {
125+
#if TARGET_OS_IOS
126+
[self.systemApplication removeObserver:self forKeyPath:@"applicationIconBadgeNumber"];
127+
#endif
128+
}
129+
100130
@end

Parse/PFInstallation.m

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,7 @@ - (void)_updateTimeZoneFromDevice {
279279

280280
- (void)_updateBadgeFromDevice {
281281
// Get the application icon and update the installation if necessary.
282-
__block NSNumber *applicationBadge;
283-
dispatch_block_t block = ^{
284-
applicationBadge = @([PFApplication currentApplication].iconBadgeNumber);
285-
};
286-
287-
if ([NSThread currentThread].isMainThread) {
288-
block();
289-
} else {
290-
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
291-
dispatch_async(dispatch_get_main_queue(), ^{
292-
block();
293-
dispatch_semaphore_signal(semaphore);
294-
});
295-
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_MSEC));
296-
if (applicationBadge == nil) {
297-
block();
298-
}
299-
}
282+
NSNumber *applicationBadge = @([PFApplication currentApplication].iconBadgeNumber);
300283
NSNumber *installationBadge = [super objectForKey:PFInstallationKeyBadge];
301284
if (installationBadge == nil || ![applicationBadge isEqualToNumber:installationBadge]) {
302285
[super setObject:applicationBadge forKey:PFInstallationKeyBadge];

Tests/Unit/InstallationUnitTests.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <OCMock/OCMock.h>
1010

1111
#import "PFInstallation.h"
12+
#import "PFApplication.h"
1213
#import "PFUnitTestCase.h"
1314
#import "Parse.h"
1415
#import "Parse_Private.h"
@@ -82,4 +83,19 @@ - (void)testInstallationImmutableFieldsCannotBeDeleted {
8283
PFAssertThrowsInvalidArgumentException([installation removeObjectForKey:@"localeIdentifier"]);
8384
}
8485

86+
- (void)testInstallationHasApplicationBadge {
87+
[PFApplication currentApplication].systemApplication.applicationIconBadgeNumber = 10;
88+
PFInstallation *installation = [PFInstallation currentInstallation];
89+
PFAssertEqualInts(installation.badge, 10, @"Installation should have the same badge as application");
90+
}
91+
92+
- (void)testInstallationSetsApplicationBadge {
93+
[PFApplication currentApplication].systemApplication.applicationIconBadgeNumber = 20;
94+
PFInstallation *installation = [PFInstallation currentInstallation];
95+
installation.badge = 5;
96+
PFAssertEqualInts(installation.badge, 5, @"Installation should have the same badge as application");
97+
PFAssertEqualInts([PFApplication currentApplication].systemApplication.applicationIconBadgeNumber, 5, @"Installation should have the same badge as application");
98+
PFAssertEqualInts([PFApplication currentApplication].iconBadgeNumber, 5, @"Installation should have the same badge as application");
99+
}
100+
85101
@end

0 commit comments

Comments
 (0)