Skip to content

Commit b922200

Browse files
Merge pull request #7 from JustScott/weather_time_since_last_update
Weather time since last update
2 parents 2d58e2a + 40a5142 commit b922200

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎src/displayapp/screens/Weather.cpp‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ namespace {
2424
}
2525
}
2626

27-
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
28-
: settingsController {settingsController}, weatherService {weatherService} {
27+
Weather::Weather(Controllers::Settings& settingsController,
28+
Controllers::SimpleWeatherService& weatherService,
29+
Controllers::DateTime& dateTimeController)
30+
: settingsController {settingsController}, weatherService {weatherService}, dateTimeController {dateTimeController} {
2931

3032
temperature = lv_label_create(lv_scr_act(), nullptr);
3133
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
3234
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
3335
lv_label_set_text(temperature, "---");
34-
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
36+
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -32);
3537
lv_obj_set_auto_realign(temperature, true);
3638

39+
lastUpdated = lv_label_create(lv_scr_act(), nullptr);
40+
lv_obj_set_style_local_text_color(lastUpdated, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
41+
lv_label_set_text(lastUpdated, "");
42+
3743
minTemperature = lv_label_create(lv_scr_act(), nullptr);
3844
lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
3945
lv_label_set_text(minTemperature, "");
@@ -123,10 +129,41 @@ void Weather::Refresh() {
123129
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
124130
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
125131
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
132+
133+
auto secondsSinceEpoch = std::chrono::duration_cast<std::chrono::seconds>(dateTimeController.CurrentDateTime().time_since_epoch());
134+
int32_t secondsSinceWeatherUpdate = secondsSinceEpoch.count() - optCurrentWeather->timestamp;
135+
int8_t minutesSinceWeatherUpdate = secondsSinceWeatherUpdate / 60;
136+
int8_t hoursSinceWeatherUpdate = secondsSinceWeatherUpdate / 3600;
137+
138+
constexpr uint8_t Y_POSITION = 108;
139+
constexpr uint8_t X_SINGLE_DIGIT_POSITION = 90;
140+
constexpr uint8_t X_TWO_DIGIT_POSITION = 78;
141+
constexpr uint8_t X_NOW_POSITION = 102;
142+
143+
lv_obj_set_pos(lastUpdated, X_SINGLE_DIGIT_POSITION, Y_POSITION);
144+
145+
if (hoursSinceWeatherUpdate > 0) {
146+
if (hoursSinceWeatherUpdate > 9) {
147+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
148+
}
149+
lv_label_set_text_fmt(lastUpdated, "%dh ago", hoursSinceWeatherUpdate);
150+
} else if (minutesSinceWeatherUpdate > 0) {
151+
if (minutesSinceWeatherUpdate > 9) {
152+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
153+
}
154+
lv_label_set_text_fmt(lastUpdated, "%dm ago", minutesSinceWeatherUpdate);
155+
} else if (secondsSinceWeatherUpdate > 30) {
156+
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
157+
lv_label_set_text_fmt(lastUpdated, "%ds ago", secondsSinceWeatherUpdate);
158+
} else if (secondsSinceWeatherUpdate < 31) {
159+
lv_obj_set_pos(lastUpdated, X_NOW_POSITION, Y_POSITION);
160+
lv_label_set_text_fmt(lastUpdated, "Now", secondsSinceWeatherUpdate);
161+
}
126162
} else {
127163
lv_label_set_text(icon, "");
128164
lv_label_set_text(condition, "");
129165
lv_label_set_text(temperature, "---");
166+
lv_label_set_text(lastUpdated, "");
130167
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
131168
lv_label_set_text(minTemperature, "");
132169
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
static bool IsAvailable(Pinetime::Controllers::FS& /*filesystem*/) {

0 commit comments

Comments
 (0)