Skip to content

Commit

Permalink
Add time since last weather update to weather app
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dariusarnold authored Mar 2, 2025
2 parents e4aaad6 + cb4e7d6 commit 09df02e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
46 changes: 43 additions & 3 deletions src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ namespace {
}
}

Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
: settingsController {settingsController}, weatherService {weatherService} {
Weather::Weather(Controllers::Settings& settingsController,
Controllers::SimpleWeatherService& weatherService,
Controllers::DateTime& dateTimeController)
: settingsController {settingsController}, weatherService {weatherService}, dateTimeController {dateTimeController} {

temperature = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_label_set_text(temperature, "---");
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -32);
lv_obj_set_auto_realign(temperature, true);

lastUpdated = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(lastUpdated, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
lv_label_set_text(lastUpdated, "");

minTemperature = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
lv_label_set_text(minTemperature, "");
Expand Down Expand Up @@ -137,10 +143,44 @@ void Weather::Refresh() {
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);

std::chrono::seconds secondsSinceEpoch =
std::chrono::duration_cast<std::chrono::seconds>(dateTimeController.CurrentDateTime().time_since_epoch());
int32_t secondsSinceWeatherUpdate = secondsSinceEpoch.count() - optCurrentWeather->timestamp;
int8_t minutesSinceWeatherUpdate = secondsSinceWeatherUpdate / 60;
int8_t hoursSinceWeatherUpdate = secondsSinceWeatherUpdate / 3600;

constexpr uint8_t Y_POSITION = 108;
constexpr uint8_t X_SINGLE_DIGIT_POSITION = 90;
constexpr uint8_t X_TWO_DIGIT_POSITION = 78;
constexpr uint8_t X_NOW_POSITION = 102;

lv_obj_set_pos(lastUpdated, X_SINGLE_DIGIT_POSITION, Y_POSITION);

if (hoursSinceWeatherUpdate > 0) {
if (hoursSinceWeatherUpdate > 9) {
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
}
lv_label_set_text_fmt(lastUpdated, "%dh ago", hoursSinceWeatherUpdate);
} else if (minutesSinceWeatherUpdate > 0) {
if (minutesSinceWeatherUpdate > 9 && minutesSinceWeatherUpdate < 60) {
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
}
lv_label_set_text_fmt(lastUpdated, "%dm ago", minutesSinceWeatherUpdate);
} else if (secondsSinceWeatherUpdate > 30) {
if (secondsSinceWeatherUpdate > 9 && secondsSinceWeatherUpdate < 60) {
lv_obj_set_pos(lastUpdated, X_TWO_DIGIT_POSITION, Y_POSITION);
}
lv_label_set_text_fmt(lastUpdated, "%ds ago", secondsSinceWeatherUpdate);
} else if (secondsSinceWeatherUpdate < 31) {
lv_obj_set_pos(lastUpdated, X_NOW_POSITION, Y_POSITION);
lv_label_set_text_fmt(lastUpdated, "Now", secondsSinceWeatherUpdate);
}
} else {
lv_label_set_text(icon, "");
lv_label_set_text(condition, "");
lv_label_set_text(temperature, "---");
lv_label_set_text(lastUpdated, "");
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_label_set_text(minTemperature, "");
lv_label_set_text(maxTemperature, "");
Expand Down
9 changes: 7 additions & 2 deletions src/displayapp/screens/Weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <lvgl/lvgl.h>
#include "displayapp/screens/Screen.h"
#include "components/ble/SimpleWeatherService.h"
#include "components/datetime/DateTimeController.h"
#include "displayapp/apps/Apps.h"
#include "displayapp/Controllers.h"
#include "Symbols.h"
Expand All @@ -20,21 +21,25 @@ namespace Pinetime {

class Weather : public Screen {
public:
Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService);
Weather(Controllers::Settings& settingsController,
Controllers::SimpleWeatherService& weatherService,
Controllers::DateTime& dateTimeController);
~Weather() override;

void Refresh() override;

private:
Controllers::Settings& settingsController;
Controllers::SimpleWeatherService& weatherService;
Controllers::DateTime& dateTimeController;

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

lv_obj_t* icon;
lv_obj_t* condition;
lv_obj_t* temperature;
lv_obj_t* lastUpdated;
lv_obj_t* minTemperature;
lv_obj_t* maxTemperature;
lv_obj_t* forecast;
Expand All @@ -49,7 +54,7 @@ namespace Pinetime {
static constexpr const char* icon = Screens::Symbols::cloudSunRain;

static Screens::Screen* Create(AppControllers& controllers) {
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
return new Screens::Weather(controllers.settingsController, *controllers.weatherController, controllers.dateTimeController);
};
};
}
Expand Down

0 comments on commit 09df02e

Please sign in to comment.