-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
115 lines (94 loc) · 2.2 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
package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
savedFileLocation string
jsonData string
logger logger.Logger
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
logger: logger.NewDefaultLogger(),
}
}
// startup is called at application startup
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.ctx = ctx
b.logger.Info("START startup")
homeDir, err := os.UserHomeDir()
if err != nil {
panic("can't fetch user home directory")
}
savedFileLocation := filepath.Join(homeDir, "wail_progress", "data.json")
data, err := os.ReadFile(savedFileLocation)
if err != nil {
newError := errors.Unwrap(err)
fmt.Println(newError)
if errors.Is(err, syscall.ENOENT) {
err := os.Mkdir(filepath.Join(homeDir, "wail_progress"), 0755)
if err != nil {
panic(err)
}
err = os.WriteFile(savedFileLocation, []byte("{}"), 0666)
if err != nil {
panic(err)
}
data = []byte("{}")
} else {
fmt.Printf("%v->\n", err)
panic(err)
}
}
b.savedFileLocation = savedFileLocation
b.SetJsonData(string(data))
}
// domReady is called after the front-end dom has been loaded
func (b *App) domReady(ctx context.Context) {
// Add your action here
}
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
b.save()
}
func (b *App) SetJsonData(jsonData string) {
b.jsonData = jsonData
}
func (b *App) GetJsonData() string {
return b.jsonData
}
func (b *App) SaveJsonData(jsonData string) error {
b.SetJsonData(jsonData)
return b.save()
}
func (b *App) save() error {
err := os.WriteFile(b.savedFileLocation, []byte(b.jsonData), 0666)
if err != nil {
return err
}
return nil
}
func (b *App) ConfirmDelete() bool {
selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "Do you really want to delete?",
Message: "Confirm",
Buttons: []string{"Yes", "No"},
DefaultButton: "No",
CancelButton: "No",
})
if err != nil {
return false
}
return selection == "Yes"
}