Skip to content

Commit 09df02e

Browse files
authored
Add time since last weather update to weather app
Provides the time since the last weather update, incrementing the duration from seconds to minutes at 60 seconds, and minutes to hours at 3600 seconds. This changes is from InfiniTimeOrg#2242.
2 parents e4aaad6 + cb4e7d6 commit 09df02e

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

src/displayapp/screens/Weather.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ namespace {
3535
}
3636
}
3737

38-
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
39-
: settingsController {settingsController}, weatherService {weatherService} {
38+
Weather::Weather(Controllers::Settings& settingsController,
39+
Controllers::SimpleWeatherService& weatherService,
40+
Controllers::DateTime& dateTimeController)
41+
: settingsController {settingsController}, weatherService {weatherService}, dateTimeController {dateTimeController} {
4042

4143
temperature = lv_label_create(lv_scr_act(), nullptr);
4244
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
4345
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
4446
lv_label_set_text(temperature, "---");
45-
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
47+
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -32);
4648
lv_obj_set_auto_realign(temperature, true);
4749

50+
lastUpdated = lv_label_create(lv_scr_act(), nullptr);
51+
lv_obj_set_style_local_text_color(lastUpdated, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
52+
lv_label_set_text(lastUpdated, "");
53+
4854
minTemperature = lv_label_create(lv_scr_act(), nullptr);
4955
lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
5056
lv_label_set_text(minTemperature, "");
@@ -137,10 +143,44 @@ void Weather::Refresh() {
137143
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
138144
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
139145
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
146+
147+
std::chrono::seconds secondsSinceEpoch =
148+
std::chrono::duration_cast<std::chrono::seconds>(dateTimeController.CurrentDateTime().time_since_epoch());
149+
int32_t secondsSinceWeatherUpdate = secondsSinceEpoch.count() - optCurrentWeather->timestamp;
150+
int8_t minutesSinceWeatherUpdate = secondsSinceWeatherUpdate / 60;
151+
int8_t hoursSinceWeatherUpdate = secondsSinceWeatherUpdate / 3600;
152+
153+
constexpr uint8_t Y_POSITION = 108;
154+
constexpr uint8_t X_SINGLE_DIGIT_POSITION = 90;
155+
constexpr uint8_t X_TWO_DIGIT_POSITION = 78;
156+
constexpr uint8_t X_NOW_POSITION = 102;
157+
158+
lv_obj_set_pos(lastUpdated, X_SINGLE_DIGIT_POSITION, Y_POSITION);
159+
160+
if (hoursSinceWeatherUpdate > 0) {
161+
if (hoursSinceWeatherUpdate > 9) {
162+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
163+
}
164+
lv_label_set_text_fmt(lastUpdated, "%dh ago", hoursSinceWeatherUpdate);
165+
} else if (minutesSinceWeatherUpdate > 0) {
166+
if (minutesSinceWeatherUpdate > 9 && minutesSinceWeatherUpdate < 60) {
167+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
168+
}
169+
lv_label_set_text_fmt(lastUpdated, "%dm ago", minutesSinceWeatherUpdate);
170+
} else if (secondsSinceWeatherUpdate > 30) {
171+
if (secondsSinceWeatherUpdate > 9 && secondsSinceWeatherUpdate < 60) {
172+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
173+
}
174+
lv_label_set_text_fmt(lastUpdated, "%ds ago", secondsSinceWeatherUpdate);
175+
} else if (secondsSinceWeatherUpdate < 31) {
176+
lv_obj_set_pos(lastUpdated, X_NOW_POSITION, Y_POSITION);
177+
lv_label_set_text_fmt(lastUpdated, "Now", secondsSinceWeatherUpdate);
178+
}
140179
} else {
141180
lv_label_set_text(icon, "");
142181
lv_label_set_text(condition, "");
143182
lv_label_set_text(temperature, "---");
183+
lv_label_set_text(lastUpdated, "");
144184
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
145185
lv_label_set_text(minTemperature, "");
146186
lv_label_set_text(maxTemperature, "");

src/displayapp/screens/Weather.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <lvgl/lvgl.h>
55
#include "displayapp/screens/Screen.h"
66
#include "components/ble/SimpleWeatherService.h"
7+
#include "components/datetime/DateTimeController.h"
78
#include "displayapp/apps/Apps.h"
89
#include "displayapp/Controllers.h"
910
#include "Symbols.h"
@@ -20,21 +21,25 @@ namespace Pinetime {
2021

2122
class Weather : public Screen {
2223
public:
23-
Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService);
24+
Weather(Controllers::Settings& settingsController,
25+
Controllers::SimpleWeatherService& weatherService,
26+
Controllers::DateTime& dateTimeController);
2427
~Weather() override;
2528

2629
void Refresh() override;
2730

2831
private:
2932
Controllers::Settings& settingsController;
3033
Controllers::SimpleWeatherService& weatherService;
34+
Controllers::DateTime& dateTimeController;
3135

3236
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::CurrentWeather>> currentWeather {};
3337
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::Forecast>> currentForecast {};
3438

3539
lv_obj_t* icon;
3640
lv_obj_t* condition;
3741
lv_obj_t* temperature;
42+
lv_obj_t* lastUpdated;
3843
lv_obj_t* minTemperature;
3944
lv_obj_t* maxTemperature;
4045
lv_obj_t* forecast;
@@ -49,7 +54,7 @@ namespace Pinetime {
4954
static constexpr const char* icon = Screens::Symbols::cloudSunRain;
5055

5156
static Screens::Screen* Create(AppControllers& controllers) {
52-
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
57+
return new Screens::Weather(controllers.settingsController, *controllers.weatherController, controllers.dateTimeController);
5358
};
5459
};
5560
}

0 commit comments

Comments
 (0)