Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

update ui on purchase

roryclear61ef2ae184f2
diff --git a/clearcam/SettingsViewController.m b/clearcam/SettingsViewController.m
index f8cca2d..6e51cae 100644
--- a/clearcam/SettingsViewController.m
+++ b/clearcam/SettingsViewController.m
@@ -12,7 +12,6 @@
@property (nonatomic, assign) BOOL isPresetsSectionExpanded; // Track if presets section is expanded
@property (nonatomic, assign) BOOL sendEmailAlertsEnabled;
@property (nonatomic, assign) BOOL encryptEmailDataEnabled;
-@property (nonatomic, assign) BOOL isPaid; // Tracks whether the user has "upgraded"
@end
@@ -24,7 +23,15 @@
// Basic setup
self.view.backgroundColor = [UIColor systemBackgroundColor];
self.title = @"Settings";
- self.isPaid = [[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"];
+
+ // Check subscription status on view load
+ [self checkSubscriptionStatus];
+
+ // Register for subscription status change notifications
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(subscriptionStatusDidChange:)
+ name:StoreManagerSubscriptionStatusDidChangeNotification
+ object:nil];
// Initialize selectedResolution and selectedPresetKey based on SettingsManager
SettingsManager *settingsManager = [SettingsManager sharedManager];
@@ -67,6 +74,38 @@
]];
}
+- (void)dealloc {
+ // Unregister from notifications to prevent crashes
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+#pragma mark - Subscription Status
+
+- (void)checkSubscriptionStatus {
+ [[StoreManager sharedInstance] verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (isActive) {
+ NSLog(@"Subscription is active. Expiry date: %@", expiryDate);
+ } else {
+ NSLog(@"No active subscription found.");
+ }
+ [self.tableView reloadData]; // Refresh the UI based on the subscription status
+ });
+ }];
+}
+
+- (void)subscriptionStatusDidChange:(NSNotification *)notification {
+ // Called when the subscription status changes (e.g., after a successful purchase)
+ dispatch_async(dispatch_get_main_queue(), ^{
+ NSLog(@"Subscription status changed. Refreshing UI.");
+ [self.tableView reloadData]; // Refresh the UI to reflect the new subscription status
+ });
+}
+
+- (BOOL)isSubscribed {
+ return [[NSUserDefaults standardUserDefaults] boolForKey:@"isSubscribed"];
+}
+
#pragma mark - Orientation Control
- (BOOL)shouldAutorotate {
@@ -95,7 +134,7 @@
NSArray *presetKeys = [[[NSUserDefaults standardUserDefaults] objectForKey:@"yolo_presets"] allKeys];
return self.isPresetsSectionExpanded ? (presetKeys.count + 2) : 1;
} else if (section == 2) {
- return self.isPaid ? 0 : 1; // Show "Upgrade to Premium" button only if not premium
+ return [self isSubscribed] ? 0 : 1; // Show "Upgrade to Premium" button only if not subscribed
}
return 0;
}
@@ -137,8 +176,8 @@
NSString *email = [[NSUserDefaults standardUserDefaults] stringForKey:@"user_email"];
cell.detailTextLabel.text = email ?: @"Not set"; // Show current email or "Not set"
- // Disable if not premium
- if (!self.isPaid) {
+ // Disable if not subscribed
+ if (![self isSubscribed]) {
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.userInteractionEnabled = NO;
@@ -158,8 +197,8 @@
[emailAlertsSwitch addTarget:self action:@selector(emailAlertsSwitchToggled:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = emailAlertsSwitch;
- // Disable if not premium
- if (!self.isPaid) {
+ // Disable if not subscribed
+ if (![self isSubscribed]) {
emailAlertsSwitch.enabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
cell.userInteractionEnabled = NO;
@@ -179,8 +218,8 @@
[encryptEmailDataSwitch addTarget:self action:@selector(encryptEmailDataSwitchToggled:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = encryptEmailDataSwitch;
- // Disable if not premium
- if (!self.isPaid) {
+ // Disable if not subscribed
+ if (![self isSubscribed]) {
encryptEmailDataSwitch.enabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
cell.userInteractionEnabled = NO;
@@ -231,7 +270,7 @@
[self showResolutionPicker];
} else if (indexPath.row == 1) {
[self showYoloIndexesPicker];
- } else if (indexPath.row == 2 && self.isPaid) { // Only allow interaction if premium
+ } else if (indexPath.row == 2 && [self isSubscribed]) { // Only allow interaction if subscribed
[self showEmailInputDialog];
}
// Ignore taps on the "Send Email Alerts" row (indexPath.row == 3) and "Encrypt Email Data" row (indexPath.row == 4)
@@ -253,13 +292,13 @@
} else if (indexPath.section == 2) {
// Upgrade to Premium button tapped
[[StoreManager sharedInstance] fetchAndPurchaseProduct];
- self.isPaid = YES; // Simulate the upgrade (no actual purchase logic for now)
- [self.tableView reloadData]; // Reload the table view to update UI (hide "Upgrade to Premium" and enable email settings)
+ // No need to manually call checkSubscriptionStatus here; the notification will handle UI updates
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
+
- (void)encryptEmailDataSwitchToggled:(UISwitch *)sender {
if (sender.on) {
// Check if a password already exists in the secrets manager
diff --git a/clearcam/StoreManager.h b/clearcam/StoreManager.h
index 081f1c6..3d0ca39 100644
--- a/clearcam/StoreManager.h
+++ b/clearcam/StoreManager.h
@@ -1,18 +1,17 @@
-//
-// StoreManager.h
-// clearcam
-//
-// Created by Rory Clear on 13/03/2025.
-//
+#import <StoreKit/StoreKit.h>
+NS_ASSUME_NONNULL_BEGIN
-#import <Foundation/Foundation.h>
-#import <StoreKit/StoreKit.h>
+// Define the notification name
+extern NSString *const StoreManagerSubscriptionStatusDidChangeNotification;
@interface StoreManager : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>
+ (instancetype)sharedInstance;
+
- (void)fetchAndPurchaseProduct;
-- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate *expiryDate))completion;
+- (void)verifySubscriptionWithCompletion:(void (^)(BOOL isActive, NSDate * _Nullable expiryDate))completion;
@end
+
+NS_ASSUME_NONNULL_END
diff --git a/clearcam/StoreManager.m b/clearcam/StoreManager.m
index d10a100..cb3097c 100644
--- a/clearcam/StoreManager.m
+++ b/clearcam/StoreManager.m
@@ -1,5 +1,8 @@
#import "StoreManager.h"
+// Define the notification name
+NSString *const StoreManagerSubscriptionStatusDidChangeNotification = @"StoreManagerSubscriptionStatusDidChangeNotification";
+
@interface StoreManager ()
@property (nonatomic, strong) SKProductsRequest *productsRequest;
@property (nonatomic, strong) SKProduct *premiumProduct;
@@ -57,7 +60,6 @@
[self purchaseProduct];
}
-
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
NSLog(@"Failed to fetch products: %@", error.localizedDescription);
}
@@ -81,6 +83,13 @@
case SKPaymentTransactionStatePurchased:
NSLog(@"Purchase successful!");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
+ // After a successful purchase, verify the subscription status
+ [self verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
+ if (isActive) {
+ // Post the notification to inform listeners of the status change
+ [[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
+ }
+ }];
break;
case SKPaymentTransactionStateFailed:
@@ -91,6 +100,13 @@
case SKPaymentTransactionStateRestored:
NSLog(@"Purchase restored.");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
+ // After restoring purchases, verify the subscription status
+ [self verifySubscriptionWithCompletion:^(BOOL isActive, NSDate *expiryDate) {
+ if (isActive) {
+ // Post the notification to inform listeners of the status change
+ [[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
+ }
+ }];
break;
default:
@@ -179,6 +195,8 @@
if (isSubscribed) {
NSLog(@"✅ Subscription is active until %@", latestExpirationDate);
+ // Post the notification to inform listeners of the status change
+ [[NSNotificationCenter defaultCenter] postNotificationName:StoreManagerSubscriptionStatusDidChangeNotification object:nil];
} else {
NSLog(@"🚨 Subscription has expired.");
}
@@ -189,7 +207,4 @@
[task resume];
}
-
-
@end
-