From 1d9ce080022d7baa1c55340d9dee2aea3cbcb58e Mon Sep 17 00:00:00 2001 From: geoffreychen777 Date: Sat, 6 Aug 2022 17:49:47 +0100 Subject: [PATCH] Save mainview and sorting preference. #124 --- packages/preload/index.ts | 2 +- .../preload/interactors/app-interactor.ts | 4 ++-- packages/preload/utils/appstate.ts | 19 ++++++++++-------- packages/preload/utils/preference.ts | 6 ++++++ packages/renderer/src/ui/app-view.vue | 20 +++++++++++++++++-- .../ui/main-view/data-view/feed-data-view.vue | 2 +- .../main-view/data-view/paper-data-view.vue | 2 +- .../renderer/src/ui/main-view/main-view.vue | 9 +++++++-- 8 files changed, 47 insertions(+), 17 deletions(-) diff --git a/packages/preload/index.ts b/packages/preload/index.ts index e134be0d..8e909c56 100644 --- a/packages/preload/index.ts +++ b/packages/preload/index.ts @@ -24,8 +24,8 @@ domReady().then(appendLoading); // ============================================================ // State and Preference -const sharedState = new SharedState(); const preference = new Preference(); +const sharedState = new SharedState(preference); // ============================================================ // Repositories diff --git a/packages/preload/interactors/app-interactor.ts b/packages/preload/interactors/app-interactor.ts index ed15e041..983ea170 100644 --- a/packages/preload/interactors/app-interactor.ts +++ b/packages/preload/interactors/app-interactor.ts @@ -65,8 +65,8 @@ export class AppInteractor { return this.sharedState.get(dest).get(); } - setState(dest: string, value: number | string | boolean) { - this.sharedState.set(dest, value); + setState(dest: string, value: number | string | boolean, publish = true) { + this.sharedState.set(dest, value, publish); } registerState( diff --git a/packages/preload/utils/appstate.ts b/packages/preload/utils/appstate.ts index a6755106..ffdcb826 100644 --- a/packages/preload/utils/appstate.ts +++ b/packages/preload/utils/appstate.ts @@ -1,5 +1,6 @@ import { randomUUID } from "crypto"; import { ipcRenderer } from "electron"; +import { Preference } from "./preference"; export class State { value: string | number | boolean; @@ -18,12 +19,14 @@ export class State { return this.value; } - set(value: string | number | boolean) { + set(value: string | number | boolean, publish = true) { if (value === this.value && this.checkDuplicated) { return; } this.value = value; - this.publishChannel.postMessage(value); + if (publish) { + this.publishChannel.postMessage(value); + } } } @@ -33,14 +36,14 @@ export class SharedState { sharedData: Record; dbState: Record; - constructor() { + constructor(preference: Preference) { this.viewState = { processingQueueCount: new State(0, false), entitiesCount: new State(0), feedEntitiesCount: new State(0), - sortBy: new State("addTime"), - sortOrder: new State("desc"), + sortBy: new State(preference.get("mainviewSortBy") as string), + sortOrder: new State(preference.get("mainviewSortOrder") as string), searchText: new State(""), searchMode: new State("general"), @@ -57,7 +60,7 @@ export class SharedState { infoInformation: new State("", false), processInformation: new State("", false), - viewType: new State("list"), + viewType: new State(preference.get("mainviewType") as string), contentType: new State("library"), theme: new State("light"), @@ -95,9 +98,9 @@ export class SharedState { return state; } - set(dest: string, value: any) { + set(dest: string, value: any, publish = true) { const state = getObj(this, dest) as State; - state.set(value); + state.set(value, publish); } register(dest: string, callback: (value: any) => void) { diff --git a/packages/preload/utils/preference.ts b/packages/preload/utils/preference.ts index 1aaf08ff..531eb2c4 100644 --- a/packages/preload/utils/preference.ts +++ b/packages/preload/utils/preference.ts @@ -84,6 +84,9 @@ export interface PreferenceStore { shortcutFlag: string; sidebarWidth: number; + mainviewSortBy: string; + mainviewSortOrder: string; + mainviewType: string; [Key: string]: unknown; } @@ -156,6 +159,9 @@ const defaultPreferences: PreferenceStore = { shortcutFlag: "CommandOrControl+F", sidebarWidth: 20, + mainviewSortBy: "addTime", + mainviewSortOrder: "desc", + mainviewType: "list", scrapers: [ { diff --git a/packages/renderer/src/ui/app-view.vue b/packages/renderer/src/ui/app-view.vue index 01fdf704..ee49ba4b 100644 --- a/packages/renderer/src/ui/app-view.vue +++ b/packages/renderer/src/ui/app-view.vue @@ -15,8 +15,10 @@ import FeedEditView from "./edit-view/feed-edit-view.vue"; import { PreferenceStore } from "../../../preload/utils/preference"; -const sortBy = ref("addTime"); -const sortOrder = ref("desc"); +const sortBy = ref(window.appInteractor.getState("viewState.sortBy") as string); +const sortOrder = ref( + window.appInteractor.getState("viewState.sortOrder") as string +); const contentType = ref("library"); const entities: Ref = ref([]); @@ -267,8 +269,22 @@ onBeforeMount(async () => { sidebarWidth.value = window.appInteractor.getPreference( "sidebarWidth" ) as number; + + // window.appInteractor.setState( + // "viewState.sortBy", + // window.appInteractor.getPreference("mainviewSortBy") as string + // ); + // window.appInteractor.setState( + // "viewState.sortOrder", + // window.appInteractor.getPreference("mainviewSortOrder") as string + // ); }); onMounted(async () => { + // window.appInteractor.setState( + // "viewState.viewType", + // window.appInteractor.getPreference("mainviewType") as string + // ); + await reloadTags(); await reloadFolders(); await reloadEntities(); diff --git a/packages/renderer/src/ui/main-view/data-view/feed-data-view.vue b/packages/renderer/src/ui/main-view/data-view/feed-data-view.vue index 3ef14c9b..ef00b490 100644 --- a/packages/renderer/src/ui/main-view/data-view/feed-data-view.vue +++ b/packages/renderer/src/ui/main-view/data-view/feed-data-view.vue @@ -23,7 +23,7 @@ const props = defineProps({ showMainPubType: Boolean, }); -const viewType = ref("list"); +const viewType = ref(window.appInteractor.getState("viewState.viewType")); const selectedIndex: Ref = ref([]); const selectedLastSingleIndex = ref(-1); diff --git a/packages/renderer/src/ui/main-view/data-view/paper-data-view.vue b/packages/renderer/src/ui/main-view/data-view/paper-data-view.vue index c0140b21..627373f4 100644 --- a/packages/renderer/src/ui/main-view/data-view/paper-data-view.vue +++ b/packages/renderer/src/ui/main-view/data-view/paper-data-view.vue @@ -29,7 +29,7 @@ const props = defineProps({ showMainNote: Boolean, }); -const viewType = ref("list"); +const viewType = ref(window.appInteractor.getState("viewState.viewType")); const selectedIndex: Ref = ref([]); const selectedLastSingleIndex = ref(-1); diff --git a/packages/renderer/src/ui/main-view/main-view.vue b/packages/renderer/src/ui/main-view/main-view.vue index 19ac4af4..208119be 100644 --- a/packages/renderer/src/ui/main-view/main-view.vue +++ b/packages/renderer/src/ui/main-view/main-view.vue @@ -35,8 +35,10 @@ const props = defineProps({ const contentType = ref("library"); -const sortBy = ref("addTime"); -const sortOrder = ref("desc"); +const sortBy = ref(window.appInteractor.getState("viewState.sortBy") as string); +const sortOrder = ref( + window.appInteractor.getState("viewState.sortOrder") as string +); const selectedIndex: Ref = ref([]); const selectedEntities: Ref = ref([]); @@ -226,14 +228,17 @@ const readSelectedFeedEntities = (read: boolean | null, clear = false) => { const switchViewType = (viewType: string) => { window.appInteractor.setState("viewState.viewType", viewType); + window.appInteractor.updatePreference("mainviewType", viewType); }; const switchSortBy = (key: string) => { window.appInteractor.setState("viewState.sortBy", key); + window.appInteractor.updatePreference("mainviewSortBy", key); }; const switchSortOrder = (order: string) => { window.appInteractor.setState("viewState.sortOrder", order); + window.appInteractor.updatePreference("mainviewSortOrder", order); }; const onMenuButtonClicked = (command: string) => {