From 6f7e99a5c4cffeac2b60401e5015844f3dd13ff6 Mon Sep 17 00:00:00 2001 From: Horst Gutmann Date: Sat, 28 Oct 2017 15:04:03 +0200 Subject: [PATCH] Fix sorting and database after restore. Closes #32. --- cmd/clocked/tasklistview.go | 2 ++ internal/database/folderbased.go | 2 ++ task.go | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/cmd/clocked/tasklistview.go b/cmd/clocked/tasklistview.go index 2cf950f..ae7d8c3 100644 --- a/cmd/clocked/tasklistview.go +++ b/cmd/clocked/tasklistview.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "sort" termbox "github.com/nsf/termbox-go" "github.com/zerok/clocked" @@ -59,6 +60,7 @@ func newTasklistView(app *application) *tasklistView { func (v *tasklistView) updateTaskList() { a := v.app tasks, _ := a.db.FilteredTasks(v.filter) + sort.Sort(clocked.ByCode(tasks)) items := make([]ScrollableListItem, 0, len(tasks)) for _, t := range tasks { items = append(items, t) diff --git a/internal/database/folderbased.go b/internal/database/folderbased.go index 9ce754c..a4444a6 100644 --- a/internal/database/folderbased.go +++ b/internal/database/folderbased.go @@ -68,6 +68,8 @@ func (d *FolderBasedDatabase) TaskByCode(code string) (clocked.Task, bool) { func (d *FolderBasedDatabase) LoadState() error { d.log.Infof("Loading state") + d.taskCodeIndex = make(map[string]struct{}) + d.taskIndex = make([]clocked.Task, 0, 20) activeCodeFile := filepath.Join(d.rootFolder, ActiveCodeFilename) tasksFolder := filepath.Join(d.rootFolder, TasksFolder) diff --git a/task.go b/task.go index fb3c9f9..57e1317 100644 --- a/task.go +++ b/task.go @@ -2,6 +2,7 @@ package clocked import ( "fmt" + "strings" "time" ) @@ -66,3 +67,17 @@ func (b *Booking) StopTime() *time.Time { t, _ := time.Parse(time.RFC3339, b.Stop) return &t } + +type ByCode []Task + +func (l ByCode) Len() int { + return len(l) +} + +func (l ByCode) Less(i int, j int) bool { + return strings.Compare(l[i].Code, l[j].Code) < 0 +} + +func (l ByCode) Swap(i int, j int) { + l[i], l[j] = l[j], l[i] +}