diff --git a/src/main.ts b/src/main.ts index 45fb7aa..6173506 100644 --- a/src/main.ts +++ b/src/main.ts @@ -94,6 +94,8 @@ export default class OmnivorePlugin extends Plugin { // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new OmnivoreSettingTab(this.app, this)); + + this.scheduleSync(); } onunload() {} @@ -106,6 +108,25 @@ export default class OmnivorePlugin extends Plugin { await this.saveData(this.settings); } + scheduleSync() { + const frequency = this.settings.frequency; + if (frequency > 0) { + // clear previous interval + if (this.settings.intervalId > 0) { + window.clearInterval(this.settings.intervalId); + } + // schedule new interval + const intervalId = window.setInterval(async () => { + await this.fetchOmnivore(false); + }, frequency * 60 * 1000); + // save new interval id + this.settings.intervalId = intervalId; + this.saveSettings(); + // clear interval when plugin is unloaded + this.registerInterval(intervalId); + } + } + async downloadFileAsAttachment(article: Article): Promise { // download pdf from the URL to the attachment folder const url = article.url; @@ -155,7 +176,7 @@ export default class OmnivorePlugin extends Plugin { return parseYaml(frontMatter[1]); } - async fetchOmnivore() { + async fetchOmnivore(manualSync = true) { const { syncAt, apiKey, @@ -177,7 +198,6 @@ export default class OmnivorePlugin extends Plugin { if (!apiKey) { new Notice("Missing Omnivore api key"); - return; } @@ -187,7 +207,7 @@ export default class OmnivorePlugin extends Plugin { try { console.log(`obsidian-omnivore starting sync since: '${syncAt}'`); - new Notice("🚀 Fetching articles ..."); + manualSync && new Notice("🚀 Fetching articles ..."); // pre-parse template preParseTemplate(template); @@ -335,7 +355,7 @@ export default class OmnivorePlugin extends Plugin { } } - new Notice("🔖 Articles fetched"); + manualSync && new Notice("🔖 Articles fetched"); this.settings.syncAt = DateTime.local().toFormat(DATE_FORMAT); } catch (e) { new Notice("Failed to fetch articles"); @@ -348,6 +368,7 @@ export default class OmnivorePlugin extends Plugin { private async resetSyncingStateSetting() { this.settings.syncing = false; + this.settings.intervalId = 0; await this.saveSettings(); } } @@ -495,6 +516,30 @@ class OmnivoreSettingTab extends PluginSettingTab { text.inputEl.setAttr("cols", 60); }); + new Setting(generalSettings) + .setName("Frequency") + .setDesc( + "Enter the frequency in minutes to sync with Omnivore automatically. 0 means manual sync" + ) + .addText((text) => + text + .setPlaceholder("Enter the frequency") + .setValue(this.plugin.settings.frequency.toString()) + .onChange(async (value) => { + // validate frequency + const frequency = parseInt(value); + if (isNaN(frequency)) { + new Notice("Frequency must be a positive integer"); + return; + } + // save frequency + this.plugin.settings.frequency = frequency; + await this.plugin.saveSettings(); + + this.plugin.scheduleSync(); + }) + ); + new Setting(generalSettings) .setName("Folder") .setDesc( diff --git a/src/settings/index.ts b/src/settings/index.ts index bdb6d47..0839fcf 100644 --- a/src/settings/index.ts +++ b/src/settings/index.ts @@ -17,6 +17,8 @@ export const DEFAULT_SETTINGS: OmnivoreSettings = { attachmentFolder: "Omnivore/attachments", version: "0.0.0", isSingleFile: false, + frequency: 0, + intervalId: 0, }; export enum Filter { @@ -47,4 +49,6 @@ export interface OmnivoreSettings { attachmentFolder: string; version: string; isSingleFile: boolean; + frequency: number; + intervalId: number; }