Skip to content

Commit

Permalink
chore: small cleanups after PR #149
Browse files Browse the repository at this point in the history
  • Loading branch information
mcndt committed Jul 14, 2024
1 parent 51ba08f commit a76f1b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
37 changes: 25 additions & 12 deletions lib/toggl/TogglService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = "-";
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/TogglSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}),
);
Expand Down
12 changes: 8 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand Down Expand Up @@ -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",
Expand All @@ -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 != "";
}
Expand Down

0 comments on commit a76f1b2

Please sign in to comment.