glance/internal/widget/stocks.go

48 lines
1.1 KiB
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"
)
type Markets struct {
widgetBase `yaml:",inline"`
StocksRequests []feed.MarketRequest `yaml:"stocks"`
MarketRequests []feed.MarketRequest `yaml:"markets"`
Sort string `yaml:"sort-by"`
Style string `yaml:"style"`
Markets feed.Markets `yaml:"-"`
2024-04-27 21:10:24 +02:00
}
func (widget *Markets) Initialize() error {
widget.withTitle("Markets").withCacheDuration(time.Hour)
if len(widget.MarketRequests) == 0 {
widget.MarketRequests = widget.StocksRequests
}
2024-04-27 21:10:24 +02:00
return nil
}
func (widget *Markets) Update(ctx context.Context) {
markets, err := feed.FetchMarketsDataFromYahoo(widget.MarketRequests)
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" {
markets.SortByAbsChange()
2024-05-01 23:28:59 +02:00
}
widget.Markets = markets
2024-04-27 21:10:24 +02:00
}
func (widget *Markets) Render() template.HTML {
return widget.render(widget, assets.MarketsTemplate)
2024-04-27 21:10:24 +02:00
}