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"
|
|
|
|
)
|
|
|
|
|
2024-05-28 19:01:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-28 19:01:26 +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
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:01:26 +02:00
|
|
|
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" {
|
2024-05-28 19:01:26 +02:00
|
|
|
markets.SortByAbsChange()
|
2024-05-01 23:28:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:01:26 +02:00
|
|
|
widget.Markets = markets
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:01:26 +02:00
|
|
|
func (widget *Markets) Render() template.HTML {
|
|
|
|
return widget.render(widget, assets.MarketsTemplate)
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|