Skip to content

Commit 6f7e99a

Browse files
committed
Fix sorting and database after restore. Closes #32.
1 parent a90685b commit 6f7e99a

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

cmd/clocked/tasklistview.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"sort"
56

67
termbox "github.com/nsf/termbox-go"
78
"github.com/zerok/clocked"
@@ -59,6 +60,7 @@ func newTasklistView(app *application) *tasklistView {
5960
func (v *tasklistView) updateTaskList() {
6061
a := v.app
6162
tasks, _ := a.db.FilteredTasks(v.filter)
63+
sort.Sort(clocked.ByCode(tasks))
6264
items := make([]ScrollableListItem, 0, len(tasks))
6365
for _, t := range tasks {
6466
items = append(items, t)

internal/database/folderbased.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ func (d *FolderBasedDatabase) TaskByCode(code string) (clocked.Task, bool) {
6868

6969
func (d *FolderBasedDatabase) LoadState() error {
7070
d.log.Infof("Loading state")
71+
d.taskCodeIndex = make(map[string]struct{})
72+
d.taskIndex = make([]clocked.Task, 0, 20)
7173
activeCodeFile := filepath.Join(d.rootFolder, ActiveCodeFilename)
7274
tasksFolder := filepath.Join(d.rootFolder, TasksFolder)
7375

task.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clocked
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67
)
78

@@ -66,3 +67,17 @@ func (b *Booking) StopTime() *time.Time {
6667
t, _ := time.Parse(time.RFC3339, b.Stop)
6768
return &t
6869
}
70+
71+
type ByCode []Task
72+
73+
func (l ByCode) Len() int {
74+
return len(l)
75+
}
76+
77+
func (l ByCode) Less(i int, j int) bool {
78+
return strings.Compare(l[i].Code, l[j].Code) < 0
79+
}
80+
81+
func (l ByCode) Swap(i int, j int) {
82+
l[i], l[j] = l[j], l[i]
83+
}

0 commit comments

Comments
 (0)