From 8149c07fac9aa651608aa761f50744edcb5b9ea1 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 11 Jul 2023 18:42:10 +0800 Subject: [PATCH] ui/map: singleton navigation requests (#28862) --- selfdrive/ui/qt/maps/map_settings.cc | 75 ++++++++++++++++------------ selfdrive/ui/qt/maps/map_settings.h | 16 ++++++ 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/selfdrive/ui/qt/maps/map_settings.cc b/selfdrive/ui/qt/maps/map_settings.cc index edcc17d810..62c3d840e9 100644 --- a/selfdrive/ui/qt/maps/map_settings.cc +++ b/selfdrive/ui/qt/maps/map_settings.cc @@ -1,5 +1,6 @@ #include "map_settings.h" +#include #include #include @@ -84,38 +85,8 @@ MapSettings::MapSettings(bool closeable, QWidget *parent) setStyleSheet("MapSettings { background-color: #333333; }"); - if (auto dongle_id = getDongleId()) { - // Fetch favorite and recent locations - { - QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations"; - RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true); - QObject::connect(repeater, &RequestRepeater::requestDone, this, &MapSettings::parseResponse); - } - - // Destination set while offline - { - QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/next"; - RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true); - HttpRequest* deleter = new HttpRequest(this); - - QObject::connect(repeater, &RequestRepeater::requestDone, [=](const QString &resp, bool success) { - if (success && resp != "null") { - if (params.get("NavDestination").empty()) { - qWarning() << "Setting NavDestination from /next" << resp; - params.put("NavDestination", resp.toStdString()); - } else { - qWarning() << "Got location from /next, but NavDestination already set"; - } - - // Send DELETE to clear destination server side - deleter->sendRequest(url, HttpRequest::Method::DELETE); - } - - // Update UI (athena can set destination at any time) - updateCurrentRoute(); - }); - } - } + QObject::connect(NavigationRequest::instance(), &NavigationRequest::locationsUpdated, this, &MapSettings::parseResponse); + QObject::connect(NavigationRequest::instance(), &NavigationRequest::nextDestinationUpdated, this, &MapSettings::updateCurrentRoute); } void MapSettings::mousePressEvent(QMouseEvent *ev) { @@ -347,3 +318,43 @@ void DestinationWidget::unset(const QString &label, bool current) { setStyleSheet(styleSheet()); } + +// singleton NavigationRequest + +NavigationRequest *NavigationRequest::instance() { + static NavigationRequest *request = new NavigationRequest(qApp); + return request; +} + +NavigationRequest::NavigationRequest(QObject *parent) : QObject(parent) { + if (auto dongle_id = getDongleId()) { + { + // Fetch favorite and recent locations + QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations"; + RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true); + QObject::connect(repeater, &RequestRepeater::requestDone, this, &NavigationRequest::locationsUpdated); + } + + { + // Destination set while offline + QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/next"; + HttpRequest *deleter = new HttpRequest(this); + RequestRepeater *repeater = new RequestRepeater(this, url, "", 10, true); + QObject::connect(repeater, &RequestRepeater::requestDone, [=](const QString &resp, bool success) { + if (success && resp != "null") { + if (params.get("NavDestination").empty()) { + qWarning() << "Setting NavDestination from /next" << resp; + params.put("NavDestination", resp.toStdString()); + } else { + qWarning() << "Got location from /next, but NavDestination already set"; + } + // Send DELETE to clear destination server side + deleter->sendRequest(url, HttpRequest::Method::DELETE); + } + + // Update UI (athena can set destination at any time) + emit nextDestinationUpdated(resp, success); + }); + } + } +} diff --git a/selfdrive/ui/qt/maps/map_settings.h b/selfdrive/ui/qt/maps/map_settings.h index 43ebb09b69..ed9d0c980b 100644 --- a/selfdrive/ui/qt/maps/map_settings.h +++ b/selfdrive/ui/qt/maps/map_settings.h @@ -23,6 +23,22 @@ const QString NAV_FAVORITE_LABEL_WORK = "work"; class NavDestination; class DestinationWidget; +class NavigationRequest : public QObject { + Q_OBJECT + +public: + static NavigationRequest *instance(); + +signals: + void locationsUpdated(const QString &response, bool success); + void nextDestinationUpdated(const QString &response, bool success); + +private: + NavigationRequest(QObject *parent); + + Params params; +}; + class MapSettings : public QFrame { Q_OBJECT public: