Optimize loading of things

pull/734/head
Michael Zanetti 2021-12-16 00:21:19 +01:00
parent 5c63c92064
commit 1ccdda578c
3 changed files with 35 additions and 20 deletions

View File

@ -302,6 +302,7 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap &param
// qCritical() << "Things received:" << qUtf8Printable(QJsonDocument::fromVariant(params).toJson(QJsonDocument::Indented));
if (params.keys().contains("things")) {
QVariantList thingsList = params.value("things").toList();
QList<Thing*> newThings;
foreach (QVariant thingVariant, thingsList) {
Thing *thing = unpackThing(this, thingVariant.toMap(), m_thingClasses);
if (!thing) {
@ -329,8 +330,9 @@ void ThingManager::getThingsResponse(int /*commandId*/, const QVariantMap &param
thing->setStateValue(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";
m_fetchingData = false;

View File

@ -101,27 +101,39 @@ QVariant Things::data(const QModelIndex &index, int role) const
void Things::addThing(Thing *thing)
{
thing->setParent(this);
beginInsertRows(QModelIndex(), m_things.count(), m_things.count());
m_things.append(thing);
addThings({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();
connect(thing, &Thing::nameChanged, this, [thing, this]() {
int idx = m_things.indexOf(thing);
if (idx < 0) return;
emit dataChanged(index(idx), index(idx), {RoleName});
});
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
int idx = m_things.indexOf(thing);
if (idx < 0) return;
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
});
connect(thing->states(), &States::dataChanged, this, [thing, this]() {
int idx = m_things.indexOf(thing);
if (idx < 0) return;
emit dataChanged(index(idx), index(idx));
});
foreach (Thing *thing, things) {
thing->setParent(this);
connect(thing, &Thing::nameChanged, this, [thing, this]() {
int idx = m_things.indexOf(thing);
if (idx < 0) return;
emit dataChanged(index(idx), index(idx), {RoleName});
});
connect(thing, &Thing::setupStatusChanged, this, [thing, this]() {
int idx = m_things.indexOf(thing);
if (idx < 0) return;
emit dataChanged(index(idx), index(idx), {RoleSetupStatus, RoleSetupDisplayMessage});
});
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 thingAdded(thing);
}
void Things::removeThing(Thing *thing)

View File

@ -65,6 +65,7 @@ public:
QVariant data(const QModelIndex & index, int role = RoleName) const override;
void addThing(Thing *thing);
void addThings(const QList<Thing*> things);
void removeThing(Thing *thing);
void clearModel();