Skip to content

Commit

Permalink
Merge pull request #8 from robbawebba/serialize
Browse files Browse the repository at this point in the history
Add Serialization of Package State
  • Loading branch information
robbawebba authored Jun 13, 2017
2 parents 250322d + 4a38bfb commit 00e0ef1
Showing 1 changed file with 76 additions and 49 deletions.
125 changes: 76 additions & 49 deletions lib/night-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function addAvailableThemes(config) {
}
// Helper function for changing editor themes: uses format ["ui", "syntax"]
function setTheme(newTheme) {
console.log("setTheme");
atom.config.set('core.themes', newTheme);
}

Expand All @@ -36,24 +37,27 @@ function setTheme(newTheme) {
*/
// callback for observing night-light.day
function packageDayThemesChange(newTheme) {
console.log("packageDayThemesChange");
var theme = [newTheme.ui, newTheme.syntax]
if (!night) {
if (manualNight) return;
if (!packageState.night) {
if (packageState.manualNight) return;
setTheme(theme);
} else if (night && manualDay) {
} else if (packageState.night && packageState.manualDay) {
setTheme(theme);
}
}
// callback for observing night-light.night
function packageNightThemesChange(newTheme) {
console.log("packageNightThemesChange");
var theme = [newTheme.ui, newTheme.syntax]
if (night) {
if (manualDay) return;
if (packageState.night) {
if (packageState.manualDay) return;
setTheme(theme);
} else if (!night && manualNight) {
} else if (!packageState.night && packageState.manualNight) {
setTheme(theme);
}
}

// callback for observing night-light.location
function locationChange({newValue: {auto,lat,lng}}) {
if (!auto) {
Expand All @@ -62,24 +66,34 @@ function locationChange({newValue: {auto,lat,lng}}) {
main.updateLocation().then(main.updateSunriseSunsetTimes).then(main.checkThemes);
}
}

// global package variables
var sunrise = null;
var sunset = null;
var lat = null;
var lng = null;
var night = false;
var manualNight = false;
var manualDay = false;
var lastRefresh = 0;
// Package State
let packageState = {
sunrise: null,
sunset: null,
lat: null,
lng: null,
night: false,
manualNight: false,
manualDay: false,
lastRefresh: 0
}

// main module
export default main = {

export default {
config: addAvailableThemes(require('./config.json')),
subscriptions: null,

activate(state) {
console.log("State: ");
console.log(state);
if (state) {
for (var key in state) {
console.log(key)
packageState[key] = state[key];
}

}
console.log(packageState)
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
this.subscriptions = new CompositeDisposable();
// Register the toggle command
Expand All @@ -102,16 +116,28 @@ export default main = {
},

serialize() {
return;
console.log("serializing...");
var serialized = {
sunrise,
sunset,
lat,
lng,
night,
manualNight,
manualDay,
lastRefresh
} = packageState
console.log(serialized)
return serialized;
},
// manually switch between appearances for different times of day
toggle() {
if (night) {
manualDay = !manualDay;
manualDay ? setTheme(getDayThemes()) : setTheme(getNightThemes());
} else if (!night) {
manualNight = !manualNight;
manualNight ? setTheme(getNightThemes()) : setTheme(getDayThemes());
if (packageState.night) {
packageState.manualDay = !packageState.manualDay;
packageState.manualDay ? setTheme(getDayThemes()) : setTheme(getNightThemes());
} else if (!packageState.night) {
packageState.manualNight = !packageState.manualNight;
packageState.manualNight ? setTheme(getNightThemes()) : setTheme(getDayThemes());
}
},
// Check lat/lng or get the pre-configured coordinates
Expand All @@ -126,8 +152,8 @@ export default main = {
json = JSON.parse(body);
atom.config.set('night-light.location.lat', json.location.lat);
atom.config.set('night-light.location.lng', json.location.lng);
lat = json.location.lat;
lng = json.location.lng;
packageState.lat = json.location.lat;
packageState.lng = json.location.lng;
resolve(json.location);
} else if (error) {
atom.notifications.addWarning("night-light: There was an error with automatically updating your location",
Expand All @@ -137,16 +163,16 @@ export default main = {
'dismissable': true
});
// No worries, just use default sunrise/sunset
lat = atom.config.get('night-light.location.lat');
lng = atom.config.get('night-light.location.lng');
resolve({lat: lat, lng:lng});
packageState.lat = atom.config.get('night-light.location.lat');
packageState.lng = atom.config.get('night-light.location.lng');
resolve({lat: packageState.lat, lng:packageState.lng});
}
});
} else {
// No worries, just use default sunrise/sunset
lat = atom.config.get('night-light.location.lat');
packageState.lat = atom.config.get('night-light.location.lat');
lng = atom.config.get('night-light.location.lng');
resolve({lat: lat, lng:lng});
resolve({lat: packageState.lat, lng:lng});
}
});
},
Expand All @@ -157,20 +183,20 @@ export default main = {
noonToday = new Date().setHours("12");
var SunCalc = require('suncalc');
var solar = SunCalc.getTimes(noonToday,location.lat, location.lng);
sunrise = solar.sunrise;
sunset = solar.sunset;
packageState.sunrise = solar.sunrise;
packageState.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]);
packageState.sunrise = new Date();
packageState.sunrise.setHours(sunriseValues[0]);
packageState.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]);
packageState.sunset = new Date();
packageState.sunset.setHours(sunsetValues[0]);
packageState.sunset.setMinutes(sunsetValues[1]);
}
resolve({sunrise: solar.sunrise, sunset: solar.sunset});
});
Expand All @@ -179,26 +205,27 @@ export default main = {
now = new Date();
// Check if we've passed either sunset or sunrise
if (now >= times.sunset || now < times.sunrise) {
night = true;
manualNight = false;
if (manualDay) return;
packageState.night = true;
packageState.manualNight = false;
if (packageState.manualDay) return;
setTheme(getNightThemes());
} else if (now >= times.sunrise) {
night = false;
manualDay = false;
if (manualNight) return;
packageState.night = false;
packageState.manualDay = false;
console.log(packageState.manualNight)
if (packageState.manualNight) return;
setTheme(getDayThemes());
}
},

tick() {
now = new Date();
// Check if the day has changed since sun times last retrieved
if ((now.getDate() != lastRefresh)) {
if ((now.getDate() != packageState.lastRefresh)) {
this.updateLocation().then(this.updateSunriseSunsetTimes).then(this.checkThemes);
lastRefresh = now.getDate();
packageState.lastRefresh = now.getDate();
} else {
this.checkThemes({sunrise, sunset});
this.checkThemes({sunrise, sunset} = packageState);
}
}
};

0 comments on commit 00e0ef1

Please sign in to comment.