Location: having issue with FileView sometimes serializing 0 instead of latitude

This commit is contained in:
quadbyte 2025-08-12 09:40:10 -04:00
parent 7cbf5b9212
commit ca8523ec1a
2 changed files with 33 additions and 23 deletions

View file

@ -64,7 +64,7 @@ Item {
Layout.fillWidth: true Layout.fillWidth: true
onEditingFinished: function () { onEditingFinished: function () {
Settings.data.location.name = text Settings.data.location.name = text
Location.resetWeather(); Location.resetWeather()
} }
} }

View file

@ -15,27 +15,20 @@ Singleton {
FileView { FileView {
path: locationFile path: locationFile
watchChanges: true
onFileChanged: reload()
onAdapterUpdated: writeAdapter() onAdapterUpdated: writeAdapter()
Component.onCompleted: function () {
reload()
}
onLoaded: function () { onLoaded: function () {
updateWeather() updateWeather()
} }
onLoadFailed: function (error) { onLoadFailed: function (error) {
if (error.toString().includes("No such file") || error === 2) { updateWeather()
// File doesn't exist, create it with default values
writeAdapter()
}
} }
JsonAdapter { JsonAdapter {
id: adapter id: adapter
property string latitude: "" property string lastLocationName: ""
property string longitude: "" property real latitude: 0
property real longitude: 0
property int weatherLastFetch: 0 property int weatherLastFetch: 0
property var weather: null property var weather: null
} }
@ -53,15 +46,19 @@ Singleton {
} }
// -------------------------------- // --------------------------------
function init() {// does nothing but ensure the singleton is created function init() {
// does nothing but ensure the singleton is created
// do not remove // do not remove
console.log("[Location] Service started") console.log("[Location] Service started")
} }
// -------------------------------- // --------------------------------
function resetWeather() { function resetWeather() {
data.latitude = "" console.log("[Location] Resetting weather data")
data.longitude = ""
data.lastLocationName = ""
data.latitude = 0
data.longitude = 0
data.weatherLastFetch = 0 data.weatherLastFetch = 0
data.weather = null data.weather = null
@ -76,7 +73,13 @@ Singleton {
return return
} }
if ((data.weatherLastFetch === "") || (data.weather === null) || (Time.timestamp >= data.weatherLastFetch + weatherUpdateFrequency)) { if (data.latitude === 0) {
console.warn("[Location] Why is my latitude zero")
}
if ((data.weatherLastFetch === "") || (data.weather === null) || data.latitude === 0 || data.longitude === 0
|| (data.lastLocationName !== Settings.data.location.name)
|| (Time.timestamp >= data.weatherLastFetch + weatherUpdateFrequency)) {
getFreshWeather() getFreshWeather()
} }
} }
@ -84,16 +87,19 @@ Singleton {
// -------------------------------- // --------------------------------
function getFreshWeather() { function getFreshWeather() {
isFetchingWeather = true isFetchingWeather = true
if (data.latitude === "" || data.longitude === "") { if (data.latitude === 0 || data.longitude === 0 || (data.lastLocationName !== Settings.data.location.name)) {
_geocodeLocation(Settings.data.location.name, function (lat, lon) { _geocodeLocation(Settings.data.location.name, function (latitude, longitude) {
console.log("[Location] Geocoded " + Settings.data.location.name + " to: " + lat + " / " + lon) console.log("[Location] Geocoded " + Settings.data.location.name + " to: " + latitude + " / " + longitude)
// Save location name
data.lastLocationName = Settings.data.location.name
// Save GPS coordinates // Save GPS coordinates
data.latitude = lat data.latitude = latitude
data.longitude = lon data.longitude = longitude
_fetchWeather(data.latitude, data.longitude, errorCallback) _fetchWeather(latitude, longitude, errorCallback)
}, errorCallback) }, errorCallback)
} else { } else {
_fetchWeather(data.latitude, data.longitude, errorCallback) _fetchWeather(data.latitude, data.longitude, errorCallback)
@ -111,6 +117,7 @@ Singleton {
if (xhr.status === 200) { if (xhr.status === 200) {
try { try {
var geoData = JSON.parse(xhr.responseText) var geoData = JSON.parse(xhr.responseText)
// console.log(JSON.stringify(geoData))
if (geoData.results && geoData.results.length > 0) { if (geoData.results && geoData.results.length > 0) {
callback(geoData.results[0].latitude, geoData.results[0].longitude) callback(geoData.results[0].latitude, geoData.results[0].longitude)
} else { } else {
@ -140,9 +147,12 @@ Singleton {
try { try {
var weatherData = JSON.parse(xhr.responseText) var weatherData = JSON.parse(xhr.responseText)
// Save to json // Save data
data.weather = weatherData data.weather = weatherData
data.weatherLastFetch = Time.timestamp data.weatherLastFetch = Time.timestamp
data.latitude = weatherData.latitude
data.longitude = weatherData.longitude
isFetchingWeather = false isFetchingWeather = false
console.log("[Location] Cached weather to disk") console.log("[Location] Cached weather to disk")
} catch (e) { } catch (e) {