Add object detection, tracking, mobile notifications, and search to any security camera.
add receive notif setting and stop spam reqs out
roryclear7c19ab80e81e
diff --git a/clearcam/Email.m b/clearcam/Email.mindex abe06ff..193bb32 100644--- a/clearcam/Email.m+++ b/clearcam/Email.m@@ -17,7 +17,7 @@- (void)sendEmailWithImageAtPath:(NSString *)imagePath {NSString *server = @"https://www.rors.ai";- if ([[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_email_server_enabled"]) {+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"]) {server = [[NSUserDefaults standardUserDefaults] valueForKey:@"own_email_server_address"];if (![server hasPrefix:@"http"]) {server = [@"http://" stringByAppendingString:server];@@ -78,7 +78,7 @@[bodyData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];// Include session_token only if not using own email server- if (![[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_email_server_enabled"]) {+ if (![[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"]) {NSString *sessionToken = [[StoreManager sharedInstance] retrieveSessionTokenFromKeychain];if (sessionToken) {[bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];diff --git a/clearcam/SceneState.m b/clearcam/SceneState.mindex 764da7c..d242926 100644--- a/clearcam/SceneState.m+++ b/clearcam/SceneState.m@@ -114,7 +114,7 @@//todo//if ([[NSUserDefaults standardUserDefaults] boolForKey:@"send_email_alerts_enabled"] &&// ([[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] ||- // [[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_email_server_enabled"])) {+ // [[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"])) {} else {NSLog(@"NOT sending an email");}@@ -145,7 +145,7 @@- (void)sendEmail:(NSString *)filePath {NSLog(@"ACC SENDING?");- if ([[NSUserDefaults standardUserDefaults] boolForKey:@"send_notif_enabled"] || !([[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] || [[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_email_server_enabled"])){ //todo add back other stuff+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"send_notif_enabled"] || !([[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"] || [[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"])){ //todo add back other stuffNSDate *now = [NSDate date];NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday) fromDate:now];NSInteger currentTime = components.hour * 60 + components.minute;diff --git a/clearcam/SettingsViewController.m b/clearcam/SettingsViewController.mindex 8e6414b..00f9110 100644--- a/clearcam/SettingsViewController.m+++ b/clearcam/SettingsViewController.m@@ -13,7 +13,8 @@@property (nonatomic, strong) NSString *selectedPresetKey; // For YOLO indexes key@property (nonatomic, assign) BOOL isPresetsSectionExpanded; // Track if presets section is expanded@property (nonatomic, assign) BOOL sendNotifEnabled;-@property (nonatomic, assign) BOOL useOwnEmailServerEnabled; // Track if "Use own email server" is enabled+@property (nonatomic, assign) BOOL receiveNotifEnabled; // New property for receiving notifications+@property (nonatomic, assign) BOOL useOwnServerEnabled;@property (nonatomic, assign) BOOL isEmailServerSectionExpanded; // Track if email server section is expanded@property (nonatomic, strong) NSString *emailServerAddress; // Store the email server address@property (nonatomic, strong) NSMutableArray<NSDictionary *> *notificationSchedules; // Array to store notification schedules@@ -74,11 +75,18 @@[defaults setBool:NO forKey:@"send_notif_enabled"];}- if ([defaults objectForKey:@"use_own_email_server_enabled"] != nil) {- self.useOwnEmailServerEnabled = [defaults boolForKey:@"use_own_email_server_enabled"];+ if ([defaults objectForKey:@"receive_notif_enabled"] != nil) {+ self.receiveNotifEnabled = [defaults boolForKey:@"receive_notif_enabled"];} else {- self.useOwnEmailServerEnabled = NO;- [defaults setBool:NO forKey:@"use_own_email_server_enabled"];+ self.receiveNotifEnabled = NO; // Off by default+ [defaults setBool:NO forKey:@"receive_notif_enabled"];+ }++ if ([defaults objectForKey:@"use_own_server_enabled"] != nil) {+ self.useOwnServerEnabled = [defaults boolForKey:@"use_own_server_enabled"];+ } else {+ self.useOwnServerEnabled = NO;+ [defaults setBool:NO forKey:@"use_own_server_enabled"];}if ([defaults objectForKey:@"threshold"] != nil) {@@ -101,7 +109,7 @@[defaults setObject:self.notificationSchedules forKey:@"notification_schedules"];}- self.isEmailServerSectionExpanded = self.useOwnEmailServerEnabled;+ self.isEmailServerSectionExpanded = self.useOwnServerEnabled;self.emailServerAddress = [defaults stringForKey:@"own_email_server_address"] ?: @"http://192.168.1.1";[defaults synchronize];@@ -156,6 +164,21 @@[defaults synchronize];}+- (void)receiveNotifSwitchToggled:(UISwitch *)sender {+ self.receiveNotifEnabled = sender.on;+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];+ [defaults setBool:self.receiveNotifEnabled forKey:@"receive_notif_enabled"];+ [defaults synchronize];++ if (sender.on) {+ NSLog(@"Receive Notifications on This Device turned ON");+ [[UIApplication sharedApplication] registerForRemoteNotifications];+ } else {+ NSLog(@"Receive Notifications on This Device turned OFF");+ [[UIApplication sharedApplication] unregisterForRemoteNotifications];+ }+}+- (void)subscriptionStatusDidChange:(NSNotification *)notification {dispatch_async(dispatch_get_main_queue(), ^{[self.tableView reloadData];@@ -184,8 +207,8 @@- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (section == 0) {- NSInteger baseRows = 9; // Stream via Wi-Fi, Resolution, Detect Objects, Manage Detection Presets, Threshold, Send Notifications, Change Encryption Password, Use own email server, Manage Notification Schedules- if (self.useOwnEmailServerEnabled && self.isEmailServerSectionExpanded) {+ NSInteger baseRows = 10; // Increased by 1 for the new toggle+ if (self.useOwnServerEnabled && self.isEmailServerSectionExpanded) {baseRows += 2; // Add Server Address and Test own server}if (self.isPresetsSectionExpanded) {@@ -281,29 +304,37 @@cell.textLabel.textColor = isPremium ? [UIColor labelColor] : [UIColor grayColor];cell.userInteractionEnabled = YES;} else if (indexPath.row == 6 + offset) {+ cell.textLabel.text = @"Receive Notifications on This Device";+ cell.accessoryType = UITableViewCellAccessoryNone;+ UISwitch *receiveNotifSwitch = [[UISwitch alloc] init];+ receiveNotifSwitch.on = self.receiveNotifEnabled;+ [receiveNotifSwitch addTarget:self action:@selector(receiveNotifSwitchToggled:) forControlEvents:UIControlEventValueChanged];+ cell.accessoryView = receiveNotifSwitch;+ cell.userInteractionEnabled = YES;+ } else if (indexPath.row == 7 + offset) {cell.textLabel.text = @"Change Encryption Password";cell.detailTextLabel.text = nil;cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;cell.textLabel.textColor = isPremium ? [UIColor labelColor] : [UIColor grayColor];cell.userInteractionEnabled = YES;- } else if (indexPath.row == 7 + offset) {- cell.textLabel.text = @"Use own email server";- cell.accessoryType = UITableViewCellAccessoryNone;- UISwitch *useOwnEmailServerSwitch = [[UISwitch alloc] init];- useOwnEmailServerSwitch.on = self.useOwnEmailServerEnabled;- [useOwnEmailServerSwitch addTarget:self action:@selector(useOwnEmailServerSwitchToggled:) forControlEvents:UIControlEventValueChanged];- cell.accessoryView = useOwnEmailServerSwitch;- cell.userInteractionEnabled = YES;} else if (indexPath.row == 8 + offset) {cell.textLabel.text = @"Manage Notification Schedules";cell.detailTextLabel.text = nil;cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;cell.userInteractionEnabled = YES;- } else if (self.useOwnEmailServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 9 + offset) {+ } else if (indexPath.row == 9 + offset) {+ cell.textLabel.text = @"Use Own Notification Server";+ cell.accessoryType = UITableViewCellAccessoryNone;+ UISwitch *useOwnEmailServerSwitch = [[UISwitch alloc] init];+ useOwnEmailServerSwitch.on = self.useOwnServerEnabled;+ [useOwnEmailServerSwitch addTarget:self action:@selector(useOwnEmailServerSwitchToggled:) forControlEvents:UIControlEventValueChanged];+ cell.accessoryView = useOwnEmailServerSwitch;+ cell.userInteractionEnabled = YES;+ } else if (self.useOwnServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 10 + offset) {cell.textLabel.text = @"Server Address";cell.detailTextLabel.text = self.emailServerAddress;cell.userInteractionEnabled = YES;- } else if (self.useOwnEmailServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 10 + offset) {+ } else if (self.useOwnServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 11 + offset) {cell.textLabel.text = @"Test own server";cell.textLabel.textColor = [UIColor systemBlueColor];cell.detailTextLabel.text = nil;@@ -395,6 +426,8 @@}// Send Notifications - handled by switch} else if (indexPath.row == 6 + offset) {+ // Receive Notifications - handled by switch+ } else if (indexPath.row == 7 + offset) {if (isPremium) {[self promptForPasswordWithCompletion:^(BOOL success) {if (success) {@@ -404,8 +437,6 @@} else {[self showPremiumRequiredAlert];}- } else if (indexPath.row == 7 + offset) {- // Use own email server - handled by switch} else if (indexPath.row == 8 + offset) {ScheduleManagementViewController *scheduleVC = [[ScheduleManagementViewController alloc] init];scheduleVC.emailSchedules = [self.notificationSchedules mutableCopy];@@ -417,9 +448,11 @@[self.tableView reloadData];};[self.navigationController pushViewController:scheduleVC animated:YES];- } else if (self.useOwnEmailServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 9 + offset) {+ } else if (indexPath.row == 9 + offset) {+ // Use Own Notification Server - handled by switch+ } else if (self.useOwnServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 10 + offset) {[self showEmailServerAddressInputDialog];- } else if (self.useOwnEmailServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 10 + offset) {+ } else if (self.useOwnServerEnabled && self.isEmailServerSectionExpanded && indexPath.row == 11 + offset) {[self testEmailServer];}}@@ -443,11 +476,11 @@}- (void)useOwnEmailServerSwitchToggled:(UISwitch *)sender {- self.useOwnEmailServerEnabled = sender.on;+ self.useOwnServerEnabled = sender.on;self.isEmailServerSectionExpanded = sender.on;NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];- [defaults setBool:self.useOwnEmailServerEnabled forKey:@"use_own_email_server_enabled"];+ [defaults setBool:self.useOwnServerEnabled forKey:@"use_own_server_enabled"];[defaults synchronize];[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];diff --git a/clearcam/StoreManager.m b/clearcam/StoreManager.mindex d65545f..4935d5f 100644--- a/clearcam/StoreManager.m+++ b/clearcam/StoreManager.m@@ -116,12 +116,18 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan}- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate *expiryDate))completion {- // Check if we have a stored receipt+ static BOOL isRequestInProgress = NO;++ if (isRequestInProgress) {+ NSLog(@"π Subscription verification already in progress. Skipping duplicate request.");+ return;+ }++ isRequestInProgress = YES; // Mark request as in progress+NSString *storedReceipt = [[NSUserDefaults standardUserDefaults] stringForKey:@"subscriptionReceipt"];- if (storedReceipt) {- NSLog(@"π Using cached receipt.");- } else {+ if (!storedReceipt) {NSLog(@"π Fetching new receipt from App Store.");NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];@@ -130,18 +136,17 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreManNSLog(@"No receipt found. User may not have purchased any subscriptions.");[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isSubscribed"];[[NSUserDefaults standardUserDefaults] synchronize];+ isRequestInProgress = NO; // Reset flagcompletion(NO, nil);return;}storedReceipt = [receiptData base64EncodedStringWithOptions:0];- // Save the receipt in NSUserDefaults[[NSUserDefaults standardUserDefaults] setObject:storedReceipt forKey:@"subscriptionReceipt"];[[NSUserDefaults standardUserDefaults] synchronize];}- // Prepare the request to the new verification endpointNSDictionary *requestDict = @{@"receipt": storedReceipt};NSError *error;NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestDict options:0 error:&error];@@ -150,6 +155,7 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreManNSLog(@"JSON serialization error: %@", error.localizedDescription);[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isSubscribed"];[[NSUserDefaults standardUserDefaults] synchronize];+ isRequestInProgress = NO; // Reset flagcompletion(NO, nil);return;}@@ -161,6 +167,8 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {+ isRequestInProgress = NO; // Reset flag when request completes+if (error) {NSLog(@"Error verifying receipt: %@", error.localizedDescription);[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isSubscribed"];@@ -170,7 +178,6 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan}NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];- NSLog(@"Response from server = %@", jsonResponse);if (!jsonResponse || ![jsonResponse isKindOfClass:[NSDictionary class]]) {NSLog(@"Invalid response from server.");@@ -183,21 +190,16 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreManBOOL isSubscribed = [jsonResponse[@"valid"] boolValue];if (isSubscribed) {- // Extract session token from responseNSString *sessionToken = jsonResponse[@"session_token"];if (sessionToken && [sessionToken isKindOfClass:[NSString class]]) {- // Store session token in Keychain[self storeSessionTokenInKeychain:sessionToken];[[NSUserDefaults standardUserDefaults] setObject:[NSDate dateWithTimeIntervalSinceNow:45 * 60] forKey:@"expiry"];- NSLog(@"Stored session token: %@", sessionToken);} else {NSLog(@"Warning: No valid session_token in response.");}- NSLog(@"β Subscription is active.");[[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];} else {- // Clear session token if subscription is invalid[self clearSessionTokenFromKeychain];NSLog(@"π¨ Subscription has expired.");}@@ -205,12 +207,13 @@ NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreMan[[NSUserDefaults standardUserDefaults] setBool:isSubscribed forKey:@"isSubscribed"];[[NSUserDefaults standardUserDefaults] synchronize];- completion(isSubscribed, nil); // No expiry date since the server doesn't return it+ completion(isSubscribed, nil);}];[task resume];}+- (void)storeSessionTokenInKeychain:(NSString *)sessionToken {NSDictionary *query = @{(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,