Skip to content

Commit

Permalink
Version 1.0.2.7
Browse files Browse the repository at this point in the history
1. Fix the crash of notifications.
  • Loading branch information
Nightonke committed Oct 12, 2017
1 parent 9c27b2b commit 564f72f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 26 deletions.
2 changes: 1 addition & 1 deletion VHGithubNotifier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
6967F9654F27A5A529E7AF2F /* [CP] Embed Pods Frameworks */ = {
Expand Down
4 changes: 2 additions & 2 deletions VHGithubNotifier/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2.6</string>
<string>1.0.2.7</string>
<key>CFBundleVersion</key>
<string>1026</string>
<string>1027</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "VHGithubNotifierManager+Notification.h"

static NSMutableArray<CNUserNotification *> *userNotifications;
static NSMutableSet<VHNotification *> *cacheNotifications;

@implementation VHGithubNotifierManager (UserNotification)

Expand All @@ -28,8 +29,7 @@ - (void)innerInitializePropertiesForUserNotification

- (void)addNotifications:(NSArray<VHNotification *> *)notifications
{
@synchronized (userNotifications)
{
IN_MAIN_THREAD({
for (VHNotification *notification in notifications)
{
CNUserNotification *userNotification = [[CNUserNotification alloc] init];
Expand All @@ -38,17 +38,15 @@ - (void)addNotifications:(NSArray<VHNotification *> *)notifications
userNotification.informativeText = [notification toNowTimeString];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:4];
[userInfo setObject:@(VHGithubUserNotificationTypeNotification) forKey:@"type"];
if (notification)
{
[userInfo setObject:notification forKey:@"notification"];
}
[userInfo setObject:@(notification.notificationId) forKey:@"notificationID"];
userNotification.hasActionButton = NO;
userNotification.feature.bannerImage = [VHUtils imageFromNotificationType:notification.type];
userNotification.userInfo = userInfo;
[[self userNotifications] push:userNotification];
}
}
[self notify];
[[self cacheNotifications] addObjectsFromArray:notifications];
[self notify];
});
}

#pragma mark - Private Methods
Expand All @@ -74,53 +72,95 @@ - (void)notify
return userNotifications;
}

- (NSMutableSet<VHNotification *> *)cacheNotifications
{
if (cacheNotifications == nil)
{
cacheNotifications = [NSMutableSet set];
}
return cacheNotifications;
}

- (VHNotification *)cacheNotificationWithID:(long long)notificationID
{
__block VHNotification *cacheNotification = nil;
[[self cacheNotifications] enumerateObjectsUsingBlock:^(VHNotification * _Nonnull obj, BOOL * _Nonnull stop) {
if (obj.notificationId == notificationID)
{
cacheNotification = obj;
*stop = YES;
}
}];
return cacheNotification;
}

- (void)removeCacheNotificationWithID:(long long)notificationID
{
__block VHNotification *cacheNotification = nil;
[[self cacheNotifications] enumerateObjectsUsingBlock:^(VHNotification * _Nonnull obj, BOOL * _Nonnull stop) {
if (obj.notificationId == notificationID)
{
cacheNotification = obj;
*stop = YES;
}
}];
if (cacheNotification)
{
[[self cacheNotifications] removeObject:cacheNotification];
}
}

- (void)storeRecordOfNotification:(CNUserNotification *)userNotification
{
dispatch_async(GLOBAL_QUEUE, ^{
@autoreleasepool
{
RLMRealm *realm = [self realm];
VHNotificationRecord *record = [[VHNotificationRecord alloc] init];
VHNotification *notification = [userNotification.userInfo objectForKey:@"notification"];
record.notificationId = notification.notificationId;
record.latestUpdateTime = notification.updateDate;
if (record.notificationId != 0 && record.latestUpdateTime)
VHNotification *notification = [self cacheNotificationWithID:[[userNotification.userInfo objectForKey:@"notificationID"] longLongValue]];
if (notification)
{
[realm beginWriteTransaction];
[realm addOrUpdateObject:record];
[realm commitWriteTransaction];
record.notificationId = notification.notificationId;
record.latestUpdateTime = notification.updateDate;
if (record.notificationId != 0 && record.latestUpdateTime)
{
[realm beginWriteTransaction];
[realm addOrUpdateObject:record];
[realm commitWriteTransaction];
}
}
}
});
}

#pragma mark - NSUserNotificationCenterDelegate

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)userNotification
{

}

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)userNotification
{
NSDictionary *userInfo = notification.userInfo;
NSDictionary *userInfo = userNotification.userInfo;
if (userInfo)
{
if ([[userInfo objectForKey:@"type"] integerValue] == VHGithubUserNotificationTypeNotification)
{
VHNotification *notification = [userInfo objectForKey:@"notification"];
VHNotification *notification = [self cacheNotificationWithID:[[userInfo objectForKey:@"notificationID"] longLongValue]];
[self openNotificationURLAndMarkAsReadBySettings:notification];
}
}
}

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)userNotification
{
return YES;
}

- (void)userNotificationCenter:(CNUserNotificationCenter *)center didRemoveNotification:(CNUserNotification *)notification
- (void)userNotificationCenter:(CNUserNotificationCenter *)center didRemoveNotification:(CNUserNotification *)userNotification
{
[self removeCacheNotificationWithID:[[userNotification.userInfo objectForKey:@"notificationID"] longLongValue]];
[self notify];
}

Expand Down
2 changes: 1 addition & 1 deletion VHGithubNotifier/Model/VHNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef NS_ENUM(NSUInteger, VHNotificationType)
VHNotificationTypePullRequest = 2,
};

@interface VHNotification : NSObject
@interface VHNotification : NSObject<NSCoding>

@property (nonatomic, assign) long long notificationId;
@property (nonatomic, strong) NSDate *lastReadDate;
Expand Down
35 changes: 35 additions & 0 deletions VHGithubNotifier/Model/VHNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,39 @@ - (NSString *)description
self.htmlUrl];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt64:self.notificationId forKey:@"notificationId"];
[aCoder encodeObject:self.lastReadDate forKey:@"lastReadDate"];
[aCoder encodeInteger:self.reason forKey:@"reason"];
[aCoder encodeObject:self.repository forKey:@"repository"];
[aCoder encodeObject:self.lastCommentUrl forKey:@"lastCommentUrl"];
[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.url forKey:@"url"];
[aCoder encodeBool:self.unread forKey:@"unread"];
[aCoder encodeObject:self.updateDate forKey:@"updateDate"];
[aCoder encodeObject:self.htmlUrl forKey:@"htmlUrl"];
[aCoder encodeInteger:self.type forKey:@"type"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
_notificationId = [aDecoder decodeInt64ForKey:@"notificationId"];
_lastReadDate = [aDecoder decodeObjectForKey:@"lastReadDate"];
_reason = [aDecoder decodeIntegerForKey:@"reason"];
_repository = [aDecoder decodeObjectForKey:@"repository"];
_lastCommentUrl = [aDecoder decodeObjectForKey:@"lastCommentUrl"];
_title = [aDecoder decodeObjectForKey:@"title"];
_url = [aDecoder decodeObjectForKey:@"url"];
_unread = [aDecoder decodeBoolForKey:@"unread"];
_updateDate = [aDecoder decodeObjectForKey:@"updateDate"];
_htmlUrl = [aDecoder decodeObjectForKey:@"htmlUrl"];
_type = [aDecoder decodeIntegerForKey:@"type"];
}
return self;
}

@end
2 changes: 1 addition & 1 deletion VHGithubNotifier/Model/VHSimpleRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2017年 黄伟平. All rights reserved.
//

@interface VHSimpleRepository : NSObject<NSCopying>
@interface VHSimpleRepository : NSObject<NSCopying, NSCoding>

@property (nonatomic, assign) long long repositoryId;
@property (nonatomic, strong) NSString *fullName;
Expand Down
23 changes: 23 additions & 0 deletions VHGithubNotifier/Model/VHSimpleRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,27 @@ - (NSUInteger)hash
return self.repositoryId;
}

- (void)encodeWithCoder:(nonnull NSCoder *)aCoder
{
[aCoder encodeInt64:self.repositoryId forKey:@"repositoryId"];
[aCoder encodeObject:self.fullName forKey:@"fullName"];
[aCoder encodeObject:self.htmlUrl forKey:@"htmlUrl"];
[aCoder encodeObject:self.ownerName forKey:@"ownerName"];
[aCoder encodeObject:self.name forKey:@"name"];
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder
{
self = [super init];
if (self)
{
_repositoryId = [aDecoder decodeInt64ForKey:@"repositoryId"];
_fullName = [aDecoder decodeObjectForKey:@"fullName"];
_htmlUrl = [aDecoder decodeObjectForKey:@"htmlUrl"];
_ownerName = [aDecoder decodeObjectForKey:@"ownerName"];
_name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}

@end

0 comments on commit 564f72f

Please sign in to comment.