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

View file

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