2014-01-03 20:27:11 +00:00
|
|
|
package todotxt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sort"
|
2014-01-04 00:43:27 +00:00
|
|
|
"time"
|
2014-01-03 20:27:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Flags for defining sort element and order.
|
|
|
|
const (
|
2023-08-23 14:55:38 +00:00
|
|
|
SortPriorityAsc = iota
|
|
|
|
SortPriorityDesc
|
|
|
|
SortCreatedDateAsc
|
|
|
|
SortCreatedDateDesc
|
|
|
|
SortCompletedDateAsc
|
|
|
|
SortCompletedDateDesc
|
|
|
|
SortDueDateAsc
|
|
|
|
SortDueDateDesc
|
2014-01-03 20:27:11 +00:00
|
|
|
)
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
// Sort allows a TodoList to be sorted by certain predefined fields.
|
2014-01-03 20:27:11 +00:00
|
|
|
// See constants SORT_* for fields and sort order.
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) Sort(sortFlag int) error {
|
2014-01-03 20:27:11 +00:00
|
|
|
switch sortFlag {
|
2023-08-23 14:55:38 +00:00
|
|
|
case SortPriorityAsc, SortPriorityDesc:
|
2023-08-23 14:17:16 +00:00
|
|
|
todolist.sortByPriority(sortFlag)
|
2023-08-23 14:55:38 +00:00
|
|
|
case SortCreatedDateAsc, SortCreatedDateDesc:
|
2023-08-23 14:17:16 +00:00
|
|
|
todolist.sortByCreatedDate(sortFlag)
|
2023-08-23 14:55:38 +00:00
|
|
|
case SortCompletedDateAsc, SortCompletedDateDesc:
|
2023-08-23 14:17:16 +00:00
|
|
|
todolist.sortByCompletedDate(sortFlag)
|
2023-08-23 14:55:38 +00:00
|
|
|
case SortDueDateAsc, SortDueDateDesc:
|
2023-08-23 14:17:16 +00:00
|
|
|
todolist.sortByDueDate(sortFlag)
|
2014-01-03 20:27:11 +00:00
|
|
|
default:
|
2014-01-04 13:13:37 +00:00
|
|
|
return errors.New("unrecognized sort option")
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-23 14:55:38 +00:00
|
|
|
func (todolist *TodoList) refresh() {
|
2023-08-23 16:10:03 +00:00
|
|
|
todolist.Sort(todolist.SortFlag)
|
|
|
|
for i, t := range todolist.Todos {
|
2023-08-23 14:55:38 +00:00
|
|
|
t.Id = i
|
|
|
|
}
|
|
|
|
}
|
2014-01-03 20:27:11 +00:00
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
type todolistSort struct {
|
|
|
|
todolists TodoList
|
|
|
|
by func(t1, t2 *Todo) bool
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (ts *todolistSort) Len() int {
|
2023-08-23 16:10:03 +00:00
|
|
|
return len(ts.todolists.Todos)
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (ts *todolistSort) Swap(l, r int) {
|
2023-08-23 16:10:03 +00:00
|
|
|
ts.todolists.Todos[l], ts.todolists.Todos[r] = ts.todolists.Todos[r], ts.todolists.Todos[l]
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (ts *todolistSort) Less(l, r int) bool {
|
2023-08-23 16:10:03 +00:00
|
|
|
return ts.by(ts.todolists.Todos[l], ts.todolists.Todos[r])
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) sortBy(by func(t1, t2 *Todo) bool) *TodoList {
|
|
|
|
ts := &todolistSort{
|
|
|
|
todolists: *todolist,
|
2014-01-03 20:27:11 +00:00
|
|
|
by: by,
|
|
|
|
}
|
|
|
|
sort.Sort(ts)
|
2023-08-23 14:17:16 +00:00
|
|
|
return todolist
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) sortByPriority(order int) *TodoList {
|
|
|
|
todolist.sortBy(func(t1, t2 *Todo) bool {
|
2023-08-23 14:55:38 +00:00
|
|
|
if order == SortPriorityAsc { // ASC
|
2014-01-03 20:27:11 +00:00
|
|
|
if t1.HasPriority() && t2.HasPriority() {
|
|
|
|
return t1.Priority < t2.Priority
|
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
return t1.HasPriority()
|
|
|
|
}
|
|
|
|
// DESC
|
|
|
|
if t1.HasPriority() && t2.HasPriority() {
|
|
|
|
return t1.Priority > t2.Priority
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
return !t1.HasPriority()
|
2014-01-03 20:27:11 +00:00
|
|
|
})
|
2023-08-23 14:17:16 +00:00
|
|
|
return todolist
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 00:43:27 +00:00
|
|
|
func sortByDate(asc bool, hasDate1, hasDate2 bool, date1, date2 time.Time) bool {
|
|
|
|
if asc { // ASC
|
|
|
|
if hasDate1 && hasDate2 {
|
|
|
|
return date1.Before(date2)
|
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
return hasDate2
|
|
|
|
}
|
|
|
|
// DESC
|
|
|
|
if hasDate1 && hasDate2 {
|
|
|
|
return date1.After(date2)
|
2014-01-04 00:43:27 +00:00
|
|
|
}
|
2014-01-04 13:13:37 +00:00
|
|
|
return !hasDate2
|
2014-01-04 00:43:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) sortByCreatedDate(order int) *TodoList {
|
|
|
|
todolist.sortBy(func(t1, t2 *Todo) bool {
|
2023-08-23 14:55:38 +00:00
|
|
|
return sortByDate(order == SortCreatedDateAsc, t1.HasCreatedDate(), t2.HasCreatedDate(), t1.CreatedDate, t2.CreatedDate)
|
2014-01-03 20:27:11 +00:00
|
|
|
})
|
2023-08-23 14:17:16 +00:00
|
|
|
return todolist
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) sortByCompletedDate(order int) *TodoList {
|
|
|
|
todolist.sortBy(func(t1, t2 *Todo) bool {
|
2023-08-23 14:55:38 +00:00
|
|
|
return sortByDate(order == SortCompletedDateAsc, t1.HasCompletedDate(), t2.HasCompletedDate(), t1.CompletedDate, t2.CompletedDate)
|
2014-01-03 20:27:11 +00:00
|
|
|
})
|
2023-08-23 14:17:16 +00:00
|
|
|
return todolist
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:17:16 +00:00
|
|
|
func (todolist *TodoList) sortByDueDate(order int) *TodoList {
|
|
|
|
todolist.sortBy(func(t1, t2 *Todo) bool {
|
2023-08-23 14:55:38 +00:00
|
|
|
return sortByDate(order == SortDueDateAsc, t1.HasDueDate(), t2.HasDueDate(), t1.DueDate, t2.DueDate)
|
2014-01-03 20:27:11 +00:00
|
|
|
})
|
2023-08-23 14:17:16 +00:00
|
|
|
return todolist
|
2014-01-03 20:27:11 +00:00
|
|
|
}
|