-
Notifications
You must be signed in to change notification settings - Fork 248
Add a timed loader class #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68c6a85
Add a timed loader class
mdmathias 501f84d
Update GIDTimedLoaader to present loading VC on main queue
mdmathias fe4fee0
Only create GIDTimedLoader in GIDSignIn if necessary
mdmathias 1b54259
Add target check for _timedLoader property
mdmathias d6557bc
Fix issue from rebase
mdmathias f54f4a7
Remove log statements
mdmathias 384d386
Add clarifying comment about activity indicator animation
mdmathias 9b5378d
Remove check in GIDSignIn for animation status
mdmathias f675864
Add doc comments to GIDTimedLoader.h
mdmathias 4d8aa26
Remove reference check for timer in GIDTimedLoader.m
mdmathias df8b89b
Check if _appCheck is configured before using
mdmathias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
| #import <TargetConditionals.h> | ||
|
|
||
| #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
|
|
||
| /// An enumeration detailing the states of the timed loader. | ||
| typedef NS_ENUM(NSUInteger, GIDTimedLoaderAnimationStatus) { | ||
| /// The timed loader has not started. | ||
| GIDTimedLoaderAnimationStatusNotStarted, | ||
| /// The timed loader's activity indicator is animating. | ||
| GIDTimedLoaderAnimationStatusAnimating, | ||
| /// The timed loader's activity indicator has stopped animating. | ||
| GIDTimedLoaderAnimationStatusStopped, | ||
| }; | ||
|
|
||
| /// The minimum animation duration time for the timed loader's activity indicator. | ||
| extern CFTimeInterval const kGIDTimedLoaderMinAnimationDuration; | ||
| /// The maximum delay to wait before the time loader will display the loading activity indicator. | ||
| extern CFTimeInterval const kGIDTimedLoaderMaxDelayBeforeAnimating; | ||
|
|
||
| @class UIViewController; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /// A type used to manage the presentation of a load screen for at least | ||
| /// `kGIDTimedLoaderMinAnimationDuration` to prevent flashing. | ||
| /// | ||
| /// `GIDTimedLoader` will also only show its loading screen until | ||
| /// `kGIDTimedLoaderMaxDelayBeforeAnimating` has expired. | ||
| @interface GIDTimedLoader : NSObject | ||
|
|
||
| /// Created this timed loading controller with the provided presenting view controller, which will | ||
| /// be used for presenting hte loading view controller with the activity indicator. | ||
| - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController; | ||
|
|
||
| - (instancetype)init NS_UNAVAILABLE; | ||
|
|
||
| /// Tells the controller to start keeping track of loading time. | ||
| - (void)startTiming; | ||
|
|
||
| /// Tells the controller to stop keeping track of loading time. | ||
| /// | ||
| /// @param completion The block to invoke upon successfully stopping. | ||
| /// @note Use the completion parameter to, for example, present the UI that should be shown after | ||
| /// the work has completed. | ||
| - (void)stopTimingWithCompletion:(void (^)(void))completion; | ||
|
|
||
| @property(nonatomic) GIDTimedLoaderAnimationStatus animationStatus; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
|
|
||
| #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import "GoogleSignIn/Sources/GIDTimedLoader/GIDTimedLoader.h" | ||
|
|
||
| #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
|
|
||
| @import UIKit; | ||
| @import CoreMedia; | ||
| #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h" | ||
| #import "GoogleSignIn/Sources/GIDAppCheck/UI/GIDActivityIndicatorViewController.h" | ||
|
|
||
| CFTimeInterval const kGIDTimedLoaderMinAnimationDuration = 1.0; | ||
| CFTimeInterval const kGIDTimedLoaderMaxDelayBeforeAnimating = 0.5; | ||
|
|
||
| @interface GIDTimedLoader () | ||
|
|
||
| @property(nonatomic, strong) UIViewController *presentingViewController; | ||
| @property(nonatomic, strong) GIDActivityIndicatorViewController *loadingViewController; | ||
| @property(nonatomic, strong, nullable) NSTimer *loadingTimer; | ||
| /// Timestamp representing when the loading view controller was presented and started animating | ||
| @property(nonatomic) CFTimeInterval loadingTimeStamp; | ||
|
|
||
| @end | ||
|
|
||
| @implementation GIDTimedLoader | ||
|
|
||
| - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController { | ||
| if (self = [super init]) { | ||
| _presentingViewController = presentingViewController; | ||
| _loadingViewController = [[GIDActivityIndicatorViewController alloc] init]; | ||
| _animationStatus = GIDTimedLoaderAnimationStatusNotStarted; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)startTiming { | ||
| if (self.animationStatus == GIDTimedLoaderAnimationStatusAnimating) { | ||
| return; | ||
| } | ||
|
|
||
| self.animationStatus = GIDTimedLoaderAnimationStatusAnimating; | ||
| self.loadingTimer = [NSTimer scheduledTimerWithTimeInterval:kGIDTimedLoaderMaxDelayBeforeAnimating | ||
| target:self | ||
| selector:@selector(presentLoadingViewController) | ||
| userInfo:nil | ||
| repeats:NO]; | ||
| } | ||
|
|
||
| - (void)presentLoadingViewController { | ||
| if (self.animationStatus == GIDTimedLoaderAnimationStatusStopped) { | ||
| return; | ||
| } | ||
| self.animationStatus = GIDTimedLoaderAnimationStatusAnimating; | ||
| self.loadingTimeStamp = CACurrentMediaTime(); | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| // Since this loading VC may be reused, the activity indicator may have been stopped; restart it | ||
| [self.loadingViewController.activityIndicator startAnimating]; | ||
| [self.presentingViewController presentViewController:self.loadingViewController | ||
| animated:YES | ||
| completion:nil]; | ||
| }); | ||
| } | ||
|
|
||
| - (void)stopTimingWithCompletion:(void (^)(void))completion { | ||
| if (self.animationStatus != GIDTimedLoaderAnimationStatusAnimating) { | ||
| return; | ||
| } | ||
|
|
||
| [self.loadingTimer invalidate]; | ||
| self.loadingTimer = nil; | ||
|
|
||
| dispatch_time_t deadline = [self remainingDurationToAnimate]; | ||
| dispatch_after(deadline, dispatch_get_main_queue(), ^{ | ||
| self.animationStatus = GIDTimedLoaderAnimationStatusStopped; | ||
| [self.loadingViewController.activityIndicator stopAnimating]; | ||
| [self.loadingViewController dismissViewControllerAnimated:YES completion:nil]; | ||
| completion(); | ||
| }); | ||
| } | ||
|
|
||
| - (dispatch_time_t)remainingDurationToAnimate { | ||
| // If we are not animating, then no need to wait | ||
| if (self.animationStatus != GIDTimedLoaderAnimationStatusAnimating) { | ||
| return 0; | ||
| } | ||
|
|
||
| CFTimeInterval now = CACurrentMediaTime(); | ||
| CFTimeInterval durationWaited = now - self.loadingTimeStamp; | ||
| // If we have already waited for the minimum animation duration, then no need to wait | ||
| if (durationWaited >= kGIDTimedLoaderMinAnimationDuration) { | ||
| return 0; | ||
| } | ||
|
|
||
| CFTimeInterval diff = kGIDTimedLoaderMinAnimationDuration - durationWaited; | ||
| int64_t diffNanos = diff * NSEC_PER_SEC; | ||
| dispatch_time_t timeToWait = dispatch_time(DISPATCH_TIME_NOW, diffNanos); | ||
| return timeToWait; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.