|
| 1 | +#include "displayapp/screens/Weather.h" |
| 2 | +#include <lvgl/lvgl.h> |
| 3 | +#include "components/ble/SimpleWeatherService.h" |
| 4 | +#include "components/settings/Settings.h" |
| 5 | +#include "displayapp/DisplayApp.h" |
| 6 | +#include "displayapp/screens/WeatherSymbols.h" |
| 7 | +#include "displayapp/InfiniTimeTheme.h" |
| 8 | + |
| 9 | +using namespace Pinetime::Applications::Screens; |
| 10 | + |
| 11 | +Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService) |
| 12 | + : settingsController {settingsController}, weatherService {weatherService} { |
| 13 | + |
| 14 | + temperature = lv_label_create(lv_scr_act(), nullptr); |
| 15 | + lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); |
| 16 | + lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); |
| 17 | + lv_label_set_text(temperature, "---"); |
| 18 | + lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30); |
| 19 | + lv_obj_set_auto_realign(temperature, true); |
| 20 | + |
| 21 | + minTemperature = lv_label_create(lv_scr_act(), nullptr); |
| 22 | + lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg); |
| 23 | + lv_label_set_text(minTemperature, ""); |
| 24 | + lv_obj_align(minTemperature, temperature, LV_ALIGN_OUT_LEFT_MID, -10, 0); |
| 25 | + lv_obj_set_auto_realign(minTemperature, true); |
| 26 | + |
| 27 | + maxTemperature = lv_label_create(lv_scr_act(), nullptr); |
| 28 | + lv_obj_set_style_local_text_color(maxTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg); |
| 29 | + lv_label_set_text(maxTemperature, ""); |
| 30 | + lv_obj_align(maxTemperature, temperature, LV_ALIGN_OUT_RIGHT_MID, 10, 0); |
| 31 | + lv_obj_set_auto_realign(maxTemperature, true); |
| 32 | + |
| 33 | + condition = lv_label_create(lv_scr_act(), nullptr); |
| 34 | + lv_obj_set_style_local_text_color(condition, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); |
| 35 | + lv_label_set_text(condition, ""); |
| 36 | + lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, -10); |
| 37 | + lv_obj_set_auto_realign(condition, true); |
| 38 | + |
| 39 | + icon = lv_label_create(lv_scr_act(), nullptr); |
| 40 | + lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); |
| 41 | + lv_obj_set_style_local_text_font(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons); |
| 42 | + lv_label_set_text(icon, ""); |
| 43 | + lv_obj_align(icon, condition, LV_ALIGN_OUT_TOP_MID, 0, 0); |
| 44 | + lv_obj_set_auto_realign(icon, true); |
| 45 | + |
| 46 | + forecast = lv_table_create(lv_scr_act(), nullptr); |
| 47 | + lv_table_set_col_cnt(forecast, Controllers::SimpleWeatherService::MaxNbForecastDays); |
| 48 | + lv_table_set_row_cnt(forecast, 4); |
| 49 | + // LV_TABLE_PART_CELL1: Default table style |
| 50 | + lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK); |
| 51 | + lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, Colors::lightGray); |
| 52 | + // LV_TABLE_PART_CELL2: Condition icon |
| 53 | + lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLACK); |
| 54 | + lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE); |
| 55 | + lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons); |
| 56 | + |
| 57 | + lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0); |
| 58 | + |
| 59 | + for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { |
| 60 | + lv_table_set_col_width(forecast, i, 48); |
| 61 | + lv_table_set_cell_type(forecast, 1, i, LV_TABLE_PART_CELL2); |
| 62 | + lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER); |
| 63 | + lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER); |
| 64 | + lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_CENTER); |
| 65 | + lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_CENTER); |
| 66 | + } |
| 67 | + |
| 68 | + taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); |
| 69 | +} |
| 70 | + |
| 71 | +Weather::~Weather() { |
| 72 | + lv_task_del(taskRefresh); |
| 73 | + lv_obj_clean(lv_scr_act()); |
| 74 | +} |
| 75 | + |
| 76 | +void Weather::Refresh() { |
| 77 | + currentWeather = weatherService.Current(); |
| 78 | + if (currentWeather.IsUpdated()) { |
| 79 | + auto optCurrentWeather = currentWeather.Get(); |
| 80 | + if (optCurrentWeather) { |
| 81 | + int16_t temp = optCurrentWeather->temperature; |
| 82 | + int16_t minTemp = optCurrentWeather->minTemperature; |
| 83 | + int16_t maxTemp = optCurrentWeather->maxTemperature; |
| 84 | + lv_color_t color = Colors::orange; |
| 85 | + if (temp <= 0) { // freezing |
| 86 | + color = Colors::blue; |
| 87 | + } else if (temp <= 400) { // ice danger |
| 88 | + color = LV_COLOR_CYAN; |
| 89 | + } else if (temp >= 2700) { // hot |
| 90 | + color = Colors::deepOrange; |
| 91 | + } |
| 92 | + char tempUnit = 'C'; |
| 93 | + if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { |
| 94 | + temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); |
| 95 | + minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp); |
| 96 | + maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); |
| 97 | + tempUnit = 'F'; |
| 98 | + } |
| 99 | + temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); |
| 100 | + minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0); |
| 101 | + maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0); |
| 102 | + lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId)); |
| 103 | + lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId)); |
| 104 | + lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit); |
| 105 | + lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color); |
| 106 | + lv_label_set_text_fmt(minTemperature, "%d°", minTemp); |
| 107 | + lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp); |
| 108 | + } else { |
| 109 | + lv_label_set_text(icon, ""); |
| 110 | + lv_label_set_text(condition, ""); |
| 111 | + lv_label_set_text(temperature, "---"); |
| 112 | + lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); |
| 113 | + lv_label_set_text(minTemperature, ""); |
| 114 | + lv_label_set_text(maxTemperature, ""); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + currentForecast = weatherService.GetForecast(); |
| 119 | + if (currentForecast.IsUpdated()) { |
| 120 | + auto optCurrentForecast = currentForecast.Get(); |
| 121 | + if (optCurrentForecast) { |
| 122 | + std::tm localTime = *std::localtime((const time_t*) &optCurrentForecast->timestamp); |
| 123 | + |
| 124 | + for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { |
| 125 | + int16_t maxTemp = optCurrentForecast->days[i].maxTemperature; |
| 126 | + int16_t minTemp = optCurrentForecast->days[i].minTemperature; |
| 127 | + if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { |
| 128 | + maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); |
| 129 | + minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp); |
| 130 | + } |
| 131 | + maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0); |
| 132 | + minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0); |
| 133 | + uint8_t wday = localTime.tm_wday + i + 1; |
| 134 | + if (wday > 6) { |
| 135 | + wday -= 7; |
| 136 | + } |
| 137 | + lv_table_set_cell_value(forecast, 0, i, WeekDays[wday]); |
| 138 | + lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId)); |
| 139 | + lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp); |
| 140 | + lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp); |
| 141 | + } |
| 142 | + } else { |
| 143 | + for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { |
| 144 | + lv_table_set_cell_value(forecast, 0, i, ""); |
| 145 | + lv_table_set_cell_value(forecast, 1, i, ""); |
| 146 | + lv_table_set_cell_value(forecast, 2, i, ""); |
| 147 | + lv_table_set_cell_value(forecast, 3, i, ""); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments