-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreams.go
375 lines (312 loc) · 7.52 KB
/
dreams.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package dreams
import (
"bytes"
"errors"
"fmt"
"image/color"
"io"
"net/http"
"net/url"
"os"
"runtime"
"sort"
"sync"
"time"
"github.com/civilware/Gnomon/structures"
"github.com/dReam-dApps/dReams/rpc"
"github.com/sirupsen/logrus"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/widget"
)
// ContainerStack used for building various
// container/label layouts to be placed in main app
type ContainerStack struct {
LeftLabel *widget.Label
RightLabel *widget.Label
TopLabel *canvas.Text
Back fyne.Container
Front fyne.Container
Actions fyne.Container
DApp *fyne.Container
}
// Saved data for users local config.json file
type SaveData struct {
Skin color.Gray16 `json:"skin"`
Daemon []string `json:"daemon"`
Tables []string `json:"tables"`
Predict []string `json:"predict"`
Sports []string `json:"sports"`
Theme string `json:"theme"`
DBtype string `json:"dbType"`
Para int `json:"paraBlocks"`
FSForce bool `json:"fastsyncForce"`
FSDiff int64 `json:"fastsyncDiff"`
Assets map[string]bool `json:"assets"`
Dapps map[string]bool `json:"dapps"`
}
// AppObject holds the main app and channels
type AppObject struct {
App fyne.App
Window fyne.Window
Background *fyne.Container
os string
configure bool
tab string
subTab string
quit chan struct{}
done chan struct{}
receive chan struct{}
channels int
}
// Select widget items for Dero assets
type AssetSelect struct {
Name string
URL string
Img canvas.Image
Select *widget.Select
}
type count struct {
i int
calling bool
closing bool
sync.RWMutex
}
var counter count
var mu sync.RWMutex
var ms = 100 * time.Millisecond
var logger = structures.Logger.WithFields(logrus.Fields{})
// Add to active channel count
func (c *count) plus() {
c.Lock()
c.i++
c.Unlock()
}
// Subtract from active channel count
func (c *count) minus() {
c.Lock()
c.i--
c.Unlock()
}
// Check active channel count
func (c *count) active() int {
c.RLock()
defer c.RUnlock()
return c.i
}
// Set what OS is being used
func (d *AppObject) SetOS() {
d.os = runtime.GOOS
}
// Check what OS is set
func (d *AppObject) OS() string {
return d.os
}
// Set main configure bool
func (d *AppObject) Configure(b bool) {
mu.Lock()
d.configure = b
mu.Unlock()
}
// Check if main app is configuring
func (d *AppObject) IsConfiguring() bool {
mu.RLock()
defer mu.RUnlock()
return d.configure
}
// Set what tab main windows is on
func (d *AppObject) SetTab(name string) {
mu.Lock()
d.tab = name
mu.Unlock()
}
// Check what tab main windows is on
func (d *AppObject) OnTab(name string) bool {
mu.RLock()
defer mu.RUnlock()
return d.tab == name
}
// Set what sub tab is being viewed
func (d *AppObject) SetSubTab(name string) {
mu.Lock()
d.subTab = name
mu.Unlock()
}
// Check what sub tab is being viewed
func (d *AppObject) OnSubTab(name string) bool {
mu.RLock()
defer mu.RUnlock()
return d.subTab == name
}
// Initialize channels
func (d *AppObject) SetChannels(i int) {
d.receive = make(chan struct{})
d.done = make(chan struct{})
d.quit = make(chan struct{})
d.channels = i
}
// Signal all available channels when we are ready for them to work
func (d *AppObject) SignalChannel() {
if !counter.closing {
counter.calling = true
for counter.active() < d.channels {
counter.plus()
d.receive <- struct{}{}
}
counter.calling = false
}
}
// Receive signal for work
func (d *AppObject) Receive() <-chan struct{} {
return d.receive
}
// Signal back to counter when work is done
func (d *AppObject) WorkDone() {
for counter.calling {
time.Sleep(ms)
}
counter.minus()
}
// Close signal for a dApp
func (d *AppObject) CloseDapp() <-chan struct{} {
return d.done
}
// Send close signal to all active dApp channels
func (d *AppObject) CloseAllDapps() {
for counter.calling {
time.Sleep(ms)
}
counter.closing = true
ch := 0
for ch < d.channels {
ch++
d.done <- struct{}{}
}
for counter.active() > 0 {
time.Sleep(time.Second)
}
counter.closing = false
}
// Stop the main apps process
func (d *AppObject) StopProcess() {
d.quit <- struct{}{}
}
// Close signal for main app
func (d *AppObject) Closing() <-chan struct{} {
return d.quit
}
// Notification pop up for main app
func (d *AppObject) Notification(title, content string) bool {
d.App.SendNotification(&fyne.Notification{Title: title, Content: content})
return true
}
// Check if runtime os is windows
func (d *AppObject) IsWindows() bool {
return d.os == "windows"
}
// Get current working directory path for prefix
func GetDir() (dir string) {
dir, err := os.Getwd()
if err != nil {
logger.Errorln("[GetDir]", err)
}
return
}
// Check if path to file exists
// - tag for log print
func FileExists(path, tag string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else if errors.Is(err, os.ErrNotExist) {
logger.Errorf("[%s] %s Not Found\n", tag, path)
return false
}
return false
}
// Download image file from URL and return as canvas.Image
func DownloadCanvas(URL, fileName string) (canvas.Image, error) {
url, err := url.Parse(URL)
if err != nil {
return *canvas.NewImageFromImage(nil), err
}
client := &http.Client{Timeout: 15 * time.Second}
response, err := client.Get(url.String())
if err != nil {
return *canvas.NewImageFromImage(nil), err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return *canvas.NewImageFromImage(nil), fmt.Errorf("received %d response code", response.StatusCode)
}
// if !strings.HasPrefix(response.Header.Get("Content-Type"), "image/") {
// return canvas.NewImageFromImage(nil), fmt.Errorf("%s does not point to an image", URL)
// }
var buf bytes.Buffer
_, err = io.Copy(&buf, response.Body)
if err != nil {
return *canvas.NewImageFromImage(nil), err
}
return *canvas.NewImageFromReader(&buf, fileName), nil
}
// Download url image file from URL and return as []byte
func DownloadBytes(URL string) ([]byte, error) {
url, err := url.Parse(URL)
if err != nil {
return nil, err
}
client := http.Client{Timeout: 15 * time.Second}
response, err := client.Get(url.String())
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download the image: status code %d", response.StatusCode)
}
// if !strings.HasPrefix(response.Header.Get("Content-Type"), "image/") {
// return nil, fmt.Errorf("%s does not point to an image", URL)
// }
image, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
return image, nil
}
// Returns Fyne theme icon for name
func FyneIcon(name fyne.ThemeIconName) fyne.Resource {
return fyne.Theme.Icon(fyne.CurrentApp().Settings().Theme(), name)
}
// Add a asset option to a AssetSelect
func (a *AssetSelect) Add(add, check string) {
if check == rpc.Wallet.Address {
opts := a.Select.Options
new_opts := append(opts, add)
a.Select.Options = new_opts
a.Sort()
a.Select.Refresh()
}
}
// Clears all assets from select options
func (a *AssetSelect) ClearAll() {
a.Select.Options = []string{}
a.Select.Selected = ""
a.Select.Refresh()
}
// Sort the select widgets options
func (a *AssetSelect) Sort() {
sort.Strings(a.Select.Options)
}
// Remove a asset from Select by name
func (a *AssetSelect) RemoveAsset(rm string) {
index := -1
for i, item := range a.Select.Options {
if item == rm {
index = i
break
}
}
if index != -1 {
a.Select.Options = append(a.Select.Options[:index], a.Select.Options[index+1:]...)
}
a.Select.Refresh()
}