mirror of
https://github.com/glanceapp/glance.git
synced 2024-11-22 16:34:35 +01:00
1bebb88d0e
Also fix bug that would remove markets if a network request failed and not show them again until Glance was restarted
48 lines
1.1 KiB
Go
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)
|
|
}
|