Skip to content

Commit

Permalink
Merge branch 'fix/randomReview' into release/1.12.5.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Newdea committed Sep 2, 2024
2 parents afaa395 + 5914fec commit a8fc2be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
29 changes: 19 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export default class SRPlugin extends Plugin {

async onload(): Promise<void> {
SRPlugin._instance = this;
Iadapter.create(this.app);
await this.loadPluginData();
this.easeByPath = new NoteEaseList(this.data.settings);
this.questionPostponementList = new QuestionPostponementList(
Expand All @@ -157,7 +158,6 @@ export default class SRPlugin extends Plugin {
if (isVersionNewerThanOther(PLUGIN_VERSION, this.data.settings.previousRelease)) {
new ReleaseNotes(this.app, this, obsidianJustInstalled ? null : PLUGIN_VERSION).open();
}
Iadapter.create(this.app);

const settings = this.data.settings;
this.algorithm = algorithms[settings.algorithm];
Expand Down Expand Up @@ -188,7 +188,15 @@ export default class SRPlugin extends Plugin {
registerTrackFileEvents(this);

if (this.data.settings.dataLocation !== DataLocation.SaveOnNoteFile) {
this.registerInterval(window.setInterval(() => this.store.save(), 5 * 60 * 1000));
this.registerInterval(
window.setInterval(
async () => {
await this.sync();
this.store.save();
},
5 * 60 * 1000,
),
);
}

this.statusBar = this.addStatusBarItem();
Expand Down Expand Up @@ -867,11 +875,11 @@ export default class SRPlugin extends Plugin {
}
};

index = IReviewNote.getNextDueNoteIndex(
deck.dueNotesCount,
this.data.settings.openRandomNote,
);
if (mqs.isDue && index >= 0) {
if (mqs.isDue && deck.dueNotesCount > 0) {
index = IReviewNote.getNextNoteIndex(
deck.dueNotesCount,
this.data.settings.openRandomNote,
);
await this.app.workspace.getLeaf().openFile(deck.scheduledNotes[index].note);
item = deck.scheduledNotes[index].item;
fShowItemInfo(item, "scheduledNoes index: " + index);
Expand All @@ -891,9 +899,10 @@ export default class SRPlugin extends Plugin {
}
}
if (!mqs.isDue && deck.newNotes.length > 0) {
const index = this.data.settings.openRandomNote
? Math.floor(Math.random() * deck.newNotes.length)
: 0;
const index = IReviewNote.getNextNoteIndex(
deck.newNotes.length,
this.data.settings.openRandomNote,
);
await this.app.workspace.getLeaf().openFile(deck.newNotes[index].note);
item = deck.newNotes[index].item;
fShowItemInfo(item, "newNotes index:" + index);
Expand Down
9 changes: 3 additions & 6 deletions src/reviewNote/review-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,13 @@ export abstract class IReviewNote {
}
}

static getNextDueNoteIndex(NotesCount: number, openRandomNote: boolean = false) {
let index = -1;
static getNextNoteIndex(NotesCount: number, openRandomNote: boolean = false) {
let index = 0;

if (NotesCount < 1) {
return -1;
}
if (!openRandomNote) {
return 0;
} else {
index = Math.floor(Math.random() * NotesCount);
index = Math.floor(Math.random() * (NotesCount - 0.1)); // avoid conner case: index == notesCount;
}
return index;
}
Expand Down

0 comments on commit a8fc2be

Please sign in to comment.