-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
60 lines (49 loc) · 1.17 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
package main
import (
"context"
"log"
"os"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/ellielle/bootdev-buddy/internal/bootdevapi"
"github.com/ellielle/bootdev-buddy/internal/cache"
"github.com/ellielle/bootdev-buddy/internal/login"
)
// App struct
type App struct {
ctx context.Context
cache cache.Cache
tokens login.BDToken
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// 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.cache = cache.NewCache(5 * time.Minute)
tokens, err := login.RefreshToken()
if err != nil {
return
}
err = login.SaveTokens(tokens)
if err != nil {
log.Print(err)
}
a.tokens = *tokens
runtime.EventsEmit(ctx, "domready")
}
// UserData sends an authenticated request to gather the user's
// data for display in the app.
func (a *App) UserData() (bootdevapi.UserData, error) {
userData, err := bootdevapi.UserInfo(a.cache, a.tokens.AccessToken)
if err != nil {
return bootdevapi.UserData{}, err
}
return userData, nil
}
func (a *App) CloseApp() {
os.Exit(0)
}