Skip to content

Commit

Permalink
weather: Pad forecast temperatures
Browse files Browse the repository at this point in the history
This ensures temperatures are correctly aligned with one another
  • Loading branch information
vkareh committed Feb 10, 2024
1 parent 851fb29 commit 13f46d6
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,32 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
// LV_TABLE_PART_CELL1: Default table style
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, Colors::lightGray);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 6);
// LV_TABLE_PART_CELL2: Condition icon
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, 6);
// LV_TABLE_PART_CELL3: Freezing
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, Colors::blue);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, 6);
// LV_TABLE_PART_CELL4: Ice
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_CYAN);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, 6);
// LV_TABLE_PART_CELL5: Normal
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, Colors::orange);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, 6);
// LV_TABLE_PART_CELL6: Hot
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, Colors::deepOrange);
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, 6);

lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
lv_table_set_col_width(forecast, i, 48);
lv_table_set_cell_type(forecast, 1, i, LV_TABLE_PART_CELL2);
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_RIGHT);
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_RIGHT);
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_RIGHT);
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_RIGHT);
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_CENTER);
}

taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
Expand Down Expand Up @@ -173,8 +167,27 @@ void Weather::Refresh() {
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
lv_table_set_cell_value(forecast, 0, i, WeekDays[wday]);
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
// Pad cells based on the largest number of digits on each column
std::string maxPadding = "";
std::string minPadding = "";
int maxSize = snprintf(nullptr, 0, "%d", maxTemp);
int minSize = snprintf(nullptr, 0, "%d", minTemp);
switch (maxSize - minSize) {
case -2:
maxPadding = " ";
break;
case -1:
maxPadding = " ";
break;
case 1:
minPadding = " ";
break;
case 2:
minPadding = " ";
break;
}
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding.data(), maxTemp);
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding.data(), minTemp);
}
} else {
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
Expand Down

0 comments on commit 13f46d6

Please sign in to comment.