Skip to content

Commit

Permalink
final code to make it only keep between 3-4 hours of data
Browse files Browse the repository at this point in the history
also only shows integer values on y axis now
  • Loading branch information
fireph committed Jul 20, 2016
1 parent f6d4101 commit cb6ff54
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions TrafficSweetSpot/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="kxq-9y-9cq" customClass="StatusMenuController" customModule="TrafficSweetSpot" customModuleProvider="target">
<connections>
<outlet property="lastTimestamp" destination="Aeg-Zy-VRP" id="qd6-BB-wKP"/>
<outlet property="statusMenu" destination="rJC-ad-A5A" id="AXq-mm-evu"/>
<outlet property="travelTime" destination="mbl-Qn-zNV" id="eNV-w2-Bgd"/>
<outlet property="travelTimeChart" destination="hAy-H5-Flh" id="c6c-E3-9Kz"/>
Expand All @@ -37,6 +38,9 @@
<menuItem title="Travel Time" id="mbl-Qn-zNV">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="Last Timestamp" id="Aeg-Zy-VRP">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="jeY-O3-9tf"/>
<menuItem title="Preferences" id="6Yp-QI-Xa2" userLabel="Preferences">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
29 changes: 26 additions & 3 deletions TrafficSweetSpot/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() {
Expand All @@ -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)
}
Expand All @@ -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;
Expand All @@ -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))
Expand All @@ -118,7 +142,6 @@ class StatusMenuController: NSObject, PreferencesWindowDelegate {

func preferencesDidUpdate() {
routes = []
routeTimestamps = []
initChartData()
updateTravelTime()
}
Expand Down
4 changes: 4 additions & 0 deletions TrafficSweetSpot/TravelTimeChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb6ff54

Please # to comment.