From 5d0a833af478b87f4fb815e37da0beb5a09c6067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Sat, 10 Jul 2021 23:21:25 +0800 Subject: Use the response data to initiate client It's correct now, but the problem is, the client pushes the catalog page faster than the web server can reload to make the newly built sample URL valid. --- controller.cxx | 76 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 32 deletions(-) (limited to 'controller.cxx') diff --git a/controller.cxx b/controller.cxx index e70a66d..6f7759f 100644 --- a/controller.cxx +++ b/controller.cxx @@ -1,55 +1,67 @@ +#include #include #include #include "controller.hxx" extern "C" { - void sign_up(char const*, char const*); - struct icclient_catalog* catalog_data(char const*); + void sign_up(char const*, char const*, void (*)(icclient_response *)); + struct icclient_catalog* catalog_data(char const*, char const*); } -Controller::Controller(QObject* parent) : - QObject{parent}, - catalog{nullptr} +static Controller* controller; +static QQmlApplicationEngine* engine; +static QICClient::Client* interchange = nullptr; +static QString* path = nullptr; +static QString* imageDir = nullptr; +static Catalog* catalog = nullptr; + +Controller::Controller(QObject* parent) : QObject{parent} { + controller = this; #ifdef __ANDROID__ QString cert{CA_BUNDLE}; - QString path{QDir{QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)}.absolutePath() - % cert.remove(0, cert.lastIndexOf("/"))}; - QFile{"assets:" % cert}.copy(path); -#endif - interchange = new Client{SAMPLEURL, IMAGE_DIR -#ifdef __ANDROID__ - , path.toLatin1().constData() + path = new QString{QDir{ + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)}.absolutePath() + % cert.remove(0, cert.lastIndexOf("/"))}; + QFile{"assets:" % cert}.copy(*path); #endif - }; - auto engine = static_cast(parent); + engine = static_cast(parent); engine->load(QUrl{QStringLiteral("qrc:/main.qml")}); auto window = engine->rootObjects()[0]; - window->setProperty("imageDir", SAMPLEURL"/images/"); connect(window, SIGNAL(signUp(QString)), this, SIGNAL(signUp(QString))); - connect(this, &Controller::signUp, [/*this,*/ + connect(this, &Controller::signUp, [](QString const& brand) { + sign_up(brand.toLatin1().constData(), path ? path->toLatin1().constData() : nullptr, + [](icclient_response* response) { + QString brand{response->data}; + icclient_free_response(response); + QString sampleUrl{QString{SECURE_SERVER} % "/" % brand}; + imageDir = new QString{"/" % brand % "/images"}; + interchange = new Client{sampleUrl.toLatin1().constData(), + imageDir->toLatin1().constData() #ifdef __ANDROID__ - path + , path->toLatin1().constData() #endif - ](QString const& brand) { - sign_up(brand.toLatin1().constData(), -#ifdef __ANDROID__ - path.toLatin1().constData() -#else - nullptr -#endif - ); -// interchange->catalog(brand); - }); - connect(interchange, &Client::gotCatalog, [this,engine,window](QString const& response) { - catalog = new Catalog{catalog_data(response.toLatin1().constData())}; - engine->rootContext()->setContextProperty("catalog", catalog); - QMetaObject::invokeMethod(window, "pushCatalog"); + }; + delete path; + auto window = engine->rootObjects()[0]; + window->setProperty("imageDir", QString{sampleUrl % "/images/"}); + controller->connect(interchange, &Client::gotCatalog, + [window](QString const& response) { + catalog = new Catalog{ + catalog_data(response.toLatin1().constData(), + imageDir->toLatin1().constData())}; + engine->rootContext()->setContextProperty("catalog", + catalog); + QMetaObject::invokeMethod(window, "pushCatalog"); + }); + interchange->allProducts(); + }); }); } Controller::~Controller() { if (catalog) delete catalog; - delete interchange; + if (imageDir) delete imageDir; + if (interchange) delete interchange; } -- cgit v1.2.3