From 30d94986c68a854c5d2fb430c9a76d7768ead682 Mon Sep 17 00:00:00 2001 From: Lars <29163322+Drumber@users.noreply.github.com> Date: Fri, 12 Aug 2022 18:05:45 +0200 Subject: [PATCH] Added last sync time to statusbar Adds an item to the statusbar that displays the time of the last successful sync. --- src/baseTypes.ts | 1 + src/langs | 2 +- src/main.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.ts | 2 ++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/baseTypes.ts b/src/baseTypes.ts index 7d540d1..4c6fafc 100644 --- a/src/baseTypes.ts +++ b/src/baseTypes.ts @@ -86,6 +86,7 @@ export interface RemotelySavePluginSettings { lang?: LangTypeAndAuto; logToDB?: boolean; skipSizeLargerThan?: number; + lastSuccessSync?: number; /** * @deprecated diff --git a/src/langs b/src/langs index 42eab5d..58c0bc5 160000 --- a/src/langs +++ b/src/langs @@ -1 +1 @@ -Subproject commit 42eab5d544961f4c7830c63ba9559375437340c0 +Subproject commit 58c0bc5057aee3825bc21b50f2d4b1c95da17b9b diff --git a/src/main.ts b/src/main.ts index 1656afe..727ce82 100644 --- a/src/main.ts +++ b/src/main.ts @@ -85,6 +85,7 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = { lang: "auto", logToDB: false, skipSizeLargerThan: -1, + lastSuccessSync: -1 }; interface OAuth2Info { @@ -125,6 +126,7 @@ export default class RemotelySavePlugin extends Plugin { settings: RemotelySavePluginSettings; db: InternalDBs; syncStatus: SyncStatusType; + statusBarElement: HTMLSpanElement; oauth2Info: OAuth2Info; currLogLevel: string; currSyncMsg?: string; @@ -355,11 +357,17 @@ export default class RemotelySavePlugin extends Plugin { this.syncStatus = "finish"; this.syncStatus = "idle"; + this.settings.lastSuccessSync = Date.now(); + if (this.syncRibbon !== undefined) { setIcon(this.syncRibbon, iconNameSyncWait); this.syncRibbon.setAttribute("aria-label", originLabel); } + if (this.statusBarElement !== undefined) { + this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + } + log.info( `${ this.manifest.id @@ -671,6 +679,15 @@ export default class RemotelySavePlugin extends Plugin { async () => this.syncRun("manual") ); + const statusBarItem = this.addStatusBarItem(); + this.statusBarElement = statusBarItem.createEl("span"); + this.statusBarElement.setAttribute("aria-label-position", "top"); + this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + // update statusbar text every 30 seconds + this.registerInterval(window.setInterval(() => { + this.updateLastSuccessSyncMsg(this.settings.lastSuccessSync); + }, 1000*30)); + this.addCommand({ id: "start-sync", name: t("command_startsync"), @@ -965,6 +982,55 @@ export default class RemotelySavePlugin extends Plugin { this.currSyncMsg = msg; } + updateLastSuccessSyncMsg(lastSuccessSyncMillis?: number) { + const t = (x: TransItemType, vars?: any) => { + return this.i18n.t(x, vars); + }; + + let lastSyncMsg = t("statusbar_lastsync_never"); + let lastSyncLabelMsg = t("statusbar_lastsync_never_label"); + + if (lastSuccessSyncMillis !== undefined && lastSuccessSyncMillis > 0) { + const deltaTime = Date.now() - lastSuccessSyncMillis; + + // create human readable time + const years = Math.floor(deltaTime / 31556952000); + const months = Math.floor(deltaTime / 2629746000); + const weeks = Math.floor(deltaTime / 604800000); + const days = Math.floor(deltaTime / 86400000); + const hours = Math.floor(deltaTime / 3600000); + const minutes = Math.floor(deltaTime / 60000); + let timeText = ""; + + if (years > 0) { + timeText = t("statusbar_time_years", { time: years }); + } else if (months > 0) { + timeText = t("statusbar_time_months", { time: months }); + } else if (weeks > 0) { + timeText = t("statusbar_time_weeks", { time: weeks }); + } else if (days > 0) { + timeText = t("statusbar_time_days", { time: days }); + } else if (hours > 0) { + timeText = t("statusbar_time_hours", { time: hours }); + } else if (minutes > 0) { + timeText = t("statusbar_time_minutes", { time: minutes }); + } else { + timeText = t("statusbar_time_lessminute"); + } + + let dateText = new Date(lastSuccessSyncMillis) + .toLocaleTimeString(navigator.language, { + weekday: "long", year: "numeric", month: "long", day: "numeric" + }); + + lastSyncMsg = t("statusbar_lastsync", { time: timeText }); + lastSyncLabelMsg = t("statusbar_lastsync_label", { date: dateText }); + } + + this.statusBarElement.setText(lastSyncMsg); + this.statusBarElement.setAttribute("aria-label", lastSyncLabelMsg); + } + /** * Because data.json contains sensitive information, * We usually want to ignore it in the version control. diff --git a/src/settings.ts b/src/settings.ts index e851c9e..b92a6d0 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -197,6 +197,8 @@ 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();