Skip to content

Commit

Permalink
1.0-beta1 - Add icloud sync at launch notification
Browse files Browse the repository at this point in the history
  • Loading branch information
千代田桃 committed Feb 25, 2022
1 parent 40e4877 commit 585fcd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Common/Backend/DeckMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "DeckManager.h"
#import "AppDelegate.h"
#import "MSWeakTimer.h"
#import <UserNotifications/UserNotifications.h>

@interface DeckMonitor ()
@property bool syncdone;
Expand All @@ -17,11 +18,15 @@ @interface DeckMonitor ()
@property (strong, nonatomic) MSWeakTimer *timer;
@property (strong) NSPersistentCloudKitContainer *persistentContainer;
@property (strong) NSDate *nextHistoryCheck;
@property bool firstsync;
@property bool firstsyncdone;
@property (strong) UNUserNotificationCenter *notificationCenter;
@end

@implementation DeckMonitor
- (instancetype)init {
if (self = [super init]) {
self.notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
AppDelegate *delegate = (AppDelegate *)NSApp.delegate;
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(receiveNotification:) name:NSPersistentStoreRemoteChangeNotification object:delegate.persistentContainer.persistentStoreCoordinator];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(receiveNotification:) name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:delegate.persistentContainer.persistentStoreCoordinator];
Expand All @@ -41,6 +46,10 @@ - (void)dealloc {
- (void)receiveNotification:(NSNotification *)notification {
NSLog(@"Notification Name: %@",notification.name);
if (!_syncinginprogress) {
if (!_firstsync) {
_firstsync = YES;
[self notifyLaunchSync];
}
_syncdone = NO;
_syncinginprogress = YES;
[self startsynctimer];
Expand Down Expand Up @@ -96,6 +105,10 @@ - (void)startsynctimer {
- (void)firetimer {
_syncdone = YES;
_timeractive = NO;
if (!_firstsyncdone) {
_firstsyncdone = YES;
[self notifySyncDone];
}
bool changesindatabase = [self checkTransactionHistory];
if ([self cardtotalschanged] || changesindatabase) {
NSLog(@"New Items detected...");
Expand Down Expand Up @@ -138,4 +151,39 @@ - (bool)checkTransactionHistory {
}
return false;
}

- (void)notifyLaunchSync {
[self showNotificationWithMessage:@"KaniManabu is syncing from iCloud since it last launched. Please wait one minute before reviewing/learning items."];
}

- (void)notifySyncDone {
[self showNotificationWithMessage:@"KaniManabu has finished syncing. You may begin reviewing/learning items."];
}

- (void)showNotificationWithMessage:(NSString *)message {
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"KaniManabu";
content.body = message;
content.sound = [UNNotificationSound defaultSound];
NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear +
NSCalendarUnitMonth + NSCalendarUnitDay +
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond fromDate:[NSDate.date dateByAddingTimeInterval:3]];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
repeats:NO];
NSString *identifier = [NSString stringWithFormat:@"kanimanabu-sync-%.f",NSDate.date.timeIntervalSince1970];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content
trigger:trigger];
[_notificationCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
else {
NSLog(@"Successfully scheduled notification: %@", identifier);
}
}];
}

@end
2 changes: 1 addition & 1 deletion KaniManabu-macOS/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[_mwc.window makeKeyAndOrderFront:self];

[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"User enabled badges");
}
Expand Down

0 comments on commit 585fcd5

Please sign in to comment.