/
main.go
267 lines (244 loc) · 5.83 KB
/
main.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
267
package main
import (
"flag"
"image"
"image/color"
"log"
"os"
"time"
"gioui.org/app"
"gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/steverusso/gio-fonts/vegur"
)
type (
C = layout.Context
D = layout.Dimensions
)
type habit struct {
ID int `json:"id"`
CreatedAt time.Time `json:"created"`
CompletedAt time.Time `json:"compl,omitempty"`
DeletedAt time.Time `json:"del,omitempty"`
Content string `json:"cont,omitempty"`
}
func (h *habit) isDone() bool {
return !h.CompletedAt.IsZero()
}
func (h *habit) isDeleted() bool {
return !h.DeletedAt.IsZero()
}
type dailySummary struct {
NumCompl int `json:"n"`
PctCompl float32 `json:"p"`
}
func newSummaryOfList(list []habit) dailySummary {
numCompl := 0
for _, h := range list {
if h.isDone() {
numCompl++
}
}
return dailySummary{
NumCompl: numCompl,
PctCompl: float32(numCompl) / float32(len(list)),
}
}
type App struct {
store *store
splashErr splashErr
home homeScreen
habits *habitScreen
}
func (a *App) handleKeyEvent(ke key.Event) {
if a.habits != nil {
switch ke.Name {
case "/":
a.habits.newItem.Focus()
case key.NameEscape:
a.habits = nil
}
return
}
}
func (a *App) layout(gtx C, th *material.Theme) D {
paint.Fill(gtx.Ops, th.Bg)
if a.store == nil {
if a.splashErr != nil {
return layout.Center.Layout(gtx, func(gtx C) D {
return material.Body1(th, a.splashErr.Error()).Layout(gtx)
})
}
return layout.Center.Layout(gtx, func(gtx C) D {
gtx.Constraints.Max.X = 300
return material.H6(th, "Loading...").Layout(gtx)
})
}
if a.habits != nil {
return a.habits.layout(gtx, th)
}
return a.home.layout(gtx, th)
}
func (a *App) mergeHabitTemplateWithToday(u applyHabitsToToday) {
fmtDate := time.Now().Format("060102")
todaysHabits, err := a.store.getHabitsForDay(fmtDate)
if err != nil {
a.home.errors.add("reading today's habits", err)
return
}
var resolved []habit
for _, tmplHabit := range u.habits {
var currentHabit *habit
for i, c := range todaysHabits {
if c.ID == tmplHabit.ID && !tmplHabit.isDeleted() {
currentHabit = &todaysHabits[i]
break
}
}
if currentHabit != nil {
resolved = append(resolved, *currentHabit)
} else {
resolved = append(resolved, tmplHabit)
}
}
if err := a.store.putHabitsForDay(fmtDate, resolved); err != nil {
a.home.errors.add("saving today's new habits", err)
return
}
a.home.setSummary(fmtDate, newSummaryOfList(resolved))
if a.home.record.fmtDate == fmtDate {
checks := make([]widget.Bool, len(resolved))
for i := range resolved {
checks[i] = widget.Bool{Value: resolved[i].isDone()}
}
a.home.record.checks = checks
a.home.record.habits = resolved
}
}
type splashErr error
type splashHandOff struct {
store *store
summaries map[string]dailySummary
record dailyRecordWidget
}
func initLoad(dbFile string, updates chan<- any) {
store, err := openStore(dbFile)
if err != nil {
updates <- splashErr(err)
return
}
summaries, err := store.getSummaries()
if err != nil {
updates <- splashErr(err)
return
}
fmtDate := time.Now().Format("060102")
todaysHabits, err := store.getHabitsForDay(fmtDate)
if err != nil {
updates <- splashErr(err)
return
}
record, err := newDailyRecord(fmtDate, todaysHabits)
if err != nil {
updates <- splashErr(err)
return
}
updates <- splashHandOff{
store: store,
summaries: summaries,
record: record,
}
}
func run(dbFile string, showFrameTimes bool) error {
updates := make(chan any)
go initLoad(dbFile, updates)
win := app.NewWindow(
app.Size(720, 720),
app.Title("Todaily"),
)
win.Perform(system.ActionCenter)
th := material.NewTheme(vegur.Collection())
th.TextSize = 17
th.Palette = material.Palette{
Bg: color.NRGBA{17, 21, 24, 255},
Fg: color.NRGBA{230, 230, 230, 255},
ContrastFg: color.NRGBA{251, 251, 251, 255},
ContrastBg: color.NRGBA{40, 170, 196, 255},
}
var a App
var ops op.Ops
for {
select {
case u := <-updates:
switch u := u.(type) {
case splashErr:
a.splashErr = u
case splashHandOff:
a.store = u.store
a.home = homeScreen{
store: a.store,
updates: updates,
gridRows: newDayGrid(u.summaries),
habitList: widget.List{List: layout.List{Axis: layout.Vertical}},
record: u.record,
invalidate: win.Invalidate,
}
case openHabitScreen:
a.habits = &habitScreen{
store: a.store,
updates: updates,
list: widget.List{List: layout.List{Axis: layout.Vertical}},
newItem: widget.Editor{SingleLine: true, Submit: true},
habits: u.items,
invalidate: win.Invalidate,
}
case applyHabitsToToday:
a.mergeHabitTemplateWithToday(u)
case closeHabitScreen:
a.habits = nil
}
win.Invalidate()
case e := <-win.Events():
switch e := e.(type) {
case system.FrameEvent:
start := time.Now()
gtx := layout.NewContext(&ops, e)
// Process any key events since the previous frame.
for _, ke := range gtx.Events(win) {
if ke, ok := ke.(key.Event); ok {
a.handleKeyEvent(ke)
}
}
// Gather key input on the entire window area.
areaStack := clip.Rect(image.Rectangle{Max: gtx.Constraints.Max}).Push(gtx.Ops)
key.InputOp{Tag: win, Keys: "/|" + key.NameEscape}.Add(gtx.Ops)
a.layout(gtx, th)
areaStack.Pop()
e.Frame(gtx.Ops)
if showFrameTimes {
log.Println(time.Since(start))
}
case system.DestroyEvent:
return e.Err
}
}
}
}
func main() {
showFrameTimes := flag.Bool("print-frame-times", false, "Print out how long each frame takes.")
flag.Parse()
dbFile := flag.Arg(0)
go func() {
if err := run(dbFile, *showFrameTimes); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}