/
app.go
266 lines (232 loc) · 6.09 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package app
import (
"context"
"fmt"
"time"
"github.com/Oppodelldog/toggleperfect/internal/log"
"github.com/Oppodelldog/toggleperfect/internal/apps/timetoggle/app/repo"
"github.com/Oppodelldog/toggleperfect/internal/display"
"github.com/Oppodelldog/toggleperfect/internal/keys"
)
type TimeToggle struct {
Display display.UpdateChannel
serverCtx context.Context
cancelServer func()
activeProject int
projects []Project
capturing bool
captureTicker CaptureTicker
updateScreenTicker *time.Ticker
projectSummary ProjectSummary
summaryOffset int
}
func (a *TimeToggle) Init() {
a.serverCtx, a.cancelServer = context.WithCancel(context.Background())
StartApiServer(a.serverCtx)
a.projects = loadProjects()
a.loadProjectSummary()
a.Display <- CreateStartScreen(a.projectSummary)
}
func (a TimeToggle) Dispose() {
a.cancelServer()
}
func (a *TimeToggle) Activate() {
a.updateScreenTicker = time.NewTicker(time.Minute * 10)
a.activate()
}
func (a *TimeToggle) activate() {
a.projects = loadProjects()
a.activeProject = 0
a.summaryOffset = 0
a.loadProjectSummary()
a.Display <- CreateStartScreen(a.projectSummary)
}
func (a TimeToggle) Deactivate() {
a.updateScreenTicker.Stop()
}
func (a *TimeToggle) HandleEvent(event keys.Event) bool {
if !a.capturing {
if event.State == keys.Clicked && a.hasProjects() {
if event.Key == keys.Key1 {
a.capturing = true
a.startCapture(a.currentProject())
a.Display <- CreateProjectScreen(a.currentProject())
return true
}
if event.Key == keys.Key2 {
a.summaryOffset--
a.loadProjectSummary()
a.Display <- CreateStartScreen(a.projectSummary)
return true
}
if event.Key == keys.Key3 {
a.projectSummary.Pagination.Page = a.projectSummary.Pagination.NextPage()
a.Display <- CreateStartScreen(a.projectSummary)
return true
}
}
if event.State == keys.PressedReleased && a.hasProjects() {
if event.Key == keys.Key2 {
a.summaryOffset = 0
a.loadProjectSummary()
a.Display <- CreateStartScreen(a.projectSummary)
return true
}
if event.Key == keys.Key3 {
a.projectSummary.Pagination.Page = 1
a.Display <- CreateStartScreen(a.projectSummary)
return true
}
}
} else if event.State == keys.Clicked {
if event.Key == keys.Key1 {
a.capturing = false
a.stopCapture()
a.Display <- CreateStartScreen(a.projectSummary)
}
if event.Key == keys.Key2 && a.hasProjects() {
projectID := a.currentProject().Name
toggleProjectClosed(projectID)
a.updateProjects()
}
if event.Key == keys.Key3 && a.hasProjects() {
a.stopCapture()
nextProject := a.nextProject()
a.startCapture(nextProject)
a.Display <- CreateProjectScreen(nextProject)
}
if event.Key == keys.Key4 && a.hasProjects() {
a.stopCapture()
previousProject := a.previousProject()
a.startCapture(previousProject)
a.Display <- CreateProjectScreen(previousProject)
}
return true
} else if event.State == keys.PressedReleased {
if event.Key == keys.Key2 && a.hasProjects() {
projectID := a.currentProject().Name
a.stopCapture()
removeProject(projectID)
a.activate()
}
}
return false
}
func (a *TimeToggle) loadProjectSummary() {
a.projectSummary = GetProjectsOverview(a.summaryOffset)
if len(a.projectSummary.Projects) == 0 {
a.projectSummary.Projects = a.projects
}
}
func (a *TimeToggle) updateProjects() {
projectID := a.currentProject().Name
a.projects = loadProjects()
a.selectProject(projectID)
a.Display <- CreateProjectScreen(a.currentProject())
}
func toggleProjectClosed(ID string) {
p, err := repo.GetProject(ID)
if err != nil {
log.Printf("error loading project for toggle closed flag: %v", err)
} else {
p.Closed = !p.Closed
err := repo.SaveProject(p)
if err != nil {
log.Printf("error saving project for toggle closed flag: %v", err)
}
}
}
func removeProject(ID string) {
err := repo.DeleteProject(ID)
if err != nil {
log.Printf("error deleting project: %v", err)
}
}
func (a *TimeToggle) startCapture(previousProject Project) {
previousProject.startCapture()
a.captureTicker = NewCaptureTicker(a.serverCtx, a.currentProject().Name)
a.captureTicker.Start()
}
func (a *TimeToggle) stopCapture() {
a.currentProject().stopCapture()
a.captureTicker.Stop()
}
func (a *TimeToggle) nextProject() Project {
a.activeProject++
if a.activeProject == len(a.projects) {
a.activeProject = 0
}
return a.currentProject()
}
func (a *TimeToggle) previousProject() Project {
a.activeProject--
if a.activeProject == -1 {
a.activeProject = len(a.projects) - 1
}
return a.currentProject()
}
func (a TimeToggle) currentProject() Project {
return a.projects[a.activeProject]
}
func (a TimeToggle) hasProjects() bool {
return len(a.projects) > 0
}
func (a *TimeToggle) selectProject(ID string) {
for i := 0; i < len(a.projects); i++ {
if a.projects[i].Name == ID {
a.activeProject = i
return
}
}
log.Printf("could not find project to select: %s", ID)
}
func loadProjects() []Project {
var projects []Project
list, err := repo.GetProjectList()
if err != nil {
err = fmt.Errorf("error loading projects: %v", err)
panic(err)
}
for _, prj := range list {
projects = append(projects, Project{
Name: prj.ID,
Description: prj.Description,
Capture: "",
Closed: prj.Closed,
})
}
return projects
}
func GetProjectsOverview(offset int) ProjectSummary {
date := time.Now().AddDate(0, 0, offset)
captures, err := repo.GetReportCaptures(repo.TimeSpanDay(date))
if err != nil {
panic(err)
}
var projects []Project
for _, project := range captures.Projects {
if project.TimeWorked < 60 {
continue
}
projects = append(projects, Project{
Name: project.ID,
Capture: fmtDuration(time.Duration(project.TimeWorked) * time.Second),
})
}
return ProjectSummary{
Date: date,
Projects: projects,
Pagination: Pagination{
Page: 1,
PerPage: 5,
NumItems: len(projects),
},
}
}
func fmtDuration(d time.Duration) string {
d = d.Round(time.Minute)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
return fmt.Sprintf("%02d:%02d", h, m)
}