75 lines
2.1 KiB
C++
75 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include "ui/apps/travel/ui/main.h"
|
|
#include "ui/apps/travel/settings/settings_handler.h"
|
|
#include "ui/interaction_handler.h"
|
|
#include "external/mtr/arrival.h"
|
|
#include "external/mtr/mtr.h"
|
|
#include "network/network.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include "esp_err.h"
|
|
#include <memory>
|
|
#include <vector>
|
|
#include <atomic>
|
|
|
|
namespace travel {
|
|
|
|
/**
|
|
* @brief Main UI Handler for Travel app
|
|
*
|
|
* Manages the MainUI instance, polling task, and MTR API interactions.
|
|
* Runs a background task to periodically fetch arrival data.
|
|
*/
|
|
class MainUIHandler {
|
|
public:
|
|
// Callback type for settings button
|
|
using SettingsButtonCallback = void (*)(void* user_data);
|
|
|
|
MainUIHandler();
|
|
~MainUIHandler();
|
|
|
|
esp_err_t init(
|
|
lv_obj_t* parent,
|
|
InteractionHandler* interaction_handler,
|
|
SettingHandler* setting_handler,
|
|
NetworkHandler* network_handler
|
|
);
|
|
esp_err_t deinit();
|
|
|
|
void register_on_settings_button_clicked(SettingsButtonCallback cb, void* user_data);
|
|
void force_refresh();
|
|
|
|
private:
|
|
static void polling_task_(void* param);
|
|
static void on_settings_button_clicked_static_(lv_event_t* e);
|
|
|
|
void on_settings_button_clicked_();
|
|
void fetch_and_update_arrivals_();
|
|
bool has_arrival_data_changed_(const std::vector<RouteArrivalData>& new_data);
|
|
std::string format_arrival_time_(const std::string& api_time);
|
|
std::string format_arrival_time_full_(const std::string& api_time);
|
|
std::string get_current_time_string_();
|
|
|
|
std::unique_ptr<MainUI> main_ui_;
|
|
SettingHandler* setting_handler_ = nullptr;
|
|
NetworkHandler* network_handler_ = nullptr;
|
|
std::unique_ptr<MTRNextTrainHandler> mtr_handler_;
|
|
|
|
// Polling task
|
|
TaskHandle_t polling_task_handle_ = nullptr;
|
|
std::atomic<bool> polling_running_{false};
|
|
SemaphoreHandle_t refresh_mutex_ = nullptr;
|
|
|
|
// Callback for settings button
|
|
SettingsButtonCallback on_settings_callback_ = nullptr;
|
|
void* settings_callback_user_data_ = nullptr;
|
|
|
|
std::vector<RouteArrivalData> cached_arrival_data_;
|
|
|
|
static constexpr uint32_t LVGL_LOCK_TIMEOUT_MS = 4000;
|
|
};
|
|
|
|
} // namespace travel
|