From e6cff004d09c516c69ff88a5c1d1b72d6128852c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 10 Jun 2021 11:05:31 +0800 Subject: [PATCH] change to asset read --- doric-Qt/example/app/demo/DoricDemoBridge.cpp | 12 +++++++++--- doric-Qt/example/app/demo/DoricDemoBridge.h | 2 +- doric-Qt/example/app/main.qml | 2 +- doric-Qt/example/app/qml.qrc | 2 +- doric-Qt/example/doric/async/DoricAsyncResult.h | 2 +- .../example/doric/loader/DoricAssetJSLoader.cpp | 4 ++++ doric-Qt/example/doric/utils/DoricUtils.h | 13 +++++++++++++ 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/doric-Qt/example/app/demo/DoricDemoBridge.cpp b/doric-Qt/example/app/demo/DoricDemoBridge.cpp index 11f9b33d..14757786 100644 --- a/doric-Qt/example/app/demo/DoricDemoBridge.cpp +++ b/doric-Qt/example/app/demo/DoricDemoBridge.cpp @@ -3,6 +3,7 @@ #include "DoricDemoBridge.h" #include "DoricPanel.h" +#include "loader/DoricJSLoaderManager.h" #include "utils/DoricDialogBridge.h" #include "utils/DoricDraggableBridge.h" #include "utils/DoricImageBridge.h" @@ -36,9 +37,9 @@ DoricDemoBridge::DoricDemoBridge(QQmlApplicationEngine *engine, QObject *parent) context->setContextProperty("draggableBridge", draggableBridge); } -void DoricDemoBridge::navigate(QVariant route) { +void DoricDemoBridge::navigate(QVariant path, QVariant index) { QString name; - switch (route.toInt()) { + switch (index.toInt()) { case 0: name = "ComponetDemo.js"; break; @@ -109,7 +110,12 @@ void DoricDemoBridge::navigate(QVariant route) { name = "TextDemo.js"; break; } - QString script = DoricUtils::readAssetFile("/doric/bundles", name); + + QString resourcePath = path.toString(); + std::shared_ptr asyncResult = + DoricJSLoaderManager::getInstance()->request(resourcePath + name); + + QString script = asyncResult->getResult(); QQmlComponent component(mEngine); const QUrl url(QStringLiteral("qrc:/doric/qml/panel.qml")); diff --git a/doric-Qt/example/app/demo/DoricDemoBridge.h b/doric-Qt/example/app/demo/DoricDemoBridge.h index 9807edbd..a1a2389a 100644 --- a/doric-Qt/example/app/demo/DoricDemoBridge.h +++ b/doric-Qt/example/app/demo/DoricDemoBridge.h @@ -11,7 +11,7 @@ public: QObject *parent = nullptr); Q_INVOKABLE - void navigate(QVariant route); + void navigate(QVariant path, QVariant index); private: QQmlApplicationEngine *mEngine; diff --git a/doric-Qt/example/app/main.qml b/doric-Qt/example/app/main.qml index e6c9bf48..7ef9ebdf 100644 --- a/doric-Qt/example/app/main.qml +++ b/doric-Qt/example/app/main.qml @@ -148,7 +148,7 @@ ApplicationWindow { MouseArea { anchors.fill: parent onClicked: { - demoBridge.navigate(index) + demoBridge.navigate("assets://src/", index) } } } diff --git a/doric-Qt/example/app/qml.qrc b/doric-Qt/example/app/qml.qrc index 6137bd70..83da511f 100644 --- a/doric-Qt/example/app/qml.qrc +++ b/doric-Qt/example/app/qml.qrc @@ -8,7 +8,7 @@ ./resources/assets_iconfont.ttf - + ../../../doric-demo/bundle/src/ComponetDemo.js ../../../doric-demo/bundle/src/Counter.js ../../../doric-demo/bundle/src/DraggableDemo.js diff --git a/doric-Qt/example/doric/async/DoricAsyncResult.h b/doric-Qt/example/doric/async/DoricAsyncResult.h index efb161bf..dd0d4975 100644 --- a/doric-Qt/example/doric/async/DoricAsyncResult.h +++ b/doric-Qt/example/doric/async/DoricAsyncResult.h @@ -10,7 +10,7 @@ private: QString result; public: - std::function resultCallback; + std::function resultCallback = [] {}; std::function exceptionCallback; std::function finishCallback; diff --git a/doric-Qt/example/doric/loader/DoricAssetJSLoader.cpp b/doric-Qt/example/doric/loader/DoricAssetJSLoader.cpp index d9c5484a..7bb8f27c 100644 --- a/doric-Qt/example/doric/loader/DoricAssetJSLoader.cpp +++ b/doric-Qt/example/doric/loader/DoricAssetJSLoader.cpp @@ -12,8 +12,12 @@ std::shared_ptr DoricAssetJSLoader::request(QString source) { QString protocol = "assets://"; QString assetPath = source.mid(protocol.length()); + QString script = DoricUtils::readAssetFile("/" + assetPath); + std::shared_ptr asyncResult = std::make_shared(); + asyncResult->setResult(script); + return asyncResult; } diff --git a/doric-Qt/example/doric/utils/DoricUtils.h b/doric-Qt/example/doric/utils/DoricUtils.h index 3e76a855..12dd75a6 100644 --- a/doric-Qt/example/doric/utils/DoricUtils.h +++ b/doric-Qt/example/doric/utils/DoricUtils.h @@ -24,6 +24,19 @@ public: return content; } + static QString readAssetFile(QString resourcePath) { + QResource resource(":" + resourcePath); + QFile *file = new QFile(resource.fileName()); + file->open(QFile::ReadOnly | QFile::Text); + QTextStream in(file); + in.setCodec("UTF-8"); + QString content = in.readAll(); + file->close(); + delete file; + + return content; + } + template static inline bool instanceof (const T *) { return std::is_base_of::value;