Skip to content

Commit

Permalink
Filter using tempDueDate
Browse files Browse the repository at this point in the history
  • Loading branch information
goyalmunish committed Feb 4, 2023
1 parent 038c8da commit af8f85a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repo_name := $(notdir ${repo_path})

.PHONY: gobuild
gobuild:
go build -v ./...
go build -v -x -o bin/ ./...

.PHONY: run
run:
Expand Down
9 changes: 5 additions & 4 deletions internal/model/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions internal/model/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/model/notes_by_due_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
1 change: 1 addition & 0 deletions internal/model/notes_by_due_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 11 additions & 9 deletions internal/model/reminder_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit af8f85a

Please sign in to comment.