gte/internal/task/localtask.go

199 lines
4.1 KiB
Go
Raw Normal View History

2021-08-20 09:06:35 +02:00
package task
2021-08-20 13:46:56 +02:00
import (
"database/sql/driver"
"fmt"
"strconv"
2021-08-20 13:46:56 +02:00
"strings"
)
2021-09-01 06:52:21 +02:00
const (
2021-09-03 09:19:36 +02:00
STATUS_FETCHED = "fetched"
STATUS_UPDATED = "updated"
STATUS_DISPATCHED = "dispatched"
2021-09-01 06:52:21 +02:00
)
2021-08-20 09:06:35 +02:00
type LocalTask struct {
Task
LocalId int
LocalUpdate *LocalUpdate
2021-09-01 06:52:21 +02:00
LocalStatus string
2021-08-20 09:06:35 +02:00
}
2021-08-20 11:27:12 +02:00
2021-08-24 07:26:58 +02:00
func (lt *LocalTask) HasUpdate() bool {
return lt.LocalUpdate.ForVersion != 0
}
2021-08-22 13:29:04 +02:00
func (lt *LocalTask) AddUpdate(update *LocalUpdate) {
if lt.LocalUpdate == nil {
lt.LocalUpdate = &LocalUpdate{}
2021-08-20 13:46:56 +02:00
}
2021-08-22 13:29:04 +02:00
lt.LocalUpdate.Add(update)
}
func (lt *LocalTask) ApplyUpdate() {
if lt.LocalUpdate == nil {
return
2021-08-20 13:46:56 +02:00
}
2021-08-22 13:29:04 +02:00
u := lt.LocalUpdate
2021-09-04 12:20:35 +02:00
if u.ForVersion != lt.Version {
2021-08-22 13:29:04 +02:00
lt.LocalUpdate = &LocalUpdate{}
return
2021-08-20 13:46:56 +02:00
}
2021-08-22 13:29:04 +02:00
for _, field := range u.Fields {
switch field {
case FIELD_ACTION:
lt.Action = u.Action
case FIELD_PROJECT:
lt.Project = u.Project
case FIELD_DUE:
lt.Due = u.Due
case FIELD_RECUR:
lt.Recur = u.Recur
case FIELD_DONE:
lt.Done = u.Done
}
2021-08-20 13:46:56 +02:00
}
2021-08-22 13:29:04 +02:00
lt.LocalUpdate = &LocalUpdate{}
2021-08-20 13:46:56 +02:00
}
2021-08-25 06:52:48 +02:00
type ById []*LocalTask
func (lt ById) Len() int { return len(lt) }
func (lt ById) Swap(i, j int) { lt[i], lt[j] = lt[j], lt[i] }
func (lt ById) Less(i, j int) bool { return lt[i].Id < lt[j].Id }
2021-08-20 11:27:12 +02:00
type ByDue []*LocalTask
func (lt ByDue) Len() int { return len(lt) }
func (lt ByDue) Swap(i, j int) { lt[i], lt[j] = lt[j], lt[i] }
func (lt ByDue) Less(i, j int) bool { return lt[j].Due.After(lt[i].Due) }
2021-08-20 13:46:56 +02:00
2021-08-20 17:50:08 +02:00
type ByDefault []*LocalTask
func (lt ByDefault) Len() int { return len(lt) }
func (lt ByDefault) Swap(i, j int) { lt[i], lt[j] = lt[j], lt[i] }
func (lt ByDefault) Less(i, j int) bool {
2022-06-05 15:00:28 +02:00
if lt[i].IsRecurrer() != lt[j].IsRecurrer() {
return lt[i].IsRecurrer()
}
2021-08-20 17:50:08 +02:00
if !lt[j].Due.Equal(lt[i].Due) {
return lt[j].Due.After(lt[i].Due)
}
if lt[i].Project != lt[j].Project {
return lt[i].Project < lt[j].Project
}
return lt[i].LocalId < lt[j].LocalId
}
2021-08-20 13:46:56 +02:00
type LocalUpdate struct {
ForVersion int
2021-08-22 13:29:04 +02:00
Fields []string
Action string
Project string
Due Date
Recur Recurrer
Done bool
2021-08-20 13:46:56 +02:00
}
2021-08-22 13:29:04 +02:00
func (lu *LocalUpdate) Add(newUpdate *LocalUpdate) {
if lu.ForVersion > newUpdate.ForVersion {
return
}
lu.ForVersion = newUpdate.ForVersion
for _, nf := range newUpdate.Fields {
switch nf {
case FIELD_ACTION:
lu.Action = newUpdate.Action
case FIELD_PROJECT:
lu.Project = newUpdate.Project
case FIELD_DUE:
lu.Due = newUpdate.Due
case FIELD_RECUR:
lu.Recur = newUpdate.Recur
case FIELD_DONE:
lu.Done = newUpdate.Done
}
add := true
for _, of := range lu.Fields {
if nf == of {
add = false
break
}
}
if add {
lu.Fields = append(lu.Fields, nf)
}
}
}
2021-08-20 13:46:56 +02:00
func (lu LocalUpdate) Value() (driver.Value, error) {
2021-09-03 10:02:08 +02:00
v := fmt.Sprintf("forversion: %d\n", lu.ForVersion)
for _, f := range lu.Fields {
switch f {
case FIELD_ACTION:
v += fmt.Sprintf("action: %s\n", lu.Action)
case FIELD_PROJECT:
v += fmt.Sprintf("project: %s\n", lu.Project)
case FIELD_RECUR:
v += fmt.Sprintf("recur: %s\n", lu.Recur.String())
case FIELD_DUE:
v += fmt.Sprintf("due: %s\n", lu.Due)
case FIELD_DONE:
v += fmt.Sprintf("done: %t\n", lu.Done)
}
}
2021-09-03 10:02:08 +02:00
return v, nil
2021-08-20 13:46:56 +02:00
}
func (lu *LocalUpdate) Scan(value interface{}) error {
body, err := driver.String.ConvertValue(value)
if err != nil {
*lu = LocalUpdate{}
return nil
}
newLu := LocalUpdate{}
for _, line := range strings.Split(body.(string), "\n") {
kv := strings.SplitN(line, ":", 2)
if len(kv) < 2 {
continue
}
k := strings.TrimSpace(kv[0])
v := strings.TrimSpace(kv[1])
switch k {
case "forversion":
d, _ := strconv.Atoi(v)
newLu.ForVersion = d
2021-08-20 13:46:56 +02:00
case "action":
newLu.Action = v
2021-09-03 10:02:08 +02:00
newLu.Fields = append(newLu.Fields, FIELD_ACTION)
2021-08-20 13:46:56 +02:00
case "project":
newLu.Project = v
2021-09-03 10:02:08 +02:00
newLu.Fields = append(newLu.Fields, FIELD_PROJECT)
2021-08-20 13:46:56 +02:00
case "recur":
newLu.Recur = NewRecurrer(v)
2021-09-03 10:02:08 +02:00
newLu.Fields = append(newLu.Fields, FIELD_RECUR)
2021-08-20 13:46:56 +02:00
case "due":
newLu.Due = NewDateFromString(v)
2021-09-03 10:02:08 +02:00
newLu.Fields = append(newLu.Fields, FIELD_DUE)
2021-08-20 13:46:56 +02:00
case "done":
if v == "true" {
newLu.Done = true
2021-09-03 10:02:08 +02:00
newLu.Fields = append(newLu.Fields, FIELD_DONE)
2021-08-20 13:46:56 +02:00
}
}
}
*lu = newLu
return nil
}