From 0f72d3d7d531e1999c95b3a536431a3a308cdec6 Mon Sep 17 00:00:00 2001 From: Ben Brooks Date: Mon, 8 Oct 2018 14:26:56 +0100 Subject: [PATCH] Fix bugs with negative times, tidy some code --- .DS_Store | Bin 6148 -> 0 bytes README.md | 13 +++++------ StatusbarCountdown/AppDelegate.swift | 32 +++++++++++++-------------- 3 files changed, 20 insertions(+), 25 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index ebd5dbc2aa24c849b3ffde9d002bf46ba6dee552..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKK~4iP3>-rbmALfCfkR$^)E`7`dBJ`F=pqmTb_K2Am_P6_PP~V)9Z*;74GC41 zEhXbPwv&vs$r^wxw(T640hqEWiYgZ~Z1V~ZDD;|kAMjwISglkWWt4L02M z2D|(3IC`S3MTb{v=Xk@KdW#$MJYk3Xhue9x?jCzT*!$;Sa`kt+xjy!htJqU85DWwZ z!N6fKz&BfEdg2&;Fc1s`13wJt{gBucbH(ClR|h*Q0f-ZtO{i-vp)sjqu2>v7LlF-p zdZ^SALp+@JRQ+ @@ -13,10 +15,5 @@ If you encounter issues regarding unknown developer, Ctrl+Click on the .app and Alternatively, clone the repo and compile in Xcode :) -## Roadmap - -- [x] v0.1 - Countdown displaying in Statusbar -- [ ] v0.2 - Configurable date and title - ## License This project is licensed under the [MIT License](LICENSE). diff --git a/StatusbarCountdown/AppDelegate.swift b/StatusbarCountdown/AppDelegate.swift index 80ca54c..f51c5fd 100644 --- a/StatusbarCountdown/AppDelegate.swift +++ b/StatusbarCountdown/AppDelegate.swift @@ -11,11 +11,11 @@ import Foundation @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { - + // TODO: Remove these hardcoded values (into a plist) // TODO: Provide a GUI config to set plist values - let countToDate = NSDate(timeIntervalSince1970: 1531086400) - let countdownName = "Diss" + let countToDate = NSDate(timeIntervalSince1970: 1597249800) // FIXME: Your countdown date here... - 2020-08-04 16:30:00 UTC + let countdownName = "Countdown Name" // FIXME: Your countdown name here... var showName = true var showSeconds = true @@ -31,7 +31,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { statusItem.menu = statusMenu formatter.minimumIntegerDigits = zeroPad ? 2 : 1 - Timer.scheduledTimer(timeInterval: 0.25, + Timer.scheduledTimer(timeInterval: 0.16, // 16ms ~ 60fps target: self, selector: #selector(tick), userInfo: nil, @@ -40,13 +40,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Calculates the difference in time from now to the specified date and sets the statusItem title @objc func tick() { - let diff = Int(countToDate.timeIntervalSinceNow) + let diffSeconds = Int(countToDate.timeIntervalSinceNow) - if (showName) { - statusItem.title = countdownName + ": " + formatTime(diff) - } else { - statusItem.title = formatTime(diff) - } + statusItem.title = (showName) ? countdownName + ": " : "" + statusItem.title! += formatTime(diffSeconds) } @@ -66,13 +63,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { } // Display seconds as Days, Hours, Minutes, Seconds. - func formatTime(_ time: Int) -> (String) { - let time = secondsToTime(time) - let days = (time.0 > 0) ? String(time.0) + "d " : "" - let hours = (time.1 > 0 || time.0 > 0) ? formatter.string(from: NSNumber(value: time.1))! + "h " : "" - let minutes = (time.2 > 0 || time.1 > 0 || time.0 > 0) ? formatter.string(from: NSNumber(value: time.2))! + "m" : "" - let seconds = (showSeconds) ? " " + formatter.string(from: NSNumber(value: time.3))! + "s" : "" - return days + hours + minutes + seconds + func formatTime(_ seconds: Int) -> (String) { + let time = secondsToTime(abs(seconds)) + let daysStr = (time.0 != 0) ? String(time.0) + "d " : "" + let hoursStr = (time.1 != 0 || time.0 != 0) ? formatter.string(from: NSNumber(value: time.1))! + "h " : "" + let minutesStr = (time.2 != 0 || time.1 != 0 || time.0 != 0) ? formatter.string(from: NSNumber(value: time.2))! + "m" : "" + let secondsStr = (showSeconds) ? " " + formatter.string(from: NSNumber(value: time.3))! + "s" : "" + let suffixStr = (seconds < 0) ? " ago" : "" // TODO: i18n? + return daysStr + hoursStr + minutesStr + secondsStr + suffixStr } // MenuItem click event to toggle showSeconds