Better weather

This commit is contained in:
quadbyte 2025-08-11 21:35:51 -04:00
parent 84c81ecb77
commit c71029487a
2 changed files with 27 additions and 8 deletions

View file

@ -21,9 +21,10 @@ NBox {
anchors.margins: Style.marginMedium * scaling anchors.margins: Style.marginMedium * scaling
spacing: Style.marginMedium * scaling spacing: Style.marginMedium * scaling
RowLayout { RowLayout {
spacing: Style.marginSmall * scaling spacing: Style.marginSmall * scaling
Text { NText {
text: Location.weatherSymbolFromCode(Location.data.weather.current_weather.weathercode) text: Location.weatherSymbolFromCode(Location.data.weather.current_weather.weathercode)
font.family: "Material Symbols Outlined" font.family: "Material Symbols Outlined"
font.pointSize: Style.fontSizeXXL * 1.25 * scaling font.pointSize: Style.fontSizeXXL * 1.25 * scaling
@ -37,13 +38,21 @@ NBox {
font.pointSize: Style.fontSizeLarge * scaling font.pointSize: Style.fontSizeLarge * scaling
} }
NText { NText {
text: "(" + Location.data. weather.timezone_abbreviation + ")" text: `(${Location.data.weather.timezone_abbreviation})`
font.pointSize: Style.fontSizeTiny * scaling font.pointSize: Style.fontSizeTiny * scaling
visible: Location.data.weather
} }
} }
NText { NText {
text: "26°C" text: {
var temp = Location.data.weather.current_weather.temperature
if (Settings.data.location.useFahrenheit) {
temp = Location.celsiusToFahrenheit(temp)
}
temp = Math.round(temp)
return `${temp}°`
}
font.pointSize: Style.fontSizeXL * scaling font.pointSize: Style.fontSizeXL * scaling
font.weight: Style.fontWeightBold font.weight: Style.fontWeightBold
} }
@ -60,9 +69,9 @@ NBox {
Layout.fillWidth: true Layout.fillWidth: true
spacing: Style.marginMedium * scaling spacing: Style.marginMedium * scaling
Repeater { Repeater {
model: 5 model: Location.data.weather.daily.time
delegate: ColumnLayout { delegate: ColumnLayout {
spacing: 2 * scaling spacing: Style.spacingSmall * scaling
NText { NText {
text: Qt.formatDateTime(new Date(Location.data.weather.daily.time[index]), "ddd") text: Qt.formatDateTime(new Date(Location.data.weather.daily.time[index]), "ddd")
font.weight: Style.fontWeightBold font.weight: Style.fontWeightBold
@ -70,6 +79,7 @@ NBox {
NText { NText {
text: Location.weatherSymbolFromCode(Location.data.weather.daily.weathercode[index]) text: Location.weatherSymbolFromCode(Location.data.weather.daily.weathercode[index])
font.family: "Material Symbols Outlined" font.family: "Material Symbols Outlined"
font.pointSize: Style.fontSizeLarge * scaling
font.weight: Style.fontWeightBold font.weight: Style.fontWeightBold
color: Colors.textSecondary color: Colors.textSecondary
} }
@ -83,8 +93,9 @@ NBox {
} }
max = Math.round(max) max = Math.round(max)
min = Math.round(min) min = Math.round(min)
return `${max}° / ${min}°` return `${max}°/${min}°`
} }
font.pointSize: Style.fontSizeSmall * scaling
color: Colors.textSecondary color: Colors.textSecondary
} }
} }

View file

@ -11,6 +11,7 @@ Singleton {
property string locationFile: Quickshell.env("NOCTALIA_WEATHER_FILE") || (Settings.cacheDir + "location.json") property string locationFile: Quickshell.env("NOCTALIA_WEATHER_FILE") || (Settings.cacheDir + "location.json")
property int weatherUpdateFrequency: 30 * 60 // 30 minutes expressed in seconds property int weatherUpdateFrequency: 30 * 60 // 30 minutes expressed in seconds
property var data: adapter // Used to access via Location.data.xxx.yyy property var data: adapter // Used to access via Location.data.xxx.yyy
property bool isFetchingWeather: false
FileView { FileView {
path: locationFile path: locationFile
@ -63,6 +64,10 @@ Singleton {
// -------------------------------- // --------------------------------
function updateWeather() { function updateWeather() {
if (isFetchingWeather) {
return
}
if ((data.weatherLastFetch === "") || (Time.timestamp >= data.weatherLastFetch + weatherUpdateFrequency)) { if ((data.weatherLastFetch === "") || (Time.timestamp >= data.weatherLastFetch + weatherUpdateFrequency)) {
getFreshWeather() getFreshWeather()
} }
@ -70,10 +75,11 @@ Singleton {
// -------------------------------- // --------------------------------
function getFreshWeather() { function getFreshWeather() {
isFetchingWeather = true
if (data.latitude === "" || data.longitude === "") { if (data.latitude === "" || data.longitude === "") {
console.log("Geocoding location") console.log("Geocoding location")
_geocodeLocation(Settings.data.location.name, function (lat, lon) { _geocodeLocation(Settings.data.location.name, function (lat, lon) {
console.log("Geocoded " + Settings.data.location.name + " to : " + lat + " / " + lon) console.log("Geocoded " + Settings.data.location.name + " to: " + lat + " / " + lon)
// Save GPS coordinates // Save GPS coordinates
data.latitude = lat data.latitude = lat
@ -128,6 +134,7 @@ Singleton {
// Save to json // Save to json
data.weather = weatherData data.weather = weatherData
data.weatherLastFetch = Time.timestamp data.weatherLastFetch = Time.timestamp
isFetchingWeather = false
console.log("Cached weather to disk") console.log("Cached weather to disk")
} catch (e) { } catch (e) {
errorCallback("Failed to parse weather data.") errorCallback("Failed to parse weather data.")
@ -144,9 +151,10 @@ Singleton {
// -------------------------------- // --------------------------------
function errorCallback(message) { function errorCallback(message) {
console.error(message) console.error(message)
isFetchingWeather = false
} }
// -------------------------------- // --------------------------------
function weatherSymbolFromCode(code) { function weatherSymbolFromCode(code) {
if (code === 0) if (code === 0)
return "sunny" return "sunny"