gask-old/model.go

240 lines
5.2 KiB
Go
Raw Normal View History

2019-02-15 17:15:26 +00:00
package main
import (
"sort"
2019-02-15 17:15:26 +00:00
"strings"
"time"
2021-03-17 17:37:26 +00:00
todotxt "git.bullercodeworks.com/brian/go-todotxt"
2019-02-15 17:15:26 +00:00
)
2019-02-26 19:22:37 +00:00
// diskListChanged returns true if the task list in todo.txt
// is different than what we have previously loaded.
func (a *AppState) diskListChanged() bool {
if !a.taskListLoaded {
return false
}
curr, err := todotxt.LoadFromFilename(a.getTodoFile())
if err != nil {
return false
}
return curr.String() != a.TaskList.String()
}
// diskDoneListChanged returns true if the task list in done.txt
// is different than what we have previously loaded.
func (a *AppState) diskDoneListChanged() bool {
if !a.doneListLoaded {
return false
}
curr, err := todotxt.LoadFromFilename(a.getDoneFile())
if err != nil {
return false
}
return curr.String() != a.DoneList.String()
}
2019-02-15 17:15:26 +00:00
func (a *AppState) addTask(taskString string) error {
2019-02-26 19:22:37 +00:00
if a.diskListChanged() {
return a.e(ResStrListChanged)
}
2019-02-15 17:15:26 +00:00
t, err := todotxt.ParseTask(taskString)
if err != nil {
return err
}
if t.CreatedDate.IsZero() {
t.CreatedDate = time.Now()
}
a.TaskList.AddTask(t)
return a.WriteList()
2019-02-15 17:15:26 +00:00
}
2019-03-14 15:29:33 +00:00
func (a *AppState) saveTask(t *todotxt.Task) error {
lt, err := a.TaskList.GetTask(t.Id)
if err != nil {
return err
}
lt.Todo = t.Todo
lt.Priority = t.Priority
lt.Projects = t.Projects
lt.Contexts = t.Contexts
lt.AdditionalTags = t.AdditionalTags
return a.WriteList()
}
2019-02-15 17:15:26 +00:00
func (a *AppState) toggleTaskComplete(id int) error {
2019-02-26 19:22:37 +00:00
if a.diskListChanged() {
return a.e(ResStrListChanged)
}
2019-02-15 17:15:26 +00:00
var task *todotxt.Task
var err error
if task, err = a.TaskList.GetTask(id); err != nil {
return err
}
if task.Completed {
task.Reopen()
} else {
task.Complete()
}
return a.WriteList()
2019-02-15 17:15:26 +00:00
}
func (a *AppState) archiveTask(id int) error {
2019-02-26 19:22:37 +00:00
if a.diskListChanged() {
return a.e(ResStrListChanged)
}
2019-02-15 17:15:26 +00:00
var err error
var task *todotxt.Task
if task, err = a.TaskList.GetTask(id); err != nil {
return err
}
2019-02-26 19:22:37 +00:00
if err := a.TaskList.ArchiveTaskToFile(*task, a.getDoneFile()); err != nil {
return err
}
a.TaskList.RemoveTask(*task)
return a.WriteList()
}
func (a *AppState) unarchiveTask(id int) error {
2019-02-26 19:22:37 +00:00
if a.diskListChanged() {
return a.e(ResStrListChanged)
}
var err error
var task *todotxt.Task
if task, err = a.DoneList.GetTask(id); err != nil {
return err
}
a.TaskList.AddTask(task)
if err = a.WriteList(); err != nil {
return err
}
a.DoneList.RemoveTask(*task)
return a.WriteDoneList()
2019-02-15 17:15:26 +00:00
}
func (a *AppState) getFilterPredicate(filter string) func(todotxt.Task) bool {
var predicates []func(todotxt.Task) bool
// If none of the 'filter' is in upper-case, do a case-insensitive filter
checkCase := true
if strings.ToLower(filter) == filter {
checkCase = false
}
filterParts := strings.Split(filter, " ")
for _, part := range filterParts {
if part == ":x" {
predicates = append(predicates, func(t todotxt.Task) bool {
return t.Completed
})
} else if part == ":-x" || part == ":o" {
predicates = append(predicates, func(t todotxt.Task) bool {
return !t.Completed
})
}
2019-02-15 17:15:26 +00:00
if strings.HasPrefix(part, "@") {
predicates = append(predicates, func(t todotxt.Task) bool {
for _, v := range t.Contexts {
if "@"+v == part {
return true
}
}
return false
})
} else if strings.HasPrefix(part, "+") {
predicates = append(predicates, func(t todotxt.Task) bool {
for _, v := range t.Projects {
if "+"+v == part {
return true
}
}
return false
})
} else {
predicates = append(predicates, func(t todotxt.Task) bool {
val := t.Original
if !checkCase {
val = strings.ToLower(t.Original)
}
return strings.Contains(val, part)
})
}
}
return func(t todotxt.Task) bool {
for _, v := range predicates {
if v(t) {
return true
}
}
return false
}
}
func (a *AppState) LoadTaskList() error {
var err error
var tl todotxt.TaskList
tl, err = todotxt.LoadFromFilename(a.getTodoFile())
a.TaskList = &tl
2019-02-26 19:22:37 +00:00
a.taskListLoaded = true
2019-02-15 17:15:26 +00:00
return err
}
func (a *AppState) LoadDoneList() error {
var err error
var tl todotxt.TaskList
tl, err = todotxt.LoadFromFilename(a.getDoneFile())
a.DoneList = &tl
2019-02-26 19:22:37 +00:00
a.doneListLoaded = true
2019-02-15 17:15:26 +00:00
return err
}
func (a *AppState) WriteList() error {
2019-02-26 19:22:37 +00:00
if !a.taskListLoaded {
return a.e(ResStrTaskListNotLoaded)
}
2019-02-15 17:15:26 +00:00
return a.TaskList.WriteToFilename(a.getTodoFile())
}
func (a *AppState) WriteDoneList() error {
2019-02-26 19:22:37 +00:00
if !a.doneListLoaded {
return a.e(ResStrDoneListNotLoaded)
}
2019-02-15 17:15:26 +00:00
return a.DoneList.WriteToFilename(a.getDoneFile())
}
func (a *AppState) AdvancedPrioritySort(t1, t2 *todotxt.Task) bool {
if t1.HasPriority() && t2.HasPriority() {
return t1.Priority < t2.Priority
} else if t1.HasPriority() && !t2.HasPriority() {
return t1.Priority <= "Y"
} else if !t1.HasPriority() && t2.HasPriority() {
return "Y" <= t2.Priority
}
return t1.HasPriority()
}
type customSort struct {
list todotxt.TaskList
by func(t1, t2 *todotxt.Task) bool
}
func (c *customSort) Len() int {
return len(c.list)
}
func (c *customSort) Swap(l, r int) {
c.list[l], c.list[r] = c.list[r], c.list[l]
}
func (c *customSort) Less(l, r int) bool {
return c.by(&c.list[l], &c.list[r])
}
type By func(t1, t2 *todotxt.Task) bool
func (by By) Sort(list []todotxt.Task) {
ts := &customSort{
list: list,
by: by,
}
sort.Sort(ts)
}