From e5429cf6eb8f83be50b4fb7d764375f7c3713265 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: Mon, 12 Jul 2021 10:54:05 +0800 Subject: Link view to give it some delay before user goes to their catalog. Update the handler to conform to the updated shop results page. Make the stack push relation more generic, that what view gets pushed is decided in the C++ controller, not the QML views. Refactor code. --- controller.cxx | 82 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 36 deletions(-) (limited to 'controller.cxx') diff --git a/controller.cxx b/controller.cxx index aa676af..c1dd2ca 100644 --- a/controller.cxx +++ b/controller.cxx @@ -11,57 +11,67 @@ static Client* shop = nullptr; static Catalog* allProducts = nullptr; extern "C" { - void sign_up(char const*, char const*, void (*)(icclient_response*)); + void sign_up(char const*, char const*, void (*)(icclient_response*)); struct icclient_catalog* catalog(char const*, char const*); } Controller::Controller(QObject* parent) : QObject{parent} { controller = this; - engine = static_cast(parent); + engine = qobject_cast(parent); engine->load(QUrl{QStringLiteral("qrc:/main.qml")}); auto window = engine->rootObjects()[0]; - connect(window, SIGNAL(signUp(QString)), this, SIGNAL(signUp(QString))); - connect(this, &Controller::signUp, [](QString const& brand) { + connect(window, SIGNAL(signUp(QString)), this, SLOT(signUp(QString))); +} + +void Controller::signUp(QString const& brand) +{ #ifdef __ANDROID__ - QString certAsset{CA_BUNDLE}; - certFile = new QString{QDir{ - QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) - }.absolutePath() % certAsset.remove(0, certAsset.lastIndexOf("/"))}; - QFile{"assets:" % certAsset}.copy(*certFile); + QString certAsset{CA_BUNDLE}; + certFile = new QString{QDir{ + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + }.absolutePath() % certAsset.remove(0, certAsset.lastIndexOf("/"))}; + QFile{"assets:" % certAsset}.copy(*certFile); #endif - sign_up(brand.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr, - [](icclient_response* response) { - const_cast(response->data)[response->numBytes] = '\0'; - QString brand{response->data}; - icclient_free_response(response); - QString sampleUrl{QString{SECURE_SERVER} % "/" % brand}; - QString imageDir{"/" % brand % "/images"}; - shop = new Client{ - sampleUrl.toLatin1().constData(), - imageDir.toLatin1().constData(), - certFile ? certFile->toLatin1().constData() : nullptr - }; - if (certFile) delete certFile; - auto window = engine->rootObjects()[0]; - window->setProperty("imageBase", QString{sampleUrl % "/images/"}); - controller->connect(shop, &Client::gotCatalog, - [imageDir,window](QString const& response) { - allProducts = new Catalog{catalog( - response.toLatin1().constData(), - imageDir.toLatin1().constData())}; - engine->rootContext()->setContextProperty("catalog", - allProducts); - QMetaObject::invokeMethod(window, "pushCatalog"); - }); - shop->allProducts(); - } - ); + sign_up(brand.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr, + [](icclient_response* response) { + const_cast(response->data)[response->numBytes] = '\0'; + QString brand{response->data}; + icclient_free_response(response); + auto window = engine->rootObjects()[0]; + QMetaObject::invokeMethod(window, "push", Q_ARG(QVariant, "Link"), Q_ARG(QVariant, brand)); + controller->connect(window, SIGNAL(pushed()), controller, SIGNAL(pushed())); + controller->connect(controller, &Controller::pushed, [window]() { + controller->connect(window->findChild("link"), SIGNAL(catalog(QString)), + controller, SLOT(catalog(QString))); + }); + }); +} + +void Controller::catalog(QString const& brand) +{ + QString sampleUrl{QString{SECURE_SERVER} % "/" % brand}; + QString imageDir{"/" % brand % "/images"}; + shop = new Client{sampleUrl.toLatin1().constData(), imageDir.toLatin1().constData(), + certFile ? certFile->toLatin1().constData() : nullptr}; + if (certFile) { + delete certFile; + certFile = nullptr; + } + connect(shop, &Client::gotCatalog, [sampleUrl,imageDir](QString const& response) { + allProducts = new Catalog{::catalog(response.toLatin1().constData(), + imageDir.toLatin1().constData())}; + engine->rootContext()->setContextProperty("catalog", allProducts); + QMetaObject::invokeMethod(engine->rootObjects()[0]->findChild("link"), "push", + Q_ARG(QVariant, "Catalog"), + Q_ARG(QVariant, QString{sampleUrl % "/images/"})); }); + shop->allProducts(); } Controller::~Controller() { + if (certFile) delete certFile; if (allProducts) delete allProducts; if (shop) delete shop; } -- cgit v1.2.3