From b3c9ac8cbb595a71b5b5e47c09ee94c8a640f9df Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 6 Jan 2024 00:03:53 +0800 Subject: [PATCH] refactor statusbar --- src/baseTypes.ts | 1 - src/langs | 2 +- src/localdb.ts | 26 ++++++++++++++++++++++++++ src/main.ts | 25 +++++++++++++++++++------ src/settings.ts | 20 ++++++++++++++++++-- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/baseTypes.ts b/src/baseTypes.ts index ba656ba..0216dfe 100644 --- a/src/baseTypes.ts +++ b/src/baseTypes.ts @@ -88,7 +88,6 @@ export interface RemotelySavePluginSettings { skipSizeLargerThan?: number; ignorePaths?: string[]; enableStatusBarInfo?: boolean; - lastSuccessSync?: number; /** * @deprecated diff --git a/src/langs b/src/langs index a8f6e1d..cdbb220 160000 --- a/src/langs +++ b/src/langs @@ -1 +1 @@ -Subproject commit a8f6e1d05def00387ee5a50a829fd91e60ae6351 +Subproject commit cdbb220a6bb0f566912ec08a8476316c8232fb79 diff --git a/src/localdb.ts b/src/localdb.ts index e214fca..e559e31 100644 --- a/src/localdb.ts +++ b/src/localdb.ts @@ -18,6 +18,7 @@ export const DEFAULT_TBL_SYNC_MAPPING = "syncmetadatahistory"; export const DEFAULT_SYNC_PLANS_HISTORY = "syncplanshistory"; export const DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING = "vaultrandomidmapping"; export const DEFAULT_TBL_LOGGER_OUTPUT = "loggeroutput"; +export const DEFAULT_TBL_SIMPLE_KV_FOR_MISC = "simplekvformisc"; export interface FileFolderHistoryRecord { key: string; @@ -58,6 +59,7 @@ export interface InternalDBs { syncPlansTbl: LocalForage; vaultRandomIDMappingTbl: LocalForage; loggerOutputTbl: LocalForage; + simpleKVForMiscTbl: LocalForage; } /** @@ -216,6 +218,10 @@ export const prepareDBs = async ( name: DEFAULT_DB_NAME, storeName: DEFAULT_TBL_LOGGER_OUTPUT, }), + simpleKVForMiscTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_SIMPLE_KV_FOR_MISC, + }), } as InternalDBs; // try to get vaultRandomID firstly @@ -680,3 +686,23 @@ export const clearExpiredLoggerOutputRecords = async (db: InternalDBs) => { }); await Promise.all(ps); }; + +export const upsertLastSuccessSyncByVault = async ( + db: InternalDBs, + vaultRandomID: string, + millis: number +) => { + await db.simpleKVForMiscTbl.setItem( + `${vaultRandomID}-lastSuccessSyncMillis`, + millis + ); +}; + +export const getLastSuccessSyncByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + return (await db.simpleKVForMiscTbl.getItem( + `${vaultRandomID}-lastSuccessSyncMillis` + )) as number; +}; diff --git a/src/main.ts b/src/main.ts index 6b70493..da8add5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,6 +32,8 @@ import { insertLoggerOutputByVault, clearExpiredLoggerOutputRecords, clearExpiredSyncPlanRecords, + upsertLastSuccessSyncByVault, + getLastSuccessSyncByVault, } from "./localdb"; import { RemoteClient } from "./remote"; import { @@ -88,7 +90,6 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = { skipSizeLargerThan: -1, ignorePaths: [], enableStatusBarInfo: true, - lastSuccessSync: -1, }; interface OAuth2Info { @@ -361,7 +362,12 @@ export default class RemotelySavePlugin extends Plugin { this.syncStatus = "finish"; this.syncStatus = "idle"; - this.settings.lastSuccessSync = Date.now(); + const lastSuccessSyncMillis = Date.now(); + await upsertLastSuccessSyncByVault( + this.db, + this.vaultRandomID, + lastSuccessSyncMillis + ); if (this.syncRibbon !== undefined) { setIcon(this.syncRibbon, iconNameSyncWait); @@ -369,7 +375,7 @@ export default class RemotelySavePlugin extends Plugin { } if (this.statusBarElement !== undefined) { - this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + this.updateLastSuccessSyncMsg(lastSuccessSyncMillis); } log.info( @@ -689,11 +695,15 @@ export default class RemotelySavePlugin extends Plugin { this.statusBarElement = statusBarItem.createEl("span"); this.statusBarElement.setAttribute("aria-label-position", "top"); - this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + this.updateLastSuccessSyncMsg( + await getLastSuccessSyncByVault(this.db, this.vaultRandomID) + ); // update statusbar text every 30 seconds this.registerInterval( - window.setInterval(() => { - this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + window.setInterval(async () => { + this.updateLastSuccessSyncMsg( + await getLastSuccessSyncByVault(this.db, this.vaultRandomID) + ); }, 1000 * 30) ); } @@ -823,6 +833,9 @@ export default class RemotelySavePlugin extends Plugin { if (this.settings.ignorePaths === undefined) { this.settings.ignorePaths = []; } + if (this.settings.enableStatusBarInfo === undefined) { + this.settings.enableStatusBarInfo = true; + } } async checkIfPresetRulesFollowed() { diff --git a/src/settings.ts b/src/settings.ts index 5d49697..fd35cd0 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -30,6 +30,7 @@ import { clearAllLoggerOutputRecords, insertLoggerOutputByVault, clearExpiredLoggerOutputRecords, + upsertLastSuccessSyncByVault, } from "./localdb"; import type RemotelySavePlugin from "./main"; // unavoidable import { RemoteClient } from "./remote"; @@ -197,8 +198,6 @@ class ChangeRemoteBaseDirModal extends Modal { button.onClick(async () => { this.plugin.settings[this.service].remoteBaseDir = this.newRemoteBaseDir; - // reset last sync time - this.plugin.settings.lastSuccessSync = -1; await this.plugin.saveSettings(); new Notice(t("modal_remotebasedir_notice")); this.close(); @@ -1612,6 +1611,23 @@ export class RemotelySaveSettingTab extends PluginSettingTab { new Notice(t("settings_enablestatusbar_reloadrequired_notice")); }); }); + + new Setting(basicDiv) + .setName(t("settings_resetstatusbar_time")) + .setDesc(t("settings_resetstatusbar_time_desc")) + .addButton((button) => { + button.setButtonText(t("settings_resetstatusbar_button")); + button.onClick(async () => { + // reset last sync time + await upsertLastSuccessSyncByVault( + this.plugin.db, + this.plugin.vaultRandomID, + -1 + ); + this.plugin.updateLastSuccessSyncMsg(-1); + new Notice(t("settings_resetstatusbar_notice")); + }); + }); } new Setting(basicDiv)