Skip to content

Commit

Permalink
Merge pull request #6 from robbawebba/observe-location-settings
Browse files Browse the repository at this point in the history
Observe package location settings for changes
  • Loading branch information
robbawebba authored May 3, 2017
2 parents d5b6b67 + c300be5 commit 250322d
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions lib/night-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function setTheme(newTheme) {
atom.config.set('core.themes', newTheme);
}

/* CONFIG CALLBACK FUNCTIONS
This callback will help keep the package and core theme settings consistent
/** CONFIG CALLBACK FUNCTIONS
* These callbacks will help keep the package and core theme settings consistent
*/
// callback for observing night-light.day
function packageDayThemesChange(newTheme) {
Expand All @@ -42,7 +42,7 @@ function packageDayThemesChange(newTheme) {
setTheme(theme);
} else if (night && manualDay) {
setTheme(theme);
}
}
}
// callback for observing night-light.night
function packageNightThemesChange(newTheme) {
Expand All @@ -54,6 +54,14 @@ function packageNightThemesChange(newTheme) {
setTheme(theme);
}
}
// callback for observing night-light.location
function locationChange({newValue: {auto,lat,lng}}) {
if (!auto) {
main.updateSunriseSunsetTimes({lat, lng}).then(main.checkThemes);
} else if (auto) {
main.updateLocation().then(main.updateSunriseSunsetTimes).then(main.checkThemes);
}
}

// global package variables
var sunrise = null;
Expand All @@ -66,7 +74,7 @@ var manualDay = false;
var lastRefresh = 0;

// main module
export default {
export default main = {

config: addAvailableThemes(require('./config.json')),
subscriptions: null,
Expand All @@ -79,10 +87,12 @@ export default {
'night-light:toggle': () => this.toggle()
}),
atom.config.observe('night-light.day', packageDayThemesChange),
atom.config.observe('night-light.night', packageNightThemesChange)
atom.config.observe('night-light.night', packageNightThemesChange),
atom.config.onDidChange('night-light.location', locationChange)
);

this.tick = this.tick.bind(this);

setTimeout(this.tick, 500); // for initialization
setInterval(this.tick, 60000);
},
Expand Down Expand Up @@ -142,29 +152,31 @@ export default {
},
// Get sunrise and sunset times based on the given lat/lng
updateSunriseSunsetTimes(location) {
if(atom.config.get('night-light.schedule.auto')) {
noonToday = new Date().setHours("12");
var SunCalc = require('suncalc');
var solar = SunCalc.getTimes(noonToday,location.lat, location.lng);
sunrise = solar.sunrise;
sunset = solar.sunset;
atom.config.set('night-light.schedule.end', String(solar.sunrise.getHours())+":"+String(solar.sunrise.getMinutes()));
atom.config.set('night-light.schedule.start', String(solar.sunset.getHours())+":"+String(solar.sunset.getMinutes()));
} else {
var sunriseValues = atom.config.get('night-light.schedule.end').split(":");
sunrise = new Date();
sunrise.setHours(sunriseValues[0]);
sunrise.setMinutes(sunriseValues[1]);

var sunsetValues = atom.config.get('night-light.schedule.start').split(":");
sunset = new Date();
sunset.setHours(sunsetValues[0]);
sunset.setMinutes(sunsetValues[1]);
}
return {sunrise: solar.sunrise, sunset: solar.sunset};
return new Promise(function(resolve, reject) {
if(atom.config.get('night-light.schedule.auto')) {
noonToday = new Date().setHours("12");
var SunCalc = require('suncalc');
var solar = SunCalc.getTimes(noonToday,location.lat, location.lng);
sunrise = solar.sunrise;
sunset = solar.sunset;
atom.config.set('night-light.schedule.end', String(solar.sunrise.getHours())+":"+String(solar.sunrise.getMinutes()));
atom.config.set('night-light.schedule.start', String(solar.sunset.getHours())+":"+String(solar.sunset.getMinutes()));
} else {
var sunriseValues = atom.config.get('night-light.schedule.end').split(":");
sunrise = new Date();
sunrise.setHours(sunriseValues[0]);
sunrise.setMinutes(sunriseValues[1]);

var sunsetValues = atom.config.get('night-light.schedule.start').split(":");
sunset = new Date();
sunset.setHours(sunsetValues[0]);
sunset.setMinutes(sunsetValues[1]);
}
resolve({sunrise: solar.sunrise, sunset: solar.sunset});
});
},
checkThemes(times){
now = new Date();
// Check if we've passed either sunset or sunrise
if (now >= times.sunset || now < times.sunrise) {
night = true;
Expand Down

0 comments on commit 250322d

Please sign in to comment.