From e0699eed72c5682bd2395ba4299ff69933c83d48 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 19 Feb 2023 14:39:02 +0100 Subject: Be more precise configuring chords --- src/view/options.ts | 70 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 15 deletions(-) (limited to 'src/view/options.ts') diff --git a/src/view/options.ts b/src/view/options.ts index 31fd631..0782fe4 100644 --- a/src/view/options.ts +++ b/src/view/options.ts @@ -1,30 +1,70 @@ +import * as Chord from 'chord' + +// Model version: upgrade whenever the model is updated. Options are loaded +// only if versions match. +const version = '2' + export interface Model { - major: boolean - minor: boolean - seventh: boolean - minorSeventh: boolean - majorSeventh: boolean + major: Chord.Chords + minor: Chord.Chords + seventh: Chord.Chords + minorSeventh: Chord.Chords + majorSeventh: Chord.Chords bpm: number beatsPerChord: number } let init: Model = { - major: true, - minor: false, - seventh: false, - minorSeventh: false, - majorSeventh: false, + major: new Set(Chord.plains), + minor: new Set(), + seventh: new Set(), + minorSeventh: new Set(), + majorSeventh: new Set(), bpm: 90, beatsPerChord: 4 } -let key: string = 'options' - export function load(): Model { - let str = localStorage[key] - return str && JSON.parse(str) || init + let storedVersion = localStorage['version'] + + if (storedVersion === undefined || storedVersion !== version) { + return init + } else { + let str = localStorage['options'] + return str && fromString(str) || init + } } export function save(options: Model): void { - localStorage[key] = JSON.stringify(options) + localStorage['version'] = version + localStorage['options'] = toString(options) +} + +function toString(o: Model): string { + return JSON.stringify({ + major: [...o.major], + minor: [...o.minor], + seventh: [...o.seventh], + minorSeventh: [...o.minorSeventh], + majorSeventh: [...o.majorSeventh], + bpm: o.bpm, + beatsPerChord: o.beatsPerChord + }) +} + +function fromString(str: string): Model | undefined { + const o = JSON.parse(str) + if (o === undefined) { + return undefined + } else { + return { + major: new Set(o.major), + minor: new Set(o.minor), + seventh: new Set(o.seventh), + minorSeventh: new Set(o.minorSeventh), + majorSeventh: new Set(o.majorSeventh), + bpm: o.bpm, + beatsPerChord: o.beatsPerChord + } + } } -- cgit v1.2.3