Skip to content

Commit

Permalink
Fixing auth not being in sync with sample app settings (#234)
Browse files Browse the repository at this point in the history
* Adds `synchronise` call after settings payment session in user defaults
* Sample app refactoring
---------

Co-authored-by: Stefan Popa <[email protected]>
  • Loading branch information
stefan-tudor and stefanpopa authored Feb 5, 2024
1 parent 447e035 commit e57be3a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ @interface MainViewController ()

@property (nonatomic, strong) JudoKit *judoKit;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) NSArray<DemoFeature *> *features;
@property (strong, nonatomic) NSSet<NSString *> *settingsToObserve;
@property (strong, nonatomic) NSArray<NSString *> *settingsToObserve;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *settingsButton;
@property (nonatomic, assign) BOOL shouldSetupJudoSDK;
@property (nonatomic, assign) BOOL shouldUpdateKitAuth;
@property (nonatomic, strong) NSURL *deepLinkURL;
@property (nonatomic, strong) IASKAppSettingsViewController *settingsViewController;
@end
Expand All @@ -63,15 +62,11 @@ @implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];

[self setupAccessibilityIdentifiers];

self.features = DemoFeature.defaultFeatures;
self.shouldSetupJudoSDK = YES;
self.settingsToObserve = [NSSet setWithArray:@[ kSandboxedKey,
kTokenKey, kSecretKey, kPaymentSessionKey,
kSessionTokenKey,
kIsTokenAndSecretOnKey, kIsPaymentSessionOnKey ]];

// Setup accessibility identifiers
self.view.accessibilityIdentifier = @"Main View";
self.settingsButton.accessibilityIdentifier = @"Settings Button";

self.shouldUpdateKitAuth = YES;
[self requestLocationPermissions];

self.judoKit = [[JudoKit alloc] initWithAuthorization:Settings.defaultSettings.authorization];
Expand All @@ -80,39 +75,44 @@ - (void)viewDidLoad {
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if (self.shouldSetupJudoSDK) {
self.shouldSetupJudoSDK = NO;
[self setupJudoSDK];
if (self.shouldUpdateKitAuth) {
self.shouldUpdateKitAuth = NO;
[self updateKitAuth];
}
}

- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}

- (void)awakeFromNib {
[super awakeFromNib];

[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(settingDidChanged:)
name:kIASKAppSettingChanged
object:nil];

for (NSString *key in self.settingsToObserve) {
[Settings.defaultSettings.defaults addObserver:self
forKeyPath:key
options:NSKeyValueObservingOptionNew
context:NULL];
}
}

- (void)setupAccessibilityIdentifiers {
self.view.accessibilityIdentifier = @"Main View";
self.settingsButton.accessibilityIdentifier = @"Settings Button";
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];

// Remove KVO observers when it's no longer needed
for (NSString *key in self.settingsToObserve) {
[Settings.defaultSettings.defaults removeObserver:self forKeyPath:key];
}
}

- (void)settingDidChanged:(NSNotification *)notification {
if (![notification.name isEqualToString:kIASKAppSettingChanged]) {
return;
}
NSSet *changes = [NSSet setWithArray:notification.userInfo.allKeys];

if ([changes intersectsSet:self.settingsToObserve]) {
self.shouldSetupJudoSDK = YES;
}

NSSet *changes = [NSSet setWithArray:notification.userInfo.allKeys];

// As settingsViewController belongs to type "IASKAppSettingsViewController, it gets nested (e.g. Apple Pay Settings),
// and therefore it is important to call this updating method on the main (parent) Settings ViewController, not necessarily
// the one that sends this notification (as it's possibly the nested one). Main Settings VS propagades these changes
Expand All @@ -122,6 +122,16 @@ - (void)settingDidChanged:(NSNotification *)notification {
}
}

// Observe changes to UserDefaults keys - to update judokit.auth in case if changes are made
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context {
if ([object isKindOfClass:NSUserDefaults.class] && [self.settingsToObserve containsObject:keyPath]) {
self.shouldUpdateKitAuth = YES;
}
}

// MARK: Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Expand All @@ -144,7 +154,7 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// MARK: Setup methods

- (void)setupJudoSDK {
- (void)updateKitAuth {
self.judoKit.isSandboxed = Settings.defaultSettings.isSandboxed;
self.judoKit.authorization = Settings.defaultSettings.authorization;
}
Expand Down Expand Up @@ -403,14 +413,25 @@ - (JPApplePayConfiguration *)applePayConfiguration {
return configuration;
}

- (NSArray<NSString *> *)settingsToObserve {
if (!_settingsToObserve) {
_settingsToObserve = @[ kSandboxedKey,
kTokenKey, kSecretKey,
kPaymentSessionKey,
kSessionTokenKey,
kIsTokenAndSecretOnKey, kIsPaymentSessionOnKey ];
}
return _settingsToObserve;
}

@end

@implementation MainViewController (TableViewDelegates)

// MARK: UITableViewDataSource

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.features.count;
return DemoFeature.defaultFeatures.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Expand All @@ -423,7 +444,7 @@ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInte

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView
cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
DemoFeature *option = self.features[indexPath.row];
DemoFeature *option = DemoFeature.defaultFeatures[indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:option.cellIdentifier forIndexPath:indexPath];

cell.textLabel.text = option.title;
Expand All @@ -436,7 +457,7 @@ - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView

- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
DemoFeature *feature = self.features[indexPath.row];
DemoFeature *feature = DemoFeature.defaultFeatures[indexPath.row];

BOOL isApplePayRelatedFeature = feature.type == DemoFeatureTypeApplePayPayment || feature.type == DemoFeatureTypeApplePayPreAuth || feature.type == DemoFeatureTypeApplePayStandalone;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ - (void)createPaymentSession {
// store these to be used for the next flow step
if (!Settings.defaultSettings.hasPaymentReferenceSetUp) {
[NSUserDefaults.standardUserDefaults setValue:paymentReference forKey:kPaymentReferenceKey];
[NSUserDefaults.standardUserDefaults synchronize];
}

if (!Settings.defaultSettings.hasConsumerReferenceSetUp) {
[NSUserDefaults.standardUserDefaults setValue:consumerReference forKey:kConsumerReferenceKey];
[NSUserDefaults.standardUserDefaults synchronize];
}

NSString *baseUrlString;
Expand Down Expand Up @@ -101,6 +103,7 @@ - (void)onCreatePaymentSessionCompleteWithData:(NSData *)data andError:(NSError

[Settings.defaultSettings updateAuthorizationWithPaymentSession:[responseDict objectForKey:@"reference"]
andToken:Settings.defaultSettings.token];

[self displaySnackBarWith:@"Success generating payment session, settings updated, navigating back to main screen."];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ static NSString *const kShouldAskForCardholderNameKey = @"should_ask_for_cardhol

@interface Settings : NSObject

@property (nonatomic, strong, readonly) NSUserDefaults *defaults;

+ (instancetype)defaultSettings;

- (instancetype)initWith:(NSUserDefaults *)defaults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
static NSString *const kDontSet = @"dontSet";

@interface Settings ()
@property (nonatomic, strong) NSUserDefaults *defaults;
@property (nonatomic, strong, readwrite) NSUserDefaults *defaults;
@end

@implementation Settings
Expand Down

0 comments on commit e57be3a

Please sign in to comment.