@@ -47,6 +47,7 @@ type WeatherData struct {
4747 unit string
4848
4949 refresh time.Time
50+ ready bool
5051
5152 response string
5253 responseMutex sync.RWMutex
@@ -57,6 +58,7 @@ func (w *WeatherData) Condition() (string, error) {
5758 w .responseMutex .RLock ()
5859 defer w .responseMutex .RUnlock ()
5960
61+ w .ready = false
6062 if strings .Contains (w .response , "Unknown location" ) {
6163 fmt .Println ("unknown location:" , w .location )
6264 return "" , nil
@@ -75,6 +77,7 @@ func (w *WeatherData) Temperature() (string, error) {
7577 w .responseMutex .RLock ()
7678 defer w .responseMutex .RUnlock ()
7779
80+ w .ready = false
7881 if strings .Contains (w .response , "Unknown location" ) {
7982 fmt .Println ("unknown location:" , w .location )
8083 return "" , nil
@@ -93,7 +96,7 @@ func (w *WeatherData) Ready() bool {
9396 w .responseMutex .RLock ()
9497 defer w .responseMutex .RUnlock ()
9598
96- return len ( w . response ) > 0
99+ return w . ready
97100}
98101
99102// Fetch retrieves weather data when required.
@@ -123,12 +126,11 @@ func (w *WeatherData) Fetch() {
123126
124127 w .refresh = time .Now ()
125128 w .response = string (body )
129+ w .ready = true
126130}
127131
128132// NewWeatherWidget returns a new WeatherWidget.
129133func NewWeatherWidget (bw * BaseWidget , opts WidgetConfig ) (* WeatherWidget , error ) {
130- bw .setInterval (opts .Interval , 60000 )
131-
132134 var location , unit , theme string
133135 _ = ConfigValue (opts .Config ["location" ], & location )
134136 _ = ConfigValue (opts .Config ["unit" ], & unit )
@@ -138,6 +140,9 @@ func NewWeatherWidget(bw *BaseWidget, opts WidgetConfig) (*WeatherWidget, error)
138140 if err != nil {
139141 return nil , err
140142 }
143+ // this needs to be called after NewButtonWidget, otherwise its value gets
144+ // overwritten by it.
145+ bw .setInterval (opts .Interval , 60000 )
141146
142147 return & WeatherWidget {
143148 ButtonWidget : widget ,
@@ -149,10 +154,20 @@ func NewWeatherWidget(bw *BaseWidget, opts WidgetConfig) (*WeatherWidget, error)
149154 }, nil
150155}
151156
157+ // RequiresUpdate returns true when the widget wants to be repainted.
158+ func (w * WeatherWidget ) RequiresUpdate () bool {
159+ return w .data .Ready () || w .ButtonWidget .RequiresUpdate ()
160+ }
161+
152162// Update renders the widget.
153163func (w * WeatherWidget ) Update (dev * streamdeck.Device ) error {
154164 go w .data .Fetch ()
155165 if ! w .data .Ready () {
166+ if w .data .response == "" {
167+ // show the background image while we wait for initial data
168+ return w .render (dev , nil )
169+ }
170+
156171 return nil
157172 }
158173
0 commit comments