From a76f1b240289941293c02ebf8da9ddafe81c83e6 Mon Sep 17 00:00:00 2001 From: Maxime Cannoodt Date: Sun, 14 Jul 2024 19:55:27 +0200 Subject: [PATCH] chore: small cleanups after PR #149 --- lib/toggl/TogglService.ts | 37 +++++++++++++++++++++++++------------ lib/ui/TogglSettingsTab.ts | 2 +- main.ts | 12 ++++++++---- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/toggl/TogglService.ts b/lib/toggl/TogglService.ts index 0394c19..6f4fb39 100644 --- a/lib/toggl/TogglService.ts +++ b/lib/toggl/TogglService.ts @@ -80,33 +80,41 @@ export default class TogglService { this._statusBarItem = this._plugin.addStatusBarItem(); this._statusBarItem.setText("Connecting to Toggl..."); - this._plugin.registerDomEvent( - this._statusBarItem, "click", () => { - new Notice('Reconnecting to Toggl...') - this.setToken(this._plugin.settings.apiToken) - } - ) + this._plugin.registerDomEvent(this._statusBarItem, "click", () => { + this.refreshApiConnection(this._plugin.settings.apiToken); + }); // Store a reference to the manager in a svelte store to avoid passing // of references around the component trees. togglService.set(this); apiStatusStore.set(ApiStatus.UNTESTED); } + private _setApiStatus(status: ApiStatus) { + this._ApiAvailable = status; + apiStatusStore.set(status); + } + /** * Creates a new toggl client object using the passed API token. * @param token the API token for the client. */ - public async setToken(token: string) { + public async refreshApiConnection(token: string) { + this._setApiStatus(ApiStatus.UNTESTED); + this._statusBarItem.setText("Connecting to Toggl..."); + if (this._apiManager != null) { + new Notice("Reconnecting to Toggl..."); + } + window.clearInterval(this._currentTimerInterval); if (token != null && token != "") { try { this._apiManager = new TogglAPI(); await this._apiManager.setToken(token); - this._ApiAvailable = ApiStatus.AVAILABLE; + this._setApiStatus(ApiStatus.AVAILABLE); } catch { console.error("Cannot connect to toggl API."); this._statusBarItem.setText("Cannot connect to Toggl API"); - this._ApiAvailable = ApiStatus.UNREACHABLE; + this._setApiStatus(ApiStatus.UNREACHABLE); this.noticeAPINotAvailable(); return; } @@ -120,7 +128,7 @@ export default class TogglService { .then((response) => setDailySummaryItems(response)); } else { this._statusBarItem.setText("Open settings to add a Toggl API token."); - this._ApiAvailable = ApiStatus.NO_TOKEN; + this._setApiStatus(ApiStatus.NO_TOKEN); this.noticeAPINotAvailable(); } apiStatusStore.set(this._ApiAvailable); @@ -202,14 +210,14 @@ export default class TogglService { try { curr = await this._apiManager.getCurrentTimer(); if (this._ApiAvailable === ApiStatus.DEGRADED) { - this._ApiAvailable = ApiStatus.AVAILABLE; + this._setApiStatus(ApiStatus.AVAILABLE); } } catch (err) { console.error("Error reaching Toggl API"); console.error(err); if (this._ApiAvailable !== ApiStatus.DEGRADED) { new Notice("Error updating active Toggl time entry. Retrying..."); - this._ApiAvailable = ApiStatus.DEGRADED; + this._setApiStatus(ApiStatus.DEGRADED); } return; } @@ -272,6 +280,11 @@ export default class TogglService { * state (e.g. details of current timer). */ private updateStatusBarText() { + if (this._ApiAvailable === ApiStatus.UNTESTED) { + this._statusBarItem.setText("Connecting to Toggl..."); + return; + } + let timer_msg = null; if (this._currentTimeEntry == null) { timer_msg = "-"; diff --git a/lib/ui/TogglSettingsTab.ts b/lib/ui/TogglSettingsTab.ts index aa88738..68b07d4 100644 --- a/lib/ui/TogglSettingsTab.ts +++ b/lib/ui/TogglSettingsTab.ts @@ -47,7 +47,7 @@ export default class TogglSettingsTab extends PluginSettingTab { .setValue(this.plugin.settings.apiToken || "") .onChange(async (value) => { this.plugin.settings.apiToken = value; - this.plugin.toggl.setToken(value); + this.plugin.toggl.refreshApiConnection(value); await this.plugin.saveSettings(); }), ); diff --git a/main.ts b/main.ts index c44d045..43a0b67 100644 --- a/main.ts +++ b/main.ts @@ -4,7 +4,9 @@ import { CODEBLOCK_LANG } from "lib/constants"; import reportBlockHandler from "lib/reports/reportBlockHandler"; import TogglService from "lib/toggl/TogglService"; import TogglSettingsTab from "lib/ui/TogglSettingsTab"; -import TogglReportView, { VIEW_TYPE_REPORT } from "lib/ui/views/TogglReportView"; +import TogglReportView, { + VIEW_TYPE_REPORT, +} from "lib/ui/views/TogglReportView"; import UserInputHelper from "lib/util/UserInputHelper"; import { settingsStore, versionLogDismissed } from "lib/util/stores"; import { Plugin, WorkspaceLeaf } from "obsidian"; @@ -25,7 +27,7 @@ export default class MyPlugin extends Plugin { // instantiate toggl class and set the API token if set in settings. this.toggl = new TogglService(this); if (this.settings.apiToken != null || this.settings.apiToken != "") { - this.toggl.setToken(this.settings.apiToken); + this.toggl.refreshApiConnection(this.settings.apiToken); this.input = new UserInputHelper(this); } @@ -83,7 +85,9 @@ export default class MyPlugin extends Plugin { active: true, type: VIEW_TYPE_REPORT, }); - this.app.workspace.revealLeaf(this.app.workspace.getLeavesOfType(VIEW_TYPE_REPORT)[0]); + this.app.workspace.revealLeaf( + this.app.workspace.getLeavesOfType(VIEW_TYPE_REPORT)[0], + ); }, id: "show-report-view", name: "Open report view", @@ -92,7 +96,7 @@ export default class MyPlugin extends Plugin { this.addCommand({ checkCallback: (checking: boolean) => { if (!checking) { - this.toggl.setToken(this.settings.apiToken); + this.toggl.refreshApiConnection(this.settings.apiToken); } else { return this.settings.apiToken != null || this.settings.apiToken != ""; }