浏览代码

Fix weather widget making HTTP request as part of validation

Which would prevent app startup if there was no internet connection
Svilen Markov 1 年之前
父节点
当前提交
d2f5dbbc26
共有 1 个文件被更改,包括 15 次插入8 次删除
  1. 15 8
      internal/widget/weather.go

+ 15 - 8
internal/widget/weather.go

@@ -27,6 +27,10 @@ var timeLabels24h = [12]string{"02:00", "04:00", "06:00", "08:00", "10:00", "12:
 func (widget *Weather) Initialize() error {
 func (widget *Weather) Initialize() error {
 	widget.withTitle("Weather").withCacheOnTheHour()
 	widget.withTitle("Weather").withCacheOnTheHour()
 
 
+	if widget.Location == "" {
+		return fmt.Errorf("location must be specified for weather widget")
+	}
+
 	if widget.HourFormat == "" || widget.HourFormat == "12h" {
 	if widget.HourFormat == "" || widget.HourFormat == "12h" {
 		widget.TimeLabels = timeLabels12h
 		widget.TimeLabels = timeLabels12h
 	} else if widget.HourFormat == "24h" {
 	} else if widget.HourFormat == "24h" {
@@ -41,18 +45,21 @@ func (widget *Weather) Initialize() error {
 		return fmt.Errorf("invalid units '%s' for weather, must be either metric or imperial", widget.Units)
 		return fmt.Errorf("invalid units '%s' for weather, must be either metric or imperial", widget.Units)
 	}
 	}
 
 
-	place, err := feed.FetchPlaceFromName(widget.Location)
-
-	if err != nil {
-		return fmt.Errorf("failed fetching data for %s: %v", widget.Location, err)
-	}
-
-	widget.Place = place
-
 	return nil
 	return nil
 }
 }
 
 
 func (widget *Weather) Update(ctx context.Context) {
 func (widget *Weather) Update(ctx context.Context) {
+	if widget.Place == nil {
+		place, err := feed.FetchPlaceFromName(widget.Location)
+
+		if err != nil {
+			widget.withError(err).scheduleEarlyUpdate()
+			return
+		}
+
+		widget.Place = place
+	}
+
 	weather, err := feed.FetchWeatherForPlace(widget.Place, widget.Units)
 	weather, err := feed.FetchWeatherForPlace(widget.Place, widget.Units)
 
 
 	if !widget.canContinueUpdateAfterHandlingErr(err) {
 	if !widget.canContinueUpdateAfterHandlingErr(err) {