gime/model.go

131 lines
2.8 KiB
Go
Raw Normal View History

2019-02-21 02:44:07 +00:00
package main
import (
2019-02-22 23:56:15 +00:00
"strings"
2019-02-21 02:44:07 +00:00
"time"
timertxt "github.com/br0xen/go-timertxt"
2019-02-21 02:44:07 +00:00
)
func (a *AppState) SetTimerFinished(id int, end time.Time) error {
var t *timertxt.Timer
var err error
if t, err = a.TimerList.GetTimer(id); err != nil {
return err
}
t.FinishDate = end
t.Finished = true
return nil
}
2019-02-22 23:56:15 +00:00
func (a *AppState) addTimer(timerString string) error {
t, err := timertxt.ParseTimer(timerString)
if err != nil {
return err
}
if t.StartDate.IsZero() {
t.StartDate = time.Now()
}
a.TimerList.AddTimer(t)
return a.WriteList()
}
func (a *AppState) archiveTimer(id int) error {
var err error
var timer *timertxt.Timer
if timer, err = a.TimerList.GetTimer(id); err != nil {
return err
}
if err := a.TimerList.ArchiveTimerToFile(*timer, app.getDoneFile()); err != nil {
return err
}
a.TimerList.RemoveTimer(*timer)
return a.WriteList()
}
func (a *AppState) unarchiveTimer(id int) error {
var err error
var timer *timertxt.Timer
if timer, err = a.DoneList.GetTimer(id); err != nil {
return err
}
a.TimerList.AddTimer(timer)
if err = a.WriteList(); err != nil {
return err
}
a.DoneList.RemoveTimer(*timer)
return a.WriteDoneList()
}
2019-02-21 02:44:07 +00:00
func (a *AppState) LoadTimerList() error {
var err error
var tl timertxt.TimerList
tl, err = timertxt.LoadFromFilename(a.getTimerFile())
tl.Sort(timertxt.SORT_UNFINISHED_START)
a.TimerList = &tl
return err
}
func (a *AppState) WriteList() error {
return a.TimerList.WriteToFilename(a.getTimerFile())
}
func (a *AppState) LoadDoneList() error {
var err error
var tl timertxt.TimerList
tl, err = timertxt.LoadFromFilename(a.getDoneFile())
a.DoneList = &tl
return err
}
func (a *AppState) WriteDoneList() error {
return a.DoneList.WriteToFilename(a.getDoneFile())
}
2019-02-22 23:56:15 +00:00
func (a *AppState) getFilterPredicate(filter string) func(timertxt.Timer) bool {
var predicates []func(timertxt.Timer) 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 strings.HasPrefix(part, "@") {
predicates = append(predicates, func(t timertxt.Timer) bool {
for _, v := range t.Contexts {
if "@"+v == part {
return true
}
}
return false
})
} else if strings.HasPrefix(part, "+") {
predicates = append(predicates, func(t timertxt.Timer) bool {
for _, v := range t.Projects {
if "+"+v == part {
return true
}
}
return false
})
} else {
predicates = append(predicates, func(t timertxt.Timer) bool {
val := t.Original
if !checkCase {
val = strings.ToLower(t.Original)
}
return strings.Contains(val, part)
})
}
}
return func(t timertxt.Timer) bool {
for _, v := range predicates {
if v(t) {
return true
}
}
return false
}
}