itd/cmd/itgui/info.go

87 lines
2.1 KiB
Go
Raw Permalink Normal View History

2021-08-26 04:18:24 +00:00
package main
import (
2022-05-01 22:22:28 +00:00
"context"
2021-08-26 04:18:24 +00:00
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
2021-10-24 20:27:14 +00:00
"go.arsenm.dev/itd/api"
2021-08-26 04:18:24 +00:00
)
2022-05-05 21:00:49 +00:00
func infoTab(ctx context.Context, client *api.Client, w fyne.Window) fyne.CanvasObject {
c := container.NewVBox()
2022-05-01 22:22:28 +00:00
2022-05-05 21:00:49 +00:00
// Create titled text for heart rate
heartRateText := newTitledText("Heart Rate", "0 BPM")
c.Add(heartRateText)
// Watch heart rate
2022-05-01 22:22:28 +00:00
heartRateCh, err := client.WatchHeartRate(ctx)
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error watching heart rate", true, w)
}
2021-10-24 20:27:14 +00:00
go func() {
2022-05-05 21:00:49 +00:00
// For every heart rate sample
2021-10-24 20:27:14 +00:00
for heartRate := range heartRateCh {
2022-05-05 21:00:49 +00:00
// Set body of titled text
heartRateText.SetBody(fmt.Sprintf("%d BPM", heartRate))
2021-10-24 20:27:14 +00:00
}
}()
2022-05-05 21:00:49 +00:00
// Create titled text for battery level
battLevelText := newTitledText("Battery Level", "0%")
c.Add(battLevelText)
// Watch battery level
battLevelCh, err := client.WatchBatteryLevel(ctx)
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error watching battery level", true, w)
}
go func() {
2022-05-05 21:00:49 +00:00
// For every battery level sample
for battLevel := range battLevelCh {
// Set body of titled text
battLevelText.SetBody(fmt.Sprintf("%d%%", battLevel))
}
}()
2021-08-26 04:18:24 +00:00
2022-05-05 21:00:49 +00:00
// Create titled text for step count
stepCountText := newTitledText("Step Count", "0 Steps")
c.Add(stepCountText)
// Watch step count
stepCountCh, err := client.WatchStepCount(ctx)
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error watching step count", true, w)
}
2021-10-24 20:27:14 +00:00
go func() {
2022-05-05 21:00:49 +00:00
// For every step count sample
for stepCount := range stepCountCh {
// Set body of titled text
stepCountText.SetBody(fmt.Sprintf("%d Steps", stepCount))
2021-10-24 20:27:14 +00:00
}
}()
2021-08-26 04:18:24 +00:00
2022-05-05 21:00:49 +00:00
// Create new titled text for address
addressText := newTitledText("Address", "")
c.Add(addressText)
// Get address
address, err := client.Address(ctx)
2021-08-26 04:18:24 +00:00
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error getting address", true, w)
2021-08-26 04:18:24 +00:00
}
2022-05-05 21:00:49 +00:00
// Set body of titled text
addressText.SetBody(address)
// Create new titled text for version
versionText := newTitledText("Version", "")
c.Add(versionText)
// Get version
version, err := client.Version(ctx)
2021-08-26 04:18:24 +00:00
if err != nil {
2022-05-05 21:00:49 +00:00
guiErr(err, "Error getting version", true, w)
2021-08-26 04:18:24 +00:00
}
2022-05-05 21:00:49 +00:00
// Set body of titled text
versionText.SetBody(version)
2021-08-26 04:18:24 +00:00
2022-05-05 21:00:49 +00:00
return container.NewVScroll(c)
2021-08-26 04:18:24 +00:00
}