Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
orlv committed Jun 6, 2016
1 parent 4b54d9e commit e2a4897
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ibreaktime/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<string>1.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.3</string>
<string>1.4</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>
Expand Down
38 changes: 18 additions & 20 deletions ibreaktime/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {
@IBAction func cyclesClicked(sender: AnyObject) {
bt.cyclesCount = 0
defaults.setValue(0, forKey: "cyclesCount")
showStatus()
updateStatus()
}

@IBAction func aboutClicked(sender: AnyObject) {
Expand All @@ -42,30 +42,28 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {

@IBAction func resetTimerClicked(sender: AnyObject) {
bt.resetTimer()
showStatus()
updateStatus()
}

func showStatus() {
func updateStatus() {
var timeString: String

defaults.setValue(bt.cyclesCount, forKey: "cyclesCount")
defaults.setValue(bt.lastCheckTime, forKey: "lastCheckTime")

if bt.leftTime > 0 {
if showSeconds {
timeString = String(format: "%d:%02d", bt.leftTime/60, bt.leftTime%60)
} else {
timeString = String(lroundf(Float(bt.leftTime)/60))
}

if bt.timeToWork {
statusItem.title = timeString
} else {
statusItem.title = "Rest: \(timeString)"
}
if showSeconds {
timeString = String(format: "%d:%02d", bt.leftTime/60, bt.leftTime%60)
} else {
if bt.timeToWork {
timeString = String(lroundf(Float(bt.leftTime)/60))
}

if bt.timeToWork {
statusItem.title = timeString
} else {
if bt.leftTime == bt.breakInterval {
statusItem.title = "Time to Rest"
} else {
statusItem.title = "Rest: \(timeString)"
}
}

Expand All @@ -74,7 +72,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {

func showSecondsCheckboxClicked(showSeconds: Bool) {
self.showSeconds = showSeconds
showStatus()
updateStatus()
}

func loadPreferences() {
Expand All @@ -96,7 +94,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {
loadPreferences()
savePreferences()

showStatus()
updateStatus()
}

override func awakeFromNib() {
Expand All @@ -116,12 +114,12 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {
bt.checkCyclesCounter(Int(-lastCheckTime.timeIntervalSinceNow))
}

showStatus()
updateStatus()
statusItem.menu = statusMenu

savePreferences()

NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(showStatus), userInfo: nil, repeats: true)
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(updateStatus), userInfo: nil, repeats: true)
}


Expand Down
17 changes: 13 additions & 4 deletions ibreaktime/breaktimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,27 @@ class Breaktimer : NSObject {
}
}

// work time is done, wait until user takes a break
if leftTime <= 0 && idleTimer.idle {
// work time is done
if leftTime <= 0 {
timeToWork = false
leftTime = breakInterval - idleTimer.maxIdleInterval
leftTime = breakInterval
cyclesCount += 1
}

// if user takes a break, reset timer
if idleTimer.idleTime >= breakInterval {
leftTime = workInterval
}
} else { // if time to break
} else { // time to break
// wait until user takes a break
if leftTime == breakInterval {
if !idleTimer.idle {
return
} else {
leftTime -= idleTimer.maxIdleInterval
}
}

leftTime -= timerInterval
if leftTime <= 0 {
playSound()
Expand Down

0 comments on commit e2a4897

Please sign in to comment.