Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. public class TimerManager{
  2. static let instance = TimerManager()
  3. private var delegates = [TimerDelegate]()
  4. var currentTimers = [TimerObject]()
  5.  
  6. public func timerAdded(timer: TimerObject){
  7. self.currentTimers.append(timer)
  8. timer.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(persistTimer(_:)), userInfo: "(TimerManager.instance.currentTimers.count-1)", repeats: false)
  9. timer.isRunning = true
  10. for delegate in delegates{
  11. delegate.startTimer(self.currentTimers[self.currentTimers.count-1])
  12. }
  13. }
  14.  
  15. @objc public func persistTimer(timer: NSTimer){
  16. if let indexString = timer.userInfo as? String{
  17. if let index = Int(indexString){
  18. if let currentTimer = self.currentTimers[safe: index]{
  19. if currentTimer.isRunning{
  20. currentTimer.timeDuration -= 1
  21. for delegate in delegates{
  22. delegate.timerStarted(index)
  23. }
  24. }
  25. }
  26. }
  27. }
  28.  
  29. }
  30.  
  31. public func addTime(timerId: Int, amount: Int){
  32. for delegate in delegates{
  33. delegate.addTimeToTimer(timerId, amount: amount)
  34. UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
  35. currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
  36. }
  37. }
  38.  
  39. public func subtractTime(timerId: Int, amount: Int){
  40. for delegate in delegates{
  41. delegate.subtractTimeFromTimer(timerId, amount: amount)
  42. UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
  43. currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
  44. }
  45. }
  46.  
  47. public func removeDelegate(removedDelegate: TimerDelegate){
  48. for i in 0..<delegates.count{
  49. if delegates[i] === removedDelegate{
  50. delegates.removeAtIndex(i)
  51. }
  52. }
  53. }
  54.  
  55. public func cancelTimer(timerId: Int){
  56. let currentTimer = currentTimers[timerId]
  57. currentTimer.timer.invalidate()
  58. UIApplication.sharedApplication().cancelLocalNotification(currentTimer.notification)
  59. currentTimers.removeAtIndex(timerId)
  60. }
  61.  
  62. private init() {}
  63. }
  64.  
  65. //Delete cell functionality
  66. public override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  67. if( editingStyle == .Delete){
  68. let screenSize: CGRect = UIScreen.mainScreen().bounds
  69. let screenWidth = screenSize.width
  70. let screenHeight = screenSize.height
  71. let deletedTimer = TimerManager.instance.currentTimers[indexPath.row]
  72. if (deletedTimer.isRunning){
  73. deletedTimer.timer.invalidate()// <-- invalidates all timers for some reason
  74. UIApplication.sharedApplication().cancelLocalNotification(deletedTimer.notification)
  75. }
  76. TimerManager.instance.currentTimers.removeAtIndex(indexPath.row)
  77. self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
  78. self.tableView.frame = CGRect.init(x: 0, y: 100, width: screenWidth, height: screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
  79. }
  80. }
  81.  
  82. public func startTimer(timer: TimerObject) {
  83. let newIndexPath = NSIndexPath(forRow: TimerManager.instance.currentTimers.count-1, inSection: 0)
  84. self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
  85. let screenSize: CGRect = UIScreen.mainScreen().bounds
  86. let screenWidth = screenSize.width
  87. let screenHeight = screenSize.height
  88. self.view.frame = CGRectMake(0, 100, screenWidth, screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
  89. self.view.alpha = 0.95
  90.  
  91. }
  92.  
  93. public class TimerObject{
  94. var alreadySet = false
  95. var isRunning = false
  96. var timer = NSTimer()
  97. var timeDuration = Int()
  98. var timerString = String()
  99. var doneString = String()
  100. let notification = UILocalNotification()
  101.  
  102. init(timeDuration: Int, timerString: String, doneString: String){
  103. self.timeDuration = timeDuration
  104. self.timerString = timerString
  105. self.doneString = doneString
  106. notification.alertBody = doneString
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement