summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-12 12:17:45 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-12 12:17:45 +0800
commit088159182a5a6d3533a7812b48323125fefc1e61 (patch)
tree968e5dd4ddabd16b90b830ec11fe2cc667a89862
parente5429cf6eb8f83be50b4fb7d764375f7c3713265 (diff)
Init shop client right away
to avoid it getting re-initialised if we ever need to request catalog again.
-rw-r--r--Link.qml4
-rw-r--r--controller.cxx52
-rw-r--r--controller.hxx4
3 files changed, 31 insertions, 29 deletions
diff --git a/Link.qml b/Link.qml
index 199d552..2bcacdc 100644
--- a/Link.qml
+++ b/Link.qml
@@ -13,7 +13,7 @@ Rectangle {
}
Text {
objectName: "link"
- signal catalog(string brand)
+ signal activated(string arg)
function push(view, arg) {
stack.push(view + ".qml", {
"arg": arg
@@ -24,7 +24,7 @@ Rectangle {
Layout.alignment: Qt.AlignCenter
text: "<a href=\"https://darapsa.com/" + arg + "\">https://darapsa.com/" + arg + "</a>"
linkColor: "#ffffff"
- onLinkActivated: catalog(arg)
+ onLinkActivated: activated(arg)
}
}
}
diff --git a/controller.cxx b/controller.cxx
index c1dd2ca..d0be5fb 100644
--- a/controller.cxx
+++ b/controller.cxx
@@ -17,11 +17,9 @@ extern "C" {
Controller::Controller(QObject* parent) : QObject{parent}
{
- controller = this;
engine = qobject_cast<QQmlApplicationEngine*>(parent);
engine->load(QUrl{QStringLiteral("qrc:/main.qml")});
- auto window = engine->rootObjects()[0];
- connect(window, SIGNAL(signUp(QString)), this, SLOT(signUp(QString)));
+ connect(engine->rootObjects()[0], SIGNAL(signUp(QString)), this, SLOT(signUp(QString)));
}
void Controller::signUp(QString const& brand)
@@ -33,45 +31,47 @@ void Controller::signUp(QString const& brand)
}.absolutePath() % certAsset.remove(0, certAsset.lastIndexOf("/"))};
QFile{"assets:" % certAsset}.copy(*certFile);
#endif
+ controller = this;
sign_up(brand.toLatin1().constData(), certFile ? certFile->toLatin1().constData() : nullptr,
[](icclient_response* response) {
const_cast<char*>(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<QObject*>("link"), SIGNAL(catalog(QString)),
- controller, SLOT(catalog(QString)));
- });
+ shop = new Client{QString{QString{SECURE_SERVER} % "/" % brand}.toLatin1().constData(),
+ QString{"/" % brand % "/images"}.toLatin1().constData(),
+ certFile ? certFile->toLatin1().constData() : nullptr};
+ if (certFile) delete certFile;
+ controller->link(brand);
});
}
-void Controller::catalog(QString const& brand)
+void Controller::link(QString const& brand)
+{
+ auto window = engine->rootObjects()[0];
+ QMetaObject::invokeMethod(window, "push", Q_ARG(QVariant, "Link"), Q_ARG(QVariant, brand));
+ connect(window, SIGNAL(pushed()), controller, SIGNAL(pushed()));
+ connect(this, &Controller::pushed, [this,window]() {
+ connect(window->findChild<QObject*>("link"), SIGNAL(activated(QString)),
+ this, SLOT(allProducts(QString)));
+ });
+}
+
+void Controller::allProducts(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);
+ connect(shop, &Client::gotCatalog, [brand](QString const& response) {
+ ::allProducts = new Catalog{catalog(response.toLatin1().constData(),
+ QString{"/" % brand % "/images"}.toLatin1().constData())};
+ engine->rootContext()->setContextProperty("catalog", ::allProducts);
QMetaObject::invokeMethod(engine->rootObjects()[0]->findChild<QObject*>("link"), "push",
Q_ARG(QVariant, "Catalog"),
- Q_ARG(QVariant, QString{sampleUrl % "/images/"}));
+ Q_ARG(QVariant,
+ QString{QString{SECURE_SERVER} % "/" % brand % "/images/"}));
});
shop->allProducts();
}
Controller::~Controller()
{
- if (certFile) delete certFile;
- if (allProducts) delete allProducts;
+ if (::allProducts) delete ::allProducts;
if (shop) delete shop;
}
diff --git a/controller.hxx b/controller.hxx
index f0f3a1f..13b981d 100644
--- a/controller.hxx
+++ b/controller.hxx
@@ -11,9 +11,11 @@ class Controller : public QObject
~Controller();
public slots:
void signUp(QString const& brand);
- void catalog(QString const& brand);
+ void allProducts(QString const& brand);
signals:
void pushed();
+ protected:
+ void link(QString const& brand);
};
#endif