14 - Disable notification option

- added plugin setting to enable/disable notifications
This commit is contained in:
SebastianMC 2022-09-26 15:03:00 +02:00
parent 527df1a74f
commit c12db12d37
1 changed files with 27 additions and 6 deletions

View File

@ -29,12 +29,14 @@ interface CustomSortPluginSettings {
additionalSortspecFile: string additionalSortspecFile: string
suspended: boolean suspended: boolean
statusBarEntryEnabled: boolean statusBarEntryEnabled: boolean
notificationsEnabled: boolean
} }
const DEFAULT_SETTINGS: CustomSortPluginSettings = { const DEFAULT_SETTINGS: CustomSortPluginSettings = {
additionalSortspecFile: 'Inbox/Inbox.md', additionalSortspecFile: 'Inbox/Inbox.md',
suspended: true, // if false by default, it would be hard to handle the auto-parse after plugin install suspended: true, // if false by default, it would be hard to handle the auto-parse after plugin install
statusBarEntryEnabled: true statusBarEntryEnabled: true,
notificationsEnabled: true
} }
const SORTSPEC_FILE_NAME: string = 'sortspec.md' const SORTSPEC_FILE_NAME: string = 'sortspec.md'
@ -53,6 +55,12 @@ export default class CustomSortPlugin extends Plugin {
sortSpecCache?: SortSpecsCollection | null sortSpecCache?: SortSpecsCollection | null
initialAutoOrManualSortingTriggered: boolean initialAutoOrManualSortingTriggered: boolean
showNotice(message: string, timeout?: number) {
if (this.settings.notificationsEnabled) {
new Notice(message, timeout)
}
}
readAndParseSortingSpec() { readAndParseSortingSpec() {
const mCache: MetadataCache = this.app.metadataCache const mCache: MetadataCache = this.app.metadataCache
let failed: boolean = false let failed: boolean = false
@ -91,14 +99,14 @@ export default class CustomSortPlugin extends Plugin {
}) })
if (this.sortSpecCache) { if (this.sortSpecCache) {
new Notice(`Parsing custom sorting specification SUCCEEDED!`) this.showNotice(`Parsing custom sorting specification SUCCEEDED!`)
} else { } else {
if (anySortingSpecFound) { if (anySortingSpecFound) {
errorMessage = errorMessage ? errorMessage : `No valid '${SORTINGSPEC_YAML_KEY}:' key(s) in YAML front matter or multiline YAML indentation error or general YAML syntax error` errorMessage = errorMessage ? errorMessage : `No valid '${SORTINGSPEC_YAML_KEY}:' key(s) in YAML front matter or multiline YAML indentation error or general YAML syntax error`
} else { } else {
errorMessage = `No custom sorting specification found or only empty specification(s)` errorMessage = `No custom sorting specification found or only empty specification(s)`
} }
new Notice(`Parsing custom sorting specification FAILED. Suspending the plugin.\n${errorMessage}`, ERROR_NOTICE_TIMEOUT) this.showNotice(`Parsing custom sorting specification FAILED. Suspending the plugin.\n${errorMessage}`, ERROR_NOTICE_TIMEOUT)
this.settings.suspended = true this.settings.suspended = true
this.saveSettings() this.saveSettings()
} }
@ -110,13 +118,13 @@ export default class CustomSortPlugin extends Plugin {
this.saveSettings() this.saveSettings()
let iconToSet: string let iconToSet: string
if (this.settings.suspended) { if (this.settings.suspended) {
new Notice('Custom sort OFF'); this.showNotice('Custom sort OFF');
this.sortSpecCache = null this.sortSpecCache = null
iconToSet = ICON_SORT_SUSPENDED iconToSet = ICON_SORT_SUSPENDED
} else { } else {
this.readAndParseSortingSpec(); this.readAndParseSortingSpec();
if (this.sortSpecCache) { if (this.sortSpecCache) {
new Notice('Custom sort ON'); this.showNotice('Custom sort ON');
this.initialAutoOrManualSortingTriggered = true this.initialAutoOrManualSortingTriggered = true
iconToSet = ICON_SORT_ENABLED_ACTIVE iconToSet = ICON_SORT_ENABLED_ACTIVE
} else { } else {
@ -180,7 +188,7 @@ export default class CustomSortPlugin extends Plugin {
this.readAndParseSortingSpec() this.readAndParseSortingSpec()
this.initialAutoOrManualSortingTriggered = true this.initialAutoOrManualSortingTriggered = true
if (this.sortSpecCache) { // successful read of sorting specifications? if (this.sortSpecCache) { // successful read of sorting specifications?
new Notice('Custom sort ON') this.showNotice('Custom sort ON')
const fileExplorerView: FileExplorerView = this.getFileExplorer() const fileExplorerView: FileExplorerView = this.getFileExplorer()
if (fileExplorerView) { if (fileExplorerView) {
setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE) setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE)
@ -342,5 +350,18 @@ class CustomSortSettingTab extends PluginSettingTab {
} }
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); }));
new Setting(containerEl)
.setName('Enable notifications of plugin states changes')
.setDesc('The plugin can show notifications about its state changes: e.g. when successfully parsed and applied'
+ ' the custom sorting specification, or, when the parsing failed. If the notification are disabled,'
+ ' the only source of plugin state is the ribbon button icon. The developer console presents the parsing'
+ ' error messages regardless if the notifications are enabled or not.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.notificationsEnabled)
.onChange(async (value) => {
this.plugin.settings.notificationsEnabled = value;
await this.plugin.saveSettings();
}));
} }
} }