-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
116 lines (89 loc) · 2.56 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
package main
import (
"fmt"
"time"
"github.com/f-person/timeow-mac/pkg/startup"
"github.com/f-person/timeow-mac/pkg/userdefaults"
"github.com/getlantern/systray"
"github.com/prashantgupta24/mac-sleep-notifier/notifier"
)
type app struct {
isPro bool
maxAllowedIdleTime time.Duration
keepTimeLogsFor time.Duration
startup startup.Startup
defaults userdefaults.UserDefaults
isIdle bool
isSleeping bool
lastIdleTime time.Time
lastActiveTime time.Time
notifier *notifier.Notifier
isSystrayReady bool
idleTimeCh chan time.Duration
notifierCh chan *notifier.Activity
breaks []period
mBreaks *systray.MenuItem
breakMenuItems []*systray.MenuItem
activePeriods []period
mActivePeriods *systray.MenuItem
activePeriodMenuItems []*systray.MenuItem
}
func main() {
fmt.Println("Started the app at", time.Now())
notifierInstance := notifier.GetInstance()
defaults := *userdefaults.Defaults()
app := app{
isPro: true,
maxAllowedIdleTime: time.Minute * time.Duration(
defaults.Integer(maxAllowedIdleTimeKey),
),
keepTimeLogsFor: time.Minute * time.Duration(
defaults.Integer(keepTimeLogsForKey),
),
defaults: defaults,
startup: startup.Startup{
AppLabel: appLabel,
AppName: appName,
},
isIdle: false,
isSleeping: false,
lastIdleTime: time.Now(),
lastActiveTime: time.Now(),
notifier: notifierInstance,
idleTimeCh: make(chan time.Duration),
notifierCh: notifierInstance.Start(),
}
// [maxAllowedIdleTime] have never been set before,
// use the default value and save it to user defaults.
if app.maxAllowedIdleTime == 0 {
app.setMaxAllowedIdleTime(int(defaultMaxAllowedIdleTime.Minutes()))
}
if app.keepTimeLogsFor == 0 {
app.setKeepTimeLogsFor(int(defaultKeepTimeLogsFor.Minutes()))
}
breaks, err := app.readPeriodsFromStorage(breaksKey)
if err == nil {
app.breaks = breaks
} else {
fmt.Printf("An error occurred while reading breaks: %v", err)
}
activePeriods, err := app.readPeriodsFromStorage(activePeriodsKey)
if err == nil {
app.activePeriods = activePeriods
} else {
fmt.Printf("An error occurred while reading active times: %v", err)
}
// Listeners
go app.activityListener()
idleTimeListenerTicker := time.NewTicker(idleListenerInterval)
go app.idleTimeListener(idleTimeListenerTicker)
timeLogCleanerTicker := time.NewTicker(timeLogCleanerInterval)
go app.timeLogCleaner(timeLogCleanerTicker)
app.checkAndCleanExpiredTimeLogs()
// Menu bar
systray.Run(func() {
app.onSystrayReady()
}, func() {
notifierInstance.Quit()
})
}