Skip to content

Commit 1a95776

Browse files
committed
merged master
2 parents 84417f5 + ce16c9c commit 1a95776

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/stores/SettingsStore.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import Store from '@/stores/Store'
22
import { Action } from '@/stores/Dispatcher'
33
import { SetCustomModelEnabled, UpdateSettings } from '@/actions/Actions'
44

5+
const STORAGE_KEY = 'settings'
6+
57
export interface Settings {
68
showDistanceInMiles: boolean
7-
drawAreasEnabled: boolean
9+
drawAreasEnabled: boolean // temporary, not persisted to localStorage
810
saveRecentLocations: boolean
911
gpxExportRte: boolean
1012
gpxExportWpt: boolean
@@ -20,24 +22,45 @@ export const defaultSettings: Settings = {
2022
gpxExportTrk: true,
2123
}
2224

25+
function loadSettings(): Settings {
26+
try {
27+
const stored = localStorage.getItem(STORAGE_KEY)
28+
if (stored) return { ...defaultSettings, ...JSON.parse(stored) }
29+
} catch {
30+
// localStorage unavailable
31+
}
32+
return defaultSettings
33+
}
34+
35+
function saveSettings(settings: Settings): void {
36+
try {
37+
const { drawAreasEnabled, ...persistent } = settings
38+
localStorage.setItem(STORAGE_KEY, JSON.stringify(persistent))
39+
} catch {
40+
// localStorage unavailable
41+
}
42+
}
43+
2344
export default class SettingsStore extends Store<Settings> {
2445
constructor() {
25-
super(defaultSettings)
46+
super(loadSettings())
2647
}
2748

2849
reduce(state: Settings, action: Action): Settings {
50+
let newState = state
2951
if (action instanceof SetCustomModelEnabled) {
3052
if (!action.enabled && state.drawAreasEnabled)
31-
return {
53+
newState = {
3254
...state,
3355
drawAreasEnabled: false,
3456
}
3557
} else if (action instanceof UpdateSettings) {
36-
return {
58+
newState = {
3759
...state,
3860
...action.updatedSettings,
3961
}
4062
}
41-
return state
63+
if (newState !== state) saveSettings(newState)
64+
return newState
4265
}
4366
}

0 commit comments

Comments
 (0)