add storage demo & plugin

This commit is contained in:
王劲鹏 2021-05-06 10:01:56 +08:00 committed by osborn
parent 660d75d1ad
commit 394cbe5962
9 changed files with 132 additions and 2 deletions

View File

@ -47,6 +47,9 @@ void DoricDemoBridge::navigate(QVariant route) {
case 10: case 10:
name = "Snake.js"; name = "Snake.js";
break; break;
case 11:
name = "StorageDemo.js";
break;
} }
QString script = DoricUtils::readAssetFile("/doric/bundles", name); QString script = DoricUtils::readAssetFile("/doric/bundles", name);

View File

@ -14,7 +14,7 @@ int main(int argc, char *argv[]) {
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
qmlRegisterType<FlexLayoutService>("pub.doric.widget", 1, 0, qmlRegisterType<FlexLayoutService>("pub.doric.widget", 1, 0,
"FlexLayoutService"); "FlexLayoutService");
// const QUrl url(QStringLiteral("qrc:/doric/qml/test-layout.qml")); // const QUrl url(QStringLiteral("qrc:/test-layout.qml"));
const QUrl url(QStringLiteral("qrc:/main.qml")); const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect( QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app, &engine, &QQmlApplicationEngine::objectCreated, &app,

View File

@ -15,7 +15,7 @@ ApplicationWindow {
ListView { ListView {
width: parent.width width: parent.width
model: 11 model: 12
delegate: Rectangle { delegate: Rectangle {
Column { Column {
anchors.centerIn: parent anchors.centerIn: parent
@ -44,6 +44,8 @@ ApplicationWindow {
return "SimpleDemo.js" return "SimpleDemo.js"
case 10: case 10:
return "Snake.js" return "Snake.js"
case 11:
return "StorageDemo.js"
} }
} }
} }

View File

@ -16,6 +16,7 @@
<file alias="PopoverDemo.js">../../../doric-demo/bundle/src/PopoverDemo.js</file> <file alias="PopoverDemo.js">../../../doric-demo/bundle/src/PopoverDemo.js</file>
<file alias="SimpleDemo.js">../../../doric-demo/bundle/src/SimpleDemo.js</file> <file alias="SimpleDemo.js">../../../doric-demo/bundle/src/SimpleDemo.js</file>
<file alias="Snake.js">../../../doric-demo/bundle/src/Snake.js</file> <file alias="Snake.js">../../../doric-demo/bundle/src/Snake.js</file>
<file alias="StorageDemo.js">../../../doric-demo/bundle/src/StorageDemo.js</file>
</qresource> </qresource>
<qresource prefix="/doric"> <qresource prefix="/doric">
<file alias="doric-sandbox.js">../../../doric-js/bundle/doric-sandbox.js</file> <file alias="doric-sandbox.js">../../../doric-js/bundle/doric-sandbox.js</file>

View File

@ -4,6 +4,7 @@
#include "plugin/DoricNetworkPlugin.h" #include "plugin/DoricNetworkPlugin.h"
#include "plugin/DoricPopoverPlugin.h" #include "plugin/DoricPopoverPlugin.h"
#include "plugin/DoricShaderPlugin.h" #include "plugin/DoricShaderPlugin.h"
#include "plugin/DoricStoragePlugin.h"
#include "shader/DoricHLayoutNode.h" #include "shader/DoricHLayoutNode.h"
#include "shader/DoricImageNode.h" #include "shader/DoricImageNode.h"
@ -18,6 +19,7 @@ DoricRegistry::DoricRegistry() {
registerNativePlugin<DoricModalPlugin>("modal"); registerNativePlugin<DoricModalPlugin>("modal");
registerNativePlugin<DoricPopoverPlugin>("popover"); registerNativePlugin<DoricPopoverPlugin>("popover");
registerNativePlugin<DoricNetworkPlugin>("network"); registerNativePlugin<DoricNetworkPlugin>("network");
registerNativePlugin<DoricStoragePlugin>("storage");
registerViewNode<DoricRootNode>("Root"); registerViewNode<DoricRootNode>("Root");
registerViewNode<DoricStackNode>("Stack"); registerViewNode<DoricStackNode>("Stack");

View File

@ -39,6 +39,7 @@ SOURCES += \
plugin/DoricNetworkPlugin.cpp \ plugin/DoricNetworkPlugin.cpp \
plugin/DoricPopoverPlugin.cpp \ plugin/DoricPopoverPlugin.cpp \
plugin/DoricShaderPlugin.cpp \ plugin/DoricShaderPlugin.cpp \
plugin/DoricStoragePlugin.cpp \
shader/DoricGroupNode.cpp \ shader/DoricGroupNode.cpp \
shader/DoricHLayoutNode.cpp \ shader/DoricHLayoutNode.cpp \
shader/DoricImageNode.cpp \ shader/DoricImageNode.cpp \
@ -112,6 +113,7 @@ HEADERS += \
plugin/DoricNetworkPlugin.h \ plugin/DoricNetworkPlugin.h \
plugin/DoricPopoverPlugin.h \ plugin/DoricPopoverPlugin.h \
plugin/DoricShaderPlugin.h \ plugin/DoricShaderPlugin.h \
plugin/DoricStoragePlugin.h \
shader/DoricGroupNode.h \ shader/DoricGroupNode.h \
shader/DoricHLayoutNode.h \ shader/DoricHLayoutNode.h \
shader/DoricImageNode.h \ shader/DoricImageNode.h \

View File

@ -90,6 +90,8 @@ v8::Local<v8::Value> Variant2JS(QVariant variant) {
jsValue = array; jsValue = array;
} else if (variant.type() == QVariant::Int) { } else if (variant.type() == QVariant::Int) {
jsValue = v8::Number::New(isolate, variant.toDouble()); jsValue = v8::Number::New(isolate, variant.toDouble());
} else if (variant.type() == QVariant::Invalid) {
jsValue = v8::Undefined(isolate);
} }
return handle_scope.Escape(jsValue); return handle_scope.Escape(jsValue);
} }

View File

@ -0,0 +1,95 @@
#include "DoricStoragePlugin.h"
#include "engine/DoricPromise.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QSettings>
const QString DoricStoragePlugin::PREF_NAME = "pref_doric";
void DoricStoragePlugin::setItem(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QJsonValue zone = jsValue["zone"];
QString key = jsValue["key"].toString();
QString value = jsValue["value"].toString();
QString prefName;
if (zone.isString()) {
prefName = PREF_NAME + "_" + zone.toString();
} else {
prefName = PREF_NAME;
}
QSettings settings;
settings.setValue(prefName + "/" + key, value);
QVariantList args;
DoricPromise::resolve(getContext(), callbackId, args);
}
void DoricStoragePlugin::getItem(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QJsonValue zone = jsValue["zone"];
QString key = jsValue["key"].toString();
QString prefName;
if (zone.isString()) {
prefName = PREF_NAME + "_" + zone.toString();
} else {
prefName = PREF_NAME;
}
QSettings settings;
QVariant value = settings.value(prefName + "/" + key);
QVariantList args;
args.append(value);
DoricPromise::resolve(getContext(), callbackId, args);
}
void DoricStoragePlugin::remove(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QJsonValue zone = jsValue["zone"];
QString key = jsValue["key"].toString();
QString prefName;
if (zone.isString()) {
prefName = PREF_NAME + "_" + zone.toString();
} else {
prefName = PREF_NAME;
}
QSettings settings;
settings.remove(prefName + "/" + key);
QVariantList args;
DoricPromise::resolve(getContext(), callbackId, args);
}
void DoricStoragePlugin::clear(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QJsonValue zone = jsValue["zone"];
QString key = jsValue["key"].toString();
QString prefName;
if (zone.isString()) {
prefName = PREF_NAME + "_" + zone.toString();
} else {
prefName = PREF_NAME;
}
QSettings settings;
settings.clear();
QVariantList args;
DoricPromise::resolve(getContext(), callbackId, args);
}

View File

@ -0,0 +1,23 @@
#ifndef DORICSTORAGEPLUGIN_H
#define DORICSTORAGEPLUGIN_H
#include "DoricNativePlugin.h"
class DoricStoragePlugin : public DoricNativePlugin {
Q_OBJECT
private:
static const QString PREF_NAME;
public:
using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void setItem(QString jsValueString, QString callbackId);
Q_INVOKABLE void getItem(QString jsValueString, QString callbackId);
Q_INVOKABLE void remove(QString jsValueString, QString callbackId);
Q_INVOKABLE void clear(QString jsValueString, QString callbackId);
};
#endif // DORICSTORAGEPLUGIN_H