-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
289 lines (247 loc) · 6.49 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/theBGuy/go-work-tracker/auto_update"
"github.com/wailsapp/wails/v2/pkg/runtime"
_ "github.com/mattn/go-sqlite3"
"gorm.io/gorm"
)
// App struct
type App struct {
ctx context.Context
db *gorm.DB
startTime time.Time
lastSave time.Time
isRunning bool
organization Organization
project Project
version string
environment string
newVersonAvailable bool
}
var ctx context.Context
var cancel context.CancelFunc
type WailsConfig struct {
Info Info `json:"info"`
}
type Info struct {
ProductVersion string `json:"productVersion"`
Environment string `json:"environment"`
}
// NewApp creates a new App application struct
func NewApp() *App {
version := os.Getenv("APP_ENV")
if version == "" {
version, _ = readVersionConfig(WailsConfigFile)
}
environment := os.Getenv("APP_ENV")
if environment == "" {
environment, _ = readEnvConfig(WailsConfigFile)
}
dbDir, err := getSaveDir(environment)
if err != nil {
panic(err)
}
fmt.Println("Starting Go Work Tracker. \nVersion: ", version, "\nEnvironment: ", environment, "\nSave directory: ", dbDir)
// Check for updates
var newVersonAvailable bool
if environment == "production" {
newVersonAvailable = auto_update.Run(version)
}
db := NewDb(dbDir)
return &App{
db: db,
version: version,
environment: environment,
newVersonAvailable: newVersonAvailable,
}
}
func (a *App) GetVersion() string {
return a.version
}
func (a *App) UpdateAvailable() bool {
return a.newVersonAvailable
}
type ActiveTimer struct {
Organization Organization `json:"organization"`
Project Project `json:"project"`
IsRunning bool `json:"isRunning"`
TimeElapsed int `json:"timeElapsed"`
}
func (a *App) GetActiveTimer() ActiveTimer {
return ActiveTimer{
Organization: a.organization,
Project: a.project,
IsRunning: a.isRunning,
TimeElapsed: a.TimeElapsed(),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.monitorTime()
a.monitorUpdates()
a.cleanupRoutine()
}
// shutdown is called at termination
func (a *App) shutdown(ctx context.Context) {
fmt.Println("Shutting down...")
if a.isRunning {
a.StopTimer()
}
}
func (a *App) monitorTime() {
ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
if a.isRunning && time.Now().Format("2006-01-02") != a.startTime.Format("2006-01-02") {
a.StopTimer()
a.StartTimer(a.organization, a.project)
runtime.EventsEmit(a.ctx, "new-day")
}
}
}()
}
func (a *App) monitorUpdates() {
if a.environment != "production" {
return
}
ticker := time.NewTicker(1 * time.Hour)
runtime.EventsOn(a.ctx, "do-update", func(optionalData ...interface{}) {
a.shutdown(a.ctx)
auto_update.Run(a.version)
})
go func() {
for range ticker.C {
if !a.newVersonAvailable {
newVersonAvailable, _ := auto_update.GetUpdateAvailable(a.version)
if newVersonAvailable {
a.newVersonAvailable = true
runtime.EventsEmit(a.ctx, "update-available")
}
}
}
}()
}
func (a *App) cleanupRoutine() {
// Run on startup then every 24 hours
a.cleanupSoftDeletedRecords()
ticker := time.NewTicker(24 * time.Hour)
go func() {
for range ticker.C {
a.cleanupSoftDeletedRecords()
}
}()
}
func (a *App) CheckForUpdates() bool {
newVersonAvailable, _ := auto_update.GetUpdateAvailable(a.version)
if newVersonAvailable {
a.newVersonAvailable = true
runtime.EventsEmit(a.ctx, "update-available")
}
return newVersonAvailable
}
func (a *App) TimerRunning() bool {
return a.isRunning
}
func (a *App) StartTimer(organization Organization, project Project) {
a.startTime = time.Now()
a.organization = organization
a.project = project
a.isRunning = true
ctx, cancel = context.WithCancel(context.Background())
go func() {
for {
select {
case <-time.After(1 * time.Minute):
a.saveTimer(a.project.ID)
case <-ctx.Done():
return
}
}
}()
}
func (a *App) StopTimer() {
if !a.isRunning {
return
}
secsWork := a.saveTimer(a.project.ID)
a.NewWorkSession(a.project.ID, secsWork)
cancel()
a.isRunning = false
a.lastSave = time.Time{}
}
// TimeElapsed returns the total seconds worked in the current timer session
func (a *App) TimeElapsed() int {
if a.isRunning {
return int(time.Since(a.startTime).Seconds())
}
return 0
}
func (a *App) ShowWindow() {
if runtime.WindowIsMinimised(a.ctx) {
runtime.WindowUnminimise(a.ctx)
} else {
runtime.WindowSetAlwaysOnTop(a.ctx, true)
}
runtime.WindowShow(a.ctx)
runtime.WindowSetAlwaysOnTop(a.ctx, false)
}
// Display a confirmation dialog
func (a *App) ConfirmAction(title string, message string) bool {
selection, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: title,
Message: message,
DefaultButton: "No",
})
if err != nil {
fmt.Println(err)
return false
}
return selection == "Yes"
}
var normalOrgX, normalOrgY int
var minOrgX, minOrgY int
// type WindowSize struct {
// Width int `json:"width"`
// Height int `json:"height"`
// }
// func getScreenSize() (WindowSize, error) {
// if err := glfw.Init(); err != nil {
// fmt.Println("failed to initialize glfw:", err)
// return WindowSize{}, err
// }
// defer glfw.Terminate()
// // Get the primary monitor
// monitor := glfw.GetPrimaryMonitor()
// if monitor == nil {
// fmt.Println("failed to get primary monitor")
// return WindowSize{}, errors.New("failed to get primary monitor")
// }
// // Get the video mode of the primary monitor
// mode := monitor.GetVideoMode()
// if mode == nil {
// fmt.Println("failed to get video mode")
// return WindowSize{}, errors.New("failed to get video mode")
// }
// return WindowSize{Width: mode.Width, Height: mode.Height}, nil
// }
func (a *App) NormalizeWindow() {
runtime.WindowSetMaxSize(a.ctx, 0, 0)
runtime.WindowCenter(a.ctx)
runtime.WindowSetSize(a.ctx, WIN_WIDTH, WIN_HEIGHT)
runtime.WindowSetMinSize(a.ctx, MIN_WIN_WIDTH, MIN_WIN_HEIGHT)
runtime.WindowCenter(a.ctx)
runtime.WindowSetAlwaysOnTop(a.ctx, false)
}
func (a *App) MinimizeWindow() {
runtime.WindowSetMinSize(a.ctx, WIDGET_WIDTH, WIDGET_HEIGHT)
runtime.WindowSetSize(a.ctx, WIDGET_WIDTH, WIDGET_HEIGHT)
runtime.WindowSetMaxSize(a.ctx, WIDGET_WIDTH, WIDGET_HEIGHT)
runtime.WindowSetAlwaysOnTop(a.ctx, true)
}