forked from f-person/timeow-mac
-
Notifications
You must be signed in to change notification settings - Fork 2
/
period.go
132 lines (115 loc) · 2.88 KB
/
period.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/getlantern/systray"
"github.com/hako/durafmt"
)
type period struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
IsSynced bool `json:"is_synced"`
}
func (p *period) duration() time.Duration {
return calculateDuration(p.Start, p.End)
}
func (p *period) string() string {
var format string
if p.Start.Day() == time.Now().Day() {
format = "15:04"
} else {
format = "2 Jan 15:04"
}
duration := p.duration()
limit := 1
if duration > time.Hour {
limit = 2
}
syncedString := ""
if p.IsSynced {
syncedString = " Synced"
}
return fmt.Sprintf(
"%s - %s (%s) %s",
p.Start.Format(format),
p.End.Format(format),
durafmt.Parse(duration).LimitFirstN(limit).String(),
syncedString,
)
}
func (a *app) readPeriodsFromStorage(key string) ([]period, error) {
var periods []period
err := a.defaults.Unmarshal(key, &periods)
return periods, err
}
func (a *app) savePeriodsToStorage(key string, periods []period) (error, error) {
// make http request POST to localhost 8000 with body is periods when key is activePeriodsKey
var syncedError error
if key == activePeriodsKey && a.webhookAddActivePeriod != "" {
syncedError = a.syncPeriods(periods)
}
return syncedError, a.defaults.Marshal(key, periods)
}
func (a *app) syncPeriods(periods []period) error {
var unsyncedPeriods []period
for _, p := range periods {
if !p.IsSynced {
unsyncedPeriods = append(unsyncedPeriods, p)
}
}
if len(unsyncedPeriods) <= 0 {
return nil
}
payload, err := json.Marshal(unsyncedPeriods)
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
resp, err := http.Post(a.webhookAddActivePeriod, "application/json", bytes.NewBuffer(payload))
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
if resp.StatusCode != http.StatusCreated {
err := fmt.Errorf("unexpected status code: %v", resp.Status)
fmt.Println(err)
return err
}
// set is_synced to true for all periods
for i := range periods {
periods[i].IsSynced = true
}
defer resp.Body.Close()
return nil
}
func updatePeriodMenuItems(
periods []period,
periodsMenuItem *systray.MenuItem,
currentMenuItems []*systray.MenuItem,
) []*systray.MenuItem {
menuItems := currentMenuItems
totalNewMenuItems := len(periods) - len(menuItems)
fmt.Printf("totalNewMenuItems: %v", totalNewMenuItems)
if totalNewMenuItems < 0 {
// Hide redundant menu items.
for i := len(menuItems) - 1; i >= len(menuItems)-(-totalNewMenuItems); i-- {
menuItems[i].Hide()
}
} else {
// Add missing menu items.
for i := 0; i < totalNewMenuItems; i++ {
item := periodsMenuItem.AddSubMenuItem("", "")
item.Disable()
item.Show()
menuItems = append(menuItems, item)
}
}
length := len(periods)
for index, entry := range periods {
menuItems[length-index-1].SetTitle(entry.string())
}
return menuItems
}