itd/cmd/itgui/info.go

118 lines
2.5 KiB
Go
Raw Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
"errors"
"fmt"
"image/color"
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
2021-10-24 20:27:14 +00:00
"go.arsenm.dev/itd/api"
2021-08-26 04:18:24 +00:00
"go.arsenm.dev/itd/internal/types"
)
2021-10-24 20:27:14 +00:00
func infoTab(parent fyne.Window, client *api.Client) *fyne.Container {
2021-08-26 04:18:24 +00:00
infoLayout := container.NewVBox(
// Add rectangle for a bit of padding
canvas.NewRectangle(color.Transparent),
)
2021-08-26 15:47:17 +00:00
// Create label for heart rate
2021-08-26 04:18:24 +00:00
heartRateLbl := newText("0 BPM", 24)
2021-08-26 15:47:17 +00:00
// Creae container to store heart rate section
2021-08-26 04:18:24 +00:00
heartRate := container.NewVBox(
newText("Heart Rate", 12),
heartRateLbl,
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(heartRate)
2021-10-24 20:27:14 +00:00
fmt.Println(3)
heartRateCh, cancel, err := client.WatchHeartRate()
onClose = append(onClose, cancel)
go func() {
for heartRate := range heartRateCh {
// Change text of heart rate label
heartRateLbl.Text = fmt.Sprintf("%d BPM", heartRate)
// Refresh label
heartRateLbl.Refresh()
}
}()
fmt.Println(4)
2021-08-26 04:18:24 +00:00
2021-08-26 15:47:17 +00:00
// Create label for battery level
2021-08-26 04:18:24 +00:00
battLevelLbl := newText("0%", 24)
2021-08-26 15:47:17 +00:00
// Create container to store battery level section
2021-08-26 04:18:24 +00:00
battLevel := container.NewVBox(
newText("Battery Level", 12),
battLevelLbl,
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(battLevel)
2021-10-24 20:27:14 +00:00
fmt.Println(5)
battLevelCh, cancel, err := client.WatchBatteryLevel()
onClose = append(onClose, cancel)
go func() {
for battLevel := range battLevelCh {
// Change text of battery level label
battLevelLbl.Text = fmt.Sprintf("%d%%", battLevel)
// Refresh label
battLevelLbl.Refresh()
}
}()
fmt.Println(6)
2021-08-26 04:18:24 +00:00
2021-10-24 20:27:14 +00:00
fmt.Println(7)
fwVerString, err := client.Version()
2021-08-26 04:18:24 +00:00
if err != nil {
2021-08-27 15:47:24 +00:00
guiErr(err, "Error getting firmware string", true, parent)
2021-08-26 04:18:24 +00:00
}
2021-10-24 20:27:14 +00:00
fmt.Println(8)
2021-08-26 04:18:24 +00:00
fwVer := container.NewVBox(
newText("Firmware Version", 12),
2021-10-24 20:27:14 +00:00
newText(fwVerString, 24),
2021-08-26 04:18:24 +00:00
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(fwVer)
2021-10-24 20:27:14 +00:00
fmt.Println(9)
btAddrString, err := client.Address()
2021-08-26 04:18:24 +00:00
if err != nil {
panic(err)
}
2021-10-24 20:27:14 +00:00
fmt.Println(10)
2021-08-26 04:18:24 +00:00
btAddr := container.NewVBox(
newText("Bluetooth Address", 12),
2021-10-24 20:27:14 +00:00
newText(btAddrString, 24),
2021-08-26 04:18:24 +00:00
canvas.NewLine(theme.ShadowColor()),
)
infoLayout.Add(btAddr)
return infoLayout
}
func getResp(line []byte) (*types.Response, error) {
var res types.Response
err := json.Unmarshal(line, &res)
if err != nil {
return nil, err
}
if res.Error {
return nil, errors.New(res.Message)
}
return &res, nil
}
func newText(t string, size float32) *canvas.Text {
text := canvas.NewText(t, theme.ForegroundColor())
text.TextSize = size
return text
}