Skip to content

Commit

Permalink
Fix sorting and database after restore. Closes #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok committed Oct 28, 2017
1 parent a90685b commit 6f7e99a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/clocked/tasklistview.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"sort"

termbox "github.com/nsf/termbox-go"
"github.com/zerok/clocked"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions internal/database/folderbased.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clocked

import (
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -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]
}

0 comments on commit 6f7e99a

Please sign in to comment.