Skip to content

Commit fd807dd

Browse files
authored
Handle KVO for currentUser with property accessors (#233)
1 parent 98fc1f4 commit fd807dd

File tree

3 files changed

+6
-53
lines changed

3 files changed

+6
-53
lines changed

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ - (BOOL)restorePreviousSignInNoRefresh {
214214
GIDProfileData *profileData = [self profileDataWithIDToken:idToken];
215215

216216
GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState profileData:profileData];
217-
[self setCurrentUserWithKVO:user];
217+
self.currentUser = user;
218218
return YES;
219219
}
220220

@@ -401,9 +401,7 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
401401
- (void)signOut {
402402
// Clear the current user if there is one.
403403
if (_currentUser) {
404-
[self willChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
405-
_currentUser = nil;
406-
[self didChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
404+
self.currentUser = nil;
407405
}
408406
// Remove all state from the keychain.
409407
[self removeAllKeychainEntries];
@@ -803,7 +801,7 @@ - (void)addSaveAuthCallback:(GIDAuthFlow *)authFlow {
803801
} else {
804802
GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
805803
profileData:handlerAuthFlow.profileData];
806-
[self setCurrentUserWithKVO:user];
804+
self.currentUser = user;
807805
}
808806
}
809807
}];
@@ -935,17 +933,6 @@ - (BOOL)handleDevicePolicyAppURL:(NSURL *)url {
935933
return YES;
936934
}
937935

938-
#pragma mark - Key-Value Observing
939-
940-
// Override |NSObject(NSKeyValueObservingCustomization)| method in order to provide custom KVO
941-
// notifications for the |currentUser| property.
942-
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
943-
if ([key isEqual:NSStringFromSelector(@selector(currentUser))]) {
944-
return NO;
945-
}
946-
return [super automaticallyNotifiesObserversForKey:key];
947-
}
948-
949936
#pragma mark - Helpers
950937

951938
- (NSError *)errorWithString:(NSString *)errorString code:(GIDSignInErrorCode)code {
@@ -1035,13 +1022,6 @@ - (GIDProfileData *)profileDataWithIDToken:(OIDIDToken *)idToken {
10351022
imageURL:[NSURL URLWithString:idToken.claims[kBasicProfilePictureKey]]];
10361023
}
10371024

1038-
// Set currentUser making appropriate KVO calls.
1039-
- (void)setCurrentUserWithKVO:(GIDGoogleUser *_Nullable)user {
1040-
[self willChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
1041-
_currentUser = user;
1042-
[self didChangeValueForKey:NSStringFromSelector(@selector(currentUser))];
1043-
}
1044-
10451025
// Try to retrieve a configuration value from an |NSBundle|'s Info.plist for a given key.
10461026
+ (nullable NSString *)configValueFromBundle:(NSBundle *)bundle forKey:(NSString *)key {
10471027
NSString *value;

GoogleSignIn/Sources/GIDSignIn_Private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
2424
// Private |GIDSignIn| methods that are used internally in this SDK and other Google SDKs.
2525
@interface GIDSignIn ()
2626

27+
// Redeclare |currentUser| as readwrite for internal use.
28+
@property(nonatomic, readwrite, nullable) GIDGoogleUser *currentUser;
29+
2730
// Private initializer for |GIDSignIn|.
2831
- (instancetype)initPrivate;
2932

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@
153153
static NSString *const kGrantedScope = @"grantedScope";
154154
static NSString *const kNewScope = @"newScope";
155155

156-
/// Unique pointer value for KVO tests.
157-
static void *kTestObserverContext = &kTestObserverContext;
158-
159156
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
160157
// This category is used to allow the test to swizzle a private method.
161158
@interface UIViewController (Testing)
@@ -263,9 +260,6 @@ @interface GIDSignInTest : XCTestCase {
263260
// The saved token request callback.
264261
OIDTokenCallback _savedTokenCallback;
265262

266-
// Set of all |GIDSignIn| key paths which were observed to change.
267-
NSMutableSet *_changedKeyPaths;
268-
269263
// Status returned by saveAuthorization:toKeychainForName:
270264
BOOL _saveAuthorizationReturnValue;
271265
}
@@ -286,7 +280,6 @@ - (void)setUp {
286280
_completionCalled = NO;
287281
_keychainSaved = NO;
288282
_keychainRemoved = NO;
289-
_changedKeyPaths = [[NSMutableSet alloc] init];
290283

291284
// Mocks
292285
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
@@ -354,11 +347,6 @@ - (void)setUp {
354347
strongSelf->_completionCalled = YES;
355348
strongSelf->_authError = error;
356349
};
357-
358-
[_signIn addObserver:self
359-
forKeyPath:NSStringFromSelector(@selector(currentUser))
360-
options:0
361-
context:kTestObserverContext];
362350
}
363351

364352
- (void)tearDown {
@@ -379,10 +367,6 @@ - (void)tearDown {
379367

380368
[_fakeMainBundle stopFaking];
381369
[super tearDown];
382-
383-
[_signIn removeObserver:self
384-
forKeyPath:NSStringFromSelector(@selector(currentUser))
385-
context:kTestObserverContext];
386370
}
387371

388372
#pragma mark - Tests
@@ -759,8 +743,6 @@ - (void)testSignOut {
759743
[_signIn signOut];
760744
XCTAssertNil(_signIn.currentUser, @"should not have a current user");
761745
XCTAssertTrue(_keychainRemoved, @"should remove keychain");
762-
XCTAssertTrue([_changedKeyPaths containsObject:NSStringFromSelector(@selector(currentUser))],
763-
@"should notify observers that signed in user changed");
764746

765747
OCMVerify([_authorization removeAuthorizationFromKeychainForName:kKeychainName
766748
useDataProtectionKeychain:YES]);
@@ -1445,16 +1427,4 @@ - (void)OAuthLoginWithAddScopesFlow:(BOOL)addScopesFlow
14451427
}
14461428
}
14471429

1448-
1449-
#pragma mark - Key Value Observing
1450-
1451-
- (void)observeValueForKeyPath:(NSString *)keyPath
1452-
ofObject:(id)object
1453-
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
1454-
context:(void *)context {
1455-
if (context == kTestObserverContext && object == _signIn) {
1456-
[_changedKeyPaths addObject:keyPath];
1457-
}
1458-
}
1459-
14601430
@end

0 commit comments

Comments
 (0)