diff --git a/qt/aqt/mediasrv.py b/qt/aqt/mediasrv.py index c58c19747..d50168d2a 100644 --- a/qt/aqt/mediasrv.py +++ b/qt/aqt/mediasrv.py @@ -19,6 +19,7 @@ from flask import Response, request from waitress.server import create_server import aqt +import anki.backend_pb2 as pb from anki import hooks from anki.rsbackend import from_json_bytes from anki.utils import devMode @@ -259,6 +260,18 @@ def graph_preferences() -> bytes: return aqt.mw.col.backend.graphs_preferences() +def set_graph_preferences() -> bytes: + data = from_json_bytes(request.data) + + underscore_data = { + "calendar_first_day_of_week": data["calendarFirstDayOfWeek"], + "card_counts_separate_inactive": data["cardCountsSeparateInactive"], + } + + data = pb.GraphsPreferencesOut(**underscore_data) + return aqt.mw.col.backend.set_graphs_preferences(input=data) + + def congrats_info() -> bytes: info = aqt.mw.col.backend.congrats_info() return info.SerializeToString() @@ -267,6 +280,7 @@ def congrats_info() -> bytes: post_handlers = dict( graphData=graph_data, graphPreferences=graph_preferences, + setGraphPreferences=set_graph_preferences, # pylint: disable=unnecessary-lambda i18nResources=lambda: aqt.mw.col.backend.i18n_resources(), congratsInfo=congrats_info, diff --git a/rslib/backend.proto b/rslib/backend.proto index 60a81eec9..2e6a9a0a3 100644 --- a/rslib/backend.proto +++ b/rslib/backend.proto @@ -117,6 +117,7 @@ service BackendService { rpc CardStats(CardID) returns (String); rpc Graphs(GraphsIn) returns (GraphsOut); rpc GraphsPreferences(Empty) returns (GraphsPreferencesOut); + rpc SetGraphsPreferences(GraphsPreferencesOut) returns (Empty); // media diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 17ef064d3..3444f8358 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -680,6 +680,11 @@ impl BackendService for Backend { self.with_col(|col| col.graphs_preferences()) } + fn set_graphs_preferences(&self, input: pb::GraphsPreferencesOut) -> BackendResult { + self.with_col(|col| col.set_graphs_preferences(input)) + .map(Into::into) + } + // decks //----------------------------------------------- diff --git a/rslib/src/stats/graphs.rs b/rslib/src/stats/graphs.rs index 3e0fbff64..eeddf68fd 100644 --- a/rslib/src/stats/graphs.rs +++ b/rslib/src/stats/graphs.rs @@ -52,6 +52,11 @@ impl Collection { card_counts_separate_inactive: true, }) } + + pub(crate) fn set_graphs_preferences(&self, prefs: pb::GraphsPreferencesOut) -> Result<()> { + // self.set_first_weekday(prefs.calendar_first_day_of_week); + Ok(()) + } } impl From for pb::RevlogEntry { diff --git a/ts/graphs/graph-helpers.ts b/ts/graphs/graph-helpers.ts index 9e94fdbf8..546f0816e 100644 --- a/ts/graphs/graph-helpers.ts +++ b/ts/graphs/graph-helpers.ts @@ -25,6 +25,12 @@ export async function getGraphPreferences(): Promise { + return (async () => { + await postRequest("/_anki/setGraphPreferences", JSON.stringify({ ...prefs })) + })() +} + // amount of data to fetch from backend export enum RevlogRange { Year = 1, diff --git a/ts/graphs/preferences.ts b/ts/graphs/preferences.ts index cc3109fc5..8906cced5 100644 --- a/ts/graphs/preferences.ts +++ b/ts/graphs/preferences.ts @@ -1,6 +1,7 @@ -import { getGraphPreferences } from "./graph-helpers"; -import { writable, get } from "svelte/store"; import type pb from "anki/backend_proto"; +import { getGraphPreferences, setGraphPreferences } from "./graph-helpers"; +import { writable, get } from "svelte/store"; + export interface CustomStore { subscribe: (getter: (value: T) => void) => () => void; @@ -28,10 +29,7 @@ function createPreference( }; } -function preparePreferences( - graphsPreferences: pb.BackendProto.GraphsPreferencesOut, - save: (prefs: pb.BackendProto.GraphsPreferencesOut) => void -): PreferenceStore { +function preparePreferences(graphsPreferences: pb.BackendProto.GraphsPreferencesOut): PreferenceStore { const preferences: Partial = {}; function constructPreferences(): pb.BackendProto.GraphsPreferencesOut { @@ -43,8 +41,7 @@ function preparePreferences( } function savePreferences(): void { - const preferences = constructPreferences(); - save(preferences); + setGraphPreferences(constructPreferences()); } for (const [key, value] of Object.entries(graphsPreferences)) { @@ -56,8 +53,5 @@ function preparePreferences( export async function getPreferences() { const initialPreferences = await getGraphPreferences(); - - return preparePreferences(initialPreferences, (prefs) => { - console.log("preferences to save", prefs); - }); + return preparePreferences(initialPreferences) }