FIRIAMElapsedTimeTracker.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRIAMElapsedTimeTracker.h"
  17. @interface FIRIAMElapsedTimeTracker ()
  18. @property(nonatomic) NSTimeInterval totalTrackedTimeSoFar;
  19. @property(nonatomic) NSTimeInterval lastTrackingStartPoint;
  20. @property(nonatomic, nonnull) id<FIRIAMTimeFetcher> timeFetcher;
  21. @property(nonatomic) BOOL tracking;
  22. @end
  23. @implementation FIRIAMElapsedTimeTracker
  24. - (NSTimeInterval)trackedTimeSoFar {
  25. if (_tracking) {
  26. return self.totalTrackedTimeSoFar + [self.timeFetcher currentTimestampInSeconds] -
  27. self.lastTrackingStartPoint;
  28. } else {
  29. return self.totalTrackedTimeSoFar;
  30. }
  31. }
  32. - (void)pause {
  33. self.tracking = NO;
  34. self.totalTrackedTimeSoFar +=
  35. [self.timeFetcher currentTimestampInSeconds] - self.lastTrackingStartPoint;
  36. }
  37. - (void)resume {
  38. self.tracking = YES;
  39. self.lastTrackingStartPoint = [self.timeFetcher currentTimestampInSeconds];
  40. }
  41. - (instancetype)initWithTimeFetcher:(id<FIRIAMTimeFetcher>)timeFetcher {
  42. if (self = [super init]) {
  43. _tracking = YES;
  44. _timeFetcher = timeFetcher;
  45. _totalTrackedTimeSoFar = 0;
  46. _lastTrackingStartPoint = [timeFetcher currentTimestampInSeconds];
  47. }
  48. return self;
  49. }
  50. @end