-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice-tab.go
More file actions
78 lines (62 loc) · 1.45 KB
/
price-tab.go
File metadata and controls
78 lines (62 loc) · 1.45 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
package main
import (
"bytes"
"errors"
"fmt"
"image"
"image/png"
"io"
"os"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
)
func (app *Config) pricesTab() *fyne.Container {
chart := app.getChart()
chartContainer := container.NewVBox(chart)
app.PriceChartContainer = chartContainer
return chartContainer
}
func (app *Config) getChart() *canvas.Image {
apiURL := fmt.Sprintf("https://goldprice.org/charts/gold_3d_b_k_%s_x.png", strings.ToLower(currency))
var img *canvas.Image
err := app.downloadFile(apiURL, "gold_chart.png")
if err != nil {
// use bundled image
img = canvas.NewImageFromResource(resourceUnreachablePng)
} else {
img = canvas.NewImageFromFile("gold_chart.png")
}
img.SetMinSize(fyne.Size{Width: 770, Height: 410})
img.FillMode = canvas.ImageFillStretch
return img
}
func (app *Config) downloadFile(URL, fileName string) error {
// get the response bytes from calling a url
resp, err := app.HTTPClient.Get(URL)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New("received wrong response code when downloading image")
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
img, _, err := image.Decode(bytes.NewReader(b))
if err != nil {
return err
}
out, err := os.Create(fmt.Sprintf("./%s", fileName))
if err != nil {
return err
}
err = png.Encode(out, img)
if err != nil {
return err
}
return nil
}