@ -284,8 +284,12 @@ const WeatherMenuButton = new Lang.Class({
this . _settings = Convenience . getSettings ( WEATHER _SETTINGS _SCHEMA ) ;
this . _settingsC = this . _settings . connect ( "changed" , function ( ) {
that . rebuildFutureWeatherUi ( ) ;
that . refreshWeatherCurrent ( false ) ;
that . refreshWeatherForecast ( false ) ;
if ( that . locationChanged ( ) ) {
that . currentWeatherCache = 'undefined' ;
that . forecastWeatherCache = 'undefined' ;
}
that . parseWeatherCurrent ( ) ;
that . parseWeatherForecast ( ) ;
} ) ;
} ,
@ -298,11 +302,23 @@ const WeatherMenuButton = new Lang.Class({
schema : schemaInterface
} ) ;
this . _settingsInterfaceC = this . _settingsInterface . connect ( "changed" , function ( ) {
that . refreshWeatherCurrent ( false ) ;
that . refreshWeatherForecast ( false ) ;
if ( that . locationChanged ( ) ) {
that . currentWeatherCache = 'undefined' ;
that . forecastWeatherCache = 'undefined' ;
}
that . parseWeatherCurrent ( ) ;
that . parseWeatherForecast ( ) ;
} ) ;
} ,
locationChanged : function ( ) {
let location = this . extractCity ( this . _city ) ;
if ( this . oldLocation != location ) {
return true ;
}
return false ;
} ,
get _clockFormat ( ) {
if ( ! this . _settingsInterface )
this . loadConfigInterface ( ) ;
@ -1088,27 +1104,11 @@ weather-storm.png = weather-storm-symbolic.svg
return 0 ;
} ,
refresh WeatherCurrent: function ( recurse ) {
if ( ! this . extractId ( this . _city ) ) {
this . updateCities ( ) ;
return 0 ;
parse WeatherCurrent: function ( ) {
if ( this . currentWeatherCache == 'undefined' ) {
this . refreshWeatherCurrent ( ) ;
return ;
}
let params = {
q : this . extractCity ( this . _city ) ,
units : 'metric'
} ;
if ( this . _appid )
params [ 'APPID' ] = this . _appid ;
this . load _json _async ( WEATHER _URL _CURRENT , params , function ( json ) {
if ( ! json )
return 0 ;
if ( Number ( json . cod ) != 200 )
return 0 ;
this . rebuildSelectCityItem ( ) ;
if ( this . _old _position _in _panel != this . _position _in _panel ) {
switch ( this . _old _position _in _panel ) {
@ -1141,9 +1141,10 @@ weather-storm.png = weather-storm-symbolic.svg
this . _old _position _in _panel = this . _position _in _panel ;
}
let json = this . currentWeatherCache ;
// Refresh current weather
let location = this . extractLocation ( this . _city ) ;
// Refresh current weather
let comment = json . weather [ 0 ] . description ;
if ( this . _translate _condition )
comment = this . get _weather _condition ( json . weather [ 0 ] . id ) ;
@ -1162,18 +1163,16 @@ weather-storm.png = weather-storm-symbolic.svg
let sunrise = json . sys . sunrise ;
let sunset = json . sys . sunset ;
if ( typeof this . lastBuildId == "undefined" )
if ( typeof this . lastBuildId == 'undefined' )
this . lastBuildId = 0 ;
if ( typeof this . lastBuildDate == "undefined" )
if ( typeof this . lastBuildDate == 'undefined' )
this . lastBuildDate = 0 ;
if ( this . lastBuildId != json . dt || ! this . lastBuildDate ) {
this . lastBuildId = json . dt ;
this . lastBuildDate = new Date ( ) ;
this . lastBuildDate = new Date ( this . lastBuildId * 1000 ) ;
}
let actualDate = new Date ( ) ;
let d = Math . floor ( ( actualDate . getTime ( ) - this . lastBuildDate . getTime ( ) ) / 86400000 ) ;
switch ( this . _pressure _units ) {
case WeatherPressureUnits . inHg :
@ -1268,10 +1267,12 @@ weather-storm.png = weather-storm-symbolic.svg
lastBuild = this . lastBuildDate . toLocaleFormat ( "%I:%M %p" ) ;
}
if ( d >= 1 ) {
let beginOfDay = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
let d = Math . floor ( ( beginOfDay . getTime ( ) - this . lastBuildDate . getTime ( ) ) / 86400000 ) ;
if ( d < 0 ) {
lastBuild = _ ( "Yesterday" ) ;
if ( d > 1 )
lastBuild = _ ( "%s days ago" ) . replace ( "%s" , d ) ;
if ( d < - 1 )
lastBuild = _ ( "%s days ago" ) . replace ( "%s" , - 1 * d ) ;
}
this . _currentWeatherIcon . icon _name = this . _weatherIcon . icon _name = iconname ;
@ -1334,45 +1335,62 @@ weather-storm.png = weather-storm-symbolic.svg
else // i.e. wind > 0 && wind_direction
this . _currentWeatherWind . text = wind _direction + ' ' + parseFloat ( wind ) . toLocaleString ( ) + ' ' + wind _unit ;
return 0 ;
} ) ;
// Repeatedly refresh weather if recurse is set
if ( recurse ) {
this . _timeoutCurrent = Mainloop . timeout _add _seconds ( this . _refresh _interval _current , Lang . bind ( this , function ( ) {
this . refreshWeatherCurrent ( true ) ;
} ) ) ;
}
return 0 ;
} ,
refreshWeatherForecas t : function ( recurse ) {
refreshWeatherCurrent : function ( recurse ) {
if ( ! this . extractId ( this . _city ) ) {
this . updateCities ( ) ;
return 0 ;
return ;
}
this . oldLocation = this . extractCity ( this . _city ) ;
let params = {
q : this . extractCity ( this . _city ) ,
units : 'metric' ,
cnt : '10'
q : this . oldLocation ,
units : 'metric'
} ;
if ( this . _appid )
if ( this . _appid )
params [ 'APPID' ] = this . _appid ;
this . load _json _async ( WEATHER _URL _FORECAS T , params , function ( json ) {
this . load _json _async ( WEATHER _URL _CURREN T , params , function ( json ) {
if ( ! json )
return 0 ;
return ;
if ( Number ( json . cod ) != 200 )
return 0 ;
return ;
if ( this . currentWeatherCache != json )
this . currentWeatherCache = json ;
this . rebuildSelectCityItem ( ) ;
this . parseWeatherCurrent ( ) ;
this . parseWeatherForecast ( ) ;
} ) ;
// Repeatedly refresh weather if recurse is set
if ( recurse ) {
this . _timeoutCurrent = Mainloop . timeout _add _seconds ( this . _refresh _interval _current , Lang . bind ( this , function ( ) {
this . refreshWeatherCurrent ( true ) ;
} ) ) ;
}
} ,
parseWeatherForecast : function ( ) {
if ( this . forecastWeatherCache == 'undefined' ) {
this . refreshWeatherForecast ( ) ;
return ;
}
let forecast = json . list ;
let forecast = this . forecastWeatherCache ;
let beginOfDay = new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) ;
// Refresh forecast
for ( let i = 0 ; i < this . _days _forecast ; i ++ ) {
let forecastUi = this . _forecast [ i ] ;
let forecastData = forecast [ i ] ;
if ( forecastData == 'undefined' )
continue ;
let t _low = forecastData . temp . min ;
let t _high = forecastData . temp . max ;
@ -1424,25 +1442,52 @@ weather-storm.png = weather-storm-symbolic.svg
comment = this . get _weather _condition ( forecastData . weather [ 0 ] . id ) ;
let forecastDate = new Date ( forecastData . dt * 1000 ) ;
let actualDate = new Date ( ) ;
let dayLeft = Math . floor ( ( actualDate . getTime ( ) - forecastDate . getTime ( ) ) / 1000 / 60 / 60 / 24 ) ;
let dayLeft = Math . floor ( ( forecastDate . getTime ( ) - beginOfDay . getTime ( ) ) / 86400000 ) ;
let date _string = _ ( "Today" ) ;
if ( dayLeft == - 1 )
if ( dayLeft == 1 )
date _string = _ ( "Tomorrow" ) ;
else if ( dayLeft < - 1 )
date _string = _ ( "In %s days" ) . replace ( "%s" , - 1 * dayLeft ) ;
else if ( dayLeft == 1 )
date _string = _ ( "Yesterday" ) ;
else if ( dayLeft > 1 )
date _string = _ ( "%s days ago" ) . replace ( "%s" , dayLeft ) ;
date _string = _ ( "In %s days" ) . replace ( "%s" , dayLeft ) ;
else if ( dayLeft == - 1 )
date _string = _ ( "Yesterday" ) ;
else if ( dayLeft < - 1 )
date _string = _ ( "%s days ago" ) . replace ( "%s" , - 1 * dayLeft ) ;
forecastUi . Day . text = date _string + ' (' + this . get _locale _day ( forecastDate . getDay ( ) ) + ')' ;
forecastUi . Temperature . text = '\u2193 ' + parseFloat ( t _low ) . toLocaleString ( ) + ' ' + this . unit _to _unicode ( ) + ' \u2191 ' + parseFloat ( t _high ) . toLocaleString ( ) + ' ' + this . unit _to _unicode ( ) ;
forecastUi . Summary . text = comment ;
forecastUi . Icon . icon _name = this . get _weather _icon _safely ( forecastData . weather [ 0 ] . id ) ;
}
} ,
refreshWeatherForecast : function ( recurse ) {
if ( ! this . extractId ( this . _city ) ) {
this . updateCities ( ) ;
return 0 ;
}
this . oldLocation = this . extractCity ( this . _city ) ;
let params = {
q : this . oldLocation ,
units : 'metric' ,
cnt : '13'
} ;
if ( this . _appid )
params [ 'APPID' ] = this . _appid ;
this . load _json _async ( WEATHER _URL _FORECAST , params , function ( json ) {
if ( ! json )
return ;
if ( Number ( json . cod ) != 200 )
return ;
if ( this . forecastWeatherCache != json . list )
this . forecastWeatherCache = json . list ;
this . parseWeatherForecast ( ) ;
} ) ;
// Repeatedly refresh weather if recurse is set