Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time since last weather update to weather app #2242

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ 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);
Expand All @@ -45,6 +47,11 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
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_fmt(lastUpdated, "");
lv_obj_align(lastUpdated, nullptr, LV_ALIGN_CENTER, -40, 0);

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,6 +144,18 @@ 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);

int64_t secondsSinceEpoch = dateTimeController.CurrentDateTime().time_since_epoch().count() / 1000000000;
int64_t secondsSinceWeatherUpdate = secondsSinceEpoch - optCurrentWeather->timestamp;
if (secondsSinceWeatherUpdate < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the 0 check? Unless the number of seconds somehow becomes negative, it shouldn't be necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did have it showing -1 a few times in the simulator, so figured I would add that just to cover it in the off chance it happened on the watch.

lv_label_set_text_fmt(lastUpdated, "0s old", secondsSinceWeatherUpdate);
} else if (secondsSinceWeatherUpdate < 60) {
lv_label_set_text_fmt(lastUpdated, "%ds old", secondsSinceWeatherUpdate);
} else if (secondsSinceWeatherUpdate > 59 && secondsSinceWeatherUpdate < 3600) {
lv_label_set_text_fmt(lastUpdated, "%dm old", secondsSinceWeatherUpdate / 60);
} else if (secondsSinceWeatherUpdate > 3599) {
lv_label_set_text_fmt(lastUpdated, "%dh old", secondsSinceWeatherUpdate / 3600);
}
} else {
lv_label_set_text(icon, "");
lv_label_set_text(condition, "");
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