Skip to content

Commit 4d2458d

Browse files
πŸŒ™ Add location based auto theme
Use lat/long | browser location data to automatically set light/dark mode. Sunrise/sunset is already returned in the openweathermap API, so this is fairly simple. Converted the functions in weather.js to async as opposed to using .then chains
1 parent 7efc591 commit 4d2458d

File tree

4 files changed

+66
-49
lines changed

4 files changed

+66
-49
lines changed

β€Žassets/js/theme.jsβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,18 @@ if (CONFIG.changeThemeByHour && CONFIG.autoChangeTheme && !CONFIG.changeThemeByO
6161
disableDark();
6262
}
6363
}
64+
65+
// there may be a better way to do this &&
66+
if (CONFIG.changeThemeByLocation && CONFIG.autoChangeTheme && !CONFIG.changeThemeByOS && !CONFIG.changeThemeByHour) {
67+
Promise.resolve(weatherPromise).then(weather => {
68+
const unix = Date.now() / 1000;
69+
if (
70+
unix >= weather.sunrise &&
71+
unix < weather.sunset
72+
) {
73+
disableDark();
74+
} else {
75+
enableDark();
76+
}
77+
});
78+
}

β€Žassets/js/weather.jsβ€Ž

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,55 @@ const iconElement = document.querySelector('.weatherIcon');
77
const tempElement = document.querySelector('.weatherValue p');
88
const descElement = document.querySelector('.weatherDescription p');
99

10-
const weather = {};
11-
weather.temperature = {
12-
unit: 'celsius',
13-
};
14-
1510
var tempUnit = CONFIG.weatherUnit;
1611

1712
const KELVIN = 273.15;
1813
const key = `${CONFIG.weatherKey}`;
19-
setPosition();
20-
21-
function setPosition(position) {
22-
if (!CONFIG.trackLocation || !navigator.geolocation) {
23-
if (CONFIG.trackLocation) {
24-
console.error('Geolocation not available');
25-
}
26-
getWeather(CONFIG.defaultLatitude, CONFIG.defaultLongitude);
27-
return;
28-
}
29-
navigator.geolocation.getCurrentPosition(
30-
pos => {
31-
getWeather(pos.coords.latitude.toFixed(3), pos.coords.longitude.toFixed(3));
32-
},
33-
err => {
34-
console.error(err);
35-
getWeather(CONFIG.defaultLatitude, CONFIG.defaultLongitude);
36-
}
37-
);
14+
const weatherPromise = getWeather();
15+
displayWeather();
16+
17+
async function setPosition() {
18+
let pos;
19+
if (!CONFIG.trackLocation || !navigator.geolocation) {
20+
if (CONFIG.trackLocation) {
21+
console.error('Geolocation not available');
22+
}
23+
pos = ({ lat: CONFIG.defaultLatitude, lon: CONFIG.defaultLongitude });
24+
}
25+
26+
pos = new Promise((resolve) => {
27+
navigator.geolocation.getCurrentPosition((pos) => {
28+
resolve({ lat: pos.coords.latitude.toFixed(3), lon: pos.coords.longitude.toFixed(3) });
29+
},
30+
() => {
31+
resolve({ lat: CONFIG.defaultLatitude, lon: CONFIG.defaultLongitude });
32+
});
33+
});
34+
return pos;
3835
}
3936

40-
function getWeather(latitude, longitude) {
41-
let api = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&lang=${CONFIG.language}&appid=${key}`;
42-
fetch(api)
43-
.then(function(response) {
44-
let data = response.json();
45-
return data;
46-
})
47-
.then(function(data) {
48-
let celsius = Math.floor(data.main.temp - KELVIN);
49-
weather.temperature.value = tempUnit == 'C' ? celsius : (celsius * 9) / 5 + 32;
50-
weather.description = data.weather[0].description;
51-
weather.iconId = data.weather[0].icon;
52-
})
53-
.then(function() {
54-
displayWeather();
55-
});
37+
async function getWeather() {
38+
const position = await setPosition();
39+
let api = `https://api.openweathermap.org/data/2.5/weather?lat=${position.lat}&lon=${position.lon}&lang=${CONFIG.language}&appid=${key}`;
40+
41+
let response = await fetch(api).catch(err => {
42+
console.log(err);
43+
});
44+
let data = await response.json();
45+
46+
let celsius = Math.floor(data.main.temp - KELVIN);
47+
return {
48+
description: data.weather[0].description,
49+
iconId: data.weather[0].icon,
50+
sunrise: data.sys.sunrise,
51+
sunset: data.sys.sunset,
52+
temperature: tempUnit == 'C' ? celsius : (celsius * 9) / 5 + 32
53+
};
5654
}
5755

58-
function displayWeather() {
59-
iconElement.innerHTML = `<img src="assets/icons/${CONFIG.weatherIcons}/${weather.iconId}.png"/>`;
60-
tempElement.innerHTML = `${weather.temperature.value.toFixed(0)}Β°<span class="darkfg">${tempUnit}</span>`;
61-
descElement.innerHTML = weather.description;
56+
async function displayWeather() {
57+
var weather = await weatherPromise;
58+
iconElement.innerHTML = `<img src="assets/icons/${CONFIG.weatherIcons}/${weather.iconId}.png"/>`;
59+
tempElement.innerHTML = `${weather.temperature.toFixed(0)}Β°<span class="darkfg">${tempUnit}</span>`;
60+
descElement.innerHTML = weather.description;
6261
}

β€Žconfig.jsβ€Ž

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ const CONFIG = {
4747
hourDarkThemeActive: '18:30',
4848
hourDarkThemeInactive: '07:00',
4949

50-
// β”Œβ” ┬ β”¬β”Œβ”¬β”β”Œβ”¬β”β”Œβ”€β”β”Œβ”β”Œβ”Œβ”€β”
51-
// β”œβ”΄β”β”‚ β”‚ β”‚ β”‚ β”‚ ││││└─┐
52-
// β””β”€β”˜β””β”€β”˜ β”΄ β”΄ β””β”€β”˜β”˜β””β”˜β””β”€β”˜
50+
// Autochange automatically based on location (sunrise/sunset). Openweathermap API key required.
51+
changeThemeByLocation: false,
52+
53+
// β”Œβ” ┬ β”¬β”Œβ”¬β”β”Œβ”¬β”β”Œβ”€β”β”Œβ”β”Œβ”Œβ”€β”
54+
// β”œβ”΄β”β”‚ β”‚ β”‚ β”‚ β”‚ ││││└─┐
55+
// β””β”€β”˜β””β”€β”˜ β”΄ β”΄ β””β”€β”˜β”˜β””β”˜β””β”€β”˜
5356

5457
firstButtonsContainer: [
5558
{

β€Žindex.htmlβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@
6060

6161
<!-- Scripts -->
6262
<script src="assets/js/layout.js"></script>
63-
<script src="assets/js/theme.js"></script>
6463
<script src="assets/js/time.js"></script>
6564
<script src="assets/js/greeting.js"></script>
66-
<script src="assets/js/weather.js"></script>
65+
<script src="assets/js/weather.js"></script>
66+
<script src="assets/js/theme.js"></script>
6767
<script src="assets/js/title.js"></script>
6868

6969
<!-- Generators -->

0 commit comments

Comments
Β (0)