From 5fc8b1965aeaf1137710581c4f0a98b400f9ed1f Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 21 Jan 2021 13:45:49 +0100 Subject: [PATCH] Add PreferenceStore with gettable/settable preferences * setting is not yet hooked up to rslib --- ts/graphs/CardCounts.svelte | 6 +-- ts/graphs/GraphsPage.svelte | 12 ++++- ts/graphs/SeparateInactiveCheckbox.svelte | 4 +- ts/graphs/preferences.ts | 62 +++++++++++++++++++++-- ts/tsconfig.json | 2 +- 5 files changed, 74 insertions(+), 12 deletions(-) diff --git a/ts/graphs/CardCounts.svelte b/ts/graphs/CardCounts.svelte index 5f6675138..4c753c13c 100644 --- a/ts/graphs/CardCounts.svelte +++ b/ts/graphs/CardCounts.svelte @@ -5,7 +5,6 @@ import type pb from "anki/backend_proto"; import type { I18n } from "anki/i18n"; import SeparateInactiveCheckbox from "./SeparateInactiveCheckbox.svelte"; - import { cardCountsSeparateInactive } from "./preferences"; export let sourceData: pb.BackendProto.GraphsOut; export let i18n: I18n; @@ -18,9 +17,10 @@ let graphData = (null as unknown) as GraphData; let tableData = (null as unknown) as TableDatum[]; + let cardCountsSeparateInactive = false; $: { - graphData = gatherData(sourceData, $cardCountsSeparateInactive, i18n); + graphData = gatherData(sourceData, cardCountsSeparateInactive, i18n); tableData = renderCards(svg as any, bounds, graphData); } @@ -56,7 +56,7 @@

{graphData.title}

- +
diff --git a/ts/graphs/GraphsPage.svelte b/ts/graphs/GraphsPage.svelte index 6a461f6a8..a38d20f15 100644 --- a/ts/graphs/GraphsPage.svelte +++ b/ts/graphs/GraphsPage.svelte @@ -4,8 +4,10 @@ diff --git a/ts/graphs/preferences.ts b/ts/graphs/preferences.ts index 8d03bfe59..a4e37ac90 100644 --- a/ts/graphs/preferences.ts +++ b/ts/graphs/preferences.ts @@ -1,15 +1,69 @@ +import { getGraphPreferences } from "./graph-helpers"; import { writable } from "svelte/store"; +import type pb from "anki/backend_proto"; -function createPreference(initialValue: unknown) { +interface CustomStore { + subscribe: (getter: (value: T) => void) => () => void; + set: (value: T) => void; + get: () => T; +} + +export type PreferenceStore = { + [K in keyof pb.BackendProto.GraphsPreferencesOut]: CustomStore< + pb.BackendProto.GraphsPreferencesOut[K] + >; +}; + +function createPreference( + initialValue: T, + savePreferences: () => void +): CustomStore { const { subscribe, set } = writable(initialValue); return { subscribe, - set: (v: unknown) => { + set: (v: T): void => { set(v); + savePreferences(); + }, + get: (): T => { + let result: any /* T */; + subscribe((value: T) => (result = value))(); + return result; }, }; } -export const calendarFirstDayOfWeek = createPreference(0); -export const cardCountsSeparateInactive = createPreference(false); +function preparePreferences( + graphsPreferences: pb.BackendProto.GraphsPreferencesOut, + save: (prefs: pb.BackendProto.GraphsPreferencesOut) => void +): PreferenceStore { + const preferences: Partial = {}; + + function constructPreferences(): pb.BackendProto.GraphsPreferencesOut { + const payload: Partial = {}; + for (const [key, pref] of Object.entries(preferences as PreferenceStore)) { + payload[key] = pref.get(); + } + return payload as pb.BackendProto.GraphsPreferencesOut; + } + + function savePreferences(): void { + const preferences = constructPreferences(); + save(preferences); + } + + for (const [key, value] of Object.entries(graphsPreferences)) { + preferences[key] = createPreference(value, savePreferences); + } + + return preferences as PreferenceStore; +} + +export async function getPreferences() { + const initialPreferences = await getGraphPreferences(); + + return preparePreferences(initialPreferences, (prefs) => { + console.log("preferences to save", prefs); + }); +} diff --git a/ts/tsconfig.json b/ts/tsconfig.json index 4e8f96d8c..883e2130b 100644 --- a/ts/tsconfig.json +++ b/ts/tsconfig.json @@ -3,7 +3,7 @@ "compilerOptions": { "target": "es6", "module": "es6", - "lib": ["es2016", "es2019.array", "dom", "dom.iterable"], + "lib": ["es2017", "es2019.array", "dom", "dom.iterable"], "baseUrl": ".", "paths": { "anki/*": ["../bazel-bin/ts/lib/*"]