feat(Sport): select Discipline

This commit is contained in:
Orangerot 2024-08-16 15:40:37 +02:00
parent 5734032475
commit 3d19fd7271
4 changed files with 226 additions and 50 deletions

View file

@ -31,13 +31,209 @@ Page {
displayText: "Disziplin: " + currentText
model: myListModel
textRole: "text"
ListModel {
id: myListModel
ListElement { key: "Archerie" }
ListElement { key: "Shooting" }
ListElement { key: "Break-Dance" }
ListElement { key: "More" }
ListElement {
text: "AquaticsArtisticSwimming"
api: "SWA"
}
ListElement {
text: "AquaticsDiving"
api: "DIV"
}
ListElement {
text: "AquaticsMarathonSwimming"
api: "OWS"
}
ListElement {
text: "AquaticsSwimming"
api: "SWM"
}
ListElement {
text: "AquaticsWaterPolo"
api: "WPO"
}
ListElement {
text: "Archery"
api: "ARC"
}
ListElement {
text: "Athletics"
api: "ATH"
}
ListElement {
text: "Badminton"
api: "BDM"
}
ListElement {
text: "Basketball3v3"
api: "BK3"
}
ListElement {
text: "Basketball"
api: "BKB"
}
ListElement {
text: "Boxing"
api: "BOX"
}
ListElement {
text: "Breaking"
api: "BKG"
}
ListElement {
text: "CanoeingSprint"
api: "CSP"
}
ListElement {
text: "CanoeingSlalom"
api: "CSL"
}
ListElement {
text: "CyclingBMXFreestyle"
api: "BMF"
}
ListElement {
text: "CyclingBMXRacing"
api: "BMX"
}
ListElement {
text: "CyclingMaountainBike"
api: "MTB"
}
ListElement {
text: "CyclingRoad"
api: "CRD"
}
ListElement {
text: "CyclingTrack"
api: "CTR"
}
ListElement {
text: "EquestrianDressage"
api: "EDR"
}
ListElement {
text: "EquestrianEventing"
api: "EVE"
}
ListElement {
text: "EquestrianJumping"
api: "EJP"
}
ListElement {
text: "Fencing"
api: "FEN"
}
ListElement {
text: "FieldHockey"
api: "HOC"
}
ListElement {
text: "Football"
api: "FBL"
}
ListElement {
text: "Golf"
api: "GLF"
}
ListElement {
text: "GymnasticsArtistic"
api: "GAR"
}
ListElement {
text: "GymnasticsRhythmic"
api: "GRY"
}
ListElement {
text: "GymnasticsTrampoline"
api: "GTR"
}
ListElement {
text: "HandballIndoor"
api: "HBL"
}
ListElement {
text: "Judo"
api: "JUD"
}
ListElement {
text: "ModernPentathlon"
api: "MPN"
}
ListElement {
text: "Rowing"
api: "ROW"
}
ListElement {
text: "Rugby7"
api: "RU7"
}
ListElement {
text: "Sailing"
api: "SAL"
}
ListElement {
text: "Shooting"
api: "SHO"
}
ListElement {
text: "Skateboarding"
api: "SKB"
}
ListElement {
text: "SportClimbing"
api: "CLB"
}
ListElement {
text: "Surfing"
api: "SRF"
}
ListElement {
text: "TableTennis"
api: "TTE"
}
ListElement {
text: "Taekwondo"
api: "TKW"
}
ListElement {
text: "Tennis"
api: "TEN"
}
ListElement {
text: "Triathlon"
api: "TRI"
}
ListElement {
text: "VolleyballBeach"
api: "VBV"
}
ListElement {
text: "VolleyballIndoor"
api: "VVO"
}
ListElement {
text: "Weightlifting"
api: "WLF"
}
ListElement {
text: "WrestlingFreestyle"
api: "WRE"
}
ListElement {
text: "WrestlingGrecoRoman"
api: "WRG"
}
}
onActivated: {
if (currentIndex >= 0) {
console.log(currentValue.api);
sports.request(currentValue.api)
}
}
}
ComboBox {
@ -80,7 +276,7 @@ Page {
id: avatar
height: 32
width: 32
source: "qrc:/qt/qml/itat/res/pictograms/ARC_small.svg"
source: "qrc:/qt/qml/itat/res/pictograms/" + sports.discipline + "_small.svg"
}
}
}

View file

@ -30,49 +30,11 @@ int main(int argc, char *argv[])
QQmlContext *objectContext = new QQmlContext(engine.rootContext());
SportModel model;
model.request();
model.request("ARC");
objectContext->setContextProperty("sports", &model);
QQmlComponent component(&engine, "qrc:/qt/qml/itat/res/gui/application.qml");
QObject *object = component.create(objectContext);
// ... delete object and objectContext when necessary
// create custom temporary event loop on stack
QEventLoop eventLoop;
// "quit()" the event-loop, when the network request "finished()"
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QString endpoint { "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC" };
// the HTTP request
QNetworkRequest req( (QUrl( endpoint )) );
QNetworkReply *reply = mgr.get(req);
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
//success
QString strReply = (QString)reply->readAll();
//parse json
// qDebug() << "Response:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString();
delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->errorString();
delete reply;
}
return app.exec();
}

View file

@ -22,7 +22,7 @@
#include <QString>
namespace {
const QString &k_requestUrl = "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC";
const QString &k_requestUrl = "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/";
}
SportModel::SportModel(QObject *parent) : QAbstractListModel(parent) {
@ -58,8 +58,18 @@ QHash<int, QByteArray> SportModel::roleNames() const {
return names;
}
void SportModel::request() {
m_reply = m_networkManager.get(QNetworkRequest( k_requestUrl ));
QString SportModel::discipline() const {
return m_discipline;
}
void SportModel::setDiscipline(const QString &discipline) {
m_discipline = discipline;
}
void SportModel::request(QString discipline) {
m_discipline = discipline;
m_reply = m_networkManager.get(QNetworkRequest( k_requestUrl + m_discipline));
qDebug() << m_reply;
connect(m_reply, &QNetworkReply::finished, this, &SportModel::parseData);
}
@ -68,8 +78,8 @@ void SportModel::parseData() {
if (m_reply->error() == QNetworkReply::NoError) {
beginResetModel();
// qDeleteAll(m_sportList);
// m_sportList.clear();
qDeleteAll(m_sportList);
m_sportList.clear();

View file

@ -3,6 +3,7 @@
#include <QAbstractListModel>
#include <QNetworkAccessManager>
#include <qcontainerfwd.h>
#include <set>
#include <QJsonObject>
@ -15,6 +16,8 @@ using namespace std;
class SportModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QString discipline READ discipline WRITE setDiscipline);
public:
enum Role {
EventName = Qt::UserRole + 1,
@ -26,12 +29,17 @@ class SportModel : public QAbstractListModel {
virtual int rowCount(const QModelIndex &parent) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
virtual QHash<int, QByteArray> roleNames() const override;
QString discipline() const;
void setDiscipline(const QString &discipline);
public slots:
void request();
void request(QString discipline);
void parseData();
private:
QList<EventInfo*> m_sportList;
QString m_discipline;
QNetworkAccessManager m_networkManager;
QNetworkReply *m_reply = nullptr;
};