feat: change background/quote interval, use api to get temperature format

This commit is contained in:
David Ralph
2021-07-17 19:38:00 +01:00
parent 8ffd0e04cb
commit 6cccad0e17
6 changed files with 115 additions and 30 deletions

View File

@@ -2,6 +2,7 @@
import React from 'react';
import EventBus from '../../../modules/helpers/eventbus';
import Interval from '../../../modules/helpers/interval';
import PhotoInformation from './PhotoInformation';
@@ -181,7 +182,7 @@ export default class Background extends React.PureComponent {
photographerURL = data.photographer_page;
}
this.setState({
const object = {
url: data.file,
type: 'api',
currentAPI: backgroundAPI,
@@ -194,7 +195,10 @@ export default class Background extends React.PureComponent {
photographerURL: photographerURL,
photoURL: photoURL
}
});
}
this.setState(object);
localStorage.setItem('currentBackground', JSON.stringify(object));
break;
case 'colour':
@@ -336,7 +340,24 @@ export default class Background extends React.PureComponent {
}
});
this.getBackground();
const interval = localStorage.getItem('backgroundchange');
if (interval && interval !== 'refresh') {
Interval(() => {
try {
document.getElementById('backgroundImage').classList.remove('fade-in');
document.getElementsByClassName('photoInformation')[0].classList.remove('fade-in');
} catch (e) {
// Disregard exception
}
this.getBackground();
}, Number(interval), 'background');
try {
this.setState(JSON.parse(localStorage.getItem('currentBackground')));
} catch (e) { this.setBackground(); }
} else {
this.getBackground();
}
}
// only set once we've got the info

View File

@@ -1,6 +1,7 @@
import React from 'react';
import EventBus from '../../../modules/helpers/eventbus';
import Interval from '../../../modules/helpers/interval';
import FileCopy from '@material-ui/icons/FilterNone';
import TwitterIcon from '@material-ui/icons/Twitter';
@@ -141,12 +142,15 @@ export default class Quote extends React.PureComponent {
return this.doOffline();
}
this.setState({
const object = {
quote: '"' + data.quote + '"',
author: data.author,
authorlink: this.getAuthorLink(data.author),
quoteLanguage: quotelanguage
});
}
this.setState(object);
localStorage.setItem('currentQuote', JSON.stringify(object));
} catch (e) {
// ..and if that fails we load one locally
this.doOffline();
@@ -218,10 +222,25 @@ export default class Quote extends React.PureComponent {
this.init();
}
});
// don't bother with the checks if we're loading for the first time
this.setZoom();
this.getQuote();
const interval = localStorage.getItem('quotechange');
if (interval && interval !== 'refresh') {
Interval(() => {
this.setZoom();
this.getQuote();
}, Number(interval), 'quote');
try {
this.setState(JSON.parse(localStorage.getItem('currentQuote')));
} catch (e) {
this.setZoom();
this.getQuote();
}
} else {
// don't bother with the checks if we're loading for the first time
this.setZoom();
this.getQuote();
}
}
componentWillUnmount() {

View File

@@ -61,8 +61,10 @@ export default class Weather extends React.PureComponent {
}
};
const tempFormat = localStorage.getItem('tempformat');
if (!this.state.weather.temp) {
data = await (await fetch (window.constants.WEATHER_URL + `/current?city=${this.state.location}&lang=${localStorage.getItem('language')}`)).json();
data = await (await fetch(window.constants.WEATHER_URL + `/current?city=${this.state.location}&lang=${localStorage.getItem('language')}&format=${tempFormat}`)).json();
}
if (data.cod === '404') {
@@ -71,37 +73,27 @@ export default class Weather extends React.PureComponent {
});
}
let temp = data.main.temp;
let temp_min = data.main.temp_min;
let temp_max = data.main.temp_max;
let temp_text = 'K';
switch (localStorage.getItem('tempformat')) {
let tempText;
switch (tempFormat) {
case 'celsius':
temp = temp - 273.15;
temp_min = temp_min - 273.15;
temp_max = temp_max - 273.15;
temp_text = '°C';
tempText = '°C';
break;
case 'fahrenheit':
temp = ((temp - 273.15) * 1.8) + 32;
temp_min = ((temp_min - 273.15) * 1.8) + 32;
temp_max = ((temp_max - 273.15) * 1.8) + 32;
temp_text = '°F';
tempText = '°F';
break;
// kelvin
default:
default:
tempText = 'K';
break;
}
this.setState({
icon: data.weather[0].icon,
temp_text: temp_text,
temp_text: tempText,
weather: {
temp: Math.round(temp),
temp: Math.round(data.main.temp),
description: data.weather[0].description,
temp_min: Math.round(temp_min),
temp_max: Math.round(temp_max),
temp_min: Math.round(data.main.temp_min),
temp_max: Math.round(data.main.temp_max),
humidity: data.main.humidity,
wind_speed: data.wind.speed,
wind_degrees: data.wind.deg,