Skip to content
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

Added multiple delegates & added method for cancelling countdown #4

Merged
merged 3 commits into from
Mar 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions CountdownLabel/CountdownLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import UIKit
import LTMorphingLabel

@objc public protocol CountdownLabelDelegate {
optional func countdownStarted()
optional func countdownPaused()
optional func countdownFinished()
optional func countdownCancelled()
optional func countingAt(timeCounted timeCounted: NSTimeInterval, timeRemaining: NSTimeInterval)
}

Expand Down Expand Up @@ -190,9 +193,6 @@ public class CountdownLabel: LTMorphingLabel {
// MARK: - Public
public extension CountdownLabel {
func start(completion: ( () -> () )? = nil) {
// set completion if needed
self.completion = completion

// pause status check
updatePauseStatusIfNeeded()

Expand All @@ -201,9 +201,15 @@ public extension CountdownLabel {

// fire!
timer.fire()

// set completion if needed
completion?()

// set delegate
countdownDelegate?.countdownStarted?()
}

func pause() {
func pause(completion: (() -> ())? = nil) {
if paused {
return
}
Expand All @@ -217,8 +223,25 @@ public extension CountdownLabel {

// reset
pausedDate = NSDate()

// set completion if needed
completion?()

// set delegate
countdownDelegate?.countdownPaused?()
}


func cancel(completion: (() -> ())? = nil) {
text = dateFormatter.stringFromDate(date1970.dateByAddingTimeInterval(0))
dispose()

// set completion if needed
completion?()

// set delegate
countdownDelegate?.countdownCancelled?()
}

func addTime(time: NSTimeInterval) {
currentTime = time + currentTime
diffDate = date1970.dateByAddingTimeInterval(currentTime)
Expand Down Expand Up @@ -283,10 +306,10 @@ private extension CountdownLabel {

// create
timer = NSTimer.scheduledTimerWithTimeInterval(defaultFireInterval,
target: self,
selector: "updateLabel",
userInfo: nil,
repeats: true)
target: self,
selector: #selector(updateLabel),
userInfo: nil,
repeats: true)

// register to NSrunloop
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
Expand Down