From cb6ff544ab77a18e15084afec6bd086cbd8b63bb Mon Sep 17 00:00:00 2001 From: fireph Date: Wed, 20 Jul 2016 14:46:38 -0700 Subject: [PATCH] final code to make it only keep between 3-4 hours of data also only shows integer values on y axis now --- TrafficSweetSpot/Base.lproj/MainMenu.xib | 4 +++ TrafficSweetSpot/StatusMenuController.swift | 29 ++++++++++++++++++--- TrafficSweetSpot/TravelTimeChartView.swift | 4 +++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/TrafficSweetSpot/Base.lproj/MainMenu.xib b/TrafficSweetSpot/Base.lproj/MainMenu.xib index 5030dac..fde5035 100644 --- a/TrafficSweetSpot/Base.lproj/MainMenu.xib +++ b/TrafficSweetSpot/Base.lproj/MainMenu.xib @@ -16,6 +16,7 @@ + @@ -37,6 +38,9 @@ + + + diff --git a/TrafficSweetSpot/StatusMenuController.swift b/TrafficSweetSpot/StatusMenuController.swift index 323e850..b0fd994 100644 --- a/TrafficSweetSpot/StatusMenuController.swift +++ b/TrafficSweetSpot/StatusMenuController.swift @@ -12,12 +12,14 @@ import Charts class StatusMenuController: NSObject, PreferencesWindowDelegate { @IBOutlet weak var statusMenu: NSMenu! @IBOutlet weak var travelTime: NSMenuItem! + @IBOutlet weak var lastTimestamp: NSMenuItem! @IBOutlet weak var travelTimeChartMenuItem: NSMenuItem! @IBOutlet weak var travelTimeChart: TravelTimeChartView! var aboutWindow: AboutWindow! var preferencesWindow: PreferencesWindow! let INTERVAL_TIME_IN_SECONDS = 300.0 + let THROW_AWAY_DATA_AFTER_MINUTES = 120.0 let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength) let mapsAPI = MapsDistanceMatrixAPI() @@ -38,13 +40,14 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { travelTimeChartMenuItem.view = travelTimeChart initChartData() updateTravelTime() - NSTimer.scheduledTimerWithTimeInterval( + let alarm = NSTimer.scheduledTimerWithTimeInterval( INTERVAL_TIME_IN_SECONDS, target: self, selector: #selector(StatusMenuController.updateTravelTime), userInfo: nil, repeats: true ) + NSRunLoop.mainRunLoop().addTimer(alarm, forMode: NSRunLoopCommonModes) } func updateTravelTime() { @@ -56,7 +59,13 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { return; } mapsAPI.fetchTravelTime(apiKey!, origin: origin!, dest: dest!) { route in - self.travelTime.title = "Current travel time: "+route.durationString + dispatch_async(dispatch_get_main_queue()) { + self.travelTime.title = "Current travel time: \(route.durationString)" + self.lastTimestamp.title = "Last fetched time: \(route.timestampString)" + } + self.statusMenu.itemChanged(self.travelTime) + self.statusMenu.itemChanged(self.lastTimestamp) + self.removeOldRoutesIfNeeded(route.timestamp) self.fillMissingRoutesIfNeeded(route.timestamp) self.appendRouteToSet(route) } @@ -71,9 +80,23 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { routesData.addXValue(route.timestampString) routesData.notifyDataChanged() travelTimeChart.update() + statusMenu.itemChanged(travelTimeChartMenuItem) } } + func removeOldRoutesIfNeeded(currentTimestamp: Double) { + var firstTimestamp = routes.first?.timestamp + if (firstTimestamp < (currentTimestamp - THROW_AWAY_DATA_AFTER_MINUTES*60 - 60*60)) { + while routes.count > 0 && firstTimestamp < (currentTimestamp - THROW_AWAY_DATA_AFTER_MINUTES*60) { + routes.removeFirst() + firstTimestamp = routes.first?.timestamp + } + initChartData() + travelTimeChart.update() + statusMenu.itemChanged(travelTimeChartMenuItem) + } + } + func fillMissingRoutesIfNeeded(currentTimestamp: Double) { if (self.routes.count == 0) { return; @@ -96,6 +119,7 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { } func initChartData() { + routeTimestamps = [] var yVals1 : [ChartDataEntry] = [ChartDataEntry]() for i in 0 ..< routes.count { yVals1.append(ChartDataEntry(value: Double(routes[i].duration)/60.0, xIndex: i)) @@ -118,7 +142,6 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate { func preferencesDidUpdate() { routes = [] - routeTimestamps = [] initChartData() updateTravelTime() } diff --git a/TrafficSweetSpot/TravelTimeChartView.swift b/TrafficSweetSpot/TravelTimeChartView.swift index 579c28b..d801454 100644 --- a/TrafficSweetSpot/TravelTimeChartView.swift +++ b/TrafficSweetSpot/TravelTimeChartView.swift @@ -24,7 +24,11 @@ class TravelTimeChartView: NSView { dispatch_async(dispatch_get_main_queue()) { if (self.lineChartView != nil) { self.lineChartView.leftAxis.axisMinValue = 0 + let numberFormatter = NSNumberFormatter() + numberFormatter.generatesDecimalNumbers = false + self.lineChartView.leftAxis.valueFormatter = numberFormatter self.lineChartView.rightAxis.axisMinValue = 0 + self.lineChartView.rightAxis.valueFormatter = numberFormatter self.lineChartView.descriptionText = "" self.lineChartView.noDataText = "No data yet" self.lineChartView.data = data