diff --git a/src/App.svelte b/src/App.svelte index 598b66f..e25e7b7 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -43,6 +43,12 @@ } }) + // Restart the auto-refresh timer whenever the interval preference changes + $effect(() => { + app.settings.refreshInterval + app.startAutoRefresh() + }) + async function tryGeolocation() { if (!navigator.geolocation) { geoError = 'Geolocation not supported. Add a location to get started.' @@ -97,10 +103,6 @@ app.selectLocation(newest.id) } } - - function handleRefresh() { - app.refreshForecast() - }
@@ -116,9 +118,6 @@ {app.selectedLocation?.name || 'WeatherLens'} - @@ -150,9 +149,6 @@ {/if}
-
diff --git a/src/components/SettingsDialog.svelte b/src/components/SettingsDialog.svelte index 9e20cfa..941aace 100644 --- a/src/components/SettingsDialog.svelte +++ b/src/components/SettingsDialog.svelte @@ -79,6 +79,28 @@ + +
+

Auto-refresh

+
+
+ Refresh interval + Automatically fetch updated weather on a schedule. Turn off to refresh only when you open the app. +
+ +
+
+

Units

diff --git a/src/lib/storage/db.js b/src/lib/storage/db.js index 9de0ec5..d7eec40 100644 --- a/src/lib/storage/db.js +++ b/src/lib/storage/db.js @@ -124,6 +124,7 @@ export async function clearAllLocations() { const DEFAULT_SETTINGS = { units: 'metric', source: 'open-meteo', + refreshInterval: 30, // minutes; 0 = no auto-refresh alertsEnabled: true, alertThresholds: { precip: 70, diff --git a/src/lib/stores/app.svelte.js b/src/lib/stores/app.svelte.js index 7f9eda5..893a724 100644 --- a/src/lib/stores/app.svelte.js +++ b/src/lib/stores/app.svelte.js @@ -33,6 +33,7 @@ export class AppStore { settings = $state({ units: 'metric', source: 'open-meteo', + refreshInterval: 30, alertsEnabled: true, alertThresholds: { precip: 70, @@ -147,6 +148,29 @@ export class AppStore { } } + /** + * Start (or restart) the auto-refresh timer based on the configured + * interval. A value of 0 (or missing) disables auto-refresh. + */ + startAutoRefresh() { + this.stopAutoRefresh() + const minutes = this.settings.refreshInterval + if (!minutes || minutes <= 0) return + this._refreshTimer = setInterval(() => { + this.refreshForecast() + }, minutes * 60 * 1000) + } + + /** + * Stop the auto-refresh timer if it is running. + */ + stopAutoRefresh() { + if (this._refreshTimer) { + clearInterval(this._refreshTimer) + this._refreshTimer = null + } + } + /** * Select a location and fetch its forecast. * @param {string} id