Hook up cardCountsSeparateInactive to PreferenceStore

This commit is contained in:
Henrik Giesel 2021-01-21 14:04:33 +01:00
parent 5fc8b1965a
commit 64352ce0d5
4 changed files with 12 additions and 14 deletions

View file

@ -2,13 +2,16 @@
import { defaultGraphBounds } from "./graph-helpers"; import { defaultGraphBounds } from "./graph-helpers";
import { gatherData, renderCards } from "./card-counts"; import { gatherData, renderCards } from "./card-counts";
import type { GraphData, TableDatum } from "./card-counts"; import type { GraphData, TableDatum } from "./card-counts";
import type { PreferenceStore } from "./preferences";
import type pb from "anki/backend_proto"; import type pb from "anki/backend_proto";
import type { I18n } from "anki/i18n"; import type { I18n } from "anki/i18n";
import SeparateInactiveCheckbox from "./SeparateInactiveCheckbox.svelte"; import SeparateInactiveCheckbox from "./SeparateInactiveCheckbox.svelte";
export let sourceData: pb.BackendProto.GraphsOut; export let sourceData: pb.BackendProto.GraphsOut;
export let i18n: I18n; export let i18n: I18n;
export let preferences: PreferenceStore;
let { cardCountsSeparateInactive } = preferences;
let svg = null as HTMLElement | SVGElement | null; let svg = null as HTMLElement | SVGElement | null;
let bounds = defaultGraphBounds(); let bounds = defaultGraphBounds();
@ -17,10 +20,9 @@
let graphData = (null as unknown) as GraphData; let graphData = (null as unknown) as GraphData;
let tableData = (null as unknown) as TableDatum[]; 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); tableData = renderCards(svg as any, bounds, graphData);
} }
@ -56,7 +58,7 @@
<h1>{graphData.title}</h1> <h1>{graphData.title}</h1>
<div class="range-box-inner"> <div class="range-box-inner">
<SeparateInactiveCheckbox {i18n} bind:cardCountsSeparateInactive /> <SeparateInactiveCheckbox {i18n} {cardCountsSeparateInactive} />
</div> </div>
<div class="counts-outer"> <div class="counts-outer">

View file

@ -62,6 +62,7 @@
<svelte:component <svelte:component
this={graph} this={graph}
{sourceData} {sourceData}
{preferences}
{revlogRange} {revlogRange}
{i18n} {i18n}
{nightMode} /> {nightMode} />

View file

@ -1,13 +1,14 @@
<script lang="typescript"> <script lang="typescript">
import type { I18n } from "anki/i18n"; import type { I18n } from "anki/i18n";
import type { CustomStore } from "./preferences";
export let i18n: I18n; export let i18n: I18n;
export let cardCountsSeparateInactive: boolean; export let cardCountsSeparateInactive: CustomStore<boolean>;
const label = i18n.tr(i18n.TR.STATISTICS_COUNTS_SEPARATE_SUSPENDED_BURIED_CARDS); const label = i18n.tr(i18n.TR.STATISTICS_COUNTS_SEPARATE_SUSPENDED_BURIED_CARDS);
</script> </script>
<label> <label>
<input type="checkbox" bind:checked={cardCountsSeparateInactive} /> <input type="checkbox" bind:checked={$cardCountsSeparateInactive} />
{label} {label}
</label> </label>

View file

@ -1,11 +1,10 @@
import { getGraphPreferences } from "./graph-helpers"; import { getGraphPreferences } from "./graph-helpers";
import { writable } from "svelte/store"; import { writable, get } from "svelte/store";
import type pb from "anki/backend_proto"; import type pb from "anki/backend_proto";
interface CustomStore<T> { export interface CustomStore<T> {
subscribe: (getter: (value: T) => void) => () => void; subscribe: (getter: (value: T) => void) => () => void;
set: (value: T) => void; set: (value: T) => void;
get: () => T;
} }
export type PreferenceStore = { export type PreferenceStore = {
@ -26,11 +25,6 @@ function createPreference<T>(
set(v); set(v);
savePreferences(); savePreferences();
}, },
get: (): T => {
let result: any /* T */;
subscribe((value: T) => (result = value))();
return result;
},
}; };
} }
@ -43,7 +37,7 @@ function preparePreferences(
function constructPreferences(): pb.BackendProto.GraphsPreferencesOut { function constructPreferences(): pb.BackendProto.GraphsPreferencesOut {
const payload: Partial<pb.BackendProto.GraphsPreferencesOut> = {}; const payload: Partial<pb.BackendProto.GraphsPreferencesOut> = {};
for (const [key, pref] of Object.entries(preferences as PreferenceStore)) { for (const [key, pref] of Object.entries(preferences as PreferenceStore)) {
payload[key] = pref.get(); payload[key] = get(pref as any);
} }
return payload as pb.BackendProto.GraphsPreferencesOut; return payload as pb.BackendProto.GraphsPreferencesOut;
} }