Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVTimer.m 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVTimer.h"
  18. #pragma mark CDVTimerItem
  19. @interface CDVTimerItem : NSObject
  20. @property (nonatomic, strong) NSString* name;
  21. @property (nonatomic, strong) NSDate* started;
  22. @property (nonatomic, strong) NSDate* ended;
  23. - (void)log;
  24. @end
  25. @implementation CDVTimerItem
  26. - (void)log
  27. {
  28. NSLog(@"[CDVTimer][%@] %fms", self.name, [self.ended timeIntervalSinceDate:self.started] * 1000.0);
  29. }
  30. @end
  31. #pragma mark CDVTimer
  32. @interface CDVTimer ()
  33. @property (nonatomic, strong) NSMutableDictionary* items;
  34. @end
  35. @implementation CDVTimer
  36. #pragma mark object methods
  37. - (id)init
  38. {
  39. if (self = [super init]) {
  40. self.items = [NSMutableDictionary dictionaryWithCapacity:6];
  41. }
  42. return self;
  43. }
  44. - (void)add:(NSString*)name
  45. {
  46. if ([self.items objectForKey:[name lowercaseString]] == nil) {
  47. CDVTimerItem* item = [CDVTimerItem new];
  48. item.name = name;
  49. item.started = [NSDate new];
  50. [self.items setObject:item forKey:[name lowercaseString]];
  51. } else {
  52. NSLog(@"Timer called '%@' already exists.", name);
  53. }
  54. }
  55. - (void)remove:(NSString*)name
  56. {
  57. CDVTimerItem* item = [self.items objectForKey:[name lowercaseString]];
  58. if (item != nil) {
  59. item.ended = [NSDate new];
  60. [item log];
  61. [self.items removeObjectForKey:[name lowercaseString]];
  62. } else {
  63. NSLog(@"Timer called '%@' does not exist.", name);
  64. }
  65. }
  66. - (void)removeAll
  67. {
  68. [self.items removeAllObjects];
  69. }
  70. #pragma mark class methods
  71. + (void)start:(NSString*)name
  72. {
  73. [[CDVTimer sharedInstance] add:name];
  74. }
  75. + (void)stop:(NSString*)name
  76. {
  77. [[CDVTimer sharedInstance] remove:name];
  78. }
  79. + (void)clearAll
  80. {
  81. [[CDVTimer sharedInstance] removeAll];
  82. }
  83. + (CDVTimer*)sharedInstance
  84. {
  85. static dispatch_once_t pred = 0;
  86. __strong static CDVTimer* _sharedObject = nil;
  87. dispatch_once(&pred, ^{
  88. _sharedObject = [[self alloc] init];
  89. });
  90. return _sharedObject;
  91. }
  92. @end