Skip to content

Commit

Permalink
Notifications (#32)
Browse files Browse the repository at this point in the history
* feat: use Notice feature from Obsidian to trigger notifications when new day has passed

setting up focus listener and disabling it based on settings

* feat: don't fully recreate view when settings have changed, just rerender when settings change etc.

* feat: new setting in settings view to disable notifications

also clean up some older settings and provide better descriptions

* chore: version 2.3.0
  • Loading branch information
Kageetai authored Mar 5, 2024
1 parent 8614661 commit 6c0cd8f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 26 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "journal-review",
"name": "Journal Review",
"version": "2.2.0",
"version": "2.3.0",
"minAppVersion": "0.15.0",
"description": "Review your daily notes on their anniversaries, like \"what happened today last year\".",
"author": "Kageetai",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-plugin-journal-review",
"version": "2.2.0",
"version": "2.3.0",
"description": "Review your daily notes on their anniversaries, like \"what happened today last year\"\n\n",
"main": "main.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {

export const DEBOUNCE_DELAY = 1000;
export const VIEW_TYPE = "on-this-day-view";
export const SETTINGS_UPDATED_EVENT = "journal-review:settings-updated";

export enum Unit {
day = "day",
Expand Down Expand Up @@ -50,6 +49,8 @@ export interface Settings {
useQuote: boolean;
openInNewPane: boolean;
showNoteTitle: boolean;
useNotifications: boolean;
date: string;
}

export const DEFAULT_SETTINGS: Settings = {
Expand All @@ -61,6 +62,8 @@ export const DEFAULT_SETTINGS: Settings = {
useQuote: true,
openInNewPane: false,
showNoteTitle: true,
useNotifications: true,
date: "",
};

export const getTimeSpanTitle = ({ number, unit, recurring }: TimeSpan) =>
Expand Down
75 changes: 63 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Plugin } from "obsidian";
import { moment, Notice, Plugin, WorkspaceLeaf } from "obsidian";
import { getAllDailyNotes } from "obsidian-daily-notes-interface";

import OnThisDayView from "./view";
import {
DEFAULT_SETTINGS,
reduceTimeSpans,
Settings,
TimeSpan,
Unit,
Expand All @@ -16,6 +18,38 @@ const label = "Open 'On this day' view";
export default class JournalReviewPlugin extends Plugin {
settings: Settings;

checkIsNewDay = () => {
if (moment(new Date()).isAfter(this.settings.date, "day")) {
this.settings.date = new Date().toISOString();
void this.saveSettings();

const noteCount = reduceTimeSpans(
this.settings.timeSpans,
getAllDailyNotes(),
this.settings.dayMargin,
this.settings.useHumanize,
).reduce((count, timeSpan) => count + timeSpan.notes.length, 0);

if (noteCount) {
new Notice(
`It's a new day! You have ${noteCount} journal entries to review. Open the "On this day" view to see them.`,
0,
);
}
}
};

setupFocusListener = () => {
if (this.settings.useNotifications) {
// setup event listener to check if it's a new day and fire notification if so
// need to wait for notes to be loaded
setTimeout(this.checkIsNewDay, 500);
addEventListener("focus", this.checkIsNewDay);
} else {
removeEventListener("focus", this.checkIsNewDay);
}
};

async onload() {
await this.loadSettings();

Expand All @@ -38,22 +72,33 @@ export default class JournalReviewPlugin extends Plugin {
VIEW_TYPE,
(leaf) => new OnThisDayView(leaf, this.settings),
);

this.setupFocusListener();
}

async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE);

await this.app.workspace.getRightLeaf(false)?.setViewState({
type: VIEW_TYPE,
active: true,
});
const { workspace } = this.app;

let leaf: WorkspaceLeaf | null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE);

if (leaves.length > 0) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
// Our view could not be found in the workspace, create a new leaf
// in the right sidebar for it
leaf = workspace.getRightLeaf(false);
await leaf?.setViewState({ type: VIEW_TYPE, active: true });
}

this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0],
);
// "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf!);
}

onunload() {}
onunload() {
removeEventListener("focus", this.checkIsNewDay);
}

async loadSettings() {
// the settings could be in an outdated format
Expand Down Expand Up @@ -81,6 +126,12 @@ export default class JournalReviewPlugin extends Plugin {

async saveSettings() {
await this.saveData(this.settings);
await this.activateView();

// rerender view
(
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0]?.view as OnThisDayView
)?.renderView();

this.setupFocusListener();
}
}
51 changes: 41 additions & 10 deletions src/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class SettingsTab extends PluginSettingTab {
containerEl.createEl("h1", { text: "Journal Review" });
containerEl.createEl("h2", { text: "Time Spans" });
containerEl.createEl("p", {
cls: "setting-item-description",
text: "Define time spans to review, e.g. '1 month' or 'every 6 months'",
});
const container = containerEl.createEl("ul");
Expand Down Expand Up @@ -114,7 +115,7 @@ export class SettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Show Note Title with previews")
.setDesc(
"Render the note title above the preview text, when showing note previews",
"Render the note title above the preview text, when showing note previews.",
)
.addToggle((toggle) =>
toggle
Expand All @@ -125,23 +126,39 @@ export class SettingsTab extends PluginSettingTab {
}),
);

const humanizeDescription = new DocumentFragment();
humanizeDescription.textContent =
"Use the 'humanization' feature from moment.js, when rendering the time spans titles. ";
humanizeDescription.createEl("a", {
text: "More info",
attr: {
href: "https://momentjs.com/docs/#/durations/humanize/",
},
});

new Setting(containerEl)
.setName("Humanize Time Spans")
.setDesc(
"Whether to use the 'humanization' feature from moment.js, when rendering the time spans",
)
.setDesc(humanizeDescription)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.useHumanize).onChange((value) => {
this.plugin.settings.useHumanize = value;
void this.plugin.saveSettings();
}),
);

const calloutsDescription = new DocumentFragment();
calloutsDescription.textContent =
"Use callouts to render note previews, using their styles based on current theme. ";
calloutsDescription.createEl("a", {
text: "More info",
attr: {
href: "https://help.obsidian.md/Editing+and+formatting/Callouts",
},
});

new Setting(containerEl)
.setName("Use Obsidian callouts for note previews")
.setDesc(
"Use callouts to render note previews, using their styles based on current theme. More info: https://help.obsidian.md/Editing+and+formatting/Callouts",
)
.setDesc(calloutsDescription)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.useCallout).onChange((value) => {
this.plugin.settings.useCallout = value;
Expand All @@ -152,8 +169,8 @@ export class SettingsTab extends PluginSettingTab {

if (!this.plugin.settings.useCallout) {
new Setting(containerEl)
.setName("Use quote element to render note previews")
.setDesc("Use `<blockquote>` element to render note previews")
.setName("Use quote element for note previews")
.setDesc("Format note previews using the HTML quote element")
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.useQuote).onChange((value) => {
this.plugin.settings.useQuote = value;
Expand Down Expand Up @@ -193,7 +210,7 @@ export class SettingsTab extends PluginSettingTab {

new Setting(containerEl)
.setName("Open in new pane")
.setDesc("Open the notes in a new pane/tab")
.setDesc("Open the notes in a new pane/tab by default")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.openInNewPane)
Expand All @@ -202,5 +219,19 @@ export class SettingsTab extends PluginSettingTab {
void this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName("Use notifications")
.setDesc(
"Use notifications (inside Obsidian) to let you know, when there are new journal entries to review. This will happen when Obsidian is focused and it's a new day.",
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.useNotifications)
.onChange((value) => {
this.plugin.settings.useNotifications = value;
void this.plugin.saveSettings();
});
});
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"2.0.8": "0.15.0",
"2.1.0": "0.15.0",
"2.1.1": "0.15.0",
"2.2.0": "0.15.0"
"2.2.0": "0.15.0",
"2.3.0": "0.15.0"
}

0 comments on commit 6c0cd8f

Please sign in to comment.