From 23122cef83abff886ebc06e266a2270554e95f52 Mon Sep 17 00:00:00 2001 From: fireph Date: Tue, 10 Oct 2017 18:17:34 -0700 Subject: [PATCH] update to include notify window that is adjustable --- TrafficSweetSpot/Info.plist | 4 +- TrafficSweetSpot/PreferencesWindow.swift | 74 ++++++-- TrafficSweetSpot/PreferencesWindow.xib | 187 +++++++++++++++++++- TrafficSweetSpot/StatusMenuController.swift | 22 ++- 4 files changed, 270 insertions(+), 17 deletions(-) diff --git a/TrafficSweetSpot/Info.plist b/TrafficSweetSpot/Info.plist index ebdf92b..ada7a7f 100644 --- a/TrafficSweetSpot/Info.plist +++ b/TrafficSweetSpot/Info.plist @@ -17,11 +17,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.4 + 1.5 CFBundleSignature ???? CFBundleVersion - 9 + 10 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) LSUIElement diff --git a/TrafficSweetSpot/PreferencesWindow.swift b/TrafficSweetSpot/PreferencesWindow.swift index 4eba7a3..fa9faf0 100644 --- a/TrafficSweetSpot/PreferencesWindow.swift +++ b/TrafficSweetSpot/PreferencesWindow.swift @@ -22,6 +22,8 @@ class PreferencesWindow: NSWindowController { @IBOutlet weak var notificationsTimeLabel: NSTextField! @IBOutlet weak var travelTimeMenuBarCheckBox: NSButton! @IBOutlet weak var checkForUpdatesCheckBox: NSButton! + @IBOutlet weak var startTimeDropdown: NSPopUpButton! + @IBOutlet weak var endTimeDropdown: NSPopUpButton! var delegate: PreferencesWindowDelegate? @@ -60,19 +62,41 @@ class PreferencesWindow: NSWindowController { if let checkForUpdatesVal = defaults.string(forKey: "checkForUpdates") { checkForUpdatesCheckBox.stringValue = checkForUpdatesVal } - } - @IBAction func onSliderUpdate(_ sender: Any) { - notificationsTimeSlider.integerValue = Int(notificationsTimeSlider.floatValue.rounded()) - let value = notificationsTimeSlider.integerValue - var hours = String(value / 60) - if (hours.count < 2) { - hours = "0" + hours + startTimeDropdown.removeAllItems() + endTimeDropdown.removeAllItems() + for index in 0...23 { + var hour = String(index) + if (index < 10) { + hour = "0" + hour + } + startTimeDropdown.addItem(withTitle: hour + ":00") + endTimeDropdown.addItem(withTitle: hour + ":00") } - var minutes = String(value % 60) - if (minutes.count < 2) { - minutes = "0" + minutes + if let startTimeNotifVal = defaults.string(forKey: "startTimeNotif") { + startTimeDropdown.selectItem(at: Int(startTimeNotifVal) ?? 17) + } else { + startTimeDropdown.selectItem(at: 17) } - notificationsTimeLabel.stringValue = hours + ":" + minutes + if let endTimeNotifVal = defaults.string(forKey: "endTimeNotif") { + endTimeDropdown.selectItem(at: Int(endTimeNotifVal) ?? 20) + } else { + endTimeDropdown.selectItem(at: 20) + } + updateStartTimeDropdownItems() + updateEndTimeDropdownItems() + updateSliderLabel() + } + + @IBAction func onSliderUpdate(_ sender: Any) { + updateSliderLabel() + } + + @IBAction func onStartTimeDropdownUpdate(_ sender: Any) { + updateEndTimeDropdownItems() + } + + @IBAction func onEndTimeDropdownUpdate(_ sender: Any) { + updateStartTimeDropdownItems() } @IBAction func saveClicked(_ sender: AnyObject) { @@ -85,8 +109,36 @@ class PreferencesWindow: NSWindowController { defaults.setValue(notificationsTimeSlider.stringValue, forKey: "notificationsTime") defaults.setValue(travelTimeMenuBarCheckBox.stringValue, forKey: "travelTimeMenuBar") defaults.setValue(checkForUpdatesCheckBox.stringValue, forKey: "checkForUpdates") + defaults.setValue(String(startTimeDropdown.indexOfSelectedItem), forKey: "startTimeNotif") + defaults.setValue(String(endTimeDropdown.indexOfSelectedItem), forKey: "endTimeNotif") defaults.synchronize() delegate?.preferencesDidUpdate() self.window?.close() } + + func updateSliderLabel() { + notificationsTimeSlider.integerValue = Int(notificationsTimeSlider.floatValue.rounded()) + let value = notificationsTimeSlider.integerValue + var hours = String(value / 60) + if (hours.count < 2) { + hours = "0" + hours + } + var minutes = String(value % 60) + if (minutes.count < 2) { + minutes = "0" + minutes + } + notificationsTimeLabel.stringValue = hours + ":" + minutes + } + + func updateStartTimeDropdownItems() { + for item in startTimeDropdown.itemArray { + item.isHidden = startTimeDropdown.index(of: item) >= endTimeDropdown.indexOfSelectedItem + } + } + + func updateEndTimeDropdownItems() { + for item in endTimeDropdown.itemArray { + item.isHidden = endTimeDropdown.index(of: item) <= startTimeDropdown.indexOfSelectedItem + } + } } diff --git a/TrafficSweetSpot/PreferencesWindow.xib b/TrafficSweetSpot/PreferencesWindow.xib index 852b011..42536ea 100644 --- a/TrafficSweetSpot/PreferencesWindow.xib +++ b/TrafficSweetSpot/PreferencesWindow.xib @@ -12,10 +12,12 @@ + + @@ -26,7 +28,7 @@ - + @@ -171,6 +173,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -178,4 +231,136 @@ + + + +YnBsaXN0MDDUAQIDBAUGVVZYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoK8QEgcI +ExQZHh8jJCsuMTtDR0tPUlUkbnVsbNUJCgsMDQ4PEBESVk5TU2l6ZVYkY2xhc3NcTlNJbWFnZUZsYWdz +Vk5TUmVwc1dOU0NvbG9ygAKAERIgwwAAgAOAC1Z7MSwgMX3SFQoWGFpOUy5vYmplY3RzoReABIAK0hUK +Gh2iGxyABYAGgAkQANIgCiEiXxAUTlNUSUZGUmVwcmVzZW50YXRpb26AB4AITxEIxE1NACoAAAAKAAAA +EAEAAAMAAAABAAEAAAEBAAMAAAABAAEAAAECAAMAAAACAAgACAEDAAMAAAABAAEAAAEGAAMAAAABAAEA +AAEKAAMAAAABAAEAAAERAAQAAAABAAAACAESAAMAAAABAAEAAAEVAAMAAAABAAIAAAEWAAMAAAABAAEA +AAEXAAQAAAABAAAAAgEcAAMAAAABAAEAAAEoAAMAAAABAAIAAAFSAAMAAAABAAEAAAFTAAMAAAACAAEA +AYdzAAcAAAf0AAAA0AAAAAAAAAf0YXBwbAIgAABtbnRyR1JBWVhZWiAH0AACAA4ADAAAAABhY3NwQVBQ +TAAAAABub25lA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N6mpqhyqj6sCq3Wr +6axcrNCtRK24ri2uoa8Wr4uwALB1sOqxYLHWskuywrM4s660JbSctRO1irYBtnm28Ldot+C4WbjRuUq5 +wro7urW7LrunvCG8m70VvY++Cr6Evv+/er/1wHDA7MFnwePCX8Lbw1jD1MRRxM7FS8XIxkbGw8dBx7/I +Pci8yTrJuco4yrfLNsu2zDXMtc01zbXONs62zzfPuNA50LrRPNG+0j/SwdNE08bUSdTL1U7V0dZV1tjX +XNfg2GTY6Nls2fHadtr724DcBdyK3RDdlt4c3qLfKd+v4DbgveFE4cziU+Lb42Pj6+Rz5PzlhOYN5pbn +H+ep6DLovOlG6dDqW+rl63Dr++yG7RHtnO4o7rTvQO/M8Fjw5fFy8f/yjPMZ86f0NPTC9VD13vZt9vv3 +ivgZ+Kj5OPnH+lf65/t3/Af8mP0p/br+S/7c/23//4AO0iUmSEldTlNNdXRhYmxlRGF0YaNISipWTlNE +YXRh0iUmTE1cTlNDb2xvclNwYWNlok4qXE5TQ29sb3JTcGFjZdIlJlBRV05TQ29sb3KiUCrSJSZTVFdO +U0ltYWdlolMqXxAPTlNLZXllZEFyY2hpdmVy0VdYVHJvb3SAAQAIABEAGgAjAC0AMgA3AEwAUgBdAGQA +awB4AH8AhwCJAIsAkACSAJQAmwCgAKsArQCvALEAtgC5ALsAvQC/AMEAxgDdAN8A4QmpCa4JuQnCCdUJ +2QnkCe0J8gn6Cf0KAgoRChUKIAooCjUKQgpXClwKYApiCmQKZgpvCnQKegqCCoQKhgqICooKjwqXHAMc +BRwKHBgcHBwjHCgcNRw4HEUcShxSHFUcWhxiHGUcdxx6HH8AAAAAAAACAQAAAAAAAABZAAAAAAAAAAAA +AAAAAAAcgQ + + + diff --git a/TrafficSweetSpot/StatusMenuController.swift b/TrafficSweetSpot/StatusMenuController.swift index f9b43da..43bd479 100644 --- a/TrafficSweetSpot/StatusMenuController.swift +++ b/TrafficSweetSpot/StatusMenuController.swift @@ -64,7 +64,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { var checkForUpdates : Bool = true var checkForUpdatesTimer : Timer? - var lastNotificationTimestamp: Double? + var lastNotificationDay: Int? var routesSet : LineChartDataSet! var routesData : LineChartData! @@ -150,12 +150,18 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { if (routesSet.entryCount > 0) { let notificationsEnabled = defaults?.string(forKey: "notificationsEnabled")?.toBool() ?? false let notificationsTime = Double((defaults?.string(forKey: "notificationsTime"))!) ?? -1.0 + let notificationsStartTime = Int((defaults?.string(forKey: "startTimeNotif"))!) ?? 17 + let notificationsEndTime = Int((defaults?.string(forKey: "endTimeNotif"))!) ?? 19 let lastEntryTime = routesSet.entryForIndex(routesSet.entryCount - 1)?.y ?? entry.y + let hour = getHourFromTimestamp(entry.x) + let day = getDayFromTimestamp(entry.x) + let withinNotifRange = hour >= notificationsStartTime && hour < notificationsEndTime if (notificationsEnabled - && entry.x - (lastNotificationTimestamp ?? 0.0) > TIME_BETWEEN_NOTIFICATIONS + && withinNotifRange + && lastNotificationDay != day && lastEntryTime > notificationsTime && entry.y <= notificationsTime) { - lastNotificationTimestamp = entry.x + lastNotificationDay = day showNotification(subtitle: "Traffic has died down 😀", text: "Travel time is now: " + String(Int(entry.y)) + " minutes") } } @@ -279,6 +285,16 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { return timeString } + func getHourFromTimestamp(_ timestamp: Double) -> Int { + let date = Date(timeIntervalSinceReferenceDate: timestamp) + return Calendar.current.component(.hour, from: date) + } + + func getDayFromTimestamp(_ timestamp: Double) -> Int { + let date = Date(timeIntervalSinceReferenceDate: timestamp) + return Calendar.current.ordinality(of: .day, in: .year, for: date)! + } + func showNotification(subtitle: String, text: String) { let notification = NSUserNotification() notification.title = "TrafficSweetSpot"