From 9ed08a15421cc389ab3b30325e7ff9c1c0c622d7 Mon Sep 17 00:00:00 2001 From: Sebin Suresh Date: Sat, 22 Mar 2025 11:19:26 -0500 Subject: [PATCH] fix: avoid calling weather.get twice in draw loop --- apps/timecal/timecal.app.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/timecal/timecal.app.js b/apps/timecal/timecal.app.js index 20b99d510..ec5f5822f 100644 --- a/apps/timecal/timecal.app.js +++ b/apps/timecal/timecal.app.js @@ -42,8 +42,11 @@ class TimeCalClock{ // X coord to center date and time text at this.dtCenterX = Bangle.appRect.w/2; - this.hasWeather = this.settings().showWeather && require('weather') && require('weather').get(); - if(this.hasWeather){ + this.weather = undefined; + if(this.settings().showWeather && require('weather')){ + this.weather = require('weather').get(); + } + if(this.weather){ this.dtCenterX =2*Bangle.appRect.w/3; } this.nrgb = [g.theme.fg, "#E00", "#0E0", "#00E"]; //fg, r ,g , b @@ -83,9 +86,11 @@ class TimeCalClock{ // require("weather").get = undefined; const d = this.date ? this.date : new Date(); - this.hasWeather = this.settings().showWeather && require('weather') && require('weather').get(); + if (this.settings().showWeather && require('weather')){ + this.weather = require('weather').get(); + } const prevCenterX = this.dtCenterX; - if(this.hasWeather){ + if(this.weather){ this.dtCenterX = 2 * Bangle.appRect.w / 3; } else { this.dtCenterX = Bangle.appRect.w / 2; @@ -98,14 +103,13 @@ class TimeCalClock{ if (this.TZOffset===undefined || this.TZOffset!==d.getTimezoneOffset()) this.drawDateAndCal(); - if(this.hasWeather){ + if(this.weather){ this.drawWeather(); } } drawWeather() { - const weather = require('weather'); - const curr = weather.get(); + const curr = this.weather; const temp = require("locale").temp(curr.temp-273.15).match(/^(\D*\d*)(.*)$/)[0]; const iconRadius = 20; const widgetHeight = 24; @@ -115,7 +119,7 @@ class TimeCalClock{ Bangle.appRect.w/3, Bangle.appRect.y + widgetHeight + iconRadius ); - weather.drawIcon(curr, Bangle.appRect.x + widgetHeight, Bangle.appRect.y + widgetHeight - 4, iconRadius); + (require('weather')).drawIcon(curr, Bangle.appRect.x + widgetHeight, Bangle.appRect.y + widgetHeight - 4, iconRadius); g .setFontAlign(0, -1) .setFont('6x8', 2)