瀏覽代碼

Add KVO in GIDGoogleUser

Send KVO notifications when authState updates tokens.
pinlu 3 年之前
父節點
當前提交
0f6554d2a8
共有 1 個文件被更改,包括 37 次插入41 次删除
  1. 37 41
      GoogleSignIn/Sources/GIDGoogleUser.m

+ 37 - 41
GoogleSignIn/Sources/GIDGoogleUser.m

@@ -45,11 +45,12 @@ NS_ASSUME_NONNULL_BEGIN
 @implementation GIDGoogleUser {
   OIDAuthState *_authState;
   GIDConfiguration *_cachedConfiguration;
-  GIDToken *_cachedAccessToken;
-  GIDToken *_cachedRefreshToken;
-  GIDToken *_cachedIdToken;
 }
 
+@synthesize accessToken = _accessToken;
+@synthesize refreshToken = _refreshToken;
+@synthesize idToken = _idToken;
+
 - (nullable NSString *)userID {
   NSString *idTokenString = self.idToken.tokenString;
   if (idTokenString) {
@@ -101,40 +102,6 @@ NS_ASSUME_NONNULL_BEGIN
   return _cachedConfiguration;
 }
 
-- (GIDToken *)accessToken {
-  @synchronized(self) {
-    if (!_cachedAccessToken) {
-      _cachedAccessToken = [[GIDToken alloc] initWithTokenString:_authState.lastTokenResponse.accessToken
-                                                  expirationDate:_authState.lastTokenResponse.
-                                                                     accessTokenExpirationDate];
-    }
-  }
-  return _cachedAccessToken;
-}
-
-- (GIDToken *)refreshToken {
-  @synchronized(self) {
-    if (!_cachedRefreshToken) {
-      _cachedRefreshToken = [[GIDToken alloc] initWithTokenString:_authState.refreshToken
-                                                   expirationDate:nil];
-    }
-  }
-  return _cachedRefreshToken;
-}
-
-- (nullable GIDToken *)idToken {
-  @synchronized(self) {
-    NSString *idTokenString = _authState.lastTokenResponse.idToken;
-    if (!_cachedIdToken && idTokenString) {
-      NSDate *idTokenExpirationDate = [[[OIDIDToken alloc]
-                                        initWithIDTokenString:idTokenString] expiresAt];
-      _cachedIdToken = [[GIDToken alloc] initWithTokenString:idTokenString
-                                              expirationDate:idTokenExpirationDate];
-    }
-  }
-  return _cachedIdToken;
-}
-
 #pragma mark - Private Methods
 
 - (instancetype)initWithAuthState:(OIDAuthState *)authState
@@ -153,10 +120,25 @@ NS_ASSUME_NONNULL_BEGIN
     _authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
     _profile = profileData;
     
-    // These three tokens will be generated in the getter and cached .
-    _cachedAccessToken = nil;
-    _cachedRefreshToken = nil;
-    _cachedIdToken = nil;
+    [self sendKVONotificationsBeforeChanges];
+    [self updateTokensWithAuthState:authState];
+    [self sendKVONotificationAfterChanges];
+  }
+}
+
+- (void)updateTokensWithAuthState:(OIDAuthState *)authState {
+  _accessToken = [[GIDToken alloc] initWithTokenString:authState.lastTokenResponse.accessToken
+                                        expirationDate:authState.lastTokenResponse.
+                                                         accessTokenExpirationDate];
+  _refreshToken = [[GIDToken alloc] initWithTokenString:authState.refreshToken
+                                         expirationDate:nil];
+  _idToken = nil;
+  NSString *idTokenString = authState.lastTokenResponse.idToken;
+  if (idTokenString) {
+    NSDate *idTokenExpirationDate = [[[OIDIDToken alloc]
+                                      initWithIDTokenString:idTokenString] expiresAt];
+    _idToken = [[GIDToken alloc] initWithTokenString:idTokenString
+                                      expirationDate:idTokenExpirationDate];
   }
 }
 
@@ -173,6 +155,20 @@ NS_ASSUME_NONNULL_BEGIN
   return nil;
 }
 
+- (void)sendKVONotificationsBeforeChanges {
+  [self willChangeValueForKey:NSStringFromSelector(@selector(accessToken))];
+  [self willChangeValueForKey:NSStringFromSelector(@selector(refreshToken))];
+  [self willChangeValueForKey:NSStringFromSelector(@selector(idToken))];
+}
+
+- (void)sendKVONotificationAfterChanges {
+  [self didChangeValueForKey:NSStringFromSelector(@selector(accessToken))];
+  [self didChangeValueForKey:NSStringFromSelector(@selector(refreshToken))];
+  [self didChangeValueForKey:NSStringFromSelector(@selector(idToken))];
+}
+
+
+
 #pragma mark - NSSecureCoding
 
 + (BOOL)supportsSecureCoding {