Skip to content

Commit

Permalink
more improvements to noteset validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dartungar committed Mar 4, 2024
1 parent 0e8bbc8 commit 0a10aa8
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 108 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "simple-note-review",
"name": "Simple Note Review",
"version": "1.2.2",
"version": "1.2.3",
"minAppVersion": "1.1.0",
"description": "Simple, customizable plugin for easy note review, resurfacing & repetition.",
"author": "dartungar",
Expand Down
2 changes: 1 addition & 1 deletion src/UI/noteset/noteSetEditModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class NoteSetEditModal extends Modal {
this._plugin.settings.noteSets[index] = this._noteSet;
}
});
this._plugin.noteSetService.validateRules(this._noteSet);
this._plugin.noteSetService.validateRulesAndSave(this._noteSet);
this._plugin.reviewService.resetNotesetQueue(this._noteSet);
this._plugin.noteSetService.updateNoteSetDisplayNameAndDescription(this._noteSet);
this._plugin.noteSetService.updateNoteSetStats(this._noteSet);
Expand Down
2 changes: 1 addition & 1 deletion src/UI/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class SimpleNoteReviewPluginSettingsTab extends PluginSettingTab {
cb.setIcon("rotate-cw")
.setTooltip("Reset review queue and update stats for this note set")
.onClick(async () => {
await this._plugin.noteSetService.validateRules(noteSet);
await this._plugin.noteSetService.validateRulesAndSave(noteSet);
await this._plugin.reviewService.resetNotesetQueue(noteSet);
await this._plugin.noteSetService.updateNoteSetStats(noteSet);
this.display();
Expand Down
6 changes: 3 additions & 3 deletions src/UI/sidebar/sidebarView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class SimpleNoteReviewSidebarView extends ItemView {
}

async onOpen() {
//await this._plugin.noteSetService.validateAllNotesets();
await this.renderView();
}

Expand Down Expand Up @@ -197,8 +196,9 @@ export class SimpleNoteReviewSidebarView extends ItemView {
cb.setIcon("rotate-cw")
.setTooltip("reset review queue for this note set")
.onClick(async () => {
await this._plugin.noteSetService.validateRules(noteSet);
await this._plugin.noteSetService.validateRulesAndSave(noteSet);
await this._plugin.reviewService.resetNotesetQueue(noteSet);
await this.renderView();
}
);
});
Expand Down Expand Up @@ -242,7 +242,7 @@ export class SimpleNoteReviewSidebarView extends ItemView {
throw error;
}

if (this._plugin.settings.currentNoteSet !== noteSet) {
if (this._plugin.settings.currentNoteSet.id !== noteSet.id) {
this._plugin.settings.currentNoteSet = noteSet;
await this._plugin.saveSettings();
this._plugin.showNotice(
Expand Down
4 changes: 4 additions & 0 deletions src/dataview/dataviewFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class DataviewFacade {
}
}

public isDataviewInitialized(): boolean {
return this._api.index.initialized;
}

public async pages(query: string): Promise<DataArray<Record<string, any>>> {
return await this.invokeAndReinitDvCacheOnError(() => this._api.pages(query));
}
Expand Down
4 changes: 4 additions & 0 deletions src/dataview/dataviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { DataviewQueryError } from "../noteSet/noteSetService";
export class DataviewService {
private _dataviewApi = new DataviewFacade();

get isDataviewInitialized(): boolean {
return this._dataviewApi.isDataviewInitialized();
}

public async getNoteSetFiles(noteSet: INoteSet): Promise<DataArray<Record<string, any>>> {
const query = this.getOrCreateBaseDataviewQuery(noteSet);
try {
Expand Down
6 changes: 1 addition & 5 deletions src/noteSet/INoteSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface INoteSet {
}

export class EmptyNoteSet implements INoteSet {
id: number
id: undefined
sortOrder: undefined
name: "new note set"
displayName: string
Expand All @@ -38,8 +38,4 @@ export class EmptyNoteSet implements INoteSet {
stats: INoteSetStats
queue: INoteQueue
validationErrors: []

constructor(id: number) {
this.id = id;
}
}
119 changes: 74 additions & 45 deletions src/noteSet/noteSetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ export class NoteSetService {

constructor(private _app: App, private _plugin: SimpleNoteReviewPlugin) {}

public async addEmptyNoteSet() {
this._plugin.settings.noteSets.push(
new EmptyNoteSet(
this._plugin.settings.noteSets.length > 0
? Math.max(
...this._plugin.settings.noteSets.map((q) => q.id)) + 1
: 1
) // id "generation"
public async saveNoteSet(noteSet: INoteSet) {
this._plugin.settings.noteSets = this._plugin.settings.noteSets.filter(
(x) => x.id !== noteSet.id
);
await this._plugin.saveSettings();
this._plugin.settings.noteSets.push(noteSet);
this._plugin.saveSettings();
}

public async deleteNoteSet(noteSet: INoteSet) {
Expand All @@ -43,6 +39,11 @@ export class NoteSetService {
await this._plugin.saveSettings();
}

public async addEmptyNoteSet() {
const emptyNoteSet = new EmptyNoteSet();
this.saveNoteSet(emptyNoteSet);
}

public updateNoteSetDisplayNames() {
this._plugin.settings.noteSets.forEach((q) =>
this.updateNoteSetDisplayNameAndDescription(q)
Expand All @@ -55,42 +56,6 @@ export class NoteSetService {
);
}

public async updateNoteSetStats(noteSet: INoteSet): Promise<void> {
await this._noteSetInfoService.updateNoteSetStats(noteSet);
}

public async validateAllNotesets(): Promise<void> {
await Promise.all(this._plugin.settings.noteSets.map(noteset => this.validateRules(noteset)));
}

public async validateRules(noteSet: INoteSet): Promise<void> {
const validationErrors = await this.getValidationErrors(noteSet);
noteSet.validationErrors = validationErrors;
this._plugin.settings.noteSets.find(x => x.id === noteSet.id).validationErrors = validationErrors;
await this._plugin.saveSettings();
}

private async getValidationErrors(noteset: INoteSet): Promise<NotesetValidationErrors[]> {
const errors: NotesetValidationErrors[] = [];
if (!noteset.queue?.filenames?.length)
errors.push(NotesetValidationErrors.QueueEmpty);

const customDvQueryIsValid = !noteset.dataviewQuery || this._dataviewService.validateQuery(noteset.dataviewQuery);
if (!customDvQueryIsValid)
errors.push(NotesetValidationErrors.CustomDataviewIncorrect);

const constructedDvQuery = this._dataviewService.getOrCreateBaseDataviewQuery(noteset);
const constructedDvQueryIsValid = await this._dataviewService.validateQuery(constructedDvQuery);
if (!constructedDvQueryIsValid)
errors.push(NotesetValidationErrors.RulesAreIncorrect)

const queueActual = await this._dataviewService.getNoteSetFiles(noteset);
if (!queueActual.length)
errors.push(NotesetValidationErrors.RulesDoNotMatchAnyNotes);

return errors;
}

public sortNoteSets(noteSets: INoteSet[]): INoteSet[] {
// Find the highest sortOrder that is defined
const maxSortOrder = noteSets.reduce((max, note) => {
Expand All @@ -115,4 +80,68 @@ export class NoteSetService {

return filledNotes;
}

public async updateNoteSetStats(noteSet: INoteSet): Promise<void> {
await this._noteSetInfoService.updateNoteSetStats(noteSet);
}

public async validateAllNotesets(): Promise<void> {
await Promise.all(
this._plugin.settings.noteSets.map((noteset) =>
this.validateRulesAndSave(noteset)
)
);
}

public async validateRulesAndSave(noteSet: INoteSet): Promise<void> {
const validationErrors = await this.getValidationErrors(noteSet);
noteSet.validationErrors = validationErrors;

this.fixQueueEmptyError(noteSet);
await this.saveNoteSet(noteSet);
}

private async getValidationErrors(
noteset: INoteSet
): Promise<NotesetValidationErrors[]> {
const errors: NotesetValidationErrors[] = [];

if (!noteset.queue?.filenames?.length)
errors.push(NotesetValidationErrors.QueueEmpty);

const customDvQueryIsValid =
!noteset.dataviewQuery ||
(await this._dataviewService.validateQuery(noteset.dataviewQuery));
if (!customDvQueryIsValid)
errors.push(NotesetValidationErrors.CustomDataviewIncorrect);

const constructedDvQuery =
this._dataviewService.getOrCreateBaseDataviewQuery(noteset);
const constructedDvQueryIsValid =
await this._dataviewService.validateQuery(constructedDvQuery);
if (!constructedDvQueryIsValid)
errors.push(NotesetValidationErrors.RulesAreIncorrect);

if (this._dataviewService.isDataviewInitialized) {
const queueActual = await this._dataviewService.getNoteSetFiles(noteset);

if (!queueActual?.length || queueActual.length === 0) {
errors.push(NotesetValidationErrors.RulesDoNotMatchAnyNotes);
}
}
return errors;
}

private async fixQueueEmptyError(noteSet: INoteSet): Promise<void> {
if (
noteSet.validationErrors.contains(
NotesetValidationErrors.QueueEmpty
) &&
!noteSet.validationErrors.contains(
NotesetValidationErrors.RulesDoNotMatchAnyNotes
)
) {
await this._plugin.reviewService.resetNotesetQueue(noteSet);
}
}
}
Loading

0 comments on commit 0a10aa8

Please sign in to comment.