From af8f85ad35694396fd764a200e70572443bd0e79 Mon Sep 17 00:00:00 2001 From: Munish Goyal Date: Sat, 4 Feb 2023 21:55:04 +1100 Subject: [PATCH] Filter using tempDueDate --- Makefile | 2 +- internal/model/note.go | 9 +++++---- internal/model/notes.go | 7 +++++++ internal/model/notes_by_due_date.go | 2 +- internal/model/notes_by_due_date_test.go | 1 + internal/model/reminder_data.go | 20 +++++++++++--------- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 6917e42..63803a5 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ repo_name := $(notdir ${repo_path}) .PHONY: gobuild gobuild: - go build -v ./... + go build -v -x -o bin/ ./... .PHONY: run run: diff --git a/internal/model/note.go b/internal/model/note.go index b127617..65ff5cc 100644 --- a/internal/model/note.go +++ b/internal/model/note.go @@ -25,10 +25,11 @@ type Note struct { // Status can be "pending", "done", or "suspended". // The "pending" status is special, and notes marked with it show up everywhere, whereas // the nodes marked with other status show up only under "Search" or their dedicated menu. - Status NoteStatus `json:"status"` - TagIds []int `json:"tag_ids"` - IsMain bool `json:"is_main"` - CompleteBy int64 `json:"complete_by"` + Status NoteStatus `json:"status"` + TagIds []int `json:"tag_ids"` + IsMain bool `json:"is_main"` + CompleteBy int64 `json:"complete_by"` + tempDueDate int64 BaseStruct } diff --git a/internal/model/notes.go b/internal/model/notes.go index 9788e85..2531b4b 100644 --- a/internal/model/notes.go +++ b/internal/model/notes.go @@ -41,6 +41,13 @@ func (notes Notes) ExternalTexts(maxStrLen int, repeatAnnuallyTagId int, repeatM return allTexts } +// PopulateTempDueDate popultes tempDueDate field of note from its CompleteBy field. +func (notes Notes) PopulateTempDueDate() { + for _, note := range notes { + note.tempDueDate = note.CompleteBy + } +} + // WithStatus filters-in notes with given status (such as "pending" status). // It returns empty Notes if no matching Note is found (even when given status doesn't exist). func (notes Notes) WithStatus(status NoteStatus) Notes { diff --git a/internal/model/notes_by_due_date.go b/internal/model/notes_by_due_date.go index bbd6f9a..1af5792 100644 --- a/internal/model/notes_by_due_date.go +++ b/internal/model/notes_by_due_date.go @@ -9,4 +9,4 @@ type NotesByDueDate []*Note func (c NotesByDueDate) Len() int { return len(c) } func (c NotesByDueDate) Swap(i, j int) { c[i], c[j] = c[j], c[i] } -func (c NotesByDueDate) Less(i, j int) bool { return c[i].CompleteBy < c[j].CompleteBy } +func (c NotesByDueDate) Less(i, j int) bool { return c[i].tempDueDate < c[j].tempDueDate } diff --git a/internal/model/notes_by_due_date_test.go b/internal/model/notes_by_due_date_test.go index 26a201f..ec13354 100644 --- a/internal/model/notes_by_due_date_test.go +++ b/internal/model/notes_by_due_date_test.go @@ -14,6 +14,7 @@ func TestNotesByDueDate(t *testing.T) { notes = append(notes, &model.Note{Text: "2", Status: model.NoteStatus_Pending, BaseStruct: model.BaseStruct{UpdatedAt: 1600000004}, CompleteBy: 1800000004}) notes = append(notes, &model.Note{Text: "3", Status: model.NoteStatus_Done, BaseStruct: model.BaseStruct{UpdatedAt: 1600000003}, CompleteBy: 1800000002}) notes = append(notes, &model.Note{Text: "4", Status: model.NoteStatus_Done, BaseStruct: model.BaseStruct{UpdatedAt: 1600000002}, CompleteBy: 1800000001}) + model.Notes(notes).PopulateTempDueDate() sort.Sort(model.NotesByDueDate(notes)) var gotTexts []string for _, value := range notes { diff --git a/internal/model/reminder_data.go b/internal/model/reminder_data.go index 4298cdc..f2778fc 100644 --- a/internal/model/reminder_data.go +++ b/internal/model/reminder_data.go @@ -428,32 +428,34 @@ func (rd *ReminderData) NotesApprachingDueDate(view string) Notes { // assuming there are at least 100 notes (on average) currentNotes := make([]*Note, 0, 100) repeatTagIDs := rd.TagIdsForGroup("repeat") + // populate tempDueDate + pendingNotes.PopulateTempDueDate() // populating currentNotes for _, note := range pendingNotes { noteIDsWithRepeat := utils.GetCommonMembersOfSlices(note.TagIds, repeatTagIDs) // first process notes WITHOUT tag with group "repeat" // start showing such notes 7 days in advance from their due date, and until they are marked done - minDay := note.CompleteBy - 7*24*60*60 + minDay := note.tempDueDate - 7*24*60*60 if view == "long" { - minDay = note.CompleteBy - 365*24*60*60 + minDay = note.tempDueDate - 365*24*60*60 } currentTimestamp := utils.CurrentUnixTimestamp() - if (len(noteIDsWithRepeat) == 0) && (note.CompleteBy != 0) && (currentTimestamp >= minDay) { + if (len(noteIDsWithRepeat) == 0) && (note.tempDueDate != 0) && (currentTimestamp >= minDay) { currentNotes = append(currentNotes, note) } // check notes with tag with group "repeat" // start showing notes with "repeat-annually" 7 days in advance // start showing notes with "repeat-monthly" 3 days in advance // don't show such notes after their due date is past by 2 day - if (len(noteIDsWithRepeat) > 0) && (note.CompleteBy != 0) { + if (len(noteIDsWithRepeat) > 0) && (note.tempDueDate != 0) { // check for repeat-annually tag - // note: for the CompleteBy date of the note, we accept only date + // note: for the tempDueDate date of the note, we accept only date // so, even if there is a time element recorded the the timestamp, // we ignore it repeatAnnuallyTag := rd.TagFromSlug("repeat-annually") repeatMonthlyTag := rd.TagFromSlug("repeat-monthly") if (repeatAnnuallyTag != nil) && utils.IsMemberOfSlice(repeatAnnuallyTag.Id, note.TagIds) { - _, noteMonth, noteDay := utils.UnixTimestampToTime(note.CompleteBy).Date() + _, noteMonth, noteDay := utils.UnixTimestampToTime(note.tempDueDate).Date() noteTimestampCurrent := utils.UnixTimestampForCorrespondingCurrentYear(int(noteMonth), noteDay) noteTimestampPrevious := noteTimestampCurrent - 365*24*60*60 noteTimestampNext := noteTimestampCurrent + 365*24*60*60 @@ -464,14 +466,14 @@ func (rd *ReminderData) NotesApprachingDueDate(view string) Notes { } shouldDisplay, matchingTimestamp := utils.MatchedTimestamp(noteTimestampCurrent, noteTimestampPrevious, noteTimestampNext, daysBefore, daysAfter) // temporarity update note's timestamp - note.CompleteBy = matchingTimestamp + note.tempDueDate = matchingTimestamp if shouldDisplay { currentNotes = append(currentNotes, note) } } // check for repeat-monthly tag if (repeatMonthlyTag != nil) && utils.IsMemberOfSlice(repeatMonthlyTag.Id, note.TagIds) { - _, _, noteDay := utils.UnixTimestampToTime(note.CompleteBy).Date() + _, _, noteDay := utils.UnixTimestampToTime(note.tempDueDate).Date() noteTimestampCurrent := utils.UnixTimestampForCorrespondingCurrentYearMonth(noteDay) noteTimestampPrevious := noteTimestampCurrent - 30*24*60*60 noteTimestampNext := noteTimestampCurrent + 30*24*60*60 @@ -482,7 +484,7 @@ func (rd *ReminderData) NotesApprachingDueDate(view string) Notes { } shouldDisplay, matchingTimestamp := utils.MatchedTimestamp(noteTimestampCurrent, noteTimestampPrevious, noteTimestampNext, daysBefore, daysAfter) // temporarity update note's timestamp - note.CompleteBy = matchingTimestamp + note.tempDueDate = matchingTimestamp if shouldDisplay { currentNotes = append(currentNotes, note) }