Optimize loading of things
This commit is contained in:
parent
5c63c92064
commit
1ccdda578c
@ -302,6 +302,7 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap ¶m
|
|||||||
// qCritical() << "Things received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
// qCritical() << "Things received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
|
||||||
if (params.keys().contains("things")) {
|
if (params.keys().contains("things")) {
|
||||||
QVariantList thingsList = params.value("things").toList();
|
QVariantList thingsList = params.value("things").toList();
|
||||||
|
QList<Thing*> newThings;
|
||||||
foreach (QVariant thingVariant, thingsList) {
|
foreach (QVariant thingVariant, thingsList) {
|
||||||
Thing *thing = unpackThing(this, thingVariant.toMap(), m_thingClasses);
|
Thing *thing = unpackThing(this, thingVariant.toMap(), m_thingClasses);
|
||||||
if (!thing) {
|
if (!thing) {
|
||||||
@ -329,8 +330,9 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap ¶m
|
|||||||
thing->setStateValue(stateTypeId, value);
|
thing->setStateValue(stateTypeId, value);
|
||||||
// qDebug() << "Set thing state value:" << thing->stateValue(stateTypeId) << value;
|
// qDebug() << "Set thing state value:" << thing->stateValue(stateTypeId) << value;
|
||||||
}
|
}
|
||||||
things()->addThing(thing);
|
newThings.append(thing);
|
||||||
}
|
}
|
||||||
|
things()->addThings(newThings);
|
||||||
}
|
}
|
||||||
qDebug() << "Initializing thing manager took" << m_connectionBenchmark.msecsTo(QDateTime::currentDateTime()) << "ms";
|
qDebug() << "Initializing thing manager took" << m_connectionBenchmark.msecsTo(QDateTime::currentDateTime()) << "ms";
|
||||||
m_fetchingData = false;
|
m_fetchingData = false;
|
||||||
|
|||||||
@ -101,27 +101,39 @@ QVariant Things::data(const QModelIndex &index, int role) const
|
|||||||
|
|
||||||
void Things::addThing(Thing *thing)
|
void Things::addThing(Thing *thing)
|
||||||
{
|
{
|
||||||
thing->setParent(this);
|
addThings({thing});
|
||||||
beginInsertRows(QModelIndex(), m_things.count(), m_things.count());
|
}
|
||||||
m_things.append(thing);
|
|
||||||
|
void Things::addThings(const QList<Thing *> things)
|
||||||
|
{
|
||||||
|
if (things.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
beginInsertRows(QModelIndex(), m_things.count(), m_things.count() + things.count() - 1);
|
||||||
|
m_things.append(things);
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
connect(thing, &Thing::nameChanged, this, [thing, this]() {
|
|
||||||
int idx = m_things.indexOf(thing);
|
foreach (Thing *thing, things) {
|
||||||
if (idx < 0) return;
|
thing->setParent(this);
|
||||||
emit dataChanged(index(idx), index(idx), {RoleName});
|
connect(thing, &Thing::nameChanged, this, [thing, this]() {
|
||||||
});
|
int idx = m_things.indexOf(thing);
|
||||||
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
|
if (idx < 0) return;
|
||||||
int idx = m_things.indexOf(thing);
|
emit dataChanged(index(idx), index(idx), {RoleName});
|
||||||
if (idx < 0) return;
|
});
|
||||||
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
|
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
|
||||||
});
|
int idx = m_things.indexOf(thing);
|
||||||
connect(thing->states(), &States::dataChanged, this, [thing, this]() {
|
if (idx < 0) return;
|
||||||
int idx = m_things.indexOf(thing);
|
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
|
||||||
if (idx < 0) return;
|
});
|
||||||
emit dataChanged(index(idx), index(idx));
|
connect(thing->states(), &States::dataChanged, this, [thing, this]() {
|
||||||
});
|
int idx = m_things.indexOf(thing);
|
||||||
|
if (idx < 0) return;
|
||||||
|
emit dataChanged(index(idx), index(idx));
|
||||||
|
});
|
||||||
|
emit thingAdded(thing);
|
||||||
|
}
|
||||||
|
|
||||||
emit countChanged();
|
emit countChanged();
|
||||||
emit thingAdded(thing);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Things::removeThing(Thing *thing)
|
void Things::removeThing(Thing *thing)
|
||||||
|
|||||||
@ -65,6 +65,7 @@ public:
|
|||||||
QVariant data(const QModelIndex & index, int role = RoleName) const override;
|
QVariant data(const QModelIndex & index, int role = RoleName) const override;
|
||||||
|
|
||||||
void addThing(Thing *thing);
|
void addThing(Thing *thing);
|
||||||
|
void addThings(const QList<Thing*> things);
|
||||||
void removeThing(Thing *thing);
|
void removeThing(Thing *thing);
|
||||||
|
|
||||||
void clearModel();
|
void clearModel();
|
||||||
|
|||||||
Reference in New Issue
Block a user