Skip to content

Commit

Permalink
Rename callback blocks to completion to support Swift async and await (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmathias authored Aug 1, 2022
1 parent b945c69 commit e46e0cb
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 172 deletions.
14 changes: 7 additions & 7 deletions GoogleSignIn/Sources/GIDAuthentication.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ - (NSString *)emmSupport {
return authorization;
}

- (void)doWithFreshTokens:(GIDAuthenticationAction)action {
- (void)doWithFreshTokens:(GIDAuthenticationCompletion)completion {
if (!([self.accessTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire ||
(self.idToken && [self.idTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire))) {
dispatch_async(dispatch_get_main_queue(), ^{
action(self, nil);
completion(self, nil);
});
return;
}
@synchronized (_authenticationHandlerQueue) {
// Push the handler into the callback queue.
[_authenticationHandlerQueue addObject:[action copy]];
[_authenticationHandlerQueue addObject:[completion copy]];
if (_authenticationHandlerQueue.count > 1) {
// This is not the first handler in the queue, no fetch is needed.
return;
Expand Down Expand Up @@ -277,9 +277,9 @@ - (void)doWithFreshTokens:(GIDAuthenticationAction)action {
authenticationHandlerQueue = [self->_authenticationHandlerQueue copy];
[self->_authenticationHandlerQueue removeAllObjects];
}
for (GIDAuthenticationAction action in authenticationHandlerQueue) {
for (GIDAuthenticationCompletion completion in authenticationHandlerQueue) {
dispatch_async(dispatch_get_main_queue(), ^{
action(error ? nil : self, error);
completion(error ? nil : self, error);
});
}
}];
Expand All @@ -289,9 +289,9 @@ - (void)doWithFreshTokens:(GIDAuthenticationAction)action {
authenticationHandlerQueue = [self->_authenticationHandlerQueue copy];
[self->_authenticationHandlerQueue removeAllObjects];
}
for (GIDAuthenticationAction action in authenticationHandlerQueue) {
for (GIDAuthenticationCompletion completion in authenticationHandlerQueue) {
dispatch_async(dispatch_get_main_queue(), ^{
action(error ? nil : self, error);
completion(error ? nil : self, error);
});
}
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
Expand Down
82 changes: 41 additions & 41 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ - (BOOL)hasPreviousSignIn {
return [authState isAuthorized];
}

- (void)restorePreviousSignInWithCallback:(nullable GIDSignInCallback)callback {
[self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCallback:callback]];
- (void)restorePreviousSignInWithCompletion:(nullable GIDSignInCompletion)completion {
[self signInWithOptions:[GIDSignInInternalOptions silentOptionsWithCompletion:completion]];
}

- (BOOL)restorePreviousSignInNoRefresh {
Expand Down Expand Up @@ -217,52 +217,52 @@ - (BOOL)restorePreviousSignInNoRefresh {
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingViewController:presentingViewController
loginHint:hint
addScopesFlow:NO
callback:callback];
addScopesFlow:NO
completion:completion];
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingViewController:presentingViewController
loginHint:hint
addScopesFlow:NO
scopes:additionalScopes
callback:callback];
completion:completion];
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingViewController:(UIViewController *)presentingViewController
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
[self signInWithConfiguration:configuration
presentingViewController:presentingViewController
hint:nil
callback:callback];
completion:completion];
}

- (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:(UIViewController *)presentingViewController
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeNoCurrentUser
userInfo:nil];
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, error);
completion(nil, error);
});
}
return;
Expand All @@ -278,7 +278,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
presentingViewController:presentingViewController
loginHint:self.currentUser.profile.email
addScopesFlow:YES
callback:callback];
completion:completion];

NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
NSMutableSet<NSString *> *grantedScopes =
Expand All @@ -290,9 +290,9 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeScopesAlreadyGranted
userInfo:nil];
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, error);
completion(nil, error);
});
}
return;
Expand All @@ -310,52 +310,52 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingWindow:presentingWindow
loginHint:hint
addScopesFlow:NO
callback:callback];
addScopesFlow:NO
completion:completion];
[self signInWithOptions:options];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
[self signInWithConfiguration:configuration
presentingWindow:presentingWindow
hint:nil
callback:callback];
completion:completion];
}

- (void)signInWithConfiguration:(GIDConfiguration *)configuration
presentingWindow:(NSWindow *)presentingWindow
hint:(nullable NSString *)hint
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
callback:(nullable GIDSignInCallback)callback {
completion:(nullable GIDSignInCompletion)completion {
GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
presentingWindow:presentingWindow
loginHint:hint
addScopesFlow:NO
scopes:additionalScopes
callback:callback];
completion:completion];
[self signInWithOptions:options];
}

- (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:(NSWindow *)presentingWindow
callback:(nullable GIDSignInCallback)callback {
presentingWindow:(NSWindow *)presentingWindow
completion:(nullable GIDSignInCompletion)completion {
// A currentUser must be available in order to complete this flow.
if (!self.currentUser) {
// No currentUser is set, notify callback of failure.
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeNoCurrentUser
userInfo:nil];
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, error);
completion(nil, error);
});
}
return;
Expand All @@ -371,7 +371,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
presentingWindow:presentingWindow
loginHint:self.currentUser.profile.email
addScopesFlow:YES
callback:callback];
completion:completion];

NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
NSMutableSet<NSString *> *grantedScopes =
Expand All @@ -383,9 +383,9 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeScopesAlreadyGranted
userInfo:nil];
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil, error);
completion(nil, error);
});
}
return;
Expand All @@ -411,7 +411,7 @@ - (void)signOut {
[self removeAllKeychainEntries];
}

- (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
- (void)disconnectWithCompletion:(nullable GIDDisconnectCompletion)completion {
GIDGoogleUser *user = _currentUser;
OIDAuthState *authState = user.authentication.authState;
if (!authState) {
Expand All @@ -428,9 +428,9 @@ - (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
if (!token) {
[self signOut];
// Nothing to do here, consider the operation successful.
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(nil);
completion(nil);
});
}
return;
Expand All @@ -453,9 +453,9 @@ - (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
if (!error) {
[self signOut];
}
if (callback) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
callback(error);
completion(error);
});
}
}];
Expand Down Expand Up @@ -538,10 +538,10 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
if (error) {
[self authenticateWithOptions:options];
} else {
if (options.callback) {
if (options.completion) {
self->_currentOptions = nil;
dispatch_async(dispatch_get_main_queue(), ^{
options.callback(self->_currentUser, nil);
options.completion(self->_currentUser, nil);
});
}
}
Expand Down Expand Up @@ -707,10 +707,10 @@ - (void)authenticateWithOptions:(GIDSignInInternalOptions *)options {
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeHasNoAuthInKeychain
userInfo:nil];
if (options.callback) {
if (options.completion) {
_currentOptions = nil;
dispatch_async(dispatch_get_main_queue(), ^{
options.callback(nil, error);
options.completion(nil, error);
});
}
return;
Expand Down Expand Up @@ -881,11 +881,11 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow {
__weak GIDAuthFlow *weakAuthFlow = authFlow;
[authFlow addCallback:^() {
GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
if (self->_currentOptions.callback) {
GIDSignInCallback callback = self->_currentOptions.callback;
if (self->_currentOptions.completion) {
GIDSignInCompletion completion = self->_currentOptions.completion;
self->_currentOptions = nil;
dispatch_async(dispatch_get_main_queue(), ^{
callback(self->_currentUser, handlerAuthFlow.error);
completion(self->_currentUser, handlerAuthFlow.error);
});
}
}];
Expand Down
14 changes: 7 additions & 7 deletions GoogleSignIn/Sources/GIDSignInInternalOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic, readonly, weak, nullable) NSWindow *presentingWindow;
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// The callback block to be called at the completion of the flow.
@property(nonatomic, readonly, nullable) GIDSignInCallback callback;
/// The completion block to be called at the completion of the flow.
@property(nonatomic, readonly, nullable) GIDSignInCompletion completion;

/// The scopes to be used during the flow.
@property(nonatomic, copy, nullable) NSArray<NSString *> *scopes;
Expand All @@ -69,32 +69,32 @@ NS_ASSUME_NONNULL_BEGIN
presentingViewController:(nullable UIViewController *)presentingViewController
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback;
completion:(nullable GIDSignInCompletion)completion;

+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingViewController:(nullable UIViewController *)presentingViewController
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback;
completion:(nullable GIDSignInCompletion)completion;

#elif TARGET_OS_OSX
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
callback:(nullable GIDSignInCallback)callback;
completion:(nullable GIDSignInCompletion)completion;

+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
presentingWindow:(nullable NSWindow *)presentingWindow
loginHint:(nullable NSString *)loginHint
addScopesFlow:(BOOL)addScopesFlow
scopes:(nullable NSArray *)scopes
callback:(nullable GIDSignInCallback)callback;
completion:(nullable GIDSignInCompletion)completion;
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST

/// Creates the options to sign in silently.
+ (instancetype)silentOptionsWithCallback:(GIDSignInCallback)callback;
+ (instancetype)silentOptionsWithCompletion:(GIDSignInCompletion)completion;

/// Creates options with the same values as the receiver, except for the "extra parameters", and
/// continuation flag, which are replaced by the arguments passed to this method.
Expand Down
Loading

0 comments on commit e46e0cb

Please sign in to comment.