feat(SportModel): request API, parse and display Model
This commit is contained in:
		
							parent
							
								
									f17c2a73e1
								
							
						
					
					
						commit
						c09350b7c9
					
				|  | @ -19,9 +19,10 @@ Page { | |||
|     bottomMargin: 48 | ||||
|     rightMargin: 48 | ||||
|     spacing: 20 | ||||
|     model: ["Albert Einstein", "Ernest Hemingway", "Hans Gude"] | ||||
|     model: sports | ||||
|     delegate: ItemDelegate { | ||||
|       text: modelData | ||||
|       required property string sportName | ||||
|       text: sportName | ||||
|       width: listView.width - listView.leftMargin - listView.rightMargin | ||||
|       height: avatar.implicitHeight + 32 | ||||
|       leftPadding: avatar.implicitWidth + 32 | ||||
|  |  | |||
|  | @ -20,6 +20,7 @@ | |||
| // console output
 | ||||
| #include <QDebug> | ||||
| // #include <iostream>
 | ||||
| #include "../model/Sport.h" | ||||
| 
 | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
|  | @ -28,6 +29,10 @@ int main(int argc, char *argv[]) | |||
|     QQmlEngine engine; | ||||
|     QQmlContext *objectContext = new QQmlContext(engine.rootContext()); | ||||
| 
 | ||||
|     SportModel model; | ||||
|     model.request(); | ||||
|     objectContext->setContextProperty("sports", &model); | ||||
| 
 | ||||
|     QQmlComponent component(&engine, "qrc:/qt/qml/itat/res/gui/application.qml"); | ||||
|     QObject *object = component.create(objectContext); | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,7 +1,9 @@ | |||
| 
 | ||||
| #include "Sport.h" | ||||
| 
 | ||||
| // categories
 | ||||
| #include <QNetworkReply> | ||||
| #include <qlogging.h> | ||||
| #include <qobject.h> | ||||
| #include <set> | ||||
| 
 | ||||
| // sorting and filtering
 | ||||
|  | @ -18,6 +20,70 @@ | |||
| #include <QJsonValueRef> | ||||
| #include <QString> | ||||
| 
 | ||||
| namespace { | ||||
|   const QString &k_requestUrl = "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC"; | ||||
| } | ||||
| 
 | ||||
| SportModel::SportModel(QObject *parent) : QAbstractListModel(parent) { | ||||
| } | ||||
| 
 | ||||
| int SportModel::rowCount(const QModelIndex &parent) const { | ||||
|   Q_UNUSED(parent); | ||||
|   return m_sportList.size(); | ||||
| } | ||||
| 
 | ||||
| QVariant SportModel::data(const QModelIndex &index, int role) const { | ||||
|   if (index.isValid() && index.row() >= 0 && index.row() < m_sportList.size()) { | ||||
|     QString sportName = m_sportList[index.row()]; | ||||
| 
 | ||||
|     return sportName; | ||||
|     // switch ((Role) role) {
 | ||||
|     //   case SportName:
 | ||||
|     // }
 | ||||
|   } | ||||
| 
 | ||||
|   return {}; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| QHash<int, QByteArray> SportModel::roleNames() const { | ||||
|   QHash<int, QByteArray> names; | ||||
|   names[SportName] = "sportName"; | ||||
| 
 | ||||
|   return names; | ||||
| } | ||||
| 
 | ||||
| void SportModel::request() { | ||||
|   m_reply = m_networkManager.get(QNetworkRequest( k_requestUrl )); | ||||
|   qDebug() << m_reply; | ||||
|   connect(m_reply, &QNetworkReply::finished, this, &SportModel::parseData); | ||||
| } | ||||
| 
 | ||||
| void SportModel::parseData() { | ||||
| 
 | ||||
|     if (m_reply->error() == QNetworkReply::NoError) { | ||||
|       beginResetModel(); | ||||
|       // qDeleteAll(m_sportList);
 | ||||
|       // m_sportList.clear();
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|       QByteArray strReply = m_reply->readAll(); | ||||
| 
 | ||||
|       //parse json
 | ||||
|       // qDebug() << "Response:" << strReply;
 | ||||
|       QJsonDocument jsonDocument = QJsonDocument::fromJson(strReply); | ||||
| 
 | ||||
|       QJsonArray sports = jsonDocument["units"].toArray(); | ||||
|       for (const auto &sport : sports) { | ||||
|         QJsonObject entry = sport.toObject(); | ||||
|         qDebug() << entry["eventUnitName"].toString(); | ||||
|         m_sportList << entry["eventUnitName"].toString(); | ||||
|       } | ||||
|       endResetModel(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // QJsonArray filter function, provide with input array and evaluation function
 | ||||
| QJsonArray filter(QJsonArray input, function<bool (QJsonObject)> eval) { | ||||
|     QJsonArray output; | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| 
 | ||||
| #ifndef ITAT_CHALLANGE_OLYMPICS_SPORT_H | ||||
| #define ITAT_CHALLANGE_OLYMPICS_SPORT_H | ||||
| 
 | ||||
| 
 | ||||
| #include <QAbstractListModel> | ||||
| #include <QNetworkAccessManager> | ||||
| #include <set> | ||||
| 
 | ||||
| #include <QJsonObject> | ||||
|  | @ -11,6 +11,28 @@ | |||
| 
 | ||||
| using namespace std; | ||||
| 
 | ||||
| class SportModel : public QAbstractListModel { | ||||
|   Q_OBJECT | ||||
| 
 | ||||
|   public:  | ||||
|     enum Role { | ||||
|       SportName = Qt::UserRole + 1 | ||||
|     }; | ||||
| 
 | ||||
|     explicit SportModel(QObject *parent = nullptr); | ||||
| 
 | ||||
|     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; | ||||
|   public slots: | ||||
|     void request(); | ||||
|     void parseData(); | ||||
| 
 | ||||
|   private: | ||||
|     QList<QString> m_sportList; | ||||
|     QNetworkAccessManager m_networkManager; | ||||
|     QNetworkReply *m_reply = nullptr; | ||||
| }; | ||||
| 
 | ||||
| class Sport { | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue