glance/internal/widget/stocks.go

43 lines
866 B
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
// TODO: rename to Markets at some point
2024-04-27 21:10:24 +02:00
type Stocks struct {
widgetBase `yaml:",inline"`
Stocks feed.Stocks `yaml:"stocks"`
Sort string `yaml:"sort-by"`
Style string `yaml:"style"`
2024-04-27 21:10:24 +02:00
}
func (widget *Stocks) Initialize() error {
widget.withTitle("Stocks").withCacheDuration(time.Hour)
return nil
}
func (widget *Stocks) Update(ctx context.Context) {
stocks, err := feed.FetchStocksDataFromYahoo(widget.Stocks)
2024-04-27 21:10:24 +02:00
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
2024-05-01 23:28:59 +02:00
if widget.Sort == "absolute-change" {
stocks.SortByAbsChange()
}
2024-04-27 21:10:24 +02:00
widget.Stocks = stocks
}
func (widget *Stocks) Render() template.HTML {
return widget.render(widget, assets.StocksTemplate)
}