glance/internal/widget/stocks.go
Svilen Markov 1bebb88d0e Rename Stocks to Markets
Also fix bug that would remove markets if a network request failed
and not show them again until Glance was restarted
2024-06-02 18:21:02 +01:00

48 lines
1.1 KiB
Go

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:"-"`
}
func (widget *Markets) Initialize() error {
widget.withTitle("Markets").withCacheDuration(time.Hour)
if len(widget.MarketRequests) == 0 {
widget.MarketRequests = widget.StocksRequests
}
return nil
}
func (widget *Markets) Update(ctx context.Context) {
markets, err := feed.FetchMarketsDataFromYahoo(widget.MarketRequests)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if widget.Sort == "absolute-change" {
markets.SortByAbsChange()
}
widget.Markets = markets
}
func (widget *Markets) Render() template.HTML {
return widget.render(widget, assets.MarketsTemplate)
}