From e2a4897d8cd1bffcac1781baa0a4e7e957beb2e0 Mon Sep 17 00:00:00 2001 From: Oleg Orlov Date: Tue, 7 Jun 2016 00:16:15 +0300 Subject: [PATCH] refactoring --- ibreaktime/Info.plist | 4 +-- ibreaktime/StatusMenuController.swift | 38 +++++++++++++-------------- ibreaktime/breaktimer.swift | 17 +++++++++--- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/ibreaktime/Info.plist b/ibreaktime/Info.plist index 13ebbe5..144038c 100644 --- a/ibreaktime/Info.plist +++ b/ibreaktime/Info.plist @@ -17,11 +17,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.3 + 1.4 CFBundleSignature ???? CFBundleVersion - 1.3 + 1.4 LSApplicationCategoryType public.app-category.productivity LSMinimumSystemVersion diff --git a/ibreaktime/StatusMenuController.swift b/ibreaktime/StatusMenuController.swift index cfc3de5..015f68f 100644 --- a/ibreaktime/StatusMenuController.swift +++ b/ibreaktime/StatusMenuController.swift @@ -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) { @@ -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)" } } @@ -74,7 +72,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { func showSecondsCheckboxClicked(showSeconds: Bool) { self.showSeconds = showSeconds - showStatus() + updateStatus() } func loadPreferences() { @@ -96,7 +94,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { loadPreferences() savePreferences() - showStatus() + updateStatus() } override func awakeFromNib() { @@ -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) } diff --git a/ibreaktime/breaktimer.swift b/ibreaktime/breaktimer.swift index 4cabe58..5264df0 100644 --- a/ibreaktime/breaktimer.swift +++ b/ibreaktime/breaktimer.swift @@ -120,10 +120,10 @@ 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 } @@ -131,7 +131,16 @@ class Breaktimer : NSObject { 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()