-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.go
More file actions
153 lines (123 loc) · 3.87 KB
/
app.go
File metadata and controls
153 lines (123 loc) · 3.87 KB
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
package main
import (
"context"
"fmt"
"log"
"path/filepath"
_ "embed"
wikibot "github.com/runik-3/builder/wikiBot"
c "github.com/runik-3/core/core"
dev "github.com/runik-3/core/device"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
//go:embed version
var version string
// App struct
type App struct {
ctx context.Context
// store app config files
runikDir string
// store raw dictionary files
dictionaryDir string
// the selected reader device
device dev.Device
config c.Config
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// Based on GNOME design standard https://developer.gnome.org/hig/guidelines/adaptive.html
const MIN_WIDTH = 1024
const MIN_HEIGHT = 600
// 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
runtime.WindowSetMinSize(ctx, MIN_WIDTH, MIN_HEIGHT)
a.checkAppConfigDirExistsIfNotCreate()
}
// pull out into config.go
func (a *App) checkAppConfigDirExistsIfNotCreate() {
configDir := c.GetUserConfigDir()
a.runikDir = filepath.Join(configDir, "runik")
a.dictionaryDir = filepath.Join(a.runikDir, "dictionaries")
c.MkdirIfNotExists(a.runikDir)
c.MkdirIfNotExists(a.dictionaryDir)
config, err := c.GetOrCreateConfig(a.runikDir)
if err != nil {
log.Fatal(err)
}
a.config = config
}
func (a *App) GetConfig() c.Response[c.Config] {
config, err := c.GetOrCreateConfig(a.runikDir)
if err != nil {
return c.Response[c.Config]{Data: c.Config{}, Error: fmt.Sprintf("There was an error fetching configuration: %s", err.Error())}
}
a.config = config
return c.Response[c.Config]{Data: config, Error: ""}
}
func (a *App) SetConfig(config c.Config) c.Response[string] {
err := c.UpdateConfig(a.runikDir, config)
if err != nil {
return c.Response[string]{Data: "", Error: fmt.Sprintf("There was an issue saving configuration: %s", err.Error())}
}
// This res has no return value
return c.Response[string]{Data: "", Error: ""}
}
func (a *App) CheckForUpdate() bool {
return c.UpdateAvailable(version)
}
func (a *App) GetVersion() c.Response[string] {
return c.Response[string]{Data: version, Error: "" }
}
func (a *App) SelectDevice() c.Response[dev.Device] {
deviceDir := a.selectDirectory(runtime.OpenDialogOptions{})
// Nothing selected
if deviceDir == "" {
if a.device != nil {
// Return existing device selection
return c.Response[dev.Device]{Data: a.device, Error: ""}
}
return c.Response[dev.Device]{Data: dev.Kobo{}, Error: ""}
}
device, err := dev.NewDevice(deviceDir, a.runikDir, dev.DeviceOptions{KindlegenPath: a.config.KindlegenPath})
if err != nil {
return c.Response[dev.Device]{Data: dev.Kobo{}, Error: err.Error()}
}
a.device = device
return c.Response[dev.Device]{Data: a.device, Error: ""}
}
func (a *App) ClearDevice() {
a.device = nil
}
func (a *App) selectDirectory(options runtime.OpenDialogOptions) string {
dirPath, err := runtime.OpenDirectoryDialog(a.ctx, options)
if err != nil {
log.Fatal(err)
}
return dirPath
}
func (a *App) SelectFile(options runtime.OpenDialogOptions) c.Response[string] {
filePath, err := runtime.OpenFileDialog(a.ctx, options)
if err != nil {
return c.Response[string]{Data: "", Error: "There was an issue selecting the file"}
}
return c.Response[string]{Data: filePath, Error: ""}
}
type GeneratorProgress struct {
Processed int
Total int
}
// Sends generator progress to frontend
func (a *App) emitProgress(processed int, total int) {
runtime.EventsEmit(a.ctx, "progressUpdate", GeneratorProgress{Processed: processed, Total: total})
}
func (a *App) GetWikiDetails(wikiUrl string) c.Response[wikibot.WikiDetails] {
details, err := wikibot.GetWikiDetails(wikiUrl)
if err != nil {
return c.Response[wikibot.WikiDetails]{Data: wikibot.WikiDetails{}, Error: err.Error()}
}
return c.Response[wikibot.WikiDetails]{Data: details, Error: ""}
}