Skip to content

Commit

Permalink
Save mainview and sorting preference. #124
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyChen777 committed Aug 6, 2022
1 parent 53db7ee commit 1d9ce08
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ domReady().then(appendLoading);

// ============================================================
// State and Preference
const sharedState = new SharedState();
const preference = new Preference();
const sharedState = new SharedState(preference);

// ============================================================
// Repositories
Expand Down
4 changes: 2 additions & 2 deletions packages/preload/interactors/app-interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 11 additions & 8 deletions packages/preload/utils/appstate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randomUUID } from "crypto";
import { ipcRenderer } from "electron";
import { Preference } from "./preference";

export class State {
value: string | number | boolean;
Expand All @@ -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);
}
}
}

Expand All @@ -33,14 +36,14 @@ export class SharedState {
sharedData: Record<string, State>;
dbState: Record<string, State>;

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"),

Expand All @@ -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"),

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions packages/preload/utils/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export interface PreferenceStore {
shortcutFlag: string;

sidebarWidth: number;
mainviewSortBy: string;
mainviewSortOrder: string;
mainviewType: string;

[Key: string]: unknown;
}
Expand Down Expand Up @@ -156,6 +159,9 @@ const defaultPreferences: PreferenceStore = {
shortcutFlag: "CommandOrControl+F",

sidebarWidth: 20,
mainviewSortBy: "addTime",
mainviewSortOrder: "desc",
mainviewType: "list",

scrapers: [
{
Expand Down
20 changes: 18 additions & 2 deletions packages/renderer/src/ui/app-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaperEntity[]> = ref([]);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const props = defineProps({
showMainPubType: Boolean,
});
const viewType = ref("list");
const viewType = ref(window.appInteractor.getState("viewState.viewType"));
const selectedIndex: Ref<number[]> = ref([]);
const selectedLastSingleIndex = ref(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const props = defineProps({
showMainNote: Boolean,
});
const viewType = ref("list");
const viewType = ref(window.appInteractor.getState("viewState.viewType"));
const selectedIndex: Ref<number[]> = ref([]);
const selectedLastSingleIndex = ref(-1);
Expand Down
9 changes: 7 additions & 2 deletions packages/renderer/src/ui/main-view/main-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<number[]> = ref([]);
const selectedEntities: Ref<PaperEntity[]> = ref([]);
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 1d9ce08

Please sign in to comment.