Created
May 2, 2015 20:15
-
-
Save gklka/7a7ff095bef207a96b04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Welcome to Pebble.js! | |
* | |
* This is where you write your app. | |
*/ | |
var UI = require('ui'); | |
var ajax = require('ajax'); | |
var Vector2 = require('vector2'); | |
// Show splash screen while waiting for data | |
var splashWindow = new UI.Window(); | |
// Text element to inform user | |
var text = new UI.Text({ | |
position: new Vector2(0, 0), | |
size: new Vector2(144, 168), | |
text: 'Downloading weather data...', | |
font: 'GOTHIC_28_BOLD', | |
color: 'black', | |
textOverflow: 'wrap', | |
textAlign: 'center', | |
backgroundColor: 'white' | |
}); | |
// Add to splashWindow and show | |
splashWindow.add(text); | |
splashWindow.show(); | |
var parseFeed = function(data, quantity) { | |
var items = []; | |
for(var i = 0; i < quantity; i++) { | |
// Always upper case the description string | |
var title = data.list[i].weather[0].main; | |
title = title.charAt(0).toUpperCase() + title.substring(1); | |
// Get date/time substring | |
var time = data.list[i].dt_txt; | |
time = time.substring(time.indexOf('/') + 1, time.indexOf(':') + 3); | |
// Add to menu items array | |
items.push({ | |
title: title, | |
subtitle: time | |
}); | |
} | |
// Finally return whole array | |
return items; | |
}; | |
// Make request to openweathermap.org | |
ajax( | |
{ | |
url:'http://api.openweathermap.org/data/2.5/forecast?q=Budapest', | |
type:'json' | |
}, | |
function(data) { | |
// Create an array of Menu items | |
var menuItems = parseFeed(data, 10); | |
// Construct Menu to show to user | |
var resultsMenu = new UI.Menu({ | |
sections: [{ | |
title: 'Current Forecast', | |
items: menuItems | |
}] | |
}); | |
// Add an action for SELECT | |
resultsMenu.on('select', function(e) { | |
// Get that forecast | |
var forecast = data.list[e.itemIndex]; | |
// Assemble body string | |
var content = data.list[e.itemIndex].weather[0].description; | |
// Capitalize first letter | |
content = content.charAt(0).toUpperCase() + content.substring(1); | |
// Add temperature, pressure etc | |
content += '\nTemperature: ' + Math.round(forecast.main.temp - 273.15) + '°C' + | |
'\nPressure: ' + Math.round(forecast.main.pressure) + ' mbar' + | |
'\nWind: ' + Math.round(forecast.wind.speed) + ' mph, ' + | |
Math.round(forecast.wind.deg) + '°'; | |
// Create the Card for detailed view | |
var detailCard = new UI.Card({ | |
title:'Details', | |
subtitle:e.item.subtitle, | |
body: content | |
}); | |
detailCard.show(); | |
}); | |
// Show the Menu, hide the splash | |
resultsMenu.show(); | |
splashWindow.hide(); | |
}, | |
function(error) { | |
console.log('Download failed: ' + error); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment