Get it all working

This commit is contained in:
Brian Buller 2023-08-23 09:55:38 -05:00
parent 7661923650
commit 84732f16b9
2 changed files with 30 additions and 23 deletions

44
sort.go
View File

@ -8,33 +8,39 @@ import (
// Flags for defining sort element and order. // Flags for defining sort element and order.
const ( const (
SORT_PRIORITY_ASC = iota SortPriorityAsc = iota
SORT_PRIORITY_DESC SortPriorityDesc
SORT_CREATED_DATE_ASC SortCreatedDateAsc
SORT_CREATED_DATE_DESC SortCreatedDateDesc
SORT_COMPLETED_DATE_ASC SortCompletedDateAsc
SORT_COMPLETED_DATE_DESC SortCompletedDateDesc
SORT_DUE_DATE_ASC SortDueDateAsc
SORT_DUE_DATE_DESC SortDueDateDesc
) )
// Sort allows a TodoList to be sorted by certain predefined fields. // Sort allows a TodoList to be sorted by certain predefined fields.
// See constants SORT_* for fields and sort order. // See constants SORT_* for fields and sort order.
func (todolist *TodoList) Sort(sortFlag int) error { func (todolist *TodoList) Sort(sortFlag int) error {
switch sortFlag { switch sortFlag {
case SORT_PRIORITY_ASC, SORT_PRIORITY_DESC: case SortPriorityAsc, SortPriorityDesc:
todolist.sortByPriority(sortFlag) todolist.sortByPriority(sortFlag)
case SORT_CREATED_DATE_ASC, SORT_CREATED_DATE_DESC: case SortCreatedDateAsc, SortCreatedDateDesc:
todolist.sortByCreatedDate(sortFlag) todolist.sortByCreatedDate(sortFlag)
case SORT_COMPLETED_DATE_ASC, SORT_COMPLETED_DATE_DESC: case SortCompletedDateAsc, SortCompletedDateDesc:
todolist.sortByCompletedDate(sortFlag) todolist.sortByCompletedDate(sortFlag)
case SORT_DUE_DATE_ASC, SORT_DUE_DATE_DESC: case SortDueDateAsc, SortDueDateDesc:
todolist.sortByDueDate(sortFlag) todolist.sortByDueDate(sortFlag)
default: default:
return errors.New("unrecognized sort option") return errors.New("unrecognized sort option")
} }
return nil return nil
} }
func (todolist *TodoList) refresh() {
todolist.Sort(todolist.sortFlag)
for i, t := range todolist.todos {
t.Id = i
}
}
type todolistSort struct { type todolistSort struct {
todolists TodoList todolists TodoList
@ -42,15 +48,15 @@ type todolistSort struct {
} }
func (ts *todolistSort) Len() int { func (ts *todolistSort) Len() int {
return len(ts.todolists) return len(ts.todolists.todos)
} }
func (ts *todolistSort) Swap(l, r int) { func (ts *todolistSort) Swap(l, r int) {
ts.todolists[l], ts.todolists[r] = ts.todolists[r], ts.todolists[l] ts.todolists.todos[l], ts.todolists.todos[r] = ts.todolists.todos[r], ts.todolists.todos[l]
} }
func (ts *todolistSort) Less(l, r int) bool { func (ts *todolistSort) Less(l, r int) bool {
return ts.by(&ts.todolists[l], &ts.todolists[r]) return ts.by(ts.todolists.todos[l], ts.todolists.todos[r])
} }
func (todolist *TodoList) sortBy(by func(t1, t2 *Todo) bool) *TodoList { func (todolist *TodoList) sortBy(by func(t1, t2 *Todo) bool) *TodoList {
@ -64,7 +70,7 @@ func (todolist *TodoList) sortBy(by func(t1, t2 *Todo) bool) *TodoList {
func (todolist *TodoList) sortByPriority(order int) *TodoList { func (todolist *TodoList) sortByPriority(order int) *TodoList {
todolist.sortBy(func(t1, t2 *Todo) bool { todolist.sortBy(func(t1, t2 *Todo) bool {
if order == SORT_PRIORITY_ASC { // ASC if order == SortPriorityAsc { // ASC
if t1.HasPriority() && t2.HasPriority() { if t1.HasPriority() && t2.HasPriority() {
return t1.Priority < t2.Priority return t1.Priority < t2.Priority
} }
@ -95,21 +101,21 @@ func sortByDate(asc bool, hasDate1, hasDate2 bool, date1, date2 time.Time) bool
func (todolist *TodoList) sortByCreatedDate(order int) *TodoList { func (todolist *TodoList) sortByCreatedDate(order int) *TodoList {
todolist.sortBy(func(t1, t2 *Todo) bool { todolist.sortBy(func(t1, t2 *Todo) bool {
return sortByDate(order == SORT_CREATED_DATE_ASC, t1.HasCreatedDate(), t2.HasCreatedDate(), t1.CreatedDate, t2.CreatedDate) return sortByDate(order == SortCreatedDateAsc, t1.HasCreatedDate(), t2.HasCreatedDate(), t1.CreatedDate, t2.CreatedDate)
}) })
return todolist return todolist
} }
func (todolist *TodoList) sortByCompletedDate(order int) *TodoList { func (todolist *TodoList) sortByCompletedDate(order int) *TodoList {
todolist.sortBy(func(t1, t2 *Todo) bool { todolist.sortBy(func(t1, t2 *Todo) bool {
return sortByDate(order == SORT_COMPLETED_DATE_ASC, t1.HasCompletedDate(), t2.HasCompletedDate(), t1.CompletedDate, t2.CompletedDate) return sortByDate(order == SortCompletedDateAsc, t1.HasCompletedDate(), t2.HasCompletedDate(), t1.CompletedDate, t2.CompletedDate)
}) })
return todolist return todolist
} }
func (todolist *TodoList) sortByDueDate(order int) *TodoList { func (todolist *TodoList) sortByDueDate(order int) *TodoList {
todolist.sortBy(func(t1, t2 *Todo) bool { todolist.sortBy(func(t1, t2 *Todo) bool {
return sortByDate(order == SORT_DUE_DATE_ASC, t1.HasDueDate(), t2.HasDueDate(), t1.DueDate, t2.DueDate) return sortByDate(order == SortDueDateAsc, t1.HasDueDate(), t2.HasDueDate(), t1.DueDate, t2.DueDate)
}) })
return todolist return todolist
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"sort"
"strings" "strings"
) )
@ -51,7 +52,7 @@ func (todolist *TodoList) GetContexts() []string {
} }
} }
} }
sort.String(ret) sort.Strings(ret)
return ret return ret
} }
func (todolist *TodoList) GetProjects() []string { func (todolist *TodoList) GetProjects() []string {
@ -113,7 +114,7 @@ func (todolist *TodoList) GetTagValuesForKey(key string) []string {
func (todolist *TodoList) GetIncompleteTasks() *TodoList { func (todolist *TodoList) GetIncompleteTasks() *TodoList {
t := *NewTodoList() t := *NewTodoList()
for _, v := range todolist.todos { for _, v := range todolist.todos {
if !v.Complete { if !v.Completed {
t.todos = append(t.todos, v) t.todos = append(t.todos, v)
} }
} }
@ -138,9 +139,9 @@ func (todolist *TodoList) AddTodo(todo *Todo) {
// AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values. // AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values.
func (todolist *TodoList) AddTodos(todos []*Todo) { func (todolist *TodoList) AddTodos(todos []*Todo) {
todolist.todos = append(todolist.todos, todos...) todolist.todos = append(todolist.todos, todos...)
todolist.Sort(SORT_START_DATE_ASC) todolist.Sort(SORT_COMPLETED_DATE_DESC)
} }
func (todolist *TodoList) Combine(other *TodoList) { todolist.AddTodo(other.todos) } func (todolist *TodoList) Combine(other *TodoList) { todolist.AddTodos(other.todos) }
// GetTodo returns the Todo with the given todo 'id' from the TodoList. // GetTodo returns the Todo with the given todo 'id' from the TodoList.
// Returns an error if Todo could not be found. // Returns an error if Todo could not be found.