Skip to content

Commit f7c87a7

Browse files
FintasticManJF002
authored andcommitted
weather: Switch to std::optional for Forecast days
Also only iterate over the number of days actually in use, rather than MaxNbForecastDays.
1 parent e247bd7 commit f7c87a7

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/components/ble/SimpleWeatherService.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace {
5252
SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
5353
auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2]));
5454

55-
std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days;
55+
std::array<std::optional<SimpleWeatherService::Forecast::Day>, SimpleWeatherService::MaxNbForecastDays> days;
5656
const uint8_t nbDaysInBuffer = dataBuffer[10];
5757
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
5858
for (int i = 0; i < nbDays; i++) {
@@ -112,9 +112,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
112112
for (int i = 0; i < 5; i++) {
113113
NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d",
114114
i,
115-
forecast->days[i].minTemperature.PreciseCelsius(),
116-
forecast->days[i].maxTemperature.PreciseCelsius(),
117-
forecast->days[i].iconId);
115+
forecast->days[i]->minTemperature.PreciseCelsius(),
116+
forecast->days[i]->maxTemperature.PreciseCelsius(),
117+
forecast->days[i]->iconId);
118118
}
119119
}
120120
break;

src/components/ble/SimpleWeatherService.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace Pinetime {
6363

6464
class Temperature {
6565
public:
66-
explicit Temperature(int16_t raw = 0) : raw {raw} {
66+
explicit Temperature(int16_t raw) : raw {raw} {
6767
}
6868

6969
[[nodiscard]] int16_t PreciseCelsius() const {
@@ -129,7 +129,7 @@ namespace Pinetime {
129129
bool operator==(const Day& other) const;
130130
};
131131

132-
std::array<Day, MaxNbForecastDays> days;
132+
std::array<std::optional<Day>, MaxNbForecastDays> days;
133133

134134
bool operator==(const Forecast& other) const;
135135
};

src/displayapp/screens/Weather.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ void Weather::Refresh() {
153153
if (optCurrentForecast) {
154154
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
155155

156-
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
157-
int16_t minTemp = optCurrentForecast->days[i].maxTemperature.Celsius();
158-
int16_t maxTemp = optCurrentForecast->days[i].minTemperature.Celsius();
156+
for (int i = 0; i < optCurrentForecast->nbDays; i++) {
157+
int16_t minTemp = optCurrentForecast->days[i]->maxTemperature.Celsius();
158+
int16_t maxTemp = optCurrentForecast->days[i]->minTemperature.Celsius();
159159
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
160-
minTemp = optCurrentForecast->days[i].maxTemperature.Fahrenheit();
161-
maxTemp = optCurrentForecast->days[i].minTemperature.Fahrenheit();
160+
minTemp = optCurrentForecast->days[i]->maxTemperature.Fahrenheit();
161+
maxTemp = optCurrentForecast->days[i]->minTemperature.Fahrenheit();
162162
}
163-
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i].maxTemperature));
164-
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i].minTemperature));
163+
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i]->maxTemperature));
164+
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i]->minTemperature));
165165
uint8_t wday = localTime.tm_wday + i + 1;
166166
if (wday > 7) {
167167
wday -= 7;
168168
}
169169
const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday));
170170
lv_table_set_cell_value(forecast, 0, i, dayOfWeek);
171-
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
171+
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i]->iconId));
172172
// Pad cells based on the largest number of digits on each column
173173
char maxPadding[3] = " ";
174174
char minPadding[3] = " ";

0 commit comments

Comments
 (0)