Skip to content

Commit e46e0cb

Browse files
authored
Rename callback blocks to completion to support Swift async and await (#187)
1 parent b945c69 commit e46e0cb

File tree

8 files changed

+174
-172
lines changed

8 files changed

+174
-172
lines changed

GoogleSignIn/Sources/GIDAuthentication.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,17 @@ - (NSString *)emmSupport {
219219
return authorization;
220220
}
221221

222-
- (void)doWithFreshTokens:(GIDAuthenticationAction)action {
222+
- (void)doWithFreshTokens:(GIDAuthenticationCompletion)completion {
223223
if (!([self.accessTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire ||
224224
(self.idToken && [self.idTokenExpirationDate timeIntervalSinceNow] < kMinimalTimeToExpire))) {
225225
dispatch_async(dispatch_get_main_queue(), ^{
226-
action(self, nil);
226+
completion(self, nil);
227227
});
228228
return;
229229
}
230230
@synchronized (_authenticationHandlerQueue) {
231231
// Push the handler into the callback queue.
232-
[_authenticationHandlerQueue addObject:[action copy]];
232+
[_authenticationHandlerQueue addObject:[completion copy]];
233233
if (_authenticationHandlerQueue.count > 1) {
234234
// This is not the first handler in the queue, no fetch is needed.
235235
return;
@@ -277,9 +277,9 @@ - (void)doWithFreshTokens:(GIDAuthenticationAction)action {
277277
authenticationHandlerQueue = [self->_authenticationHandlerQueue copy];
278278
[self->_authenticationHandlerQueue removeAllObjects];
279279
}
280-
for (GIDAuthenticationAction action in authenticationHandlerQueue) {
280+
for (GIDAuthenticationCompletion completion in authenticationHandlerQueue) {
281281
dispatch_async(dispatch_get_main_queue(), ^{
282-
action(error ? nil : self, error);
282+
completion(error ? nil : self, error);
283283
});
284284
}
285285
}];
@@ -289,9 +289,9 @@ - (void)doWithFreshTokens:(GIDAuthenticationAction)action {
289289
authenticationHandlerQueue = [self->_authenticationHandlerQueue copy];
290290
[self->_authenticationHandlerQueue removeAllObjects];
291291
}
292-
for (GIDAuthenticationAction action in authenticationHandlerQueue) {
292+
for (GIDAuthenticationCompletion completion in authenticationHandlerQueue) {
293293
dispatch_async(dispatch_get_main_queue(), ^{
294-
action(error ? nil : self, error);
294+
completion(error ? nil : self, error);
295295
});
296296
}
297297
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ - (BOOL)hasPreviousSignIn {
187187
return [authState isAuthorized];
188188
}
189189

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

194194
- (BOOL)restorePreviousSignInNoRefresh {
@@ -217,52 +217,52 @@ - (BOOL)restorePreviousSignInNoRefresh {
217217
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
218218
presentingViewController:(UIViewController *)presentingViewController
219219
hint:(nullable NSString *)hint
220-
callback:(nullable GIDSignInCallback)callback {
220+
completion:(nullable GIDSignInCompletion)completion {
221221
GIDSignInInternalOptions *options =
222222
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
223223
presentingViewController:presentingViewController
224224
loginHint:hint
225-
addScopesFlow:NO
226-
callback:callback];
225+
addScopesFlow:NO
226+
completion:completion];
227227
[self signInWithOptions:options];
228228
}
229229

230230
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
231231
presentingViewController:(UIViewController *)presentingViewController
232232
hint:(nullable NSString *)hint
233233
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
234-
callback:(nullable GIDSignInCallback)callback {
234+
completion:(nullable GIDSignInCompletion)completion {
235235
GIDSignInInternalOptions *options =
236236
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
237237
presentingViewController:presentingViewController
238238
loginHint:hint
239239
addScopesFlow:NO
240240
scopes:additionalScopes
241-
callback:callback];
241+
completion:completion];
242242
[self signInWithOptions:options];
243243
}
244244

245245
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
246246
presentingViewController:(UIViewController *)presentingViewController
247-
callback:(nullable GIDSignInCallback)callback {
247+
completion:(nullable GIDSignInCompletion)completion {
248248
[self signInWithConfiguration:configuration
249249
presentingViewController:presentingViewController
250250
hint:nil
251-
callback:callback];
251+
completion:completion];
252252
}
253253

254254
- (void)addScopes:(NSArray<NSString *> *)scopes
255255
presentingViewController:(UIViewController *)presentingViewController
256-
callback:(nullable GIDSignInCallback)callback {
256+
completion:(nullable GIDSignInCompletion)completion {
257257
// A currentUser must be available in order to complete this flow.
258258
if (!self.currentUser) {
259259
// No currentUser is set, notify callback of failure.
260260
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
261261
code:kGIDSignInErrorCodeNoCurrentUser
262262
userInfo:nil];
263-
if (callback) {
263+
if (completion) {
264264
dispatch_async(dispatch_get_main_queue(), ^{
265-
callback(nil, error);
265+
completion(nil, error);
266266
});
267267
}
268268
return;
@@ -278,7 +278,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
278278
presentingViewController:presentingViewController
279279
loginHint:self.currentUser.profile.email
280280
addScopesFlow:YES
281-
callback:callback];
281+
completion:completion];
282282

283283
NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
284284
NSMutableSet<NSString *> *grantedScopes =
@@ -290,9 +290,9 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
290290
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
291291
code:kGIDSignInErrorCodeScopesAlreadyGranted
292292
userInfo:nil];
293-
if (callback) {
293+
if (completion) {
294294
dispatch_async(dispatch_get_main_queue(), ^{
295-
callback(nil, error);
295+
completion(nil, error);
296296
});
297297
}
298298
return;
@@ -310,52 +310,52 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
310310
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
311311
presentingWindow:(NSWindow *)presentingWindow
312312
hint:(nullable NSString *)hint
313-
callback:(nullable GIDSignInCallback)callback {
313+
completion:(nullable GIDSignInCompletion)completion {
314314
GIDSignInInternalOptions *options =
315315
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
316316
presentingWindow:presentingWindow
317317
loginHint:hint
318-
addScopesFlow:NO
319-
callback:callback];
318+
addScopesFlow:NO
319+
completion:completion];
320320
[self signInWithOptions:options];
321321
}
322322

323323
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
324324
presentingWindow:(NSWindow *)presentingWindow
325-
callback:(nullable GIDSignInCallback)callback {
325+
completion:(nullable GIDSignInCompletion)completion {
326326
[self signInWithConfiguration:configuration
327327
presentingWindow:presentingWindow
328328
hint:nil
329-
callback:callback];
329+
completion:completion];
330330
}
331331

332332
- (void)signInWithConfiguration:(GIDConfiguration *)configuration
333333
presentingWindow:(NSWindow *)presentingWindow
334334
hint:(nullable NSString *)hint
335335
additionalScopes:(nullable NSArray<NSString *> *)additionalScopes
336-
callback:(nullable GIDSignInCallback)callback {
336+
completion:(nullable GIDSignInCompletion)completion {
337337
GIDSignInInternalOptions *options =
338338
[GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
339339
presentingWindow:presentingWindow
340340
loginHint:hint
341341
addScopesFlow:NO
342342
scopes:additionalScopes
343-
callback:callback];
343+
completion:completion];
344344
[self signInWithOptions:options];
345345
}
346346

347347
- (void)addScopes:(NSArray<NSString *> *)scopes
348-
presentingWindow:(NSWindow *)presentingWindow
349-
callback:(nullable GIDSignInCallback)callback {
348+
presentingWindow:(NSWindow *)presentingWindow
349+
completion:(nullable GIDSignInCompletion)completion {
350350
// A currentUser must be available in order to complete this flow.
351351
if (!self.currentUser) {
352352
// No currentUser is set, notify callback of failure.
353353
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
354354
code:kGIDSignInErrorCodeNoCurrentUser
355355
userInfo:nil];
356-
if (callback) {
356+
if (completion) {
357357
dispatch_async(dispatch_get_main_queue(), ^{
358-
callback(nil, error);
358+
completion(nil, error);
359359
});
360360
}
361361
return;
@@ -371,7 +371,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
371371
presentingWindow:presentingWindow
372372
loginHint:self.currentUser.profile.email
373373
addScopesFlow:YES
374-
callback:callback];
374+
completion:completion];
375375

376376
NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
377377
NSMutableSet<NSString *> *grantedScopes =
@@ -383,9 +383,9 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
383383
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
384384
code:kGIDSignInErrorCodeScopesAlreadyGranted
385385
userInfo:nil];
386-
if (callback) {
386+
if (completion) {
387387
dispatch_async(dispatch_get_main_queue(), ^{
388-
callback(nil, error);
388+
completion(nil, error);
389389
});
390390
}
391391
return;
@@ -411,7 +411,7 @@ - (void)signOut {
411411
[self removeAllKeychainEntries];
412412
}
413413

414-
- (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
414+
- (void)disconnectWithCompletion:(nullable GIDDisconnectCompletion)completion {
415415
GIDGoogleUser *user = _currentUser;
416416
OIDAuthState *authState = user.authentication.authState;
417417
if (!authState) {
@@ -428,9 +428,9 @@ - (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
428428
if (!token) {
429429
[self signOut];
430430
// Nothing to do here, consider the operation successful.
431-
if (callback) {
431+
if (completion) {
432432
dispatch_async(dispatch_get_main_queue(), ^{
433-
callback(nil);
433+
completion(nil);
434434
});
435435
}
436436
return;
@@ -453,9 +453,9 @@ - (void)disconnectWithCallback:(nullable GIDDisconnectCallback)callback {
453453
if (!error) {
454454
[self signOut];
455455
}
456-
if (callback) {
456+
if (completion) {
457457
dispatch_async(dispatch_get_main_queue(), ^{
458-
callback(error);
458+
completion(error);
459459
});
460460
}
461461
}];
@@ -538,10 +538,10 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options {
538538
if (error) {
539539
[self authenticateWithOptions:options];
540540
} else {
541-
if (options.callback) {
541+
if (options.completion) {
542542
self->_currentOptions = nil;
543543
dispatch_async(dispatch_get_main_queue(), ^{
544-
options.callback(self->_currentUser, nil);
544+
options.completion(self->_currentUser, nil);
545545
});
546546
}
547547
}
@@ -707,10 +707,10 @@ - (void)authenticateWithOptions:(GIDSignInInternalOptions *)options {
707707
NSError *error = [NSError errorWithDomain:kGIDSignInErrorDomain
708708
code:kGIDSignInErrorCodeHasNoAuthInKeychain
709709
userInfo:nil];
710-
if (options.callback) {
710+
if (options.completion) {
711711
_currentOptions = nil;
712712
dispatch_async(dispatch_get_main_queue(), ^{
713-
options.callback(nil, error);
713+
options.completion(nil, error);
714714
});
715715
}
716716
return;
@@ -881,11 +881,11 @@ - (void)addCompletionCallback:(GIDAuthFlow *)authFlow {
881881
__weak GIDAuthFlow *weakAuthFlow = authFlow;
882882
[authFlow addCallback:^() {
883883
GIDAuthFlow *handlerAuthFlow = weakAuthFlow;
884-
if (self->_currentOptions.callback) {
885-
GIDSignInCallback callback = self->_currentOptions.callback;
884+
if (self->_currentOptions.completion) {
885+
GIDSignInCompletion completion = self->_currentOptions.completion;
886886
self->_currentOptions = nil;
887887
dispatch_async(dispatch_get_main_queue(), ^{
888-
callback(self->_currentUser, handlerAuthFlow.error);
888+
completion(self->_currentUser, handlerAuthFlow.error);
889889
});
890890
}
891891
}];

GoogleSignIn/Sources/GIDSignInInternalOptions.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ NS_ASSUME_NONNULL_BEGIN
5454
@property(nonatomic, readonly, weak, nullable) NSWindow *presentingWindow;
5555
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
5656

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

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

7474
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
7575
presentingViewController:(nullable UIViewController *)presentingViewController
7676
loginHint:(nullable NSString *)loginHint
7777
addScopesFlow:(BOOL)addScopesFlow
7878
scopes:(nullable NSArray *)scopes
79-
callback:(nullable GIDSignInCallback)callback;
79+
completion:(nullable GIDSignInCompletion)completion;
8080

8181
#elif TARGET_OS_OSX
8282
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
8383
presentingWindow:(nullable NSWindow *)presentingWindow
8484
loginHint:(nullable NSString *)loginHint
8585
addScopesFlow:(BOOL)addScopesFlow
86-
callback:(nullable GIDSignInCallback)callback;
86+
completion:(nullable GIDSignInCompletion)completion;
8787

8888
+ (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)configuration
8989
presentingWindow:(nullable NSWindow *)presentingWindow
9090
loginHint:(nullable NSString *)loginHint
9191
addScopesFlow:(BOOL)addScopesFlow
9292
scopes:(nullable NSArray *)scopes
93-
callback:(nullable GIDSignInCallback)callback;
93+
completion:(nullable GIDSignInCompletion)completion;
9494
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
9595

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

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

0 commit comments

Comments
 (0)