Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TimerManager{
- static let instance = TimerManager()
- private var delegates = [TimerDelegate]()
- var currentTimers = [TimerObject]()
- public func timerAdded(timer: TimerObject){
- self.currentTimers.append(timer)
- timer.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(persistTimer(_:)), userInfo: "(TimerManager.instance.currentTimers.count-1)", repeats: false)
- timer.isRunning = true
- for delegate in delegates{
- delegate.startTimer(self.currentTimers[self.currentTimers.count-1])
- }
- }
- @objc public func persistTimer(timer: NSTimer){
- if let indexString = timer.userInfo as? String{
- if let index = Int(indexString){
- if let currentTimer = self.currentTimers[safe: index]{
- if currentTimer.isRunning{
- currentTimer.timeDuration -= 1
- for delegate in delegates{
- delegate.timerStarted(index)
- }
- }
- }
- }
- }
- }
- public func addTime(timerId: Int, amount: Int){
- for delegate in delegates{
- delegate.addTimeToTimer(timerId, amount: amount)
- UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
- currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
- }
- }
- public func subtractTime(timerId: Int, amount: Int){
- for delegate in delegates{
- delegate.subtractTimeFromTimer(timerId, amount: amount)
- UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
- currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
- }
- }
- public func removeDelegate(removedDelegate: TimerDelegate){
- for i in 0..<delegates.count{
- if delegates[i] === removedDelegate{
- delegates.removeAtIndex(i)
- }
- }
- }
- public func cancelTimer(timerId: Int){
- let currentTimer = currentTimers[timerId]
- currentTimer.timer.invalidate()
- UIApplication.sharedApplication().cancelLocalNotification(currentTimer.notification)
- currentTimers.removeAtIndex(timerId)
- }
- private init() {}
- }
- //Delete cell functionality
- public override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
- if( editingStyle == .Delete){
- let screenSize: CGRect = UIScreen.mainScreen().bounds
- let screenWidth = screenSize.width
- let screenHeight = screenSize.height
- let deletedTimer = TimerManager.instance.currentTimers[indexPath.row]
- if (deletedTimer.isRunning){
- deletedTimer.timer.invalidate()// <-- invalidates all timers for some reason
- UIApplication.sharedApplication().cancelLocalNotification(deletedTimer.notification)
- }
- TimerManager.instance.currentTimers.removeAtIndex(indexPath.row)
- self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
- self.tableView.frame = CGRect.init(x: 0, y: 100, width: screenWidth, height: screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
- }
- }
- public func startTimer(timer: TimerObject) {
- let newIndexPath = NSIndexPath(forRow: TimerManager.instance.currentTimers.count-1, inSection: 0)
- self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
- let screenSize: CGRect = UIScreen.mainScreen().bounds
- let screenWidth = screenSize.width
- let screenHeight = screenSize.height
- self.view.frame = CGRectMake(0, 100, screenWidth, screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
- self.view.alpha = 0.95
- }
- public class TimerObject{
- var alreadySet = false
- var isRunning = false
- var timer = NSTimer()
- var timeDuration = Int()
- var timerString = String()
- var doneString = String()
- let notification = UILocalNotification()
- init(timeDuration: Int, timerString: String, doneString: String){
- self.timeDuration = timeDuration
- self.timerString = timerString
- self.doneString = doneString
- notification.alertBody = doneString
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement