mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-05 13:32:05 +08:00
ui/map: singleton navigation requests (#28862)
old-commit-hash: 8149c07fac9aa651608aa761f50744edcb5b9ea1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "map_settings.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include <vector>
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user