From 615a23e7d15b6f415c7d729c9fdc6e93e1a8aaea Mon Sep 17 00:00:00 2001 From: Oleg Orlov Date: Tue, 7 Jun 2016 00:49:57 +0300 Subject: [PATCH] added total work time counter --- ibreaktime/Base.lproj/MainMenu.xib | 8 +++++++ ibreaktime/Info.plist | 4 ++-- ibreaktime/PreferencesWindow.xib | 32 +++++++++++++-------------- ibreaktime/StatusMenuController.swift | 16 ++++++++++++-- ibreaktime/breaktimer.swift | 10 ++++++--- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/ibreaktime/Base.lproj/MainMenu.xib b/ibreaktime/Base.lproj/MainMenu.xib index 4e1a46d..9ae15d3 100644 --- a/ibreaktime/Base.lproj/MainMenu.xib +++ b/ibreaktime/Base.lproj/MainMenu.xib @@ -18,6 +18,8 @@ + + @@ -28,6 +30,12 @@ + + + + + + diff --git a/ibreaktime/Info.plist b/ibreaktime/Info.plist index 144038c..825a167 100644 --- a/ibreaktime/Info.plist +++ b/ibreaktime/Info.plist @@ -17,11 +17,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.4 + 1.5 CFBundleSignature ???? CFBundleVersion - 1.4 + 1.5 LSApplicationCategoryType public.app-category.productivity LSMinimumSystemVersion diff --git a/ibreaktime/PreferencesWindow.xib b/ibreaktime/PreferencesWindow.xib index 10fe89c..48de3fa 100644 --- a/ibreaktime/PreferencesWindow.xib +++ b/ibreaktime/PreferencesWindow.xib @@ -20,14 +20,14 @@ - + - + - + @@ -35,7 +35,7 @@ - + @@ -43,7 +43,7 @@ - + @@ -51,7 +51,7 @@ - + @@ -59,7 +59,7 @@ - + @@ -67,7 +67,7 @@ - + @@ -75,7 +75,7 @@ - + @@ -83,7 +83,7 @@ - + @@ -91,7 +91,7 @@ - + @@ -99,7 +99,7 @@ - + @@ -107,7 +107,7 @@ - + @@ -115,8 +115,8 @@ - - + + @@ -137,7 +137,7 @@ - + diff --git a/ibreaktime/StatusMenuController.swift b/ibreaktime/StatusMenuController.swift index 015f68f..d8108d4 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") - updateStatus() +// updateStatus() } @IBAction func aboutClicked(sender: AnyObject) { @@ -45,11 +45,19 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { updateStatus() } + @IBOutlet weak var totalWorkTimeMenuItem: NSMenuItem! + + @IBAction func totalWorkTimeClicked(sender: AnyObject) { + bt.totalWorkTime = 0 + defaults.setValue(0, forKey: "totalWorkTime") + } + func updateStatus() { var timeString: String defaults.setValue(bt.cyclesCount, forKey: "cyclesCount") defaults.setValue(bt.lastCheckTime, forKey: "lastCheckTime") + defaults.setValue(bt.totalWorkTime, forKey: "totalWorkTime") if showSeconds { timeString = String(format: "%d:%02d", bt.leftTime/60, bt.leftTime%60) @@ -68,6 +76,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { } cyclesMenuItem.title = "Cycles: \(bt.cyclesCount)" + totalWorkTimeMenuItem.title = String(format: "Total Work Time: %d:%02d (~%d Cycles)", bt.totalWorkTime/60, bt.totalWorkTime%60, bt.totalWorkTime/bt.workInterval) } func showSecondsCheckboxClicked(showSeconds: Bool) { @@ -102,7 +111,6 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { preferencesWindow.delegate = self aboutWindow = AboutWindow() - showSeconds = defaults.boolForKey("showSeconds") bt = Breaktimer(defaults.integerForKey("workInterval"), defaults.integerForKey("breakInterval"), defaults.integerForKey("maxIdleInterval")) @@ -114,6 +122,10 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { bt.checkCyclesCounter(Int(-lastCheckTime.timeIntervalSinceNow)) } + if bt.cyclesCount > 0 { + bt.totalWorkTime = defaults.integerForKey("totalWorkTime") + } + updateStatus() statusItem.menu = statusMenu diff --git a/ibreaktime/breaktimer.swift b/ibreaktime/breaktimer.swift index 5264df0..02281e8 100644 --- a/ibreaktime/breaktimer.swift +++ b/ibreaktime/breaktimer.swift @@ -19,6 +19,7 @@ class Breaktimer : NSObject { var timeToWork = true var leftTime = 0 var cyclesCount = 0 + var totalWorkTime = 0 func updateLeftTime(newInterval: Int, _ prevInterval: Int) { if newInterval > prevInterval { @@ -70,8 +71,8 @@ class Breaktimer : NSObject { var cyclesResetIdleInterval: Int { set { - // between 1 and 12 hours. defaulf: 5 hours - if newValue < 60 * 60 || newValue > 12 * 60 * 60 { + // between 1 and 12 hours. defaulf: 5 hours. 0: disable + if newValue < 0 || newValue > 12 * 60 * 60 { _cyclesResetIdleInterval = 5 * 60 * 60 } else { _cyclesResetIdleInterval = newValue @@ -93,7 +94,8 @@ class Breaktimer : NSObject { // check if we need reset cycles counter func checkCyclesCounter(intervalFromLastTimerTick: Int) { - if (intervalFromLastTimerTick > cyclesResetIdleInterval) || (idleTimer.idleTime > cyclesResetIdleInterval) { + if cyclesResetIdleInterval > 0 && + ((intervalFromLastTimerTick > cyclesResetIdleInterval) || (idleTimer.idleTime > cyclesResetIdleInterval)) { cyclesCount = 0 } } @@ -115,6 +117,7 @@ class Breaktimer : NSObject { if timeToWork { if !idleTimer.idle && leftTime > 0 { leftTime -= timerInterval + totalWorkTime += timerInterval if leftTime <= 0 { playSound() } @@ -135,6 +138,7 @@ class Breaktimer : NSObject { // wait until user takes a break if leftTime == breakInterval { if !idleTimer.idle { + totalWorkTime += timerInterval return } else { leftTime -= idleTimer.maxIdleInterval